/
updatersettingsdialog.cpp
240 lines (205 loc) · 7.42 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/** @file updatersettingsdialog.cpp Dialog for configuring automatic updates.
* @ingroup updater
*
* @authors Copyright © 2012-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "updater/updatersettingsdialog.h"
#include "updater/updatersettings.h"
#include "clientapp.h"
#include "ui/widgets/labelwidget.h"
#include "ui/widgets/choicewidget.h"
#include "ui/widgets/variabletogglewidget.h"
#include "GridLayout"
#include "SignalAction"
#include <de/Log>
#include <QDesktopServices>
using namespace de;
static QString defaultLocationName()
{
#ifdef DENG2_QT_5_0_OR_NEWER
QString name = QStandardPaths::displayName(QStandardPaths::CacheLocation);
#else
QString name = QDesktopServices::displayName(QDesktopServices::CacheLocation);
#endif
if(name.isEmpty())
{
name = "Temporary Files";
}
return name;
}
DENG2_PIMPL(UpdaterSettingsDialog),
DENG2_OBSERVES(ToggleWidget, Toggle)
{
ToggleWidget *autoCheck;
ChoiceWidget *freqs;
LabelWidget *lastChecked;
ChoiceWidget *channels;
ChoiceWidget *paths;
ToggleWidget *deleteAfter;
Instance(Public *i, Mode mode) : Base(i)
{
ScrollAreaWidget &area = self.area();
// Create the widgets.
area.add(autoCheck = new ToggleWidget);
area.add(freqs = new ChoiceWidget);
area.add(lastChecked = new LabelWidget);
area.add(channels = new ChoiceWidget);
area.add(paths = new ChoiceWidget);
area.add(deleteAfter = new ToggleWidget);
LabelWidget *releaseLabel = new LabelWidget;
area.add(releaseLabel);
LabelWidget *pathLabel = new LabelWidget;
area.add(pathLabel);
autoCheck->setText(tr("Check for updates:"));
freqs->items()
<< new ChoiceItem(tr("At startup"), UpdaterSettings::AtStartup)
<< new ChoiceItem(tr("Daily"), UpdaterSettings::Daily)
<< new ChoiceItem(tr("Biweekly"), UpdaterSettings::Biweekly)
<< new ChoiceItem(tr("Weekly"), UpdaterSettings::Weekly)
<< new ChoiceItem(tr("Monthly"), UpdaterSettings::Monthly);
lastChecked->margins().setTop("");
releaseLabel->setText("Release type:");
channels->items()
<< new ChoiceItem(tr("Stable"), UpdaterSettings::Stable)
<< new ChoiceItem(tr("Unstable/Candidate"), UpdaterSettings::Unstable);
pathLabel->setText(tr("Download location:"));
paths->items()
<< new ChoiceItem(defaultLocationName(),
UpdaterSettings::defaultDownloadPath().toString());
deleteAfter->setText(tr("Delete file after install"));
fetch();
autoCheck->audienceForToggle += this;
// Place the widgets into a grid.
GridLayout layout(area.contentRule().left(), area.contentRule().top());
layout.setGridSize(2, 0);
layout.setColumnAlignment(0, ui::AlignRight); // Labels aligned to the right.
layout << *autoCheck << *freqs
<< Const(0) << *lastChecked
<< *releaseLabel << *channels
<< *pathLabel << *paths
<< Const(0) << *deleteAfter;
area.setContentSize(layout.width(), layout.height());
self.buttons().items()
<< new DialogButtonItem(DialogWidget::Default | DialogWidget::Accept)
<< new DialogButtonItem(DialogWidget::Reject);
if(mode == WithApplyAndCheckButton)
{
self.buttons().items()
<< new DialogButtonItem(DialogWidget::Action, tr("Apply & Check Now"),
new SignalAction(thisPublic, SLOT(applyAndCheckNow())));
}
}
void fetch()
{
UpdaterSettings st;
String ago = st.lastCheckAgo();
if(!ago.isEmpty())
{
lastChecked->setText(_E(D)_E(t) + tr("Last checked %1.").arg(st.lastCheckAgo()));
}
else
{
lastChecked->setText(_E(D)_E(t) + tr("Never checked."));
}
autoCheck->setActive(!st.onlyCheckManually());
freqs->enable(!st.onlyCheckManually());
freqs->setSelected(freqs->items().findData(st.frequency()));
channels->setSelected(channels->items().findData(st.channel()));
setDownloadPath(st.downloadPath());
deleteAfter->setActive(st.deleteAfterUpdate());
}
void toggleStateChanged(ToggleWidget &toggle)
{
if(&toggle == autoCheck)
{
freqs->enable(autoCheck->isActive());
}
}
void apply()
{
UpdaterSettings st;
st.setOnlyCheckManually(autoCheck->isInactive());
ui::Data::Pos sel = freqs->selected();
if(sel != ui::Data::InvalidPos)
{
st.setFrequency(UpdaterSettings::Frequency(freqs->items().at(sel).data().toInt()));
}
sel = channels->selected();
if(sel != ui::Data::InvalidPos)
{
st.setChannel(UpdaterSettings::Channel(channels->items().at(sel).data().toInt()));
}
//st.setDownloadPath(pathList->itemData(pathList->currentIndex()).toString());
st.setDeleteAfterUpdate(deleteAfter->isActive());
}
void setDownloadPath(QString const &/*dir*/)
{
paths->setSelected(0);
/*
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(Mode mode, String const &name)
: DialogWidget(name, WithHeading), d(new Instance(this, mode))
{
heading().setText(tr("Updater Settings"));
}
void UpdaterSettingsDialog::applyAndCheckNow()
{
accept();
ClientApp::app().updater().checkNow();
}
void UpdaterSettingsDialog::finish(int result)
{
if(result)
{
d->apply();
}
DialogWidget::finish(result);
}
/*
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());
}
}
}
*/