Update.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "../../Global/Initialisierung.h"
  2. #include <MausEreignis.h>
  3. #include "../../Global/Variablen.h"
  4. // Inhalt der UpdateGUI klasse aus UpdateGUI.h
  5. // Konstruktor
  6. Update::Update( Schrift *zSchrift, FBalken *fb, int dg, std::function< void(bool) > after )
  7. : Thread()
  8. {
  9. p = 0;
  10. this->fb = fb;
  11. updateAbbrechen = 0;
  12. this->dg = dg;
  13. this->after = after;
  14. }
  15. // Destruktor
  16. Update::~Update()
  17. {
  18. if( fb )
  19. fb->release();
  20. if( p )
  21. p->release();
  22. }
  23. // nicht constant
  24. void Update::setPatcher( Patcher *p )
  25. {
  26. this->p = p;
  27. }
  28. void Update::abbrechen()
  29. {
  30. updateAbbrechen = 1;
  31. }
  32. void Update::herunterladen()
  33. {
  34. if( isRunning() || !p )
  35. return;
  36. start();
  37. getThis();
  38. }
  39. void Update::thread()
  40. {
  41. int dgId = dg;
  42. Text err;
  43. if( !p->update( dgId, &updateAbbrechen, (FBalken*)fb->getThis(), 0, &err ) )
  44. {
  45. updateAbbrechen = 1;
  46. if( nachLogin && nachLogin->zNachrichtenListe() )
  47. nachLogin->zNachrichtenListe()->addNachricht( new Text( "Fehler" ), err.getThis(), new Text( "Ok" ) );
  48. }
  49. run = 0;
  50. }
  51. void Update::threadEnd()
  52. {
  53. __super::threadEnd();
  54. after( !updateAbbrechen );
  55. release();
  56. }
  57. // constant
  58. int Update::getDateiGruppe() const
  59. {
  60. return dg;
  61. }
  62. // löscht das objekt wenn es nicht mehr gebraucht wird und beendet den Thread
  63. Thread *Update::release()
  64. {
  65. if( ref == 2 && run )
  66. {
  67. updateAbbrechen = 1;
  68. warteAufThread( 5000 );
  69. if( isRunning() )
  70. {
  71. ende();
  72. after( 0 );
  73. }
  74. }
  75. return Thread::release();
  76. }
  77. // inhalt der UpdateHandler Klasse aus UpdateGUI.h
  78. // Konstruktor
  79. UpdateHandler::UpdateHandler()
  80. {
  81. patcher = new Patcher();
  82. updates = new RCArray< Update >();
  83. ref = 1;
  84. }
  85. // Destruktor
  86. UpdateHandler::~UpdateHandler()
  87. {
  88. updates->release();
  89. patcher->release();
  90. }
  91. // privat
  92. void UpdateHandler::lock()
  93. {
  94. cs.lock();
  95. }
  96. void UpdateHandler::unlock()
  97. {
  98. cs.unlock();
  99. }
  100. // nicht constant
  101. bool UpdateHandler::add( Update *update )
  102. {
  103. lock();
  104. if( hat( update->getDateiGruppe() ) )
  105. {
  106. update->release();
  107. unlock();
  108. return 0;
  109. }
  110. int anz = updates->getEintragAnzahl();
  111. update->setPatcher( patcher->getThis() );
  112. updates->add( update );
  113. unlock();
  114. return 1;
  115. }
  116. void UpdateHandler::remove( int dg )
  117. {
  118. lock();
  119. int anz = updates->getEintragAnzahl();
  120. for( int i = 0; i < anz; i++ )
  121. {
  122. if( updates->z( i )->getDateiGruppe() == dg )
  123. {
  124. updates->remove( i );
  125. break;
  126. }
  127. }
  128. unlock();
  129. }
  130. bool UpdateHandler::hat( int dg )
  131. {
  132. lock();
  133. bool ret = 0;
  134. int anz = updates->getEintragAnzahl();
  135. for( int i = 0; i < anz; i++ )
  136. {
  137. if( updates->z( i )->getDateiGruppe() == dg )
  138. {
  139. ret = 1;
  140. break;
  141. }
  142. }
  143. unlock();
  144. return ret;
  145. }
  146. // Reference Counting
  147. UpdateHandler *UpdateHandler::getThis()
  148. {
  149. ref++;
  150. return this;
  151. }
  152. UpdateHandler *UpdateHandler::release()
  153. {
  154. ref--;
  155. if( !ref )
  156. delete this;
  157. return 0;
  158. }