-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPkUpdates.cxx
380 lines (299 loc) · 11.7 KB
/
PkUpdates.cxx
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
This file is part of liquidshell.
SPDX-FileCopyrightText: 2017 - 2024 Martin Koller <kollix@aon.at>
SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <PkUpdates.hxx>
#include <PkUpdateList.hxx>
#include <QIcon>
#include <QDateTime>
#include <QPainter>
#include <QDebug>
#include <KLocalizedString>
#include <KIconLoader>
#include <KConfig>
#include <KConfigGroup>
//--------------------------------------------------------------------------------
PkUpdates::PkUpdates(QWidget *parent)
: SysTrayItem(parent)
{
KConfig config;
KConfigGroup group = config.group("SoftwareUpdates");
if ( !group.hasKey("enabled") ) // create config entry so that one knows it exists
group.writeEntry("enabled", true);
bool isEnabled = group.readEntry("enabled", true);
if ( !isEnabled )
{
hide();
return;
}
setPixmap(currentPixmap = QIcon::fromTheme("system-software-update").pixmap(size()));
connect(KIconLoader::global(), &KIconLoader::iconLoaderSettingsChanged, this, [this]() { createToolTip(); });
// check every hour if the next checkpoint was reached. This ensures that
// we check for updates even when the computer was suspended for a while
// and therefore a normal QTimer timeout for 1 day will not be reached in 1 day
// TODO check again. start() was missing
updateTimer.setInterval(3600 * 1000);
updateTimer.start();
connect(&updateTimer, &QTimer::timeout, this, &PkUpdates::checkForUpdatesReached);
QString nextCheck = locale().toString(QDateTime::currentDateTime().addMSecs(updateTimer.interval()),
QLocale::ShortFormat);
setToolTip(i18n("Next check: %1", nextCheck));
//QTimer::singleShot(0, this, &PkUpdates::checkForUpdatesReached);
}
//--------------------------------------------------------------------------------
void PkUpdates::checkForUpdatesReached()
{
if ( updateList && updateList->isInstallInProgress() )
return;
QDateTime current = QDateTime::currentDateTime();
if ( !nextCheck.isValid() || (current >= nextCheck) )
{
checkForUpdates();
nextCheck = current.addDays(1);
}
}
//--------------------------------------------------------------------------------
void PkUpdates::checkForUpdates()
{
setPixmap(currentPixmap = QIcon::fromTheme("system-software-update").pixmap(size()));
setToolTip(i18n("Checking for updates ..."));
packages.clear();
setRefreshProgress(0);
PackageKit::Transaction *transaction = PackageKit::Daemon::refreshCache(true);
connect(transaction, &PackageKit::Transaction::errorCode, this, &PkUpdates::transactionError);
connect(transaction, &PackageKit::Transaction::finished, this, &PkUpdates::refreshFinished);
connect(transaction, &PackageKit::Transaction::percentageChanged, this,
[this, transaction]()
{
if ( (transaction->percentage() <= 100) && (transaction->status() != PackageKit::Transaction::StatusFinished) )
{
setRefreshProgress(transaction->percentage() / 2); // first step: refresh cache, second: get updates
setToolTip(i18n("Checking for updates ... %1%", refreshProgress));
}
});
}
//--------------------------------------------------------------------------------
void PkUpdates::refreshFinished(PackageKit::Transaction::Exit status, uint runtime)
{
Q_UNUSED(runtime)
Q_UNUSED(status)
// don't stop on exit error; it could e.g. only be an error on one of the repos
//if ( status != PackageKit::Transaction::ExitSuccess )
//return;
/////////////////
// hmm ... does that do anything on openSuse ? seems not implemented there
/*
{
PackageKit::Transaction *transaction = PackageKit::Daemon::getDistroUpgrades();
connect(transaction, &PackageKit::Transaction::errorCode, this, &PkUpdates::transactionError);
connect(transaction, &PackageKit::Transaction::distroUpgrade, this,
[this](PackageKit::Transaction::DistroUpgrade type, const QString &name, const QString &description)
{
qDebug() << "distroUpgrade" << type << name << description;
});
}
*/
/////////////////
PackageKit::Transaction *transaction = PackageKit::Daemon::getUpdates();
connect(transaction, &PackageKit::Transaction::package, this, &PkUpdates::package);
connect(transaction, &PackageKit::Transaction::errorCode, this, &PkUpdates::transactionError);
connect(transaction, &PackageKit::Transaction::finished, this,
[this]()
{
if ( updateList )
updateList->setPackages(packages);
setRefreshProgress(100);
createToolTip(true);
});
connect(transaction, &PackageKit::Transaction::percentageChanged, this,
[this, transaction]()
{
if ( (transaction->percentage() <= 100) && (transaction->status() != PackageKit::Transaction::StatusFinished) )
{
setRefreshProgress(50 + (transaction->percentage() / 2)); // second step in checking for updates
setToolTip(i18n("Checking for updates ... %1%", refreshProgress));
}
});
}
//--------------------------------------------------------------------------------
void PkUpdates::package(PackageKit::Transaction::Info info, const QString &packageID, const QString &summary)
{
PackageData pkg;
pkg.id = packageID;
pkg.summary = summary;
packages.insert(info, pkg);
}
//--------------------------------------------------------------------------------
void PkUpdates::transactionError(PackageKit::Transaction::Error error, const QString &details)
{
Q_UNUSED(error)
setToolTip(i18n("Last check: %1\nError on checking for updates: %2",
locale().toString(QDateTime::currentDateTime(), QLocale::ShortFormat), details));
KNotification::event("update error", i18n("Software Update Error"), details,
QIcon::fromTheme("dialog-error").pixmap(32));
setRefreshProgress(100);
}
//--------------------------------------------------------------------------------
void PkUpdates::createToolTip(bool notify)
{
PackageKit::Transaction::Info info = PackageKit::Transaction::InfoUnknown;
QString tooltip;
int count = 0;
QList<PackageData> list = packages.values(PackageKit::Transaction::InfoSecurity);
if ( list.count() )
{
info = PackageKit::Transaction::InfoSecurity;
count += list.count();
tooltip += i18np("%1 <b>security</b> update available", "%1 <b>security</b> updates available", list.count());
addItems(tooltip, list);
}
list = packages.values(PackageKit::Transaction::InfoImportant);
if ( list.count() )
{
if ( info == PackageKit::Transaction::InfoUnknown )
info = PackageKit::Transaction::InfoImportant;
count += list.count();
tooltip += i18np("%1 <i>important</i> update available", "%1 <i>important</i> updates available", list.count());
addItems(tooltip, list);
}
list = packages.values(PackageKit::Transaction::InfoBugfix);
if ( list.count() )
{
if ( info == PackageKit::Transaction::InfoUnknown )
info = PackageKit::Transaction::InfoBugfix;
count += list.count();
tooltip += i18np("%1 bugfix update available", "%1 bugfix updates available", list.count());
addItems(tooltip, list);
}
int others = packages.count() - count;
if ( tooltip.isEmpty() )
{
if ( others )
{
setToolTip(i18np("Last check: %1\nNo important updates available\n%2 other",
"Last check: %1\nNo important updates available\n%2 others",
locale().toString(QDateTime::currentDateTime(), QLocale::ShortFormat), others));
if ( QIcon::hasThemeIcon("software-update-available") )
setPixmap(currentPixmap = QIcon::fromTheme("software-update-available").pixmap(size()));
else
setPixmap(currentPixmap = QIcon::fromTheme("update-low").pixmap(size()));
}
else
{
setToolTip(i18n("Last check: %1\nNo updates available",
locale().toString(QDateTime::currentDateTime(), QLocale::ShortFormat)));
setPixmap(currentPixmap = QIcon::fromTheme("update-none").pixmap(size()));
}
}
else
{
if ( others )
tooltip += i18np("<br>%1 other", "<br>%1 others", others);
tooltip = i18n("<html>Last check: %1<br>%2</html>",
locale().toString(QDateTime::currentDateTime(), QLocale::ShortFormat), tooltip);
setToolTip(tooltip);
QString icon;
switch ( info )
{
case PackageKit::Transaction::InfoSecurity:
{
if ( QIcon::hasThemeIcon("software-update-urgent") )
icon = "software-update-urgent";
else
icon = "update-high";
break;
}
case PackageKit::Transaction::InfoImportant:
{
if ( QIcon::hasThemeIcon("software-update-urgent") )
icon = "software-update-urgent";
else
icon = "update-medium";
break;
}
default: // PackageKit::Transaction::InfoBugfix: // and others
{
if ( QIcon::hasThemeIcon("software-update-available") )
icon = "software-update-available";
else
icon = "update-low";
break;
}
}
setPixmap(currentPixmap = QIcon::fromTheme(icon).pixmap(size()));
if ( notify )
{
if ( notification )
notification->close(); // close previous notification if it's still here
notification = KNotification::event("updates available", i18n("Software Updates Available"), tooltip,
QIcon::fromTheme(icon).pixmap(32));
}
}
}
//--------------------------------------------------------------------------------
void PkUpdates::addItems(QString &tooltip, const QList<PackageData> &list) const
{
tooltip += "<ul>";
int count = std::min(3, int(list.count()));
if ( list.count() == 4 ) // if there's just one more, show it directly instead of "1 more"
count++;
for (int i = 0; i < count; i++)
tooltip += "<li>" + list[i].summary + "</li>";
if ( list.count() > 4 )
tooltip += i18n("<li>%1 more ...</li>", list.count() - count);
tooltip += "</ul>";
}
//--------------------------------------------------------------------------------
QWidget *PkUpdates::getDetailsList()
{
if ( !updateList )
{
updateList = new PkUpdateList(this);
updateList->setPackages(packages);
updateList->setRefreshProgress(refreshProgress);
connect(updateList, &PkUpdateList::refreshRequested, this, &PkUpdates::checkForUpdates);
connect(updateList, &PkUpdateList::packageInstalled, this, &PkUpdates::packageInstalled);
connect(updateList, &PkUpdateList::packageCountToInstall, this, &PkUpdates::packageCountToInstallChanged);
}
return updateList;
}
//--------------------------------------------------------------------------------
void PkUpdates::packageInstalled(const QString &id)
{
for (PackageList::iterator it = packages.begin(); it != packages.end(); ++it)
{
if ( (*it).id == id )
{
packages.erase(it);
break;
}
}
createToolTip();
}
//--------------------------------------------------------------------------------
void PkUpdates::setRefreshProgress(int progress)
{
refreshProgress = progress;
if ( updateList )
updateList->setRefreshProgress(refreshProgress);
}
//--------------------------------------------------------------------------------
void PkUpdates::packageCountToInstallChanged(int num)
{
if ( num == 0 )
{
setPixmap(currentPixmap);
return;
}
QPixmap pix = currentPixmap;
QPainter painter(&pix);
QFont f = font();
f.setPixelSize(contentsRect().width() / 2);
f.setBold(true);
painter.setFont(f);
painter.drawText(contentsRect(), Qt::AlignCenter, QString::number(num));
painter.end();
setPixmap(pix);
}
//--------------------------------------------------------------------------------