Update.cpp 2.8 KB

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