Commit 87e14561 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧
Browse files

notifier: make it possible to have the notifier hidden

But still regularly check for updates

CCBUG: 413053
parent 08ef630b
add_definitions(-DTRANSLATION_DOMAIN=\"plasma-discover-notifier\")
add_executable(DiscoverNotifier BackendNotifierFactory.cpp DiscoverNotifier.cpp main.cpp)
add_executable(DiscoverNotifier BackendNotifierFactory.cpp DiscoverNotifier.cpp NotifierItem.cpp main.cpp)
target_link_libraries(DiscoverNotifier
KF5::Notifications
KF5::I18n
......
......@@ -24,8 +24,6 @@
#include <QDBusInterface>
#include <QDBusPendingReply>
#include <QNetworkConfigurationManager>
#include <KConfig>
#include <KConfigGroup>
#include <KRun>
#include <KLocalizedString>
#include <KNotification>
......
/***************************************************************************
* Copyright © 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com> *
* *
* 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) version 3 or any later version *
* accepted by the membership of KDE e.V. (or its successor approved *
* by the membership of KDE e.V.), which shall act as a proxy *
* defined in Section 14 of version 3 of the license. *
* *
* 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, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#include "NotifierItem.h"
#include <KStatusNotifierItem>
#include <KLocalizedString>
#include <QMenu>
#include <QDebug>
KStatusNotifierItem::ItemStatus sniStatus(DiscoverNotifier::State state)
{
// switch (state) {
// case DiscoverNotifier::Offline:
// case DiscoverNotifier::NoUpdates:
// return KStatusNotifierItem::Passive;
// case DiscoverNotifier::NormalUpdates:
// case DiscoverNotifier::SecurityUpdates:
// case DiscoverNotifier::RebootRequired:
// return KStatusNotifierItem::Active;
// }
return KStatusNotifierItem::Active;
}
NotifierItem::NotifierItem()
{
}
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_notifier, &DiscoverNotifier::stateChanged, this, &NotifierItem::refresh);
connect(m_item, &KStatusNotifierItem::activateRequested, &m_notifier, [this]() {
m_notifier.showDiscoverUpdates();
});
QMenu* menu = new QMenu;
connect(m_item, &QObject::destroyed, menu, &QObject::deleteLater);
auto discoverAction = menu->addAction(QIcon::fromTheme(QStringLiteral("plasma-discover")), i18n("Open Software Center..."));
connect(discoverAction, &QAction::triggered, &m_notifier, &DiscoverNotifier::showDiscover);
auto updatesAction = menu->addAction(QIcon::fromTheme(QStringLiteral("system-software-update")), i18n("See Updates..."));
connect(updatesAction, &QAction::triggered, &m_notifier, &DiscoverNotifier::showDiscoverUpdates);
auto refreshAction = menu->addAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18n("Refresh..."));
connect(refreshAction, &QAction::triggered, &m_notifier, &DiscoverNotifier::recheckSystemUpdateNeeded);
auto f = [menu, this]() {
auto refreshAction = menu->addAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18n("Restart..."));
connect(refreshAction, &QAction::triggered, &m_notifier, &DiscoverNotifier::recheckSystemUpdateNeeded);
};
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);
refresh();
}
void NotifierItem::refresh()
{
Q_ASSERT(m_item);
m_item->setStatus(sniStatus(m_notifier.state()));
m_item->setIconByName(m_notifier.iconName());
m_item->setToolTipSubTitle(m_notifier.message());
}
void NotifierItem::setVisible(bool visible)
{
if (visible == m_visible)
return;
m_visible = visible;
if (m_visible)
setupNotifierItem();
else
delete m_item;
}
/***************************************************************************
* Copyright © 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com> *
* *
* 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) version 3 or any later version *
* accepted by the membership of KDE e.V. (or its successor approved *
* by the membership of KDE e.V.), which shall act as a proxy *
* defined in Section 14 of version 3 of the license. *
* *
* 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, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#ifndef NOTIFIERITEM_H
#define NOTIFIERITEM_H
#include <QObject>
#include <KStatusNotifierItem>
#include "DiscoverNotifier.h"
class NotifierItem : public QObject
{
Q_OBJECT
public:
NotifierItem();
void setupNotifierItem();
void refresh();
bool isVisible() const { return m_visible; }
void setVisible(bool visible);
private:
bool m_visible = false;
DiscoverNotifier m_notifier;
QPointer<KStatusNotifierItem> m_item;
};
#endif // NOTIFIERITEM_H
......@@ -24,27 +24,16 @@
#include <KAboutData>
#include <KCrash>
#include <KDBusService>
#include <KConfig>
#include <KConfigGroup>
#include <KSharedConfig>
#include <QCommandLineParser>
#include <QDBusMessage>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDebug>
#include "DiscoverNotifier.h"
#include "../DiscoverVersion.h"
KStatusNotifierItem::ItemStatus sniStatus(DiscoverNotifier::State state)
{
switch (state) {
case DiscoverNotifier::Offline:
case DiscoverNotifier::NoUpdates:
return KStatusNotifierItem::Passive;
case DiscoverNotifier::NormalUpdates:
case DiscoverNotifier::SecurityUpdates:
case DiscoverNotifier::RebootRequired:
return KStatusNotifierItem::Active;
}
return KStatusNotifierItem::Active;
}
#include "NotifierItem.h"
#include "../DiscoverVersion.h"
int main(int argc, char** argv)
{
......@@ -53,6 +42,8 @@ int main(int argc, char** argv)
KCrash::setFlags(KCrash::AutoRestart);
NotifierItem notifier;
bool hide = false;
{
KAboutData about(QStringLiteral("DiscoverNotifier"), i18n("Discover Notifier"), version, i18n("System update status notifier"),
KAboutLicense::GPL, i18n("© 2010-2019 Plasma Development Team"));
......@@ -60,10 +51,16 @@ int main(int argc, char** argv)
about.setProductName("discover/discover");
about.setProgramLogo(app.windowIcon());
about.setTranslator(
i18ndc(nullptr, "NAME OF TRANSLATORS", "Your names"),
i18ndc(nullptr, "EMAIL OF TRANSLATORS", "Your emails"));
QCommandLineParser parser;
QCommandLineOption replaceOption({QStringLiteral("replace")},
i18n("Replace an existing instance"));
parser.addOption(replaceOption);
QCommandLineOption hideOption({QStringLiteral("hide")}, i18n("Do not show the notifier"), i18n("hidden"), QStringLiteral("false"));
parser.addOption(hideOption);
about.setupCommandLine(&parser);
parser.process(app);
about.processCommandLine(&parser);
......@@ -79,53 +76,21 @@ int main(int argc, char** argv)
QCoreApplication::processEvents(QEventLoop::AllEvents);
}
}
}
KDBusService service(KDBusService::Unique);
DiscoverNotifier notifier;
KStatusNotifierItem item;
item.setTitle(i18n("Updates"));
item.setToolTipTitle(i18n("Updates"));
const auto config = KSharedConfig::openConfig();
KConfigGroup group(config, "Behavior");
auto refresh = [&notifier, &item](){
item.setStatus(sniStatus(notifier.state()));
item.setIconByName(notifier.iconName());
item.setToolTipSubTitle(notifier.message());
};
QObject::connect(&notifier, &DiscoverNotifier::stateChanged, &item, refresh);
QObject::connect(&item, &KStatusNotifierItem::activateRequested, &notifier, [&notifier]() {
notifier.showDiscoverUpdates();
});
QMenu* menu = new QMenu;
auto discoverAction = menu->addAction(QIcon::fromTheme(QStringLiteral("plasma-discover")), i18n("Open Software Center..."));
QObject::connect(discoverAction, &QAction::triggered, &notifier, &DiscoverNotifier::showDiscover);
auto updatesAction = menu->addAction(QIcon::fromTheme(QStringLiteral("system-software-update")), i18n("See Updates..."));
QObject::connect(updatesAction, &QAction::triggered, &notifier, &DiscoverNotifier::showDiscoverUpdates);
auto refreshAction = menu->addAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18n("Refresh..."));
QObject::connect(refreshAction, &QAction::triggered, &notifier, &DiscoverNotifier::recheckSystemUpdateNeeded);
auto f = [menu, &notifier]() {
auto refreshAction = menu->addAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18n("Restart..."));
QObject::connect(refreshAction, &QAction::triggered, &notifier, &DiscoverNotifier::recheckSystemUpdateNeeded);
};
if (notifier.needsReboot())
f();
else
QObject::connect(&notifier, &DiscoverNotifier::needsRebootChanged, menu, f);
if (parser.isSet(hideOption)) {
hide = parser.value(hideOption) == QLatin1String("true");
group.writeEntry<bool>("Hide", hide);
config->sync();
} else {
hide = group.readEntry<bool>("Hide", false);
}
}
QObject::connect(&notifier, &DiscoverNotifier::newUpgradeAction, menu, [menu](UpgradeAction* a) {
QAction* action = new QAction(a->description(), menu);
QObject::connect(action, &QAction::triggered, a, &UpgradeAction::trigger);
menu->addAction(action);
});
item.setContextMenu(menu);
refresh();
KDBusService service(KDBusService::Unique);
notifier.setVisible(!hide);
return app.exec();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment