maemo5statusupdater.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include <QDebug>
  2. #include <QRegExp>
  3. #include "defines.h"
  4. #include "maemo5defines.h"
  5. #include "maemo5statusupdater.h"
  6. #define PROGRESS_MAX 1000
  7. Maemo5StatusUpdater::Maemo5StatusUpdater(Maemo5QtProjectGenerator & generator,
  8. QObject * parent)
  9. : IStatusUpdater(parent),
  10. m_generator(generator)
  11. {
  12. // Connect generator messages
  13. connect(&m_generator,
  14. SIGNAL(started(GeneratorTarget)),
  15. this,
  16. SLOT(start(GeneratorTarget)));
  17. connect(&m_generator,
  18. SIGNAL(finished(int)),
  19. this,
  20. SLOT(stop()));
  21. connect(&(m_generator.outputView()),
  22. SIGNAL(message(QString)),
  23. this,
  24. SLOT(increase(QString)));
  25. // Some defaults if others are not provided
  26. m_phasesInAction = 10;
  27. m_maximumValue = PROGRESS_MAX;
  28. m_lastLogCounter = 0;
  29. // Reset values that can be changed outside
  30. reset();
  31. }
  32. Maemo5StatusUpdater::~Maemo5StatusUpdater()
  33. {
  34. ;
  35. }
  36. void Maemo5StatusUpdater::updateGeneratorStatus(bool manualTrigger)
  37. {
  38. // If progress is indefinite, we're doing some time consuming
  39. // task with indefinite progress, so do not update the status.
  40. if (m_indefinite)
  41. return;
  42. GeneratorPhase phase = m_generator.getCurrentPhase();
  43. if (phase == M5_ENDED_SUCCESSFULLY || phase == M5_ENDED_WITH_ERRORS)
  44. {
  45. m_currentValue = m_maximumValue;
  46. m_statusText = m_generator.getStatusText();
  47. emit updated();
  48. emit finished(phase == M5_ENDED_SUCCESSFULLY);
  49. }
  50. else if (!manualTrigger)
  51. {
  52. // Progressbar steps in phase
  53. int phaseStep = m_maximumValue / m_phasesInAction;
  54. // Progressbar steps in one message. Phases have very different amount of messages,
  55. // so we'll calculate an approximation for this.
  56. int messageStep = phaseStep / phaseLength(phase);
  57. int currentValue = m_currentValue;
  58. int nextPhaseStart = (currentValue / phaseStep + 1) * phaseStep;
  59. if (phase != m_lastPhase)
  60. {
  61. // Set the progressbar to the beginning of the next step
  62. m_statusText = m_generator.getStatusText();
  63. m_currentValue = nextPhaseStart;
  64. emit updated();
  65. }
  66. else
  67. {
  68. // Update at every step, but only up to the next phase's start
  69. if ((currentValue + messageStep) < nextPhaseStart)
  70. {
  71. m_currentValue = currentValue + messageStep;
  72. emit updated();
  73. }
  74. }
  75. }
  76. m_lastPhase = phase;
  77. }
  78. void Maemo5StatusUpdater::setIndefiniteProgress(bool indefinite)
  79. {
  80. m_indefinite = indefinite;
  81. }
  82. void Maemo5StatusUpdater::setPhases(int phases)
  83. {
  84. m_phasesInAction = phases;
  85. }
  86. void Maemo5StatusUpdater::reset()
  87. {
  88. m_currentValue = 0;
  89. m_logSize = 0;
  90. m_phase = M5_INITIAL;
  91. m_lastPhase = M5_INITIAL;
  92. m_statusText = m_generator.getStatusText();
  93. m_indefinite = false;
  94. emit updated();
  95. }
  96. int Maemo5StatusUpdater::maximumProgress()
  97. {
  98. return m_maximumValue;
  99. }
  100. int Maemo5StatusUpdater::currentProgress()
  101. {
  102. return m_currentValue;
  103. }
  104. QString Maemo5StatusUpdater::statusText()
  105. {
  106. return m_statusText;
  107. }
  108. int Maemo5StatusUpdater::logSize()
  109. {
  110. return m_logSize;
  111. }
  112. QFileInfo Maemo5StatusUpdater::pkgFileInfo()
  113. {
  114. QFileInfo
  115. rv;
  116. // sample pattern to look for, as .deb file has the same
  117. // base name:
  118. // dpkg-genchanges >../stew_0.0.1-1_armel.changes
  119. // Look for the deb (.changes) file name in the lastlog
  120. QRegExp rx;
  121. rx.setPattern(".*dpkg-genchanges >\\.\\./([^ ]+armel)\\.changes.*");
  122. for (int i = 0;i < LASTLOG_ITEMS; i++)
  123. {
  124. qDebug() << "CHECK ---- " << m_lastLog[i];
  125. if (rx.indexIn(m_lastLog[i]) != -1)
  126. {
  127. qDebug() << "Match!";
  128. QString
  129. pkgFile(m_generator.generatorData().projectDirectory() + "/" + rx.cap(1));
  130. pkgFile += ".deb";
  131. QFileInfo info(pkgFile);
  132. qDebug() << "Found generated deb file:" << info.canonicalFilePath();
  133. if (info.exists())
  134. {
  135. qDebug() << "Exists!";
  136. rv = info;
  137. break;
  138. }
  139. else
  140. {
  141. qDebug() << "Does not exist!";
  142. break;
  143. }
  144. }
  145. }
  146. return rv;
  147. }
  148. void Maemo5StatusUpdater::increase(QString message)
  149. {
  150. // Set the last log messages to an array
  151. m_lastLog[m_lastLogCounter] = message;
  152. m_lastLogCounter = (m_lastLogCounter + 1) % LASTLOG_ITEMS;
  153. // See if progress is on and status should be updated
  154. if (m_phasesInAction > 0)
  155. {
  156. m_logSize++;
  157. updateGeneratorStatus(false);
  158. }
  159. }
  160. void Maemo5StatusUpdater::start(GeneratorTarget target)
  161. {
  162. reset();
  163. switch (target)
  164. { // TODO REMOVE PREVIEW - it will never happen
  165. case PREVIEW:
  166. m_phasesInAction = 3;
  167. break;
  168. case BUILD:
  169. m_phasesInAction = 9; // TODO much less phases - how many
  170. break;
  171. case REBUILD:
  172. m_phasesInAction = 8; // TODO much less phases - how many?
  173. break;
  174. default:
  175. m_indefinite = true;
  176. break;
  177. }
  178. }
  179. void Maemo5StatusUpdater::stop()
  180. {
  181. updateGeneratorStatus(true);
  182. }
  183. int Maemo5StatusUpdater::phaseLength(GeneratorPhase phase)
  184. {
  185. switch (phase.m_phase)
  186. {
  187. case M5_INITIAL:
  188. return 1; // TODO check value
  189. case M5_UNZIP_WIDGET:
  190. return 150; // TODO check value
  191. case M5_DPKG_BUILDPACKAGE:
  192. return 6; // TODO check value !!!!
  193. case M5_ENDED_SUCCESSFULLY:
  194. return 1; // TODO check value
  195. case M5_ENDED_WITH_ERRORS:
  196. return 1; // TODO check value
  197. default:
  198. return 10; // TODO check value
  199. }
  200. }