aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/updateinfo/settingspage.cpp
blob: 587102bce7546e60c9410596485194e4843ddfea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "settingspage.h"

#include "updateinfoplugin.h"
#include "updateinfotr.h"

#include <coreplugin/coreconstants.h>

#include <utils/qtcassert.h>
#include <utils/progressindicator.h>
#include <utils/layoutbuilder.h>

#include <QDate>
#include <QCheckBox>
#include <QComboBox>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>

namespace UpdateInfo {
namespace Internal {

class UpdateInfoSettingsPageWidget final : public Core::IOptionsPageWidget
{
public:
    UpdateInfoSettingsPageWidget(UpdateInfoPlugin *plugin)
        : m_plugin(plugin)
    {
        setWindowTitle(Tr::tr("Configure Filters"));

        m_updatesGroupBox = new QGroupBox(Tr::tr("Automatic Check for Updates"));
        m_updatesGroupBox->setCheckable(true);
        m_updatesGroupBox->setChecked(true);

        m_infoLabel = new QLabel(Tr::tr("Automatically runs a scheduled check for updates on "
                                        "a time interval basis. The automatic check for updates "
                                        "will be performed at the scheduled date, or the next "
                                        "startup following it."));
        m_infoLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
        m_infoLabel->setWordWrap(true);

        m_checkIntervalComboBox = new QComboBox;
        m_nextCheckDateLabel = new QLabel;
        m_checkForNewQtVersions = new QCheckBox(Tr::tr("Check for new Qt versions"));

        using namespace Utils::Layouting;

        Column {
            m_infoLabel,
            Row {
                Form {
                    new QLabel(Tr::tr("Check interval basis:")), m_checkIntervalComboBox, br,
                    new QLabel(Tr::tr("Next check date:")), m_nextCheckDateLabel
                },
                st
            },
            m_checkForNewQtVersions
        }.attachTo(m_updatesGroupBox);

        m_lastCheckDateLabel = new QLabel;

        m_checkNowButton = new QPushButton(Tr::tr("Check Now"));

        m_messageLabel = new QLabel;

        Column {
            m_updatesGroupBox,
            Row {
                new QLabel(Tr::tr("Last check date:")),
                m_lastCheckDateLabel,
                st,
                Row {
                    m_messageLabel,
                    st,
                    m_checkNowButton
                }
            },
            st
        }.attachTo(this);

        m_checkIntervalComboBox->setCurrentIndex(-1);

        m_lastCheckDateLabel->setText(Tr::tr("Not checked yet"));

        m_checkIntervalComboBox->addItem(Tr::tr("Daily"), UpdateInfoPlugin::DailyCheck);
        m_checkIntervalComboBox->addItem(Tr::tr("Weekly"), UpdateInfoPlugin::WeeklyCheck);
        m_checkIntervalComboBox->addItem(Tr::tr("Monthly"), UpdateInfoPlugin::MonthlyCheck);
        UpdateInfoPlugin::CheckUpdateInterval interval = m_plugin->checkUpdateInterval();
        for (int i = 0; i < m_checkIntervalComboBox->count(); i++) {
            if (m_checkIntervalComboBox->itemData(i).toInt() == interval) {
                m_checkIntervalComboBox->setCurrentIndex(i);
                break;
            }
        }

        m_updatesGroupBox->setChecked(m_plugin->isAutomaticCheck());
        m_checkForNewQtVersions->setChecked(m_plugin->isCheckingForQtVersions());

        updateLastCheckDate();
        checkRunningChanged(m_plugin->isCheckForUpdatesRunning());

        connect(m_checkNowButton, &QPushButton::clicked,
                m_plugin, &UpdateInfoPlugin::startCheckForUpdates);
        connect(m_checkIntervalComboBox, &QComboBox::currentIndexChanged,
                this, &UpdateInfoSettingsPageWidget::updateNextCheckDate);
        connect(m_plugin, &UpdateInfoPlugin::lastCheckDateChanged,
                this, &UpdateInfoSettingsPageWidget::updateLastCheckDate);
        connect(m_plugin, &UpdateInfoPlugin::checkForUpdatesRunningChanged,
                this, &UpdateInfoSettingsPageWidget::checkRunningChanged);
        connect(m_plugin, &UpdateInfoPlugin::newUpdatesAvailable,
                this, &UpdateInfoSettingsPageWidget::newUpdatesAvailable);
    }

