/
updatersettingsdialog.cpp
190 lines (163 loc) · 5.55 KB
/
updatersettingsdialog.cpp
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
#include "updatersettingsdialog.h"
#include "updatersettings.h"
#include <QDesktopServices>
#include <QVBoxLayout>
#include <QFormLayout>
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QComboBox>
#include <QDialogButtonBox>
#include <QFileDialog>
#include <de/Log>
#include <QDebug>
static QString defaultLocationName()
{
QString name = QDesktopServices::displayName(QDesktopServices::TempLocation);
if(name.isEmpty())
{
name = "Temporary Files";
}
return name;
}
struct UpdaterSettingsDialog::Instance
{
UpdaterSettingsDialog* self;
QCheckBox* autoCheck;
QComboBox* freqList;
QLabel* lastChecked;
QComboBox* channelList;
QComboBox* pathList;
QCheckBox* deleteAfter;
Instance(UpdaterSettingsDialog* dlg) : self(dlg)
{
self->setWindowFlags(self->windowFlags() & ~Qt::WindowContextHelpButtonHint);
self->setWindowTitle(tr("Updater Settings"));
QVBoxLayout* mainLayout = new QVBoxLayout;
self->setLayout(mainLayout);
QFormLayout* form = new QFormLayout;
mainLayout->addLayout(form);
freqList = new QComboBox;
freqList->addItem(tr("At startup"), UpdaterSettings::AtStartup);
freqList->addItem(tr("Daily"), UpdaterSettings::Daily);
freqList->addItem(tr("Biweekly"), UpdaterSettings::Biweekly);
freqList->addItem(tr("Weekly"), UpdaterSettings::Weekly);
freqList->addItem(tr("Monthly"), UpdaterSettings::Monthly);
autoCheck = new QCheckBox(tr("&Check for updates:"));
form->addRow(autoCheck, freqList);
QLayoutItem* item = form->itemAt(0, QFormLayout::LabelRole);
item->setAlignment(Qt::AlignVCenter);
lastChecked = new QLabel;
form->addRow(new QWidget, lastChecked);
channelList = new QComboBox;
channelList->addItem(tr("Stable"), UpdaterSettings::Stable);
channelList->addItem(tr("Unstable/Candidate"), UpdaterSettings::Unstable);
form->addRow(tr("&Release type:"), channelList);
pathList = new QComboBox;
pathList->addItem(defaultLocationName(),
UpdaterSettings::defaultDownloadPath());
pathList->addItem(tr("Select folder..."), "");
form->addRow(tr("&Download location:"), pathList);
deleteAfter = new QCheckBox(tr("D&elete file after install"));
form->addRow(new QWidget, deleteAfter);
QDialogButtonBox* bbox = new QDialogButtonBox;
mainLayout->addWidget(bbox);
// Buttons.
QPushButton* ok = bbox->addButton(QDialogButtonBox::Ok);
QPushButton* cancel = bbox->addButton(QDialogButtonBox::Cancel);
fetch();
// Connections.
QObject::connect(autoCheck, SIGNAL(toggled(bool)), self, SLOT(autoCheckToggled(bool)));
QObject::connect(pathList, SIGNAL(activated(int)), self, SLOT(pathActivated(int)));
QObject::connect(ok, SIGNAL(clicked()), self, SLOT(accept()));
QObject::connect(cancel, SIGNAL(clicked()), self, SLOT(reject()));
}
void fetch()
{
UpdaterSettings st;
lastChecked->setText(tr("<small>Last checked on %1.</small>")
.arg(st.lastCheckTime().asText(de::Time::FriendlyFormat)));
autoCheck->setChecked(!st.onlyCheckManually());
freqList->setEnabled(!st.onlyCheckManually());
freqList->setCurrentIndex(freqList->findData(st.frequency()));
channelList->setCurrentIndex(channelList->findData(st.channel()));
setDownloadPath(st.downloadPath());
deleteAfter->setChecked(st.deleteAfterUpdate());
}
void apply()
{
UpdaterSettings st;
st.setOnlyCheckManually(!autoCheck->isChecked());
int sel = freqList->currentIndex();
if(sel >= 0)
{
st.setFrequency(UpdaterSettings::Frequency(freqList->itemData(sel).toInt()));
}
sel = channelList->currentIndex();
if(sel >= 0)
{
st.setChannel(UpdaterSettings::Channel(channelList->itemData(sel).toInt()));
}
st.setDownloadPath(pathList->itemData(pathList->currentIndex()).toString());
st.setDeleteAfterUpdate(deleteAfter->isChecked());
}
void setDownloadPath(const QString& dir)
{
if(dir != UpdaterSettings::defaultDownloadPath())
{
// Remove extra items.
while(pathList->count() > 2)
{
pathList->removeItem(0);
}
pathList->insertItem(0, QDir(dir).dirName(), dir);
pathList->setCurrentIndex(0);
}
else
{
pathList->setCurrentIndex(pathList->findData(dir));
}
}
};
UpdaterSettingsDialog::UpdaterSettingsDialog(QWidget *parent)
: QDialog(parent)
{
d = new Instance(this);
}
UpdaterSettingsDialog::~UpdaterSettingsDialog()
{
delete d;
}
void UpdaterSettingsDialog::fetch()
{
d->fetch();
}
void UpdaterSettingsDialog::accept()
{
d->apply();
QDialog::accept();
}
void UpdaterSettingsDialog::reject()
{
QDialog::reject();
}
void UpdaterSettingsDialog::autoCheckToggled(bool set)
{
d->freqList->setEnabled(set);
}
void UpdaterSettingsDialog::pathActivated(int index)
{
QString path = d->pathList->itemData(index).toString();
if(path.isEmpty())
{
QString dir = QFileDialog::getExistingDirectory(this, tr("Download Folder"), QDir::homePath());
if(!dir.isEmpty())
{
d->setDownloadPath(dir);
}
else
{
d->setDownloadPath(UpdaterSettings::defaultDownloadPath());
}
}
}