From 4d91eb4a85026b7cb85f1e452bafb42f53419f99 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Wed, 16 Nov 2016 10:46:52 +0100 Subject: [PATCH] feat(app): provide a custom notifier --- tests/CMakeLists.txt | 4 +-- tests/src/app/App.cpp | 27 ++----------------- tests/src/app/App.hpp | 6 ++--- .../Notifier.cpp} | 20 +++++++------- .../Notifier.hpp} | 19 ++++++------- tests/ui/views/App/MainWindow/Contacts.qml | 2 +- 6 files changed, 25 insertions(+), 53 deletions(-) rename tests/src/components/{notification/Notification.cpp => notifier/Notifier.cpp} (84%) rename tests/src/components/{notification/Notification.hpp => notifier/Notifier.hpp} (58%) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d1072a064..c89bbb7eb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -34,7 +34,7 @@ set(SOURCES src/components/contacts/ContactsListModel.cpp src/components/contacts/ContactsListProxyModel.cpp src/components/linphone/LinphoneCore.cpp - src/components/notification/Notification.cpp + src/components/notifier/Notifier.cpp src/components/settings/AccountSettingsModel.cpp src/components/settings/SettingsModel.cpp src/components/timeline/TimelineModel.cpp @@ -49,7 +49,7 @@ set(HEADERS src/components/contacts/ContactsListModel.hpp src/components/contacts/ContactsListProxyModel.hpp src/components/linphone/LinphoneCore.hpp - src/components/notification/Notification.hpp + src/components/notifier/Notifier.hpp src/components/presence/Presence.hpp src/components/settings/AccountSettingsModel.hpp src/components/settings/SettingsModel.hpp diff --git a/tests/src/app/App.cpp b/tests/src/app/App.cpp index d527d4013..dcb762333 100644 --- a/tests/src/app/App.cpp +++ b/tests/src/app/App.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -57,9 +56,6 @@ void App::initContentApp () { qWarning("System tray not found on this system."); else setTrayIcon(); - - // Set notification attr. - setNotificationAttributes(); } void App::registerTypes () { @@ -100,8 +96,8 @@ void App::addContextProperties () { // Other. context->setContextProperty("LinphoneCore", LinphoneCore::getInstance()); - m_notification = new Notification(); - context->setContextProperty("Notification", m_notification); + m_notifier = new Notifier(); + context->setContextProperty("Notifier", m_notifier); } void App::setTrayIcon () { @@ -137,22 +133,3 @@ void App::setTrayIcon () { m_system_tray_icon->setToolTip("Linphone"); m_system_tray_icon->show(); } - -void App::setNotificationAttributes () { - if (!m_system_tray_icon) { - return; - } - - QDesktopWidget *desktop = QApplication::desktop(); - - QRect icon_rect = m_system_tray_icon->geometry(); - QRect screen_rect = desktop->screenGeometry(); - - int x = icon_rect.x() / 2 + icon_rect.width() / 2; - int y = icon_rect.y() / 2 + icon_rect.height() / 2; - - Qt::Edges edge = (x < screen_rect.width() / 2) ? Qt::LeftEdge : Qt::RightEdge; - edge |= (y < screen_rect.height() / 2) ? Qt::TopEdge : Qt::BottomEdge; - - m_notification->setEdge(edge); -} diff --git a/tests/src/app/App.hpp b/tests/src/app/App.hpp index a236dec2d..1ff86295a 100644 --- a/tests/src/app/App.hpp +++ b/tests/src/app/App.hpp @@ -7,7 +7,7 @@ #include #include -#include "../components/notification/Notification.hpp" +#include "../components/notifier/Notifier.hpp" // =================================================================== @@ -37,14 +37,12 @@ private: void addContextProperties (); void setTrayIcon (); - void setNotificationAttributes (); - QQmlApplicationEngine m_engine; QQmlFileSelector *m_file_selector = nullptr; QSystemTrayIcon *m_system_tray_icon = nullptr; QTranslator m_translator; - Notification *m_notification = nullptr; + Notifier *m_notifier = nullptr; static App *m_instance; }; diff --git a/tests/src/components/notification/Notification.cpp b/tests/src/components/notifier/Notifier.cpp similarity index 84% rename from tests/src/components/notification/Notification.cpp rename to tests/src/components/notifier/Notifier.cpp index a9a23161f..a29322bf7 100644 --- a/tests/src/components/notification/Notification.cpp +++ b/tests/src/components/notifier/Notifier.cpp @@ -2,7 +2,7 @@ #include #include "../../app/App.hpp" -#include "Notification.hpp" +#include "Notifier.hpp" #define NOTIFICATION_SHOW_METHOD_NAME "show" @@ -18,7 +18,7 @@ // Helpers. inline int getNotificationSize (const QObject &object, const char *property) { - QVariant variant = object.property(property); + QVariant variant(object.property(property)); bool so_far_so_good; int size = variant.toInt(&so_far_so_good); @@ -44,17 +44,17 @@ bool setProperty (QObject &object, const char *property, const T &value) { // ------------------------------------------------------------------- -Notification::Notification (QObject *parent) : +Notifier::Notifier (QObject *parent) : QObject(parent) { QQmlEngine *engine = App::getInstance()->getEngine(); // Build components. - m_components[Notification::Call] = new QQmlComponent( + m_components[Notifier::Call] = new QQmlComponent( engine, QUrl("qrc:/ui/modules/Linphone/Notifications/CallNotification.qml") ); // Check errors. - for (int i = 0; i < Notification::MaxNbTypes; i++) { + for (int i = 0; i < Notifier::MaxNbTypes; i++) { QQmlComponent &component = *m_components[i]; if (component.isError()) { qWarning() << "Errors found in `Notification` component " @@ -64,14 +64,14 @@ Notification::Notification (QObject *parent) : } } -Notification::~Notification () { - for (int i = 0; i < Notification::MaxNbTypes; i++) +Notifier::~Notifier () { + for (int i = 0; i < Notifier::MaxNbTypes; i++) delete m_components[i]; } // ------------------------------------------------------------------- -void Notification::showCallMessage ( +void Notifier::showCallMessage ( int timeout, const QString &sip_address ) { @@ -88,12 +88,12 @@ void Notification::showCallMessage ( } // Create instance and set attributes. - QObject *object = m_components[Notification::Call]->create(); + QObject *object = m_components[Notifier::Call]->create(); int offset = getNotificationSize(*object, NOTIFICATION_HEIGHT_PROPERTY); if ( offset == -1 || - !::setProperty(*object, NOTIFICATION_EDGE_PROPERTY_NAME, m_edge) || + !::setProperty(*object, NOTIFICATION_EDGE_PROPERTY_NAME, Qt::TopEdge | Qt::RightEdge) || !::setProperty(*object, NOTIFICATION_OFFSET_PROPERTY_NAME, m_offset) ) { delete object; diff --git a/tests/src/components/notification/Notification.hpp b/tests/src/components/notifier/Notifier.hpp similarity index 58% rename from tests/src/components/notification/Notification.hpp rename to tests/src/components/notifier/Notifier.hpp index 104e5cfd0..25830b9ce 100644 --- a/tests/src/components/notification/Notification.hpp +++ b/tests/src/components/notifier/Notifier.hpp @@ -1,5 +1,5 @@ -#ifndef NOTIFICATION_H_ -#define NOTIFICATION_H_ +#ifndef NOTIFIER_H_ +#define NOTIFIER_H_ #include #include @@ -7,12 +7,12 @@ // =================================================================== -class Notification : public QObject { +class Notifier : public QObject { Q_OBJECT; public: - Notification (QObject *parent = Q_NULLPTR); - virtual ~Notification (); + Notifier (QObject *parent = Q_NULLPTR); + virtual ~Notifier (); enum Type { Call, @@ -20,15 +20,12 @@ public: }; Q_ENUM(Type); - void setEdge (Qt::Edges edge) { - m_edge = edge; - } - public slots: void showCallMessage (int timeout, const QString &sip_address); private: - Qt::Edges m_edge = Qt::RightEdge | Qt::TopEdge; + void computePositions (); + QQmlComponent *m_components[MaxNbTypes]; int m_offset = 0; @@ -36,4 +33,4 @@ private: QMutex m_mutex; }; -#endif // NOTIFICATION_H_ +#endif // NOTIFIER_H_ diff --git a/tests/ui/views/App/MainWindow/Contacts.qml b/tests/ui/views/App/MainWindow/Contacts.qml index 13598c324..d42206755 100644 --- a/tests/ui/views/App/MainWindow/Contacts.qml +++ b/tests/ui/views/App/MainWindow/Contacts.qml @@ -36,7 +36,7 @@ ColumnLayout { }) } - spacing: Notification.showCallMessage(5000, "toto@toto.com") || 0 + spacing: Notifier.showCallMessage(5000, "toto@toto.com") || 0 // ----------------------------------------------------------------- // Search Bar & actions.