File indexing completed on 2024-04-21 05:45:11

0001 /*
0002     SPDX-FileCopyrightText: 2018 Jonathan Riddell <jr@jriddell.org>
0003     SPDX-FileCopyrightText: 2018 Harald Sitter <sitter@kde.org>
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "notifier.h"
0008 
0009 #include <KLocalizedString>
0010 #include <KNotification>
0011 #include <KStatusNotifierItem>
0012 
0013 #include <QDate>
0014 
0015 #include "upgraderwatcher.h"
0016 
0017 void Notifier::show(const QString &name, const QString &version, const QDate &eolDate)
0018 {
0019     // Delayed init. Otherwise we have the KSNI up when no upgrades are available.
0020     init();
0021 
0022     const QString title = i18n("Upgrade available");
0023     QString text = i18nc("For example, 'KDE neon 18.04 is available.'", "%1 %2 is available.", name, version);
0024     if (eolDate.isValid() && eolDate > QDate::currentDate()) {
0025         text.append(i18np("\nYour device will stop receiving updates and security fixes in 1 day - please upgrade when possible.",
0026                           "\nYour device will stop receiving updates and security fixes in %1 days - please upgrade when possible.",
0027                           -eolDate.daysTo(QDate::currentDate())));
0028     } else if (!eolDate.isNull()) {
0029         text.append(
0030             i18n("\nYour device is no longer receiving updates or security fixes - please upgrade now."));
0031     }
0032     const QString icon = QStringLiteral("system-software-update");
0033 
0034     m_notifier->setIconByName(icon);
0035     m_notifier->setToolTipIconByName(icon);
0036     m_notifier->setToolTipTitle(title);
0037     m_notifier->setToolTipSubTitle(text);
0038     m_notifier->setStatus(KStatusNotifierItem::Active);
0039     m_notifier->setCategory(KStatusNotifierItem::SystemServices);
0040     m_notifier->setStandardActionsEnabled(false);
0041 
0042     // This replaces a potentially pre-existing notification. Notifications
0043     // are auto-delted, so we need to do no house keeping here. This will
0044     // automatically replace the previous notification.
0045     auto notification = new KNotification(QStringLiteral("notification"),
0046                                           KNotification::Persistent | KNotification::DefaultEvent,
0047                                           this);
0048     notification->setIconName(icon);
0049     notification->setActions(QStringList{QStringLiteral("Upgrade")});
0050     notification->setTitle(title);
0051     notification->setText(text);
0052     connect(notification, &KNotification::action1Activated,
0053             this, &Notifier::activateRequested);
0054     notification->sendEvent();
0055 }
0056 
0057 void Notifier::init()
0058 {
0059     if (m_notifier) {
0060         return;
0061     }
0062 
0063     m_notifier = new KStatusNotifierItem(this);
0064     connect(m_notifier, &KStatusNotifierItem::activateRequested,
0065             this, &Notifier::activateRequested);
0066 
0067     // Watch upgrader running and delete the KSNI while it is up to prevent
0068     // the user from triggering an upgrade while upgrading.
0069     auto watcher = UpgraderWatcher::self();
0070     connect(watcher, &UpgraderWatcher::upgraderRunning, this, [this]() {
0071         delete m_notifier;
0072         m_notifier = nullptr;
0073     });
0074 }
0075 
0076