Changeset View
Changeset View
Standalone View
Standalone View
notifier/main.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 Aleix Pol Gonzalez <aleixpol@blue-systems.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU Library/Lesser General Public License | ||||
6 | * version 2, or (at your option) any later version, as published by the | ||||
7 | * Free Software Foundation | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU Library/Lesser General Public | ||||
15 | * License along with this program; if not, write to the | ||||
16 | * Free Software Foundation, Inc., | ||||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include <QApplication> | ||||
21 | #include <KStatusNotifierItem> | ||||
22 | #include <QMenu> | ||||
23 | #include <KLocalizedString> | ||||
24 | #include <KCrash> | ||||
25 | #include <KDBusService> | ||||
26 | #include "DiscoverNotifier.h" | ||||
27 | | ||||
28 | KStatusNotifierItem::ItemStatus sniStatus(DiscoverNotifier::State state) | ||||
29 | { | ||||
30 | switch (state) { | ||||
31 | case DiscoverNotifier::Offline: | ||||
32 | case DiscoverNotifier::NoUpdates: | ||||
33 | return KStatusNotifierItem::Passive; | ||||
34 | case DiscoverNotifier::NormalUpdates: | ||||
35 | case DiscoverNotifier::SecurityUpdates: | ||||
36 | case DiscoverNotifier::RebootRequired: | ||||
37 | return KStatusNotifierItem::Active; | ||||
38 | } | ||||
39 | return KStatusNotifierItem::Active; | ||||
40 | } | ||||
41 | | ||||
42 | int main(int argc, char** argv) | ||||
43 | { | ||||
44 | QApplication app(argc, argv); | ||||
45 | DiscoverNotifier notifier; | ||||
46 | | ||||
47 | KDBusService service(KDBusService::Unique); | ||||
48 | KCrash::setFlags(KCrash::AutoRestart); | ||||
49 | | ||||
50 | KStatusNotifierItem item; | ||||
51 | item.setTitle(i18n("Updates")); | ||||
52 | item.setToolTipTitle(i18n("Updates")); | ||||
53 | | ||||
54 | auto refresh = [¬ifier, &item](){ | ||||
55 | item.setStatus(sniStatus(notifier.state())); | ||||
56 | item.setIconByName(notifier.iconName()); | ||||
57 | item.setToolTipSubTitle(notifier.message()); | ||||
58 | }; | ||||
59 | | ||||
60 | QObject::connect(¬ifier, &DiscoverNotifier::stateChanged, &item, refresh); | ||||
61 | | ||||
62 | QObject::connect(&item, &KStatusNotifierItem::activateRequested, ¬ifier, [¬ifier]() { | ||||
63 | notifier.showDiscoverUpdates(); | ||||
64 | }); | ||||
65 | | ||||
66 | QMenu* menu = new QMenu; | ||||
67 | auto discoverAction = menu->addAction(QIcon::fromTheme(QStringLiteral("plasma-discover")), i18n("Open Software Center...")); | ||||
68 | QObject::connect(discoverAction, &QAction::triggered, ¬ifier, &DiscoverNotifier::showDiscover); | ||||
69 | | ||||
70 | auto updatesAction = menu->addAction(QIcon::fromTheme(QStringLiteral("system-software-update")), i18n("See Updates...")); | ||||
71 | QObject::connect(updatesAction, &QAction::triggered, ¬ifier, &DiscoverNotifier::showDiscoverUpdates); | ||||
72 | | ||||
73 | auto refreshAction = menu->addAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18n("Refresh...")); | ||||
74 | QObject::connect(refreshAction, &QAction::triggered, ¬ifier, &DiscoverNotifier::recheckSystemUpdateNeeded); | ||||
75 | | ||||
76 | auto f = [menu, ¬ifier]() { | ||||
77 | auto refreshAction = menu->addAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18n("Restart...")); | ||||
78 | QObject::connect(refreshAction, &QAction::triggered, ¬ifier, &DiscoverNotifier::recheckSystemUpdateNeeded); | ||||
79 | }; | ||||
80 | if (notifier.needsReboot()) | ||||
81 | f(); | ||||
82 | else | ||||
83 | QObject::connect(¬ifier, &DiscoverNotifier::needsRebootChanged, menu, f); | ||||
84 | | ||||
85 | QObject::connect(¬ifier, &DiscoverNotifier::newUpgradeAction, menu, [menu](UpgradeAction* a) { | ||||
86 | QAction* action = new QAction(a->description(), menu); | ||||
87 | QObject::connect(action, &QAction::triggered, a, &UpgradeAction::trigger); | ||||
88 | menu->addAction(action); | ||||
89 | }); | ||||
90 | item.setContextMenu(menu); | ||||
91 | refresh(); | ||||
92 | | ||||
93 | return app.exec(); | ||||
94 | } |