File: NotifierItem.cpp

package info (click to toggle)
plasma-discover 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,288 kB
  • sloc: cpp: 30,576; xml: 2,710; python: 311; sh: 5; makefile: 5
file content (150 lines) | stat: -rw-r--r-- 5,621 bytes parent folder | download
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
/*
 *   SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
 *
 *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
 */

#include "NotifierItem.h"
#include "updatessettings.h"
#include <KLocalizedString>
#include <QMenu>

NotifierItem::NotifierItem(const std::chrono::seconds &checkDelay)
    : m_notifier(checkDelay)
{
    connect(&m_notifier, &DiscoverNotifier::stateChanged, this, &NotifierItem::refreshStatusNotifierVisibility);
}

void NotifierItem::setupNotifierItem()
{
    Q_ASSERT(!m_item);
    m_item = new KStatusNotifierItem(QStringLiteral("org.kde.DiscoverNotifier"), this);
    m_item->setTitle(i18n("Updates"));
    m_item->setToolTipTitle(i18n("Updates"));

    connect(m_item, &KStatusNotifierItem::activateRequested, &m_notifier, [this]() {
        if (m_notifier.needsReboot()) {
            m_notifier.rebootPrompt();
        } else {
            m_notifier.showDiscoverUpdates(m_item->providedToken());
        }
    });

    QMenu *menu = new QMenu;
    connect(m_item, &QObject::destroyed, menu, &QObject::deleteLater);
    auto discoverAction = menu->addAction(QIcon::fromTheme(QStringLiteral("plasmadiscover")),
                                          i18nc("@action:button Opens Discover's main UI to analyze the updates", "Open Discover…"));
    connect(discoverAction, &QAction::triggered, &m_notifier, [this] {
        // If there's updates open directly on the updates page, otherwise show the main page
        if (m_notifier.hasUpdates() || m_notifier.hasSecurityUpdates()) {
            m_notifier.showDiscoverUpdates(m_item->providedToken());
        } else {
            m_notifier.showDiscover(m_item->providedToken());
        }
    });

    auto updatesAction =
        menu->addAction(QIcon::fromTheme(QStringLiteral("system-software-update")), i18nc("@action:button Starts an update in the background", "Start Update"));
    connect(updatesAction, &QAction::triggered, &m_notifier, &DiscoverNotifier::startUnattendedUpdates);

    auto refreshAction = menu->addAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18n("Refresh…"));
    connect(refreshAction, &QAction::triggered, &m_notifier, &DiscoverNotifier::recheckSystemUpdateNeededAndNotifyApp);

    if (m_notifier.needsReboot()) {
        menu->addSeparator();

        auto rebootAction = menu->addAction(QIcon::fromTheme(QStringLiteral("system-reboot-update")), i18n("Install Updates and Restart…"));
        connect(rebootAction, &QAction::triggered, &m_notifier, &DiscoverNotifier::rebootPrompt);

        auto shutdownAction = menu->addAction(QIcon::fromTheme(QStringLiteral("system-shutdown-update")), i18n("Install Updates and Shut Down…"));
        connect(shutdownAction, &QAction::triggered, &m_notifier, &DiscoverNotifier::shutdownPrompt);
    }

    auto f = [this]() {
        m_item->setTitle(i18n("Restart to apply installed updates"));
        m_item->setToolTipTitle(i18n("Click to restart the device"));
        m_item->setIconByName(QStringLiteral("system-reboot-update"));
    };
    if (m_notifier.needsReboot())
        f();
    else
        connect(&m_notifier, &DiscoverNotifier::needsRebootChanged, menu, f);

    connect(&m_notifier, &DiscoverNotifier::newUpgradeAction, menu, [menu](UpgradeAction *a) {
        QAction *action = new QAction(a->description(), menu);
        connect(action, &QAction::triggered, a, &UpgradeAction::trigger);
        menu->addAction(action);
    });
    m_item->setContextMenu(menu);
    m_item->setStatus(KStatusNotifierItem::Active);
    refresh();
}

void NotifierItem::refreshStatusNotifierVisibility()
{
    bool shouldShow = shouldShowStatusNotifier();
    if (!m_item && shouldShow) {
        setStatusNotifierVisibility(true);
    } else if (m_item && !shouldShow) {
        setStatusNotifierVisibility(false);
    }
    refresh();
}

void NotifierItem::setStatusNotifierEnabled(bool enabled)
{
    m_statusNotifierEnabled = enabled;
    refreshStatusNotifierVisibility();
}

void NotifierItem::refresh()
{
    if (!m_item) {
        return;
    }
    m_item->setIconByName(m_notifier.iconName());
    m_item->setToolTipSubTitle(m_notifier.message());
}

void NotifierItem::setStatusNotifierVisibility(bool visible)
{
    if (visible) {
        Q_ASSERT(!m_item);
        setupNotifierItem();
    } else {
        Q_ASSERT(m_item);
        delete m_item;
    }
}

bool NotifierItem::shouldShowStatusNotifier() const
{
    if (!isStatusNotifierEnabled()) {
        return false;
    }

    // Only show the status notifier if there is something to notify about
    // BUG: 413053
    switch (m_notifier.state()) {
    case DiscoverNotifier::Busy:
    case DiscoverNotifier::RebootRequired:
        return true;
    case DiscoverNotifier::NormalUpdates: {
        // Only show the status notifier on next notification time
        // BUG: 466693
        const QDateTime earliestNextNotificationTime = m_notifier.lastNotificationTime().addSecs(m_notifier.settings()->requiredNotificationInterval());

        return m_notifier.settings()->requiredNotificationInterval() > 0 &&
            !(earliestNextNotificationTime.isValid() && earliestNextNotificationTime > QDateTime::currentDateTimeUtc());
    }
    case DiscoverNotifier::SecurityUpdates:
        //...unless it's a security update, which should always be shown if the user wants notifications at all
        return m_notifier.settings()->requiredNotificationInterval() > 0;
    case DiscoverNotifier::Offline:
    case DiscoverNotifier::NoUpdates:
    default:
        return false;
    }
}

#include "moc_NotifierItem.cpp"