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