Debian Codegraph @

src/GUI/LauncherNotificationsController.cxx

raw symbols SCIP
1 // Written by James Turner, started October 2020
2 //
3 // Copyright (C) 2020 James Turner <[email protected]>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 #include "LauncherNotificationsController.hxx"
20
21 #include <QAbstractListModel>
22 #include <QDebug>
23 #include <QQmlEngine>
24 #include <QSettings>
25
26 #include "SettingsWrapper.hxx"
27
28 static LauncherNotificationsController* static_instance = nullptr;
29
30 namespace {
31
32 const int IdRole = Qt::UserRole + 1;
33 const int SourceRole = Qt::UserRole + 2;
34 const int ArgsRole = Qt::UserRole + 3;
35
36 } // namespace
37
38 class LauncherNotificationsController::NotificationsModel : public QAbstractListModel
39 {
40 public:
41 int rowCount(const QModelIndex&) const override
42 {
43 return static_cast<int>(_data.size());
44 }
45
46 QVariant data(const QModelIndex& index, int role) const override
47 {
48 const int row = index.row();
49 if ((row < 0) || (row >= static_cast<int>(_data.size()))) {
50 return {};
51 }
52
53 const auto& d = _data.at(row);
54 switch (role) {
55 case IdRole: return d.id;
56 case SourceRole: return d.source;
57 case ArgsRole: return QVariant::fromValue(d.args);
58 default:
59 break;
60 }
61
62 return {};
63 }
64
65 QHash<int, QByteArray> roleNames() const override
66 {
67 QHash<int, QByteArray> result = QAbstractListModel::roleNames();
68 result[IdRole] = "id";
69 result[SourceRole] = "source";
70 result[ArgsRole] = "args";
71 return result;
72 }
73
74 void removeIndex(int row)
75 {
76 // work aroud the role-by-role destruction order of model data
77 // clear out the source first so the Loader unloads, before we
78 // null args. This avoids 'args is null' warnings from the loaded
79 // notification
80 _data[row].source.clear();
81 const auto idx = index(row, 0);
82 emit dataChanged(idx, idx, {SourceRole});
83
84 // now we can remove everything else
85 beginRemoveRows({}, row, row);
86 _data.erase(_data.begin() + row);
87 endRemoveRows();
88 }
89
90 void append(QString id, QUrl source, QJSValue args)
91 {
92 const int newRow = static_cast<int>(_data.size());
93 beginInsertRows({}, newRow, newRow);
94 _data.push_back({id, source, args});
95 endInsertRows();
96 }
97
98 struct Data {
99 QString id;
100 QUrl source;
101 QJSValue args;
102 };
103
104 std::vector<Data> _data;
105 };
106
107 LauncherNotificationsController::LauncherNotificationsController(QObject* pr, QQmlEngine* engine) : QObject(pr)
108 {
109 Q_ASSERT(static_instance == nullptr);
110 static_instance = this;
111
112 _model = new NotificationsModel;
113
114 _qmlEngine = engine;
115 }
116
117 LauncherNotificationsController::~LauncherNotificationsController()
118 {
119 static_instance = nullptr;
120 }
121
122 LauncherNotificationsController* LauncherNotificationsController::instance()
123 {
124 return static_instance;
125 }
126
127 QAbstractItemModel* LauncherNotificationsController::notifications() const
128 {
129 return _model;
130 }
131
132 QJSValue LauncherNotificationsController::argsForIndex(int index) const
133 {
134 if ((index < 0) || (index >= static_cast<int>(_model->_data.size()))) {
135 return {};
136 }
137 const auto& d = _model->_data.at(index);
138 qDebug() << Q_FUNC_INFO << index;
139 return d.args;
140 }
141
142 QJSEngine* LauncherNotificationsController::jsEngine()
143 {
144 return _qmlEngine;
145 }
146
147 void LauncherNotificationsController::dismissIndex(int index)
148 {
149 const auto& d = _model->_data.at(index);
150
151 // if the notificsation supports persistent dismissal, then record this
152 // fact in the global settings, so we don't show it again.
153 // restore defaults will of course clear these settings, but that's
154 // desirable anyway.
155 if (d.args.property("persistent-dismiss").toBool()) {
156 auto settings = flightgear::getQSettings();
157 settings.beginGroup("dismissed-notifications");
158 settings.setValue(d.id, true);
159 }
160
161 _model->removeIndex(index);
162 }
163
164 void LauncherNotificationsController::postNotification(QString id, QUrl source, QJSValue args)
165 {
166 const bool supportsPersistentDismiss = args.property("persistent-dismiss").toBool();
167 if (supportsPersistentDismiss) {
168 auto settings = flightgear::getQSettings();
169 settings.beginGroup("dismissed-notifications");
170 bool alreadyDimissed = settings.value(id).toBool();
171 if (alreadyDimissed) {
172 return;
173 }
174 }
175
176 _model->append(id, source, args);
177 }