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