    void apply() final;

private:
    void newUpdatesAvailable(bool available);
    void checkRunningChanged(bool running);
    void updateLastCheckDate();
    void updateNextCheckDate();
    UpdateInfoPlugin::CheckUpdateInterval currentCheckInterval() const;

    QPointer<Utils::ProgressIndicator> m_progressIndicator;

    UpdateInfoPlugin *m_plugin;

    QGroupBox *m_updatesGroupBox;
    QLabel *m_infoLabel;
    QComboBox *m_checkIntervalComboBox;
    QLabel *m_nextCheckDateLabel;
    QCheckBox *m_checkForNewQtVersions;
    QLabel *m_lastCheckDateLabel;
    QPushButton *m_checkNowButton;
    QLabel *m_messageLabel;
};

UpdateInfoPlugin::CheckUpdateInterval UpdateInfoSettingsPageWidget::currentCheckInterval() const
{
    return static_cast<UpdateInfoPlugin::CheckUpdateInterval>
            (m_checkIntervalComboBox->itemData(m_checkIntervalComboBox->currentIndex()).toInt());
}

void UpdateInfoSettingsPageWidget::newUpdatesAvailable(bool available)
{
    const QString message = available
            ? Tr::tr("New updates are available.")
            : Tr::tr("No new updates are available.");
    m_messageLabel->setText(message);
}

void UpdateInfoSettingsPageWidget::checkRunningChanged(bool running)
{
    m_checkNowButton->setDisabled(running);

    if (running) {
        if (!m_progressIndicator) {
            m_progressIndicator = new Utils::ProgressIndicator(Utils::ProgressIndicatorSize::Large);
            m_progressIndicator->attachToWidget(this);
        }
        m_progressIndicator->show();
    } else {
        if (m_progressIndicator) {
            delete m_progressIndicator;
        }
    }

    const QString message = running
            ? Tr::tr("Checking for updates...") : QString();
    m_messageLabel->setText(message);
}

void UpdateInfoSettingsPageWidget::updateLastCheckDate()
{
    const QDate date = m_plugin->lastCheckDate();
    QString lastCheckDateString;
    if (date.isValid())
        lastCheckDateString = date.toString();
    else
        lastCheckDateString = Tr::tr("Not checked yet");

    m_lastCheckDateLabel->setText(lastCheckDateString);

    updateNextCheckDate();
}

void UpdateInfoSettingsPageWidget::updateNextCheckDate()
{
    QDate date = m_plugin->nextCheckDate(currentCheckInterval());
    if (!date.isValid() || date < QDate::currentDate())
        date = QDate::currentDate();

    m_nextCheckDateLabel->setText(date.toString());
}

void UpdateInfoSettingsPageWidget::apply()
{
    m_plugin->setCheckUpdateInterval(currentCheckInterval());
    m_plugin->setAutomaticCheck(m_updatesGroupBox->isChecked());
    m_plugin->setCheckingForQtVersions(m_checkForNewQtVersions->isChecked());
}

// SettingsPage

SettingsPage::SettingsPage(UpdateInfoPlugin *plugin)
{
    setId(FILTER_OPTIONS_PAGE_ID);
    setCategory(Core::Constants::SETTINGS_CATEGORY_CORE);
    setDisplayName(Tr::tr("Update"));
    setWidgetCreator([plugin] { return new UpdateInfoSettingsPageWidget(plugin); });
}

} // Internal
} // UpdateInfoPlugin