From a8add4a5b37e193190c648a5043a15a4f762d096 Mon Sep 17 00:00:00 2001 From: nowrep Date: Sat, 23 Apr 2011 22:33:25 +0200 Subject: [PATCH] DesktopNotificationsFactory class now supports native notifications (on Linux) and popupWidget notification --- src/app/qupzilla.cpp | 5 +- src/data/icons.qrc | 1 - .../desktopnotification.cpp | 44 +++++++++++-- .../desktopnotification.h | 25 +++++-- .../desktopnotificationsfactory.cpp | 66 +++++++++++++++---- .../desktopnotificationsfactory.h | 18 ++++- src/other/aboutdialog.cpp | 1 + 7 files changed, 129 insertions(+), 31 deletions(-) diff --git a/src/app/qupzilla.cpp b/src/app/qupzilla.cpp index fc464e2a6..a601c897f 100644 --- a/src/app/qupzilla.cpp +++ b/src/app/qupzilla.cpp @@ -83,7 +83,6 @@ QupZilla::QupZilla(bool tryRestore, QUrl startUrl) : connect(mApp, SIGNAL(message(MainApplication::MessageType,bool)), this, SLOT(receiveMessage(MainApplication::MessageType,bool))); } -#include "desktopnotification.h" void QupZilla::loadSettings() { QSettings settings(m_activeProfil+"settings.ini", QSettings::IniFormat); @@ -129,8 +128,7 @@ void QupZilla::loadSettings() m_actionPrivateBrowsing->setChecked( mApp->webSettings()->testAttribute(QWebSettings::PrivateBrowsingEnabled) ); m_privateBrowsing->setVisible( mApp->webSettings()->testAttribute(QWebSettings::PrivateBrowsingEnabled) ); -// DesktopNotification* notif = new DesktopNotification(QPixmap(), "bla", "ble", 10000); -// notif->show(); + setWindowIcon(QIcon(":/icons/qupzilla.png")); if (!makeTransparent) return; @@ -150,7 +148,6 @@ void QupZilla::loadSettings() QtWin::extendFrameIntoClientArea(this); setContentsMargins(0, 0, 0, 0); } - setWindowIcon(QIcon(":/icons/qupzilla.png")); } void QupZilla::receiveMessage(MainApplication::MessageType mes, bool state) diff --git a/src/data/icons.qrc b/src/data/icons.qrc index f9be4bd5c..3b82fb949 100644 --- a/src/data/icons.qrc +++ b/src/data/icons.qrc @@ -70,6 +70,5 @@ icons/preferences/stock_keyring.png icons/other/list-add.png icons/other/adblock.png - icons/other/notifbackground.png diff --git a/src/desktopnotifications/desktopnotification.cpp b/src/desktopnotifications/desktopnotification.cpp index c0ed35542..cd314d23e 100644 --- a/src/desktopnotifications/desktopnotification.cpp +++ b/src/desktopnotifications/desktopnotification.cpp @@ -1,9 +1,12 @@ #include "desktopnotification.h" #include "ui_desktopnotification.h" -DesktopNotification::DesktopNotification(const QPixmap &icon, const QString &heading, const QString &text, int timeout) +DesktopNotification::DesktopNotification(bool settingPosition) : QWidget(0) , ui(new Ui::DesktopNotification) + , m_settingPosition(settingPosition) + , m_timeout(6000) + , m_timer(new QTimer(this)) { ui->setupUi(this); setStyleSheet("background:transparent;"); @@ -17,11 +20,23 @@ DesktopNotification::DesktopNotification(const QPixmap &icon, const QString &hea setWindowFlags(flags); setWindowOpacity(0.9); - ui->icon->setPixmap(icon); - ui->heading->setText(QString("%1").arg(heading)); - ui->text->setText(text); + m_timer->setSingleShot(true); + connect(m_timer, SIGNAL(timeout()), this, SLOT(close())); +} - QTimer::singleShot(timeout, this, SLOT(close())); +void DesktopNotification::show() +{ + ui->icon->setPixmap(m_icon); + ui->heading->setText(QString("%1").arg(m_heading)); + ui->text->setText(m_text); + + if (!m_settingPosition) { + m_timer->stop(); + m_timer->setInterval(m_timeout); + m_timer->start(); + } + + QWidget::show(); } void DesktopNotification::enterEvent(QEvent *e) @@ -38,8 +53,23 @@ void DesktopNotification::leaveEvent(QEvent *e) void DesktopNotification::mousePressEvent(QMouseEvent *e) { - Q_UNUSED(e) - close(); + if (!m_settingPosition) { + close(); + return; + } + + if (e->button() == Qt::LeftButton) { + m_dragPosition = e->globalPos() - frameGeometry().topLeft(); + e->accept(); + } +} + +void DesktopNotification::mouseMoveEvent(QMouseEvent *e) +{ + if (e->buttons() & Qt::LeftButton) { + move(e->globalPos() - m_dragPosition); + e->accept(); + } } DesktopNotification::~DesktopNotification() diff --git a/src/desktopnotifications/desktopnotification.h b/src/desktopnotifications/desktopnotification.h index 484af3851..65b3a079e 100644 --- a/src/desktopnotifications/desktopnotification.h +++ b/src/desktopnotifications/desktopnotification.h @@ -3,6 +3,7 @@ #include #include +#include namespace Ui { class DesktopNotification; @@ -13,15 +14,29 @@ class DesktopNotification : public QWidget Q_OBJECT public: - explicit DesktopNotification(const QPixmap &icon, const QString &heading, const QString &text, int timeout); + explicit DesktopNotification(bool settingPosition = false); + void setPixmap(const QPixmap &icon) { m_icon = icon; } + void setHeading(const QString &heading) { m_heading = heading; } + void setText(const QString &text) { m_text = text; } + void setTimeout(int timeout) { m_timeout = timeout; } + void show(); ~DesktopNotification(); private: - void enterEvent(QEvent *e); - void leaveEvent(QEvent *e); - void mousePressEvent(QMouseEvent *e); + void enterEvent(QEvent* e); + void leaveEvent(QEvent* e); + void mousePressEvent(QMouseEvent* e); + void mouseMoveEvent(QMouseEvent* e); - Ui::DesktopNotification *ui; + Ui::DesktopNotification* ui; + bool m_settingPosition; + QPoint m_dragPosition; + + QPixmap m_icon; + QString m_heading; + QString m_text; + int m_timeout; + QTimer* m_timer; }; #endif // DESKTOPNOTIFICATION_H diff --git a/src/desktopnotifications/desktopnotificationsfactory.cpp b/src/desktopnotifications/desktopnotificationsfactory.cpp index 98cb476cb..fd8a34bb9 100644 --- a/src/desktopnotifications/desktopnotificationsfactory.cpp +++ b/src/desktopnotifications/desktopnotificationsfactory.cpp @@ -1,17 +1,57 @@ #include "desktopnotificationsfactory.h" +#include "desktopnotification.h" +#include "mainapplication.h" -DesktopNotificationsFactory::DesktopNotificationsFactory(QObject *parent) : - QObject(parent) +DesktopNotificationsFactory::DesktopNotificationsFactory(QObject* parent) + : QObject(parent) + , m_uint(0) { - QDBusInterface dbus("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", QDBusConnection::sessionBus()); - QVariantList args; - args.append("qupzilla"); - args.append(QVariant::UInt); - args.append("/home/david/a.png"); - args.append("Summary"); - args.append("Body of notification"); - args.append(QStringList()); - args.append(QVariantMap()); - args.append(-1); - dbus.callWithArgumentList(QDBus::AutoDetect, "Notify", args); + loadSettings(); +} + +void DesktopNotificationsFactory::loadSettings() +{ + QSettings settings(mApp->getActiveProfil()+"settings.ini", QSettings::IniFormat); + settings.beginGroup("Notifications"); + m_enabled = settings.value("Enabled", true).toBool(); + m_timeout = settings.value("Timeout", 6000).toInt(); +#ifdef Q_WS_X11 + m_notifType = settings.value("UseNativeDesktop", true).toBool() ? DesktopNative : PopupWidget; +#else + m_notifType = PopupWidget; +#endif + m_position = settings.value("Position", QPoint(10, 10)).toPoint(); + settings.endGroup(); +} + +void DesktopNotificationsFactory::notify(const QPixmap &icon, const QString &heading, const QString &text) +{ + switch (m_notifType) { + case PopupWidget: + if (!m_desktopNotif) + m_desktopNotif = new DesktopNotification(); + m_desktopNotif->setPixmap(icon); + m_desktopNotif->setHeading(heading); + m_desktopNotif->setText(text); + m_desktopNotif->setTimeout(m_timeout); + m_desktopNotif->show(); + break; + + case DesktopNative: + QDBusInterface dbus("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", QDBusConnection::sessionBus()); + QVariantList args; + args.append("qupzilla"); + args.append(m_uint); + args.append(""); //FIXME:store pixmap in temp folder and provide full path to it + args.append(heading); + args.append(text); + args.append(QStringList()); + args.append(QVariantMap()); + args.append(m_timeout); + QDBusMessage message = dbus.callWithArgumentList(QDBus::Block, "Notify", args); + QVariantList list = message.arguments(); + if (list.count() > 0) + m_uint = list.at(0).toInt(); + break; + } } diff --git a/src/desktopnotifications/desktopnotificationsfactory.h b/src/desktopnotifications/desktopnotificationsfactory.h index fb37259da..3d6afe796 100644 --- a/src/desktopnotifications/desktopnotificationsfactory.h +++ b/src/desktopnotifications/desktopnotificationsfactory.h @@ -4,17 +4,33 @@ #include #include #include +#include +#include +#include +#include +class DesktopNotification; class DesktopNotificationsFactory : public QObject { Q_OBJECT public: - explicit DesktopNotificationsFactory(QObject *parent = 0); + enum Type { DesktopNative, PopupWidget }; + explicit DesktopNotificationsFactory(QObject* parent = 0); + void notify(const QPixmap &icon, const QString &heading, const QString &text); signals: public slots: + void loadSettings(); +private: + bool m_enabled; + int m_timeout; + Type m_notifType; + QPoint m_position; + + QPointer m_desktopNotif; + unsigned int m_uint; }; #endif // DESKTOPNOTIFICATIONSFACTORY_H diff --git a/src/other/aboutdialog.cpp b/src/other/aboutdialog.cpp index abeb7a1ef..95285d960 100644 --- a/src/other/aboutdialog.cpp +++ b/src/other/aboutdialog.cpp @@ -38,6 +38,7 @@ AboutDialog::AboutDialog(QWidget* parent) : connect(ui->authorsButton, SIGNAL(clicked()), this, SLOT(buttonClicked())); showAbout(); + } void AboutDialog::buttonClicked()