Update.cpp 2.8 KB

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