githandler.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #ifndef GITHANDLER_H
  2. #define GITHANDLER_H
  3. #include <QObject>
  4. #include <repositorymodel.h>
  5. #include <gitbranch.h>
  6. #include <QFutureWatcher>
  7. class CommitModel;
  8. class CommitGraph;
  9. class BranchListModel;
  10. class TagListModel;
  11. class QFileSystemWatcher;
  12. class GitConsole;
  13. using CommitModelContainer = QHash<QString, QPointer<CommitModel>>;
  14. class GitHandler : public QObject
  15. {
  16. Q_OBJECT
  17. Q_PROPERTY(RepositoryModel* repositories READ repositories NOTIFY repositoriesChanged)
  18. Q_PROPERTY(CommitGraph* graph READ graph NOTIFY graphChanged)
  19. Q_PROPERTY(GitRepository* activeRepo READ activeRepo NOTIFY activeRepoChanged)
  20. Q_PROPERTY(GitDiff* activeDiff READ activeDiff WRITE setActiveDiff NOTIFY activeDiffChanged)
  21. Q_PROPERTY(CommitModel* commits READ commits WRITE setCommits NOTIFY commitsChanged)
  22. Q_PROPERTY(BranchListModel* branchList READ branchList CONSTANT)
  23. Q_PROPERTY(TagListModel* tagList READ tagList CONSTANT)
  24. Q_PROPERTY(GitConsole* console READ console CONSTANT)
  25. Q_PROPERTY(bool isBusy READ isBusy NOTIFY isBusyChanged)
  26. Q_PROPERTY(QUrl homePath READ homePath CONSTANT)
  27. public:
  28. GitHandler();
  29. virtual ~GitHandler();
  30. Q_INVOKABLE void open(const QString &path, bool activate = false);
  31. Q_INVOKABLE void open(const QUrl &url, bool activate = false);
  32. Q_INVOKABLE void activateRepository(int i);
  33. Q_INVOKABLE void diff(GitCommit* a, GitCommit* b);
  34. Q_INVOKABLE void diff(); //Dif of workdir
  35. Q_INVOKABLE void diffReset();
  36. void onDiffReady();
  37. Q_INVOKABLE void copy(const QString& sha1);
  38. Q_INVOKABLE void pull(GitBranch::PullStrategy strategy) const;
  39. RepositoryModel* repositories() const
  40. {
  41. return m_repositories;
  42. }
  43. CommitGraph* graph() const
  44. {
  45. return m_graph;
  46. }
  47. GitRepository* activeRepo() const
  48. {
  49. return m_activeRepo;
  50. }
  51. GitDiff* activeDiff() const;
  52. CommitModel* commits() const
  53. {
  54. return m_commits;
  55. }
  56. BranchListModel* branchList() const
  57. {
  58. return m_branchList;
  59. }
  60. TagListModel* tagList() const
  61. {
  62. return m_tagList;
  63. }
  64. GitConsole* console() const
  65. {
  66. return m_console;
  67. }
  68. bool isBusy() const
  69. {
  70. return !m_graphTask.isCanceled() && !m_graphTask.isFinished();
  71. }
  72. QUrl homePath() const
  73. {
  74. return m_homePath;
  75. }
  76. public slots:
  77. void setActiveDiff(GitDiff* activeDiff);
  78. void setCommits(CommitModel* commits)
  79. {
  80. if (m_commits == commits)
  81. return;
  82. m_commits = commits;
  83. emit commitsChanged(commits);
  84. }
  85. void setActiveRepo(GitRepository* repo);
  86. signals:
  87. void activeRepoChanged(GitRepository* activeRepo);
  88. void repositoriesChanged(RepositoryModel* repositories);
  89. void graphChanged(CommitGraph* graph);
  90. void activeDiffChanged(GitDiff* activeDiff);
  91. void commitsChanged(CommitModel* commits);
  92. void isBusyChanged();
  93. protected:
  94. QString lastError() const;
  95. private:
  96. void updateModels();
  97. static CommitGraph* updateGraph(const GitOid& head, const BranchContainer &branches);
  98. void onGraphReady();
  99. void loadCachedRepos();
  100. RepositoryModel* m_repositories;
  101. CommitModel* m_commits;
  102. CommitGraph* m_graph;
  103. GitRepository* m_activeRepo;
  104. QPointer<GitDiff> m_activeDiff;
  105. BranchListModel* m_branchList;
  106. TagListModel* m_tagList;
  107. QFileSystemWatcher* m_activeRepoWatcher;
  108. GitConsole* m_console;
  109. QFutureWatcher<GitDiff*> m_diffTask;
  110. QFutureWatcher<CommitGraph*> m_graphTask;
  111. bool m_isBusy;
  112. QUrl m_homePath;
  113. // GitOid m_constantHead; //TODO:
  114. };
  115. Q_DECLARE_METATYPE(GitBranch::PullStrategy)
  116. #endif // GITHANDLER_H