1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
23 #include <QApplication>
27 #include <QStringList>
29 #include "abstractnotificationbackend.h"
30 #include "buffermodel.h"
31 #include "chatlinemodel.h"
32 #include "contextmenuactionprovider.h"
34 #include "qtuimessageprocessor.h"
35 #include "qtuisettings.h"
36 #include "qtuistyle.h"
37 #include "systemtray.h"
38 #include "toolbaractionprovider.h"
42 QtUi *QtUi::_instance = nullptr;
43 MainWin *QtUi::_mainWin = nullptr;
44 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
45 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
49 , _systemIconTheme{QIcon::themeName()}
51 if (_instance != nullptr) {
52 qWarning() << "QtUi has been instantiated again!";
57 if (Quassel::isOptionSet("icontheme")) {
58 _systemIconTheme = Quassel::optionValue("icontheme");
59 QIcon::setThemeName(_systemIconTheme);
62 QtUiSettings uiSettings;
63 Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
67 QApplication::setWindowIcon(QIcon::fromTheme("quassel"));
69 setContextMenuActionProvider(new ContextMenuActionProvider(this));
70 setToolBarActionProvider(new ToolBarActionProvider(this));
72 setUiStyle(new QtUiStyle(this));
73 _mainWin = new MainWin();
75 setMainWidget(_mainWin);
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)));
85 unregisterAllNotificationBackends();
95 QtUiSettings uiSettings;
96 uiSettings.initAndNotify("UseSystemTrayIcon", this, SLOT(useSystemTrayChanged(QVariant)), true);
98 GraphicalUi::init(); // needs to be called after the mainWin is initialized
102 MessageModel *QtUi::createMessageModel(QObject *parent)
104 return new ChatLineModel(parent);
108 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent)
110 return new QtUiMessageProcessor(parent);
114 void QtUi::connectedToCore()
116 _mainWin->connectedToCore();
120 void QtUi::disconnectedFromCore()
122 _mainWin->disconnectedFromCore();
123 GraphicalUi::disconnectedFromCore();
127 void QtUi::useSystemTrayChanged(const QVariant &v)
129 _useSystemTray = v.toBool();
130 SystemTray *tray = mainWindow()->systemTray();
131 if (_useSystemTray) {
132 if (tray->isSystemTrayAvailable())
133 tray->setVisible(true);
136 if (tray->isSystemTrayAvailable() && mainWindow()->isVisible())
137 tray->setVisible(false);
142 bool QtUi::haveSystemTray()
144 return mainWindow()->systemTray()->isSystemTrayAvailable() && instance()->_useSystemTray;
148 bool QtUi::isHidingMainWidgetAllowed() const
150 return haveSystemTray();
154 void QtUi::minimizeRestore(bool show)
156 SystemTray *tray = mainWindow()->systemTray();
158 if (tray && !_useSystemTray)
159 tray->setVisible(false);
162 if (tray && _useSystemTray)
163 tray->setVisible(true);
165 GraphicalUi::minimizeRestore(show);
169 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend)
171 if (!_notificationBackends.contains(backend)) {
172 _notificationBackends.append(backend);
173 instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
178 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend)
180 _notificationBackends.removeAll(backend);
184 void QtUi::unregisterAllNotificationBackends()
186 _notificationBackends.clear();
190 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends()
192 return _notificationBackends;
196 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString &sender, const QString &text)
198 static int notificationId = 0;
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;
208 void QtUi::closeNotification(uint notificationId)
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);
222 void QtUi::closeNotifications(BufferId bufferId)
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);
236 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications()
238 return _notifications;
242 void QtUi::notificationActivated(uint notificationId)
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;
250 Client::bufferModel()->switchToBuffer(bufId);
256 closeNotification(notificationId);
258 activateMainWidget();
262 void QtUi::bufferMarkedAsRead(BufferId bufferId)
264 if (bufferId.isValid()) {
265 closeNotifications(bufferId);
270 std::vector<std::pair<QString, QString>> QtUi::availableIconThemes() const
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") }
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);
293 QString QtUi::systemIconTheme() const
295 return _systemIconTheme;
299 void QtUi::setupIconTheme()
301 // Add paths to our own icon sets to the theme search paths
302 QStringList themePaths = QIcon::themeSearchPaths();
303 themePaths.removeAll(":/icons"); // this should come last
304 for (auto &&dataDir : Quassel::dataDirPaths()) {
305 QString iconDir{dataDir + "icons"};
306 if (QFileInfo{iconDir}.isDir()) {
307 themePaths << iconDir;
310 themePaths << ":/icons";
311 QIcon::setThemeSearchPaths(themePaths);
317 void QtUi::refreshIconTheme()
319 // List of available fallback themes
320 QStringList availableThemes;
321 for (auto &&themePair : availableIconThemes()) {
322 availableThemes << themePair.first;
325 if (availableThemes.isEmpty()) {
326 // We could probably introduce a more sophisticated fallback handling, such as putting the "most important" icons into hicolor,
327 // but this just gets complex for no good reason. We really rely on a supported theme to be installed, if not system-wide, then
328 // as part of the Quassel installation (which is enabled by default anyway).
329 qWarning() << tr("No supported icon theme installed, you'll lack icons! Supported are the KDE/Plasma themes Breeze, Breeze Dark and Oxygen.");
334 QString fallbackTheme{s.value("Icons/FallbackTheme").toString()};
336 if (fallbackTheme.isEmpty() || !availableThemes.contains(fallbackTheme)) {
337 if (availableThemes.contains(_systemIconTheme)) {
338 fallbackTheme = _systemIconTheme;
341 fallbackTheme = availableThemes.first();
345 if (_systemIconTheme.isEmpty() || _systemIconTheme == fallbackTheme || s.value("Icons/OverrideSystemTheme", false).toBool()) {
346 // We have a valid fallback theme and want to override the system theme (if it's even defined), so we're basically done
347 QIcon::setThemeName(fallbackTheme);
348 emit iconThemeRefreshed();
352 #if QT_VERSION >= 0x050000
353 // At this point, we have a system theme that we don't want to override, but that may not contain all
355 // We create a dummy theme that inherits first from the system theme, then from the supported fallback.
356 // This rather ugly hack allows us to inject the fallback into the inheritance chain, so non-standard
357 // icons missing in the system theme will be filled in by the fallback.
358 // Since we can't get notified when the system theme changes, this means that a restart may be required
359 // to apply a theme change... but you can't have everything, I guess.
360 if (!_dummyThemeDir) {
361 _dummyThemeDir.reset(new QTemporaryDir{});
362 if (!_dummyThemeDir->isValid() || !QDir{_dummyThemeDir->path()}.mkpath("icons/quassel-icon-proxy/apps/32")) {
363 qWarning() << "Could not create temporary directory for proxying the system icon theme, using fallback";
364 QIcon::setThemeName(fallbackTheme);
365 emit iconThemeRefreshed();
368 // Add this to XDG_DATA_DIRS, otherwise KIconLoader complains
369 auto xdgDataDirs = qgetenv("XDG_DATA_DIRS");
370 if (!xdgDataDirs.isEmpty())
372 xdgDataDirs += _dummyThemeDir->path();
373 qputenv("XDG_DATA_DIRS", xdgDataDirs);
375 QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << _dummyThemeDir->path() + "/icons");
378 QFile indexFile{_dummyThemeDir->path() + "/icons/quassel-icon-proxy/index.theme"};
379 if (!indexFile.open(QFile::WriteOnly|QFile::Truncate)) {
380 qWarning() << "Could not create index file for proxying the system icon theme, using fallback";
381 QIcon::setThemeName(fallbackTheme);
382 emit iconThemeRefreshed();
386 // Write a dummy index file that is sufficient to make QIconLoader happy
387 auto indexContents = QString{
389 "Name=quassel-icon-proxy\n"
391 "Directories=apps/32\n"
392 "[apps/32]\nSize=32\nType=Fixed\n"
393 }.arg(_systemIconTheme, fallbackTheme);
394 if (indexFile.write(indexContents.toLatin1()) < 0) {
395 qWarning() << "Could not write index file for proxying the system icon theme, using fallback";
396 QIcon::setThemeName(fallbackTheme);
397 emit iconThemeRefreshed();
401 QIcon::setThemeName("quassel-icon-proxy");
403 // Qt4 doesn't support QTemporaryDir. Since it's deprecated and slated to be removed soon anyway, we don't bother
404 // writing a replacement and simply don't support not overriding the system theme.
405 QIcon::setThemeName(fallbackTheme);
406 emit iconThemeRefreshed();