-
Notifications
You must be signed in to change notification settings - Fork 2
/
notificationservice.cpp
127 lines (96 loc) · 3.95 KB
/
notificationservice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (C) 2018-2024 QuasarApp.
* Distributed under the GPLv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "notificationservice.h"
namespace QmlNotificationService {
NotificationService::NotificationService(QObject * ptr): QObject (ptr) {
qRegisterMetaType<NotificationData>("NotificationData");
qRegisterMetaType<QList<NotificationData>> ("QList<NotificationData>");
_history = new HistoryNotificationsModel();
QQmlEngine::setObjectOwnership(_history, QQmlEngine::CppOwnership);
connect(_history, &HistoryNotificationsModel::rowsInserted, this, &NotificationService::countNotificationsChanged);
connect(_history, &HistoryNotificationsModel::rowsRemoved, this, &NotificationService::countNotificationsChanged);
connect(_history, &HistoryNotificationsModel::modelReset, this, &NotificationService::countNotificationsChanged);
}
NotificationService::~NotificationService() {
delete _history;
}
NotificationData NotificationService::notify() const {
return _notify;
}
NotificationData NotificationService::question() const {
return _question;
}
void NotificationService::setNotify(const NotificationData& notify) {
if (_notify != notify) {
_history->addHistoryObject(notify);
}
_notify = notify;
emit notifyChanged();
}
int NotificationService::setQuestion(const Listner& listner, const NotificationData &question) {
_question = question;
int questionCode = rand();
_question.setCode(questionCode);
emit questionChanged();
_listners[questionCode] = listner;
return _question.type();
}
void NotificationService::questionComplete(bool accepted, int code) {
if (_listners.contains(code)) {
auto listner = _listners.value(code);
listner(accepted);
_listners.remove(code);
}
emit questionCompleted(accepted, code);
}
void NotificationService::setNotify(const QString &title,
const QString &text,
const QString &img,
int type) {
setNotify(NotificationData(title, text, img,
static_cast<NotificationData::Type>(type)));
}
int NotificationService::setQuestion(QObject *listnerObject,
const QString &listnerMethod,
const QString &title,
const QString &text,
const QString &img,
int code) {
Listner listner = [listnerObject, listnerMethod](bool accept) {
const QByteArray stringData = listnerMethod.toLatin1();
char method[100];
method[qMin(99, stringData.size())] = '\0';
std::copy(stringData.constBegin(), stringData.constBegin() + qMin(99, stringData.size()), method);
QMetaObject::invokeMethod(listnerObject, method,
Qt::QueuedConnection, Q_ARG(bool, accept));
};
return setQuestion(listner, NotificationData(title, text, img, code));
}
int NotificationService::setQuestion(const Listner& listner,
const QString &title,
const QString &text,
const QString &img,
int code) {
return setQuestion(listner, NotificationData(title, text, img, code));
}
QString NotificationService::libVersion() {
return QML_NOTIFY_VERSION;
}
NotificationService *NotificationService::getService() {
static auto service = new NotificationService;
return service;
}
QObject *NotificationService::history() const {
return _history;
}
void NotificationService::showHistory() {
emit sigShowHistory();
}
int NotificationService::notificationsCount() const {
return _history->rowCount({});
}
}