qtui: Clean up SystemTray
[quassel.git] / src / qtui / qtui.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU 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                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "qtui.h"
22
23 #include <QApplication>
24 #include <QFile>
25 #include <QFileInfo>
26 #include <QIcon>
27 #include <QStringList>
28
29 #include "abstractnotificationbackend.h"
30 #include "buffermodel.h"
31 #include "chatlinemodel.h"
32 #include "contextmenuactionprovider.h"
33 #include "mainwin.h"
34 #include "qtuimessageprocessor.h"
35 #include "qtuisettings.h"
36 #include "qtuistyle.h"
37 #include "systemtray.h"
38 #include "toolbaractionprovider.h"
39 #include "types.h"
40 #include "util.h"
41
42 QtUi *QtUi::_instance = nullptr;
43 MainWin *QtUi::_mainWin = nullptr;
44 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
45 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
46
47 QtUi::QtUi()
48     : GraphicalUi()
49     , _systemIconTheme{QIcon::themeName()}
50 {
51     if (_instance != nullptr) {
52         qWarning() << "QtUi has been instantiated again!";
53         return;
54     }
55     _instance = this;
56
57     if (Quassel::isOptionSet("icontheme")) {
58         _systemIconTheme = Quassel::optionValue("icontheme");
59         QIcon::setThemeName(_systemIconTheme);
60     }
61
62     QtUiSettings uiSettings;
63     Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
64
65     setupIconTheme();
66
67     QApplication::setWindowIcon(QIcon::fromTheme("quassel"));
68
69     setContextMenuActionProvider(new ContextMenuActionProvider(this));
70     setToolBarActionProvider(new ToolBarActionProvider(this));
71
72     setUiStyle(new QtUiStyle(this));
73     _mainWin = new MainWin();
74
75     setMainWidget(_mainWin);
76
77     connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
78     connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
79     connect(Client::instance(), SIGNAL(bufferMarkedAsRead(BufferId)), SLOT(closeNotifications(BufferId)));
80 }
81
82
83 QtUi::~QtUi()
84 {
85     unregisterAllNotificationBackends();
86     delete _mainWin;
87     _mainWin = nullptr;
88     _instance = nullptr;
89 }
90
91
92 void QtUi::init()
93 {
94     _mainWin->init();
95     QtUiSettings uiSettings;
96     uiSettings.initAndNotify("UseSystemTrayIcon", this, SLOT(useSystemTrayChanged(QVariant)), true);
97
98     GraphicalUi::init(); // needs to be called after the mainWin is initialized
99 }
100
101
102 MessageModel *QtUi::createMessageModel(QObject *parent)
103 {
104     return new ChatLineModel(parent);
105 }
106
107
108 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent)
109 {
110     return new QtUiMessageProcessor(parent);
111 }
112
113
114 void QtUi::connectedToCore()
115 {
116     _mainWin->connectedToCore();
117 }
118
119
120 void QtUi::disconnectedFromCore()
121 {
122     _mainWin->disconnectedFromCore();
123     GraphicalUi::disconnectedFromCore();
124 }
125
126
127 void QtUi::useSystemTrayChanged(const QVariant &v)
128 {
129     _useSystemTray = v.toBool();
130     SystemTray *tray = mainWindow()->systemTray();
131     if (_useSystemTray) {
132         if (tray->isSystemTrayAvailable())
133             tray->setVisible(true);
134     }
135     else {
136         if (tray->isSystemTrayAvailable() && mainWindow()->isVisible())
137             tray->setVisible(false);
138     }
139 }
140
141
142 bool QtUi::haveSystemTray()
143 {
144     return mainWindow()->systemTray()->isSystemTrayAvailable() && instance()->_useSystemTray;
145 }
146
147
148 bool QtUi::isHidingMainWidgetAllowed() const
149 {
150     return haveSystemTray();
151 }
152
153
154 void QtUi::minimizeRestore(bool show)
155 {
156     SystemTray *tray = mainWindow()->systemTray();
157     if (show) {
158         if (tray && !_useSystemTray)
159             tray->setVisible(false);
160     }
161     else {
162         if (tray && _useSystemTray)
163             tray->setVisible(true);
164     }
165     GraphicalUi::minimizeRestore(show);
166 }
167
168
169 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend)
170 {
171     if (!_notificationBackends.contains(backend)) {
172         _notificationBackends.append(backend);
173         instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
174     }
175 }
176
177
178 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend)
179 {
180     _notificationBackends.removeAll(backend);
181 }
182
183
184 void QtUi::unregisterAllNotificationBackends()
185 {
186     _notificationBackends.clear();
187 }
188
189
190 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends()
191 {
192     return _notificationBackends;
193 }
194
195
196 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString &sender, const QString &text)
197 {
198     static int notificationId = 0;
199
200     AbstractNotificationBackend::Notification notification(++notificationId, bufId, type, sender, text);
201     _notifications.append(notification);
202     foreach(AbstractNotificationBackend *backend, _notificationBackends)
203     backend->notify(notification);
204     return notificationId;
205 }
206
207
208 void QtUi::closeNotification(uint notificationId)
209 {
210     QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
211     while (i != _notifications.end()) {
212         if (i->notificationId == notificationId) {
213             foreach(AbstractNotificationBackend *backend, _notificationBackends)
214             backend->close(notificationId);
215             i = _notifications.erase(i);
216         }
217         else ++i;
218     }
219 }
220
221
222 void QtUi::closeNotifications(BufferId bufferId)
223 {
224     QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
225     while (i != _notifications.end()) {
226         if (!bufferId.isValid() || i->bufferId == bufferId) {
227             foreach(AbstractNotificationBackend *backend, _notificationBackends)
228             backend->close(i->notificationId);
229             i = _notifications.erase(i);
230         }
231         else ++i;
232     }
233 }
234
235
236 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications()
237 {
238     return _notifications;
239 }
240
241
242 void QtUi::notificationActivated(uint notificationId)
243 {
244     if (notificationId != 0) {
245         QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
246         while (i != _notifications.end()) {
247             if (i->notificationId == notificationId) {
248                 BufferId bufId = i->bufferId;
249                 if (bufId.isValid())
250                     Client::bufferModel()->switchToBuffer(bufId);
251                 break;
252             }
253             ++i;
254         }
255     }
256     closeNotification(notificationId);
257
258     activateMainWidget();
259 }
260
261
262 void QtUi::bufferMarkedAsRead(BufferId bufferId)
263 {
264     if (bufferId.isValid()) {
265         closeNotifications(bufferId);
266     }
267 }
268
269
270 std::vector<std::pair<QString, QString>> QtUi::availableIconThemes() const
271 {
272     //: Supported icon theme names
273     static const std::vector<std::pair<QString, QString>> supported {
274         { "breeze", tr("Breeze") },
275         { "breeze-dark", tr("Breeze Dark") },
276         { "oxygen", tr("Oxygen") }
277     };
278
279     std::vector<std::pair<QString, QString>> result;
280     for (auto &&themePair : supported) {
281         for (auto &&dir : QIcon::themeSearchPaths()) {
282             if (QFileInfo{dir + "/" + themePair.first + "/index.theme"}.exists()) {
283                 result.push_back(themePair);
284                 break;
285             }
286         }
287     }
288
289     return result;
290 }
291
292
293 void QtUi::setupIconTheme()
294 {
295     // Add paths to our own icon sets to the theme search paths
296     QStringList themePaths = QIcon::themeSearchPaths();
297     themePaths.removeAll(":/icons");  // this should come last
298     for (auto &&dataDir : Quassel::dataDirPaths()) {
299         QString iconDir{dataDir + "icons"};
300         if (QFileInfo{iconDir}.isDir()) {
301             themePaths << iconDir;
302         }
303     }
304     themePaths << ":/icons";
305     QIcon::setThemeSearchPaths(themePaths);
306
307     refreshIconTheme();
308 }
309
310
311 void QtUi::refreshIconTheme()
312 {
313     // List of available fallback themes
314     QStringList availableThemes;
315     for (auto &&themePair : availableIconThemes()) {
316         availableThemes << themePair.first;
317     }
318
319     if (availableThemes.isEmpty()) {
320         // We could probably introduce a more sophisticated fallback handling, such as putting the "most important" icons into hicolor,
321         // but this just gets complex for no good reason. We really rely on a supported theme to be installed, if not system-wide, then
322         // as part of the Quassel installation (which is enabled by default anyway).
323         qWarning() << tr("No supported icon theme installed, you'll lack icons! Supported are the KDE/Plasma themes Breeze, Breeze Dark and Oxygen.");
324         return;
325     }
326
327     UiStyleSettings s;
328     QString fallbackTheme{s.value("Icons/FallbackTheme").toString()};
329
330     if (fallbackTheme.isEmpty() || !availableThemes.contains(fallbackTheme)) {
331         if (availableThemes.contains(_systemIconTheme)) {
332             fallbackTheme = _systemIconTheme;
333         }
334         else {
335             fallbackTheme = availableThemes.first();
336         }
337     }
338
339     if (_systemIconTheme.isEmpty() || _systemIconTheme == fallbackTheme || s.value("Icons/OverrideSystemTheme", false).toBool()) {
340         // We have a valid fallback theme and want to override the system theme (if it's even defined), so we're basically done
341         QIcon::setThemeName(fallbackTheme);
342         emit iconThemeRefreshed();
343         return;
344     }
345
346 #if QT_VERSION >= 0x050000
347     // At this point, we have a system theme that we don't want to override, but that may not contain all
348     // required icons.
349     // We create a dummy theme that inherits first from the system theme, then from the supported fallback.
350     // This rather ugly hack allows us to inject the fallback into the inheritance chain, so non-standard
351     // icons missing in the system theme will be filled in by the fallback.
352     // Since we can't get notified when the system theme changes, this means that a restart may be required
353     // to apply a theme change... but you can't have everything, I guess.
354     if (!_dummyThemeDir) {
355         _dummyThemeDir.reset(new QTemporaryDir{});
356         if (!_dummyThemeDir->isValid() || !QDir{_dummyThemeDir->path()}.mkpath("icons/quassel-icon-proxy/apps/32")) {
357             qWarning() << "Could not create temporary directory for proxying the system icon theme, using fallback";
358             QIcon::setThemeName(fallbackTheme);
359             emit iconThemeRefreshed();
360             return;
361         }
362         // Add this to XDG_DATA_DIRS, otherwise KIconLoader complains
363         auto xdgDataDirs = qgetenv("XDG_DATA_DIRS");
364         if (!xdgDataDirs.isEmpty())
365             xdgDataDirs += ":";
366         xdgDataDirs += _dummyThemeDir->path();
367         qputenv("XDG_DATA_DIRS", xdgDataDirs);
368
369         QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << _dummyThemeDir->path() + "/icons");
370     }
371
372     QFile indexFile{_dummyThemeDir->path() + "/icons/quassel-icon-proxy/index.theme"};
373     if (!indexFile.open(QFile::WriteOnly|QFile::Truncate)) {
374         qWarning() << "Could not create index file for proxying the system icon theme, using fallback";
375         QIcon::setThemeName(fallbackTheme);
376         emit iconThemeRefreshed();
377         return;
378     }
379
380     // Write a dummy index file that is sufficient to make QIconLoader happy
381     auto indexContents = QString{
382             "[Icon Theme]\n"
383             "Name=quassel-icon-proxy\n"
384             "Inherits=%1,%2\n"
385             "Directories=apps/32\n"
386             "[apps/32]\nSize=32\nType=Fixed\n"
387     }.arg(_systemIconTheme, fallbackTheme);
388     if (indexFile.write(indexContents.toLatin1()) < 0) {
389         qWarning() << "Could not write index file for proxying the system icon theme, using fallback";
390         QIcon::setThemeName(fallbackTheme);
391         emit iconThemeRefreshed();
392         return;
393     }
394     indexFile.close();
395     QIcon::setThemeName("quassel-icon-proxy");
396 #else
397     // Qt4 doesn't support QTemporaryDir. Since it's deprecated and slated to be removed soon anyway, we don't bother
398     // writing a replacement and simply don't support not overriding the system theme.
399     QIcon::setThemeName(fallbackTheme);
400     emit iconThemeRefreshed();
401 #endif
402 }