Initial D-Bus notifications.

This commit is contained in:
Martin Rotter 2015-07-28 20:43:40 +02:00
parent 4287b4ccd1
commit 77c6fdeac3
2 changed files with 36 additions and 78 deletions

View File

@ -43,16 +43,18 @@ Notification::Notification() : QWidget(0), m_title(QString()), m_text(QString())
m_clickSlot(NULL) {
#if defined(Q_OS_LINUX)
m_dBusActiveNotification = 0;
m_dBusInterface = new QDBusInterface("org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
QDBusConnection::sessionBus(), this);
qDBusRegisterMetaType<QImage>();
if (m_dBusInterface->isValid()) {
// We have correct connection to interface.
//m_dBusInterface.connect()
m_dBusInterface->connection().connect("org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
"NotificationClosed",
this, SLOT(notificationClosed(uint,uint)));
}
#endif
@ -64,56 +66,6 @@ Notification::~Notification() {
qDebug("Destroying Notification instance.");
}
#if defined(Q_OS_LINUX)
QDBusArgument &operator<<(QDBusArgument& arg, const QImage& image) {
if(image.isNull()) {
arg.beginStructure();
arg << 0 << 0 << 0 << false << 0 << 0 << QByteArray();
arg.endStructure();
return arg;
}
QImage scaled = image.scaledToHeight(100, Qt::SmoothTransformation);
scaled = scaled.convertToFormat(QImage::Format_ARGB32);
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
// ABGR -> ARGB
QImage i = scaled.rgbSwapped();
#else
// ABGR -> GBAR
QImage i(scaled.size(), scaled.format());
for (int y = 0; y < i.height(); ++y) {
QRgb* p = (QRgb*) scaled.scanLine(y);
QRgb* q = (QRgb*) i.scanLine(y);
QRgb* end = p + scaled.width();
while (p < end) {
*q = qRgba(qGreen(*p), qBlue(*p), qAlpha(*p), qRed(*p));
p++;
q++;
}
}
#endif
arg.beginStructure();
arg << i.width();
arg << i.height();
arg << i.bytesPerLine();
arg << i.hasAlphaChannel();
int channels = i.isGrayscale() ? 1 : (i.hasAlphaChannel() ? 4 : 3);
arg << i.depth() / channels;
arg << channels;
arg << QByteArray(reinterpret_cast<const char*>(i.bits()), i.byteCount());
arg.endStructure();
return arg;
}
const QDBusArgument &operator>>(const QDBusArgument& arg, QImage&) {
// This is needed to link but shouldn't be called.
Q_ASSERT(0);
return arg;
}
#endif
bool Notification::areNotificationsActivated() {
return qApp->settings()->value(GROUP(GUI), SETTING(GUI::UseFancyNotifications)).toBool();
}
@ -130,28 +82,30 @@ void Notification::notify(const QString &text, const QString &title, const QIcon
m_icon = icon.pixmap(NOTIFICATION_ICON_SIZE, NOTIFICATION_ICON_SIZE);
#if defined(Q_OS_LINUX)
/*
if (m_dBusInterface->isValid()) {
QVariantMap hints;
hints["image-data"] = QImage();// ;
hints["image-path"] = ""; // "application-exit";
QList<QVariant> argument_list;
argument_list << APP_NAME; // app_name
argument_list << (uint)0; // replace_id
argument_list << ""; // app_icon
argument_list << "aaa"; // summary
argument_list << "bbb"; // body
argument_list << QStringList(); // actions
argument_list << hints; // hints
argument_list << (int)1000; // timeout in ms
argument_list << APP_NAME; // app_name
argument_list << (uint)0; // replace_id
argument_list << ""; // app_icon
argument_list << title; // summary
argument_list << text; // body
argument_list << QStringList(); // actions
argument_list << hints; // hints
argument_list << (int)-1; // timeout in ms
//m_dBusInterface->callWithArgumentList(QDBus::AutoDetect, "Notify", argument_list);
// TODO: obrazky https://dev.visucore.com/bitcoin/doxygen/notificator_8cpp_source.html
return;
QDBusMessage response = m_dBusInterface->callWithArgumentList(QDBus::AutoDetect, "Notify", argument_list);
if (response.arguments().size() == 1) {
// Message was sent, notification should display.
m_dBusActiveNotification = response.arguments().at(0).toUInt();
}
}
*/
#endif
#else
if (m_clickTarget != NULL && m_clickSlot != NULL) {
// Connect invokation target.
connect(this, SIGNAL(clicked()), m_clickTarget, m_clickSlot, Qt::QueuedConnection);
@ -164,6 +118,7 @@ void Notification::notify(const QString &text, const QString &title, const QIcon
QTimer::singleShot(0, this, SLOT(repaint()));
m_timerId = startTimer(10000);
#endif
}
void Notification::notify(const QString &text, const QString &title, QSystemTrayIcon::MessageIcon icon,
@ -187,6 +142,12 @@ void Notification::cancel() {
}
}
void Notification::notificationClosed(uint id, uint reason) {
if (m_clickTarget != NULL && m_clickSlot != NULL && m_dBusActiveNotification == id && reason == 2) {
QMetaObject::invokeMethod(m_clickTarget, m_clickSlot);
}
}
void Notification::updateGeometries() {
// Calculate width and height of notification with given icon and text.
QFont bold_font = font();

View File

@ -24,18 +24,9 @@
#if defined(Q_OS_LINUX)
#include <QtDBus/QDBusArgument>
#include <QImage>
class QDBusInterface;
#endif
#if defined(Q_OS_LINUX)
QDBusArgument &operator<<(QDBusArgument& arg, const QImage& image);
const QDBusArgument &operator>>(const QDBusArgument& arg, QImage&);
#endif
class Notification : public QWidget {
Q_OBJECT
@ -59,6 +50,11 @@ class Notification : public QWidget {
// Loads settings.
void loadSettings();
#if defined(Q_OS_LINUX)
private slots:
void notificationClosed(uint id, uint reason);
#endif
protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
@ -93,6 +89,7 @@ class Notification : public QWidget {
#if defined(Q_OS_LINUX)
QDBusInterface *m_dBusInterface;
uint m_dBusActiveNotification;
#endif
};