| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "miscellaneous/notificationfactory.h" |
| 4 | |
| 5 | #include "3rd-party/boolinq/boolinq.h" |
| 6 | #include "definitions/definitions.h" |
| 7 | #include "miscellaneous/application.h" |
| 8 | #include "miscellaneous/settings.h" |
| 9 | |
| 10 | #include <QRegularExpression> |
| 11 | |
| 12 | NotificationFactory::NotificationFactory(QObject* parent) : QObject(parent) {} |
| 13 | |
| 14 | QList<Notification> NotificationFactory::allNotifications() const { |
| 15 | return m_notifications; |
| 16 | } |
| 17 | |
| 18 | bool NotificationFactory::areNotificationsEnabled() const { |
| 19 | return qApp->settings()->value(GROUP(GUI), SETTING(GUI::EnableNotifications)).toBool(); |
| 20 | } |
| 21 | |
| 22 | bool NotificationFactory::useToastNotifications() const { |
| 23 | return qApp->settings()->value(GROUP(GUI), SETTING(GUI::UseToastNotifications)).toBool(); |
| 24 | } |
| 25 | |
| 26 | Notification NotificationFactory::notificationForEvent(Notification::Event event) const { |
| 27 | if (!qApp->settings()->value(GROUP(GUI), SETTING(GUI::EnableNotifications)).toBool()) { |
| 28 | return Notification(); |
| 29 | } |
| 30 | |
| 31 | auto good_n = boolinq::from(m_notifications).where([event](const Notification& n) { |
| 32 | return n.event() == event; |
| 33 | }); |
| 34 | |
| 35 | if (good_n.count() <= 0) { |
| 36 | qDebugNN << LOGSEC_CORE << "Notification for event" << QUOTE_W_SPACE(int(event)) << "not found"; |
| 37 | |
| 38 | return Notification(); |
| 39 | } |
| 40 | else { |
| 41 | return good_n.first(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void NotificationFactory::load(Settings* settings) { |
| 46 | auto notif_keys = settings->allKeys(GROUP(Notifications)).filter(QRegularExpression(QSL("^\\d+$"))); |
| 47 | |
| 48 | m_notifications.clear(); |
| 49 | |
| 50 | for (const auto& key : notif_keys) { |
| 51 | auto event = Notification::Event(key.toInt()); |
| 52 | auto data = settings->value(GROUP(Notifications), key).toStringList(); |
| 53 | auto balloon = data.at(0).toInt() != 0; |
| 54 | auto sound_path = data.at(1); |
| 55 | auto volume = data.size() > 2 ? data.at(2).toInt() : DEFAULT_NOTIFICATION_VOLUME; |
| 56 | auto play_sound = data.size() > 3 ? data.at(3).toInt() != 0 : true; |
| 57 | auto dialog = data.size() > 4 ? data.at(4).toInt() != 0 : false; |
| 58 | |
| 59 | m_notifications.append(Notification(event, balloon, dialog, play_sound, sound_path, volume)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void NotificationFactory::save(const QList<Notification>& new_notifications, Settings* settings) { |
| 64 | settings->remove(GROUP(Notifications)); |
| 65 | m_notifications = new_notifications; |
| 66 | |
| 67 | for (const auto& n : std::as_const(m_notifications)) { |
| 68 | settings->setValue(GROUP(Notifications), |
| 69 | QString::number(int(n.event())), |
| 70 | QStringList{n.balloonEnabled() ? QSL("1") : QSL("0"), |
| 71 | n.soundPath(), |
| 72 | QString::number(n.volume()), |
| 73 | n.soundEnabled() ? QSL("1") : QSL("0"), |
| 74 | n.dialogEnabled() ? QSL("1") : QSL("0")}); |
| 75 | } |
| 76 | } |