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