You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 Mark Roszko <mark.roszko@gmail.com>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef BACKGROUND_JOBS_MONITOR_H
  25. #define BACKGROUND_JOBS_MONITOR_H
  26. #include <kicommon.h>
  27. #include <widgets/progress_reporter_base.h>
  28. #include <functional>
  29. #include <memory>
  30. #include <shared_mutex>
  31. #include <vector>
  32. class PROGRESS_REPORTER;
  33. class wxString;
  34. class KISTATUSBAR;
  35. struct BACKGROUND_JOB;
  36. class BACKGROUND_JOB_REPORTER;
  37. class BACKGROUND_JOB_LIST;
  38. class BACKGROUND_JOBS_MONITOR;
  39. class wxWindow;
  40. class wxCloseEvent;
  41. class KICOMMON_API BACKGROUND_JOB_REPORTER : public PROGRESS_REPORTER_BASE
  42. {
  43. public:
  44. BACKGROUND_JOB_REPORTER( BACKGROUND_JOBS_MONITOR* aMonitor,
  45. std::shared_ptr<BACKGROUND_JOB> aJob );
  46. void SetTitle( const wxString& aTitle ) override
  47. {
  48. }
  49. void Report( const wxString& aMessage ) override;
  50. void Cancel() { m_cancelled.store( true ); }
  51. void AdvancePhase() override;
  52. void SetNumPhases( int aNumPhases ) override;
  53. private:
  54. bool updateUI() override;
  55. BACKGROUND_JOBS_MONITOR* m_monitor;
  56. std::shared_ptr<BACKGROUND_JOB> m_job;
  57. wxString m_title;
  58. wxString m_report;
  59. };
  60. struct KICOMMON_API BACKGROUND_JOB
  61. {
  62. public:
  63. wxString m_name;
  64. wxString m_status;
  65. std::shared_ptr<BACKGROUND_JOB_REPORTER> m_reporter;
  66. int m_maxProgress;
  67. int m_currentProgress;
  68. };
  69. class KICOMMON_API BACKGROUND_JOBS_MONITOR
  70. {
  71. friend class BACKGROUND_JOB_REPORTER;
  72. friend class BACKGROUND_JOB_LIST;
  73. public:
  74. BACKGROUND_JOBS_MONITOR();
  75. /**
  76. * Creates a background job with the given name
  77. *
  78. * @param aName is the displayed title for the event
  79. */
  80. std::shared_ptr<BACKGROUND_JOB> Create( const wxString& aName );
  81. /**
  82. * Removes the given background job from any lists and frees it
  83. */
  84. void Remove( std::shared_ptr<BACKGROUND_JOB> job );
  85. /**
  86. * Shows the background job list
  87. */
  88. void ShowList( wxWindow* aParent, wxPoint aPos );
  89. /**
  90. * Add a status bar for handling
  91. */
  92. void RegisterStatusBar( KISTATUSBAR* aStatusBar );
  93. /**
  94. * Removes status bar from handling
  95. */
  96. void UnregisterStatusBar( KISTATUSBAR* aStatusBar );
  97. private:
  98. /**
  99. * Handles removing the shown list window from our list of shown windows
  100. */
  101. void onListWindowClosed( wxCloseEvent& aEvent );
  102. /**
  103. * Handles job status updates, intended to be called by BACKGROUND_JOB_REPORTER only
  104. */
  105. void jobUpdated( std::shared_ptr<BACKGROUND_JOB> aJob );
  106. /**
  107. * Holds a reference to all active background jobs
  108. * Access to this vector should be protected by locks since threads may Create or Remove at will
  109. * to register their activity
  110. */
  111. std::vector<std::shared_ptr<BACKGROUND_JOB>> m_jobs;
  112. std::vector<BACKGROUND_JOB_LIST*> m_shownDialogs;
  113. std::vector<KISTATUSBAR*> m_statusBars;
  114. /// Mutex to protect access to the m_jobs vector
  115. mutable std::shared_mutex m_mutex;
  116. };
  117. #endif