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"
35 #include "qtuimessageprocessor.h"
36 #include "qtuisettings.h"
37 #include "qtuistyle.h"
38 #include "systemtray.h"
39 #include "toolbaractionprovider.h"
43 MainWin *QtUi::_mainWin = nullptr;
44 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
45 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
49 QtUi *_instance{nullptr};
54 QtUi *QtUi::instance()
57 _instance = new QtUi();
65 , _systemIconTheme{QIcon::themeName()}
67 if (Quassel::isOptionSet("icontheme")) {
68 _systemIconTheme = Quassel::optionValue("icontheme");
69 QIcon::setThemeName(_systemIconTheme);
72 QtUiSettings uiSettings;
73 Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
77 QApplication::setWindowIcon(icon::get("quassel"));
79 setContextMenuActionProvider(new ContextMenuActionProvider(this));
80 setToolBarActionProvider(new ToolBarActionProvider(this));
82 setUiStyle(new QtUiStyle(this));
83 _mainWin = new MainWin();
85 setMainWidget(_mainWin);
87 connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
88 connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
89 connect(Client::instance(), SIGNAL(bufferMarkedAsRead(BufferId)), SLOT(closeNotifications(BufferId)));
95 unregisterAllNotificationBackends();
105 QtUiSettings uiSettings;
106 uiSettings.initAndNotify("UseSystemTrayIcon", this, SLOT(useSystemTrayChanged(QVariant)), true);
108 GraphicalUi::init(); // needs to be called after the mainWin is initialized
112 MessageModel *QtUi::createMessageModel(QObject *parent)
114 return new ChatLineModel(parent);
118 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent)
120 return new QtUiMessageProcessor(parent);
124 void QtUi::connectedToCore()
126 _mainWin->connectedToCore();
130 void QtUi::disconnectedFromCore()
132 _mainWin->disconnectedFromCore();
133 GraphicalUi::disconnectedFromCore();
137 void QtUi::useSystemTrayChanged(const QVariant &v)
139 _useSystemTray = v.toBool();
140 SystemTray *tray = mainWindow()->systemTray();
141 if (_useSystemTray) {
142 if (tray->isSystemTrayAvailable())
143 tray->setVisible(true);
146 if (tray->isSystemTrayAvailable() && mainWindow()->isVisible())
147 tray->setVisible(false);
152 bool QtUi::haveSystemTray()
154 return mainWindow()->systemTray()->isSystemTrayAvailable() && instance()->_useSystemTray;
158 bool QtUi::isHidingMainWidgetAllowed() const
160 return haveSystemTray();
164 void QtUi::minimizeRestore(bool show)
166 SystemTray *tray = mainWindow()->systemTray();
168 if (tray && !_useSystemTray)
169 tray->setVisible(false);
172 if (tray && _useSystemTray)
173 tray->setVisible(true);
175 GraphicalUi::minimizeRestore(show);
179 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend)
181 if (!_notificationBackends.contains(backend)) {
182 _notificationBackends.append(backend);
183 instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
188 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend)
190 _notificationBackends.removeAll(backend);
194 void QtUi::unregisterAllNotificationBackends()
196 _notificationBackends.clear();
200 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends()
202 return _notificationBackends;
206 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString &sender, const QString &text)
208 static int notificationId = 0;
210 AbstractNotificationBackend::Notification notification(++notificationId, bufId, type, sender, text);
211 _notifications.append(notification);
212 foreach(AbstractNotificationBackend *backend, _notificationBackends)
213 backend->notify(notification);
214 return notificationId;
218 void QtUi::closeNotification(uint notificationId)
220 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
221 while (i != _notifications.end()) {
222 if (i->notificationId == notificationId) {
223 foreach(AbstractNotificationBackend *backend, _notificationBackends)
224 backend->close(notificationId);
225 i = _notifications.erase(i);
232 void QtUi::closeNotifications(BufferId bufferId)
234 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
235 while (i != _notifications.end()) {
236 if (!bufferId.isValid() || i->bufferId == bufferId) {
237 foreach(AbstractNotificationBackend *backend, _notificationBackends)
238 backend->close(i->notificationId);
239 i = _notifications.erase(i);
246 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications()
248 return _notifications;
252 void QtUi::notificationActivated(uint notificationId)
254 if (notificationId != 0) {
255 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
256 while (i != _notifications.end()) {
257 if (i->notificationId == notificationId) {
258 BufferId bufId = i->bufferId;
260 Client::bufferModel()->switchToBuffer(bufId);
266 closeNotification(notificationId);
268 activateMainWidget();
272 void QtUi::bufferMarkedAsRead(BufferId bufferId)
274 if (bufferId.isValid()) {
275 closeNotifications(bufferId);
280 std::vector<std::pair<QString, QString>> QtUi::availableIconThemes() const
282 //: Supported icon theme names
283 static const std::vector<std::pair<QString, QString>> supported {
284 { "breeze", tr("Breeze") },
285 { "breeze-dark", tr("Breeze Dark") },
286 #ifdef WITH_OXYGEN_ICONS
287 { "oxygen", tr("Oxygen") }
291 std::vector<std::pair<QString, QString>> result;
292 for (auto &&themePair : supported) {
293 for (auto &&dir : QIcon::themeSearchPaths()) {
294 if (QFileInfo{dir + "/" + themePair.first + "/index.theme"}.exists()) {
295 result.push_back(themePair);
305 QString QtUi::systemIconTheme() const
307 return _systemIconTheme;
311 void QtUi::setupIconTheme()
313 // Add paths to our own icon sets to the theme search paths
314 QStringList themePaths = QIcon::themeSearchPaths();
315 themePaths.removeAll(":/icons"); // this should come last
316 for (auto &&dataDir : Quassel::dataDirPaths()) {
317 QString iconDir{dataDir + "icons"};
318 if (QFileInfo{iconDir}.isDir()) {
319 themePaths << iconDir;
322 themePaths << ":/icons";
323 QIcon::setThemeSearchPaths(themePaths);
329 void QtUi::refreshIconTheme()
331 // List of available fallback themes
332 QStringList availableThemes;
333 for (auto &&themePair : availableIconThemes()) {
334 availableThemes << themePair.first;
337 if (availableThemes.isEmpty()) {
338 // We could probably introduce a more sophisticated fallback handling, such as putting the "most important" icons into hicolor,
339 // but this just gets complex for no good reason. We really rely on a supported theme to be installed, if not system-wide, then
340 // as part of the Quassel installation (which is enabled by default anyway).
341 qWarning() << tr("No supported icon theme installed, you'll lack icons! Supported are the KDE/Plasma themes Breeze, Breeze Dark and Oxygen.");
346 QString fallbackTheme{s.value("Icons/FallbackTheme").toString()};
348 if (fallbackTheme.isEmpty() || !availableThemes.contains(fallbackTheme)) {
349 if (availableThemes.contains(_systemIconTheme)) {
350 fallbackTheme = _systemIconTheme;
353 fallbackTheme = availableThemes.first();
357 if (_systemIconTheme.isEmpty() || _systemIconTheme == fallbackTheme || s.value("Icons/OverrideSystemTheme", true).toBool()) {
358 // We have a valid fallback theme and want to override the system theme (if it's even defined), so we're basically done
359 QIcon::setThemeName(fallbackTheme);
360 emit iconThemeRefreshed();
364 #if QT_VERSION >= 0x050000
365 // At this point, we have a system theme that we don't want to override, but that may not contain all
367 // We create a dummy theme that inherits first from the system theme, then from the supported fallback.
368 // This rather ugly hack allows us to inject the fallback into the inheritance chain, so non-standard
369 // icons missing in the system theme will be filled in by the fallback.
370 // Since we can't get notified when the system theme changes, this means that a restart may be required
371 // to apply a theme change... but you can't have everything, I guess.
372 if (!_dummyThemeDir) {
373 _dummyThemeDir.reset(new QTemporaryDir{});
374 if (!_dummyThemeDir->isValid() || !QDir{_dummyThemeDir->path()}.mkpath("icons/quassel-icon-proxy/apps/32")) {
375 qWarning() << "Could not create temporary directory for proxying the system icon theme, using fallback";
376 QIcon::setThemeName(fallbackTheme);
377 emit iconThemeRefreshed();
380 // Add this to XDG_DATA_DIRS, otherwise KIconLoader complains
381 auto xdgDataDirs = qgetenv("XDG_DATA_DIRS");
382 if (!xdgDataDirs.isEmpty())
384 xdgDataDirs += _dummyThemeDir->path();
385 qputenv("XDG_DATA_DIRS", xdgDataDirs);
387 QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << _dummyThemeDir->path() + "/icons");
390 QFile indexFile{_dummyThemeDir->path() + "/icons/quassel-icon-proxy/index.theme"};
391 if (!indexFile.open(QFile::WriteOnly|QFile::Truncate)) {
392 qWarning() << "Could not create index file for proxying the system icon theme, using fallback";
393 QIcon::setThemeName(fallbackTheme);
394 emit iconThemeRefreshed();
398 // Write a dummy index file that is sufficient to make QIconLoader happy
399 auto indexContents = QString{
401 "Name=quassel-icon-proxy\n"
403 "Directories=apps/32\n"
404 "[apps/32]\nSize=32\nType=Fixed\n"
405 }.arg(_systemIconTheme, fallbackTheme);
406 if (indexFile.write(indexContents.toLatin1()) < 0) {
407 qWarning() << "Could not write index file for proxying the system icon theme, using fallback";
408 QIcon::setThemeName(fallbackTheme);
409 emit iconThemeRefreshed();
413 QIcon::setThemeName("quassel-icon-proxy");
415 // Qt4 doesn't support QTemporaryDir. Since it's deprecated and slated to be removed soon anyway, we don't bother
416 // writing a replacement and simply don't support not overriding the system theme.
417 QIcon::setThemeName(fallbackTheme);
418 emit iconThemeRefreshed();