1 #include "asemannativenotification.h"
2 #include "asemannativenotificationitem.h"
3 
4 class AsemanNativeNotificationPrivate
5 {
6 public:
7     QHash<uint, AsemanNativeNotificationItem*> items;
8     uint last_id;
9 
10     QColor color;
11 };
12 
AsemanNativeNotification(QObject * parent)13 AsemanNativeNotification::AsemanNativeNotification(QObject *parent) :
14     QObject(parent)
15 {
16     p = new AsemanNativeNotificationPrivate;
17     p->last_id = 1000;
18 }
19 
setColor(const QColor & color)20 void AsemanNativeNotification::setColor(const QColor &color)
21 {
22     if(p->color == color)
23         return;
24 
25     p->color = color;
26     emit colorChanged();
27 }
28 
color() const29 QColor AsemanNativeNotification::color() const
30 {
31     return p->color;
32 }
33 
sendNotify(const QString & title,const QString & body,const QString & icon,uint replace_id,int timeOut,const QStringList & actions)34 uint AsemanNativeNotification::sendNotify(const QString &title, const QString &body, const QString &icon, uint replace_id, int timeOut, const QStringList &actions)
35 {
36     uint result = replace_id;
37 
38     AsemanNativeNotificationItem *item = p->items.value(replace_id);
39     if(!item)
40     {
41         item = new AsemanNativeNotificationItem();
42         item->setFixedWidth(400);
43         item->setColor(p->color);
44 
45         p->items.insert(p->last_id, item);
46 
47         result = p->last_id;
48         p->last_id++;
49 
50         connect(item, SIGNAL(destroyed()), SLOT(itemClosed()) );
51         connect(item, SIGNAL(actionTriggered(QString)), SLOT(actionTriggered(QString)) );
52     }
53 
54     item->setTitle(title);
55     item->setBody(body);
56     item->setIcon(icon);
57     item->setActions(actions);
58     item->setTimeOut(timeOut);
59     item->show();
60 
61     return result;
62 }
63 
closeNotification(uint id)64 void AsemanNativeNotification::closeNotification(uint id)
65 {
66     AsemanNativeNotificationItem *item = p->items.value(id);
67     if(!id)
68         return;
69 
70     item->close();
71 }
72 
itemClosed()73 void AsemanNativeNotification::itemClosed()
74 {
75     AsemanNativeNotificationItem *item = static_cast<AsemanNativeNotificationItem*>(sender());
76     if(!item)
77         return;
78 
79     uint id = p->items.key(item);
80     if(!id)
81         return;
82 
83     p->items.remove(id);
84     emit notifyClosed(id);
85 }
86 
actionTriggered(const QString & action)87 void AsemanNativeNotification::actionTriggered(const QString &action)
88 {
89     AsemanNativeNotificationItem *item = static_cast<AsemanNativeNotificationItem*>(sender());
90     if(!item)
91         return;
92 
93     uint id = p->items.key(item);
94     if(!id)
95         return;
96 
97     emit notifyAction(id, action);
98     item->close();
99 }
100 
~AsemanNativeNotification()101 AsemanNativeNotification::~AsemanNativeNotification()
102 {
103     delete p;
104 }
105