1 /***************************************************************************
2 * Copyright (C) 2013-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "dockmanagernotificationbackend.h"
25 #include <QHBoxLayout>
28 #include "clientbacklogmanager.h"
29 #include "clientsettings.h"
30 #include "coreconnection.h"
33 DockManagerNotificationBackend::DockManagerNotificationBackend(QObject* parent)
34 : AbstractNotificationBackend(parent)
35 , _bus(QDBusConnection::sessionBus())
37 NotificationSettings notificationSettings;
38 _enabled = notificationSettings.value("DockManager/Enabled", false).toBool();
40 notificationSettings.notify("DockManager/Enabled", this, &DockManagerNotificationBackend::enabledChanged);
42 _dock = new QDBusInterface("net.launchpad.DockManager", "/net/launchpad/DockManager", "net.launchpad.DockManager", _bus, this);
43 if (_dock->isValid()) {
44 _bus.connect("net.launchpad.DockManager",
45 "/net/launchpad/DockManager",
46 "net.launchpad.DockManager",
49 SLOT(itemAdded(QDBusObjectPath)));
52 // evil implementations (awn) use fd.o
53 _dock = new QDBusInterface("org.freedesktop.DockManager", "/org/freedesktop/DockManager", "org.freedesktop.DockManager", _bus, this);
54 if (_dock->isValid()) {
55 _bus.connect("org.freedesktop.DockManager",
56 "/org/freedesktop/DockManager",
57 "org.freedesktop.DockManager",
60 SLOT(itemAdded(QDBusObjectPath)));
63 _available = _enabled = false;
69 itemAdded(QDBusObjectPath());
71 connect(Client::coreConnection(),
72 &CoreConnection::progressValueChanged,
74 selectOverload<int>(&DockManagerNotificationBackend::updateProgress));
75 connect(Client::coreConnection(), &CoreConnection::synchronized, this, &DockManagerNotificationBackend::synchronized);
78 void DockManagerNotificationBackend::itemAdded(QDBusObjectPath p)
85 // stupid implementations (awn; kde?) use wrong casing of PID, but proper type
86 QDBusReply<QList<QDBusObjectPath>> paths = _dock->call("GetItemsByPid", (int)QCoreApplication::applicationPid());
87 if (!paths.isValid()) {
88 // stupid implementations (i.e. docky) use uint, but proper casing
89 paths = _dock->call("GetItemsByPID", (unsigned int)QCoreApplication::applicationPid());
90 if (!paths.isValid()) {
91 qDebug() << "DBus error:" << paths.error().message();
95 if (paths.value().count() == 0) { // no icon for this instance
99 QString path = paths.value()[0].path(); // no sense in using multiple icons for one instance
100 _item = new QDBusInterface("org.freedesktop.DockManager", path, "org.freedesktop.DockItem", _bus, this);
103 void DockManagerNotificationBackend::updateProgress(int progress)
105 if (!_enabled || !_item)
108 CoreConnection* c = Client::instance()->coreConnection();
110 if (c->progressMaximum() == c->progressMinimum())
113 perc = (progress - c->progressMinimum()) * 100 / (c->progressMaximum() - c->progressMinimum());
115 QHash<QString, QVariant> args;
116 args["progress"] = perc;
117 _item->call("UpdateDockItem", args);
120 void DockManagerNotificationBackend::updateProgress(int done, int total)
122 if (!_enabled || !_item)
127 disconnect(Client::backlogManager(), nullptr, this, nullptr);
131 perc = (done * 100) / total;
133 QHash<QString, QVariant> args;
134 args["progress"] = perc;
135 _item->call("UpdateDockItem", args);
138 void DockManagerNotificationBackend::synchronized()
140 connect(Client::backlogManager(),
141 &ClientBacklogManager::updateProgress,
143 selectOverload<int, int>(&DockManagerNotificationBackend::updateProgress));
146 void DockManagerNotificationBackend::notify(const Notification& notification)
148 if (!_enabled || !_item) {
151 if (notification.type != Highlight && notification.type != PrivMsg) {
155 QHash<QString, QVariant> args;
156 args["attention"] = true;
157 args["badge"] = QString::number(++_count);
158 _item->call("UpdateDockItem", args);
161 void DockManagerNotificationBackend::close(uint notificationId)
163 Q_UNUSED(notificationId);
167 QHash<QString, QVariant> args;
168 args["attention"] = false;
169 args["badge"] = --_count == 0 ? QString() : QString::number(_count);
170 _item->call("UpdateDockItem", args);
173 void DockManagerNotificationBackend::enabledChanged(const QVariant& v)
175 _enabled = v.toBool();
177 if (!_enabled && _item) {
178 QHash<QString, QVariant> args;
179 args["attention"] = false;
180 args["badge"] = QString();
181 _item->call("UpdateDockItem", args);
185 SettingsPage* DockManagerNotificationBackend::createConfigWidget() const
187 return new ConfigWidget(_available);
190 /***************************************************************************/
192 DockManagerNotificationBackend::ConfigWidget::ConfigWidget(bool enabled, QWidget* parent)
193 : SettingsPage("Internal", "DockManagerNotification", parent)
195 auto* layout = new QHBoxLayout(this);
196 layout->addWidget(enabledBox = new QCheckBox(tr("Mark dockmanager entry"), this));
197 enabledBox->setVisible(enabled);
199 connect(enabledBox, &QAbstractButton::toggled, this, &ConfigWidget::widgetChanged);
202 void DockManagerNotificationBackend::ConfigWidget::widgetChanged()
204 bool changed = enabled != enabledBox->isChecked();
206 if (changed != hasChanged())
207 setChangedState(changed);
210 bool DockManagerNotificationBackend::ConfigWidget::hasDefaults() const
215 void DockManagerNotificationBackend::ConfigWidget::defaults()
217 enabledBox->setChecked(false);
221 void DockManagerNotificationBackend::ConfigWidget::load()
223 NotificationSettings s;
224 enabled = s.value("DockManager/Enabled", false).toBool();
226 enabledBox->setChecked(enabled);
227 setChangedState(false);
230 void DockManagerNotificationBackend::ConfigWidget::save()
232 NotificationSettings s;
233 s.setValue("DockManager/Enabled", enabledBox->isChecked());