Commit 41cab406 authored by Ronan Abhamon's avatar Ronan Abhamon

feat(notifier): supports visible property changed to false

parent fbfa00a3
#include <QQuickWindow>
#include <QTimer> #include <QTimer>
#include <QtDebug> #include <QtDebug>
...@@ -13,6 +14,7 @@ ...@@ -13,6 +14,7 @@
// Arbitrary hardcoded values. // Arbitrary hardcoded values.
#define NOTIFICATION_SPACING 10 #define NOTIFICATION_SPACING 10
#define N_MAX_NOTIFICATIONS 15 #define N_MAX_NOTIFICATIONS 15
#define MAX_TIMEOUT 60000
// =================================================================== // ===================================================================
...@@ -71,24 +73,18 @@ Notifier::~Notifier () { ...@@ -71,24 +73,18 @@ Notifier::~Notifier () {
// ------------------------------------------------------------------- // -------------------------------------------------------------------
void Notifier::showCallMessage ( QObject *Notifier::createNotification (Notifier::NotificationType type) {
int timeout,
const QString &sip_address
) {
qDebug() << "Show call notification message. (addr=" <<
sip_address << ")";
m_mutex.lock(); m_mutex.lock();
// Check existing instances. // Check existing instances.
if (m_n_instances >= N_MAX_NOTIFICATIONS) { if (m_n_instances >= N_MAX_NOTIFICATIONS) {
qWarning() << "Unable to create another notification"; qWarning() << "Unable to create another notification";
m_mutex.unlock(); m_mutex.unlock();
return; return nullptr;
} }
// Create instance and set attributes. // Create instance and set attributes.
QObject *object = m_components[Notifier::Call]->create(); QObject *object = m_components[type]->create();
int offset = getNotificationSize(*object, NOTIFICATION_HEIGHT_PROPERTY); int offset = getNotificationSize(*object, NOTIFICATION_HEIGHT_PROPERTY);
if ( if (
...@@ -97,7 +93,7 @@ void Notifier::showCallMessage ( ...@@ -97,7 +93,7 @@ void Notifier::showCallMessage (
) { ) {
delete object; delete object;
m_mutex.unlock(); m_mutex.unlock();
return; return nullptr;
} }
m_offset = (offset + m_offset) + NOTIFICATION_SPACING; m_offset = (offset + m_offset) + NOTIFICATION_SPACING;
...@@ -105,22 +101,63 @@ void Notifier::showCallMessage ( ...@@ -105,22 +101,63 @@ void Notifier::showCallMessage (
m_mutex.unlock(); m_mutex.unlock();
return object;
}
void Notifier::showNotification (QObject *notification, int timeout) {
if (timeout > MAX_TIMEOUT) {
timeout = MAX_TIMEOUT;
}
// Display notification. // Display notification.
QMetaObject::invokeMethod( QMetaObject::invokeMethod(
object, NOTIFICATION_SHOW_METHOD_NAME, notification, NOTIFICATION_SHOW_METHOD_NAME,
Qt::DirectConnection Qt::DirectConnection
); );
// Destroy it after timeout. // Called explicitly (by a click on notification for example)
QTimer::singleShot(timeout, this, [object, this]() { // or when single shot happen and if notification is visible.
delete object; QObject::connect(
notification->findChild<QQuickWindow *>(),
&QQuickWindow::visibleChanged,
[this](const bool &value) {
qDebug() << "Update notifications counter, hidden notification detected.";
m_mutex.lock(); if (value) {
m_n_instances--; qFatal("A notification cannot be visible twice!");
return;
}
if (m_n_instances == 0) m_mutex.lock();
m_offset = 0;
m_mutex.unlock(); m_n_instances--;
if (m_n_instances == 0)
m_offset = 0;
m_mutex.unlock();
}
);
// Destroy it after timeout.
QTimer::singleShot(timeout, this, [notification]() {
delete notification;
}); });
} }
// -------------------------------------------------------------------
void Notifier::showCallMessage (
int timeout,
const QString &sip_address
) {
qDebug() << "Show call notification message. (addr=" <<
sip_address << ")";
QObject *object = createNotification(Notifier::Call);
if (!object)
return;
showNotification(object, timeout);
}
...@@ -14,20 +14,23 @@ public: ...@@ -14,20 +14,23 @@ public:
Notifier (QObject *parent = Q_NULLPTR); Notifier (QObject *parent = Q_NULLPTR);
virtual ~Notifier (); virtual ~Notifier ();
enum Type { enum NotificationType {
Call, Call,
MaxNbTypes MaxNbTypes
}; };
Q_ENUM(Type);
public slots: public slots:
void showCallMessage (int timeout, const QString &sip_address); void showCallMessage (int timeout, const QString &sip_address);
private: private:
QObject *createNotification (NotificationType type);
void handleNotificationHidden ();
void showNotification (QObject *notification, int timeout);
QQmlComponent *m_components[MaxNbTypes]; QQmlComponent *m_components[MaxNbTypes];
int m_offset = 0; int m_offset = 0;
int m_n_instances = 0; unsigned int m_n_instances = 0;
QMutex m_mutex; QMutex m_mutex;
}; };
......
...@@ -13,7 +13,7 @@ TestCase { ...@@ -13,7 +13,7 @@ TestCase {
function test_notificationHeightProperty () { function test_notificationHeightProperty () {
compare(Utils.isInteger(notification.notificationHeight), true) compare(Utils.isInteger(notification.notificationHeight), true)
} }
function test_notificationOffsetProperty () { function test_notificationOffsetProperty () {
compare(Utils.isInteger(notification.notificationOffset), true) compare(Utils.isInteger(notification.notificationOffset), true)
...@@ -22,4 +22,8 @@ TestCase { ...@@ -22,4 +22,8 @@ TestCase {
function test_notificationShowMethod () { function test_notificationShowMethod () {
compare(Utils.isFunction(notification.show), true) compare(Utils.isFunction(notification.show), true)
} }
function test_childWindow () {
compare(Utils.qmlTypeof(notification.data[0], 'QQuickWindowQmlImpl'), true)
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment