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 void QtUi::setupIconTheme()
295 // Add paths to our own icon sets to the theme search paths
296 QStringList themePaths = QIcon::themeSearchPaths();
297 themePaths.removeAll(":/icons"); // this should come last
298 for (auto &&dataDir : Quassel::dataDirPaths()) {
299 QString iconDir{dataDir + "icons"};
300 if (QFileInfo{iconDir}.isDir()) {
301 themePaths << iconDir;
304 themePaths << ":/icons";
305 QIcon::setThemeSearchPaths(themePaths);
311 void QtUi::refreshIconTheme()
313 // List of available fallback themes
314 QStringList availableThemes;
315 for (auto &&themePair : availableIconThemes()) {
316 availableThemes << themePair.first;
319 if (availableThemes.isEmpty()) {
320 // We could probably introduce a more sophisticated fallback handling, such as putting the "most important" icons into hicolor,
321 // but this just gets complex for no good reason. We really rely on a supported theme to be installed, if not system-wide, then
322 // as part of the Quassel installation (which is enabled by default anyway).
323 qWarning() << tr("No supported icon theme installed, you'll lack icons! Supported are the KDE/Plasma themes Breeze, Breeze Dark and Oxygen.");
328 QString fallbackTheme{s.value("Icons/FallbackTheme").toString()};
330 if (fallbackTheme.isEmpty() || !availableThemes.contains(fallbackTheme)) {
331 if (availableThemes.contains(_systemIconTheme)) {
332 fallbackTheme = _systemIconTheme;
335 fallbackTheme = availableThemes.first();
339 if (_systemIconTheme.isEmpty() || _systemIconTheme == fallbackTheme || s.value("Icons/OverrideSystemTheme", false).toBool()) {
340 // We have a valid fallback theme and want to override the system theme (if it's even defined), so we're basically done
341 QIcon::setThemeName(fallbackTheme);
342 emit iconThemeRefreshed();
346 #if QT_VERSION >= 0x050000
347 // At this point, we have a system theme that we don't want to override, but that may not contain all
349 // We create a dummy theme that inherits first from the system theme, then from the supported fallback.
350 // This rather ugly hack allows us to inject the fallback into the inheritance chain, so non-standard
351 // icons missing in the system theme will be filled in by the fallback.
352 // Since we can't get notified when the system theme changes, this means that a restart may be required
353 // to apply a theme change... but you can't have everything, I guess.
354 if (!_dummyThemeDir) {
355 _dummyThemeDir.reset(new QTemporaryDir{});
356 if (!_dummyThemeDir->isValid() || !QDir{_dummyThemeDir->path()}.mkpath("icons/quassel-icon-proxy/apps/32")) {
357 qWarning() << "Could not create temporary directory for proxying the system icon theme, using fallback";
358 QIcon::setThemeName(fallbackTheme);
359 emit iconThemeRefreshed();
362 // Add this to XDG_DATA_DIRS, otherwise KIconLoader complains
363 auto xdgDataDirs = qgetenv("XDG_DATA_DIRS");
364 if (!xdgDataDirs.isEmpty())
366 xdgDataDirs += _dummyThemeDir->path();
367 qputenv("XDG_DATA_DIRS", xdgDataDirs);
369 QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << _dummyThemeDir->path() + "/icons");
372 QFile indexFile{_dummyThemeDir->path() + "/icons/quassel-icon-proxy/index.theme"};
373 if (!indexFile.open(QFile::WriteOnly|QFile::Truncate)) {
374 qWarning() << "Could not create index file for proxying the system icon theme, using fallback";
375 QIcon::setThemeName(fallbackTheme);
376 emit iconThemeRefreshed();
380 // Write a dummy index file that is sufficient to make QIconLoader happy
381 auto indexContents = QString{
383 "Name=quassel-icon-proxy\n"
385 "Directories=apps/32\n"
386 "[apps/32]\nSize=32\nType=Fixed\n"
387 }.arg(_systemIconTheme, fallbackTheme);
388 if (indexFile.write(indexContents.toLatin1()) < 0) {
389 qWarning() << "Could not write index file for proxying the system icon theme, using fallback";
390 QIcon::setThemeName(fallbackTheme);
391 emit iconThemeRefreshed();
395 QIcon::setThemeName("quassel-icon-proxy");
397 // Qt4 doesn't support QTemporaryDir. Since it's deprecated and slated to be removed soon anyway, we don't bother
398 // writing a replacement and simply don't support not overriding the system theme.
399 QIcon::setThemeName(fallbackTheme);
400 emit iconThemeRefreshed();