SimpleQmlNotify/notificationservice.cpp

113 lines
3.4 KiB
C++
Raw Normal View History

2020-05-23 02:30:51 +03:00
/*
2022-12-31 11:28:53 +03:00
* Copyright (C) 2018-2023 QuasarApp.
2020-05-23 02:30:51 +03:00
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
2019-11-19 15:20:38 +03:00
#include "notificationservice.h"
2021-10-21 18:46:18 +03:00
2023-04-10 14:59:27 +03:00
2019-11-19 15:20:38 +03:00
namespace QmlNotificationService {
NotificationService::NotificationService(QObject * ptr): QObject (ptr) {
qRegisterMetaType<NotificationData>("NotificationData");
qRegisterMetaType<QList<NotificationData>> ("QList<NotificationData>");
2023-04-10 14:59:27 +03:00
qRegisterMetaType<QSharedPointer<HistoryNotificationsModel>>("QSharedPointer<HistoryNotificationsModel>");
setNotify("Test", "Test");
2019-11-19 15:20:38 +03:00
}
NotificationData NotificationService::notify() const {
return _notify;
}
2021-01-18 23:20:03 +03:00
NotificationData NotificationService::question() const {
return _question;
}
2019-11-19 15:20:38 +03:00
void NotificationService::setNotify(const NotificationData& notify) {
if (_notify != notify) {
2023-04-10 15:06:06 +03:00
_history->addHistoryObject(notify);
}
2019-11-19 15:20:38 +03:00
_notify = notify;
emit notifyChanged();
}
2021-10-21 18:46:18 +03:00
int NotificationService::setQuestion(const Listner& listner, const NotificationData &question) {
2021-01-18 23:20:03 +03:00
_question = question;
2021-10-21 18:46:18 +03:00
int questionCode = rand();
_question.setCode(questionCode);
2021-01-18 23:20:03 +03:00
emit questionChanged();
2021-10-21 18:46:18 +03:00
_listners[questionCode] = listner;
return _question.type();
2021-01-18 23:20:03 +03:00
}
void NotificationService::questionComplete(bool accepted, int code) {
2021-10-21 18:46:18 +03:00
if (_listners.contains(code)) {
auto listner = _listners.value(code);
listner(accepted);
_listners.remove(code);
}
2021-01-18 23:20:03 +03:00
emit questionCompleted(accepted, code);
}
2019-11-19 15:20:38 +03:00
void NotificationService::setNotify(const QString &title,
const QString &text,
const QString &img,
int type) {
setNotify(NotificationData(title, text, img,
static_cast<NotificationData::Type>(type)));
}
2021-10-21 18:46:18 +03:00
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));
2021-01-18 23:20:03 +03:00
}
2021-11-23 21:49:27 +03:00
QString NotificationService::libVersion() {
return QML_NOTIFY_VERSION;
}
2019-11-19 15:20:38 +03:00
NotificationService *NotificationService::getService() {
static auto service = new NotificationService;
return service;
}
2023-04-10 15:06:06 +03:00
QObject *NotificationService::history() const{
return _history.data();
}
2019-11-19 15:20:38 +03:00
}