From 18859cb960aa6de0dbb393186392b3449b5177ff Mon Sep 17 00:00:00 2001 From: blue Date: Mon, 18 Apr 2022 19:54:42 +0300 Subject: [PATCH] first ideas for notifications --- shared/global.cpp | 35 +++++++++++++++++++++++++++++++++++ shared/global.h | 14 ++++++++++++-- ui/models/roster.cpp | 4 ++-- ui/models/roster.h | 4 ++-- ui/squawk.cpp | 31 ++----------------------------- ui/squawk.h | 5 ++--- 6 files changed, 55 insertions(+), 38 deletions(-) diff --git a/shared/global.cpp b/shared/global.cpp index 122bc79..14ae90d 100644 --- a/shared/global.cpp +++ b/shared/global.cpp @@ -19,6 +19,7 @@ #include "global.h" #include "enums.h" +#include "ui/models/roster.h" Shared::Global* Shared::Global::instance = 0; const std::set Shared::Global::supportedImagesExts = {"png", "jpg", "webp", "jpeg", "gif", "svg"}; @@ -94,6 +95,8 @@ Shared::Global::Global(): }), defaultSystemStyle(QApplication::style()->objectName()), defaultSystemPalette(QApplication::palette()), + rosterModel(new Models::Roster()), + dbus("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", QDBusConnection::sessionBus()), pluginSupport({ {"KWallet", false}, {"openFileManagerWindowJob", false}, @@ -349,6 +352,38 @@ void Shared::Global::setStyle(const QString& style) } } +void Shared::Global::notify(const QString& account, const Shared::Message& msg) +{ + QString name = QString(instance->rosterModel->getContactName(account, msg.getPenPalJid())); + QString path = QString(instance->rosterModel->getContactIconPath(account, msg.getPenPalJid(), msg.getPenPalResource())); + QVariantList args; + args << QString(QCoreApplication::applicationName()); + args << QVariant(QVariant::UInt); //TODO some normal id + if (path.size() > 0) { + args << path; + } else { + args << QString("mail-message"); //TODO should here better be unknown user icon? + } + if (msg.getType() == Shared::Message::groupChat) { + args << msg.getFromResource() + " from " + name; + } else { + args << name; + } + + QString body(msg.getBody()); + QString oob(msg.getOutOfBandUrl()); + if (body == oob) { + body = tr("Attached file"); + } + + args << body; + args << QStringList(); + args << QVariantMap(); + args << 3000; + instance->dbus.callWithArgumentList(QDBus::AutoDetect, "Notify", args); +} + + #define FROM_INT_INPL(Enum) \ template<> \ Enum Shared::Global::fromInt(int src) \ diff --git a/shared/global.h b/shared/global.h index 2056639..fcd8105 100644 --- a/shared/global.h +++ b/shared/global.h @@ -42,12 +42,18 @@ #include #include #include +#include + +class Squawk; +namespace Models { + class Roster; +} namespace Shared { class Global { Q_DECLARE_TR_FUNCTIONS(Global) - + friend class ::Squawk; public: struct FileInfo { enum class Preview { @@ -64,7 +70,9 @@ namespace Shared { }; Global(); - + + static void notify(const QString& account, const Shared::Message& msg); + static Global* getInstance(); static QString getName(Availability av); static QString getName(ConnectionState cs); @@ -122,6 +130,8 @@ namespace Shared { private: static Global* instance; + Models::Roster* rosterModel; + QDBusInterface dbus; std::map pluginSupport; std::map fileCache; diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index 1355fe3..e5ada43 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -763,7 +763,7 @@ void Models::Roster::removeAccount(const QString& account) acc->deleteLater(); } -QString Models::Roster::getContactName(const QString& account, const QString& jid) +QString Models::Roster::getContactName(const QString& account, const QString& jid) const { ElId id(account, jid); std::map::const_iterator cItr = contacts.find(id); @@ -907,7 +907,7 @@ bool Models::Roster::groupHasContact(const QString& account, const QString& grou } } -QString Models::Roster::getContactIconPath(const QString& account, const QString& jid, const QString& resource) +QString Models::Roster::getContactIconPath(const QString& account, const QString& jid, const QString& resource) const { ElId id(account, jid); std::map::const_iterator cItr = contacts.find(id); diff --git a/ui/models/roster.h b/ui/models/roster.h index 08d5afc..28f4d30 100644 --- a/ui/models/roster.h +++ b/ui/models/roster.h @@ -65,7 +65,7 @@ public: void addRoomParticipant(const QString& account, const QString& jid, const QString& name, const QMap& data); void changeRoomParticipant(const QString& account, const QString& jid, const QString& name, const QMap& data); void removeRoomParticipant(const QString& account, const QString& jid, const QString& name); - QString getContactName(const QString& account, const QString& jid); + QString getContactName(const QString& account, const QString& jid) const; QVariant data ( const QModelIndex& index, int role ) const override; Qt::ItemFlags flags(const QModelIndex &index) const override; @@ -77,7 +77,7 @@ public: std::deque groupList(const QString& account) const; bool groupHasContact(const QString& account, const QString& group, const QString& contactJID) const; - QString getContactIconPath(const QString& account, const QString& jid, const QString& resource); + QString getContactIconPath(const QString& account, const QString& jid, const QString& resource) const; Account* getAccount(const QString& name); QModelIndex getAccountIndex(const QString& name); QModelIndex getGroupIndex(const QString& account, const QString& name); diff --git a/ui/squawk.cpp b/ui/squawk.cpp index a0f16b2..a08f38b 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -28,10 +28,9 @@ Squawk::Squawk(QWidget *parent) : preferences(nullptr), about(nullptr), dialogueQueue(this), - rosterModel(), + rosterModel(*(Shared::Global::getInstance()->rosterModel)), conversations(), contextMenu(new QMenu()), - dbus("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", QDBusConnection::sessionBus()), vCards(), currentConversation(nullptr), restoreSelection(), @@ -441,33 +440,7 @@ void Squawk::changeMessage(const QString& account, const QString& jid, const QSt void Squawk::notify(const QString& account, const Shared::Message& msg) { - QString name = QString(rosterModel.getContactName(account, msg.getPenPalJid())); - QString path = QString(rosterModel.getContactIconPath(account, msg.getPenPalJid(), msg.getPenPalResource())); - QVariantList args; - args << QString(QCoreApplication::applicationName()); - args << QVariant(QVariant::UInt); //TODO some normal id - if (path.size() > 0) { - args << path; - } else { - args << QString("mail-message"); //TODO should here better be unknown user icon? - } - if (msg.getType() == Shared::Message::groupChat) { - args << msg.getFromResource() + " from " + name; - } else { - args << name; - } - - QString body(msg.getBody()); - QString oob(msg.getOutOfBandUrl()); - if (body == oob) { - body = tr("Attached file"); - } - - args << body; - args << QStringList(); - args << QVariantMap(); - args << 3000; - dbus.callWithArgumentList(QDBus::AutoDetect, "Notify", args); + Shared::Global::notify(account, msg); } void Squawk::onConversationMessage(const Shared::Message& msg) diff --git a/ui/squawk.h b/ui/squawk.h index 5a77f17..aa52153 100644 --- a/ui/squawk.h +++ b/ui/squawk.h @@ -22,7 +22,6 @@ #include #include #include -#include #include #include @@ -43,6 +42,7 @@ #include "dialogqueue.h" #include "shared/shared.h" +#include "shared/global.h" namespace Ui { class Squawk; @@ -124,10 +124,9 @@ private: Settings* preferences; About* about; DialogQueue dialogueQueue; - Models::Roster rosterModel; + Models::Roster& rosterModel; Conversations conversations; QMenu* contextMenu; - QDBusInterface dbus; std::map vCards; Conversation* currentConversation; QModelIndex restoreSelection;