install-progress.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef PKGLIB_IPROGRESS_H
  2. #define PKGLIB_IPROGRESS_H
  3. #include <apt-pkg/macros.h>
  4. #include <string>
  5. #include <unistd.h>
  6. #include <signal.h>
  7. #include <vector>
  8. namespace APT {
  9. namespace Progress {
  10. class PackageManager;
  11. PackageManager* PackageManagerProgressFactory();
  12. class PackageManager
  13. {
  14. private:
  15. /** \brief dpointer placeholder */
  16. void *d;
  17. protected:
  18. std::string progress_str;
  19. float percentage;
  20. int last_reported_progress;
  21. public:
  22. PackageManager()
  23. : percentage(0.0), last_reported_progress(-1) {};
  24. virtual ~PackageManager() {};
  25. /* Global Start/Stop */
  26. virtual void Start(int /*child_pty*/=-1) {};
  27. virtual void Stop() {};
  28. /* When dpkg is invoked (may happen multiple times for each
  29. * install/remove block
  30. */
  31. virtual void StartDpkg() {};
  32. virtual pid_t fork() {return fork(); };
  33. virtual void Pulse() {};
  34. virtual long GetPulseInterval() {
  35. return 500000;
  36. };
  37. virtual bool StatusChanged(std::string PackageName,
  38. unsigned int StepsDone,
  39. unsigned int TotalSteps,
  40. std::string HumanReadableAction);
  41. virtual void Error(std::string /*PackageName*/,
  42. unsigned int /*StepsDone*/,
  43. unsigned int /*TotalSteps*/,
  44. std::string /*ErrorMessage*/) {}
  45. virtual void ConffilePrompt(std::string /*PackageName*/,
  46. unsigned int /*StepsDone*/,
  47. unsigned int /*TotalSteps*/,
  48. std::string /*ConfMessage*/) {}
  49. };
  50. class PackageManagerProgressFd : public PackageManager
  51. {
  52. protected:
  53. int OutStatusFd;
  54. int StepsDone;
  55. int StepsTotal;
  56. void WriteToStatusFd(std::string msg);
  57. public:
  58. PackageManagerProgressFd(int progress_fd);
  59. virtual void StartDpkg();
  60. virtual void Stop();
  61. virtual bool StatusChanged(std::string PackageName,
  62. unsigned int StepsDone,
  63. unsigned int TotalSteps,
  64. std::string HumanReadableAction);
  65. virtual void Error(std::string PackageName,
  66. unsigned int StepsDone,
  67. unsigned int TotalSteps,
  68. std::string ErrorMessage);
  69. virtual void ConffilePrompt(std::string PackageName,
  70. unsigned int StepsDone,
  71. unsigned int TotalSteps,
  72. std::string ConfMessage);
  73. };
  74. class PackageManagerProgressDeb822Fd : public PackageManager
  75. {
  76. protected:
  77. int OutStatusFd;
  78. int StepsDone;
  79. int StepsTotal;
  80. void WriteToStatusFd(std::string msg);
  81. public:
  82. PackageManagerProgressDeb822Fd(int progress_fd);
  83. virtual void StartDpkg();
  84. virtual void Stop();
  85. virtual bool StatusChanged(std::string PackageName,
  86. unsigned int StepsDone,
  87. unsigned int TotalSteps,
  88. std::string HumanReadableAction);
  89. virtual void Error(std::string PackageName,
  90. unsigned int StepsDone,
  91. unsigned int TotalSteps,
  92. std::string ErrorMessage);
  93. virtual void ConffilePrompt(std::string PackageName,
  94. unsigned int StepsDone,
  95. unsigned int TotalSteps,
  96. std::string ConfMessage);
  97. };
  98. class PackageManagerFancy : public PackageManager
  99. {
  100. private:
  101. static void staticSIGWINCH(int);
  102. static std::vector<PackageManagerFancy*> instances;
  103. APT_HIDDEN bool DrawStatusLine();
  104. protected:
  105. void SetupTerminalScrollArea(int nr_rows);
  106. void HandleSIGWINCH(int);
  107. typedef struct {
  108. int rows;
  109. int columns;
  110. } TermSize;
  111. TermSize GetTerminalSize();
  112. sighandler_t old_SIGWINCH;
  113. int child_pty;
  114. public:
  115. PackageManagerFancy();
  116. ~PackageManagerFancy();
  117. virtual void Start(int child_pty=-1);
  118. virtual void Stop();
  119. virtual bool StatusChanged(std::string PackageName,
  120. unsigned int StepsDone,
  121. unsigned int TotalSteps,
  122. std::string HumanReadableAction);
  123. // return a progress bar of the given size for the given progress
  124. // percent between 0.0 and 1.0 in the form "[####...]"
  125. static std::string GetTextProgressStr(float percent, int OutputSize);
  126. };
  127. class PackageManagerText : public PackageManager
  128. {
  129. public:
  130. virtual bool StatusChanged(std::string PackageName,
  131. unsigned int StepsDone,
  132. unsigned int TotalSteps,
  133. std::string HumanReadableAction);
  134. };
  135. } // namespace Progress
  136. } // namespace APT
  137. #endif