install-progress.h 5.2 KB

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