1 /***************************************************************************
2 * Copyright (C) 2013 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"
23 #include <QHBoxLayout>
28 #include "clientsettings.h"
29 #include "coreconnection.h"
30 #include "clientbacklogmanager.h"
32 DockManagerNotificationBackend::DockManagerNotificationBackend(QObject *parent)
33 : AbstractNotificationBackend(parent), _bus(QDBusConnection::sessionBus()), _dock(0), _item(0), _count(0)
35 NotificationSettings notificationSettings;
36 _enabled = notificationSettings.value("DockManager/Enabled", false).toBool();
38 notificationSettings.notify("DockManager/Enabled", this, SLOT(enabledChanged(const QVariant &)));
40 _dock = new QDBusInterface("net.launchpad.DockManager", "/net/launchpad/DockManager", "net.launchpad.DockManager", _bus, this);
41 if (_dock->isValid()) {
42 _bus.connect("net.launchpad.DockManager", "/net/launchpad/DockManager", "net.launchpad.DockManager", "ItemAdded", this, SLOT(itemAdded(QDBusObjectPath)));
44 // evil implementations (awn) use fd.o
45 _dock = new QDBusInterface("org.freedesktop.DockManager", "/org/freedesktop/DockManager", "org.freedesktop.DockManager", _bus, this);
46 if (_dock->isValid()) {
47 _bus.connect("org.freedesktop.DockManager", "/org/freedesktop/DockManager", "org.freedesktop.DockManager", "ItemAdded", this, SLOT(itemAdded(QDBusObjectPath)));
49 _available = _enabled = false;
55 itemAdded(QDBusObjectPath());
57 connect(Client::coreConnection(), SIGNAL(progressValueChanged(int)), this, SLOT(updateProgress(int)));
58 connect(Client::coreConnection(), SIGNAL(synchronized()), this, SLOT(synchronized()));
62 void DockManagerNotificationBackend::itemAdded(QDBusObjectPath p)
69 // stupid implementations (awn; kde?) use wrong casing of PID, but proper type
70 QDBusReply<QList<QDBusObjectPath> > paths = _dock->call("GetItemsByPid", (int)QCoreApplication::applicationPid());
71 if (!paths.isValid()) {
72 // stupid implementations (i.e. docky) use uint, but proper casing
73 paths = _dock->call("GetItemsByPID", (unsigned int)QCoreApplication::applicationPid());
74 if (!paths.isValid()) {
75 qDebug() << "DBus error:" << paths.error().message();
79 if (paths.value().count() == 0) { // no icon for this instance
83 QString path = paths.value()[0].path(); // no sense in using multiple icons for one instance
84 _item = new QDBusInterface("org.freedesktop.DockManager", path, "org.freedesktop.DockItem", _bus, this);
88 void DockManagerNotificationBackend::updateProgress(int progress)
90 if (!_enabled || !_item)
93 CoreConnection *c = Client::instance()->coreConnection();
95 if (c->progressMaximum() == c->progressMinimum())
98 perc = (progress - c->progressMinimum()) * 100 / (c->progressMaximum() - c->progressMinimum());
100 QHash<QString, QVariant> args;
101 args["progress"] = perc;
102 _item->call("UpdateDockItem", args);
106 void DockManagerNotificationBackend::updateProgress(int done, int total)
108 if (!_enabled || !_item)
113 disconnect(Client::backlogManager(), 0, this, 0);
116 perc = (done * 100) / total;
118 QHash<QString, QVariant> args;
119 args["progress"] = perc;
120 _item->call("UpdateDockItem", args);
124 void DockManagerNotificationBackend::synchronized()
126 connect(Client::backlogManager(), SIGNAL(updateProgress(int, int)), this, SLOT(updateProgress(int, int)));
130 void DockManagerNotificationBackend::notify(const Notification ¬ification)
132 if (!_enabled || !_item) {
135 if (notification.type != Highlight && notification.type != PrivMsg) {
139 QHash<QString, QVariant> args;
140 args["attention"] = true;
141 args["badge"] = QString::number(++_count);
142 _item->call("UpdateDockItem", args);
146 void DockManagerNotificationBackend::close(uint notificationId)
148 Q_UNUSED(notificationId);
152 QHash<QString, QVariant> args;
153 args["attention"] = false;
154 args["badge"] = --_count == 0 ? QString() : QString::number(_count);
155 _item->call("UpdateDockItem", args);
159 void DockManagerNotificationBackend::enabledChanged(const QVariant &v)
161 _enabled = v.toBool();
163 if (!_enabled && _item) {
164 QHash<QString, QVariant> args;
165 args["attention"] = false;
166 args["badge"] = QString();
167 _item->call("UpdateDockItem", args);
172 SettingsPage *DockManagerNotificationBackend::createConfigWidget() const
174 return new ConfigWidget(_available);
178 /***************************************************************************/
180 DockManagerNotificationBackend::ConfigWidget::ConfigWidget(bool enabled, QWidget *parent)
181 : SettingsPage("Internal", "DockManagerNotification", parent)
183 QHBoxLayout *layout = new QHBoxLayout(this);
184 layout->addWidget(enabledBox = new QCheckBox(tr("Mark dockmanager entry"), this));
185 enabledBox->setEnabled(enabled);
187 connect(enabledBox, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
191 void DockManagerNotificationBackend::ConfigWidget::widgetChanged()
193 bool changed = enabled != enabledBox->isChecked();
195 if (changed != hasChanged()) setChangedState(changed);
199 bool DockManagerNotificationBackend::ConfigWidget::hasDefaults() const
205 void DockManagerNotificationBackend::ConfigWidget::defaults()
207 enabledBox->setChecked(false);
212 void DockManagerNotificationBackend::ConfigWidget::load()
214 NotificationSettings s;
215 enabled = s.value("DockManager/Enabled", false).toBool();
217 enabledBox->setChecked(enabled);
218 setChangedState(false);
222 void DockManagerNotificationBackend::ConfigWidget::save()
224 NotificationSettings s;
225 s.setValue("DockManager/Enabled", enabledBox->isChecked());