1 | #include "JenkinsWidget.h" |
2 | |
3 | #include <RepoFetcher.h> |
4 | #include <JobContainer.h> |
5 | #include <GitQlientSettings.h> |
6 | #include <GitBase.h> |
7 | |
8 | #include <QTimer> |
9 | #include <QButtonGroup> |
10 | #include <QStackedLayout> |
11 | #include <QPushButton> |
12 | #include <QHBoxLayout> |
13 | #include <QNetworkAccessManager> |
14 | |
15 | namespace Jenkins |
16 | { |
17 | |
18 | JenkinsWidget::JenkinsWidget(const QSharedPointer<GitBase> &git, QWidget *parent) |
19 | : QWidget(parent) |
20 | , mGit(git) |
21 | , mStackedLayout(new QStackedLayout()) |
22 | , mBodyLayout(new QHBoxLayout()) |
23 | , mBtnGroup(new QButtonGroup()) |
24 | , mButtonsLayout(new QVBoxLayout()) |
25 | , mTimer(new QTimer(this)) |
26 | { |
27 | setObjectName("JenkinsWidget" ); |
28 | |
29 | GitQlientSettings settings(mGit->getGitDir()); |
30 | const auto url = settings.localValue("BuildSystemUrl" , "" ).toString(); |
31 | const auto user = settings.localValue("BuildSystemUser" , "" ).toString(); |
32 | const auto token = settings.localValue("BuildSystemToken" , "" ).toString(); |
33 | |
34 | mConfig = IFetcher::Config { user, token, nullptr }; |
35 | mConfig.accessManager.reset(new QNetworkAccessManager()); |
36 | |
37 | const auto superBtnsLayout = new QVBoxLayout(); |
38 | superBtnsLayout->setContentsMargins(QMargins()); |
39 | superBtnsLayout->setSpacing(0); |
40 | superBtnsLayout->addLayout(mButtonsLayout); |
41 | superBtnsLayout->addStretch(); |
42 | |
43 | mBodyLayout->setSpacing(0); |
44 | mBodyLayout->addLayout(superBtnsLayout); |
45 | mBodyLayout->addLayout(mStackedLayout); |
46 | |
47 | const auto layout = new QHBoxLayout(this); |
48 | layout->setContentsMargins(QMargins()); |
49 | layout->setSpacing(0); |
50 | layout->addLayout(mBodyLayout); |
51 | |
52 | setMinimumSize(800, 600); |
53 | |
54 | mRepoFetcher = new RepoFetcher(mConfig, url, this); |
55 | connect(mRepoFetcher, &RepoFetcher::signalViewsReceived, this, &JenkinsWidget::configureGeneralView); |
56 | |
57 | #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) |
58 | connect(mBtnGroup, &QButtonGroup::idClicked, mStackedLayout, &QStackedLayout::setCurrentIndex); |
59 | #else |
60 | connect(mBtnGroup, SIGNAL(buttonClicked(int)), mStackedLayout, SLOT(setCurrentIndex(int))); |
61 | #endif |
62 | |
63 | mTimer->setInterval(15 * 60 * 1000); // 15 mins |
64 | } |
65 | |
66 | JenkinsWidget::~JenkinsWidget() |
67 | { |
68 | delete mBtnGroup; |
69 | } |
70 | |
71 | void JenkinsWidget::reload() const |
72 | { |
73 | mTimer->stop(); |
74 | mRepoFetcher->triggerFetch(); |
75 | mTimer->start(); |
76 | } |
77 | |
78 | void JenkinsWidget::configureGeneralView(const QVector<JenkinsViewInfo> &views) |
79 | { |
80 | for (auto &view : views) |
81 | { |
82 | if (!mViews.contains(view)) |
83 | { |
84 | const auto button = new QPushButton(view.name); |
85 | button->setObjectName("JenkinsWidgetTabButton" ); |
86 | button->setCheckable(true); |
87 | |
88 | const auto container = new JobContainer(mConfig, view, this); |
89 | container->setObjectName("JobContainer" ); |
90 | connect(container, &JobContainer::signalJobAreViews, this, &JenkinsWidget::configureGeneralView); |
91 | connect(container, &JobContainer::gotoBranch, this, &JenkinsWidget::gotoBranch); |
92 | connect(container, &JobContainer::gotoPullRequest, this, &JenkinsWidget::gotoPullRequest); |
93 | |
94 | mJobsMap.insert(view.name, container); |
95 | |
96 | mButtonsLayout->addWidget(button); |
97 | const auto id = mStackedLayout->addWidget(container); |
98 | mBtnGroup->addButton(button, id); |
99 | |
100 | mViews.append(view); |
101 | |
102 | if (mViews.count() == 1) |
103 | button->setChecked(true); |
104 | } |
105 | else |
106 | mJobsMap[view.name]->reload(); |
107 | } |
108 | } |
109 | |
110 | } |
111 | |