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 QtUi *QtUi::_instance = nullptr;
44 MainWin *QtUi::_mainWin = nullptr;
45 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
46 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
50 , _systemIconTheme{QIcon::themeName()}
52 if (_instance != nullptr) {
53 qWarning() << "QtUi has been instantiated again!";
58 if (Quassel::isOptionSet("icontheme")) {
59 _systemIconTheme = Quassel::optionValue("icontheme");
60 QIcon::setThemeName(_systemIconTheme);
63 QtUiSettings uiSettings;
64 Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
68 QApplication::setWindowIcon(icon::get("quassel"));
70 setContextMenuActionProvider(new ContextMenuActionProvider(this));
71 setToolBarActionProvider(new ToolBarActionProvider(this));
73 setUiStyle(new QtUiStyle(this));
74 _mainWin = new MainWin();
76 setMainWidget(_mainWin);
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)));
86 unregisterAllNotificationBackends();
96 QtUiSettings uiSettings;
97 uiSettings.initAndNotify("UseSystemTrayIcon", this, SLOT(useSystemTrayChanged(QVariant)), true);
99 GraphicalUi::init(); // needs to be called after the mainWin is initialized
103 MessageModel *QtUi::createMessageModel(QObject *parent)
105 return new ChatLineModel(parent);
109 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent)
111 return new QtUiMessageProcessor(parent);
115 void QtUi::connectedToCore()
117 _mainWin->connectedToCore();
121 void QtUi::disconnectedFromCore()
123 _mainWin->disconnectedFromCore();
124 GraphicalUi::disconnectedFromCore();
128 void QtUi::useSystemTrayChanged(const QVariant &v)
130 _useSystemTray = v.toBool();
131 SystemTray *tray = mainWindow()->systemTray();
132 if (_useSystemTray) {
133 if (tray->isSystemTrayAvailable())
134 tray->setVisible(true);
137 if (tray->isSystemTrayAvailable() && mainWindow()->isVisible())
138 tray->setVisible(false);
143 bool QtUi::haveSystemTray()
145 return mainWindow()->systemTray()->isSystemTrayAvailable() && instance()->_useSystemTray;
149 bool QtUi::isHidingMainWidgetAllowed() const
151 return haveSystemTray();
155 void QtUi::minimizeRestore(bool show)
157 SystemTray *tray = mainWindow()->systemTray();
159 if (tray && !_useSystemTray)
160 tray->setVisible(false);
163 if (tray && _useSystemTray)
164 tray->setVisible(true);
166 GraphicalUi::minimizeRestore(show);
170 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend)
172 if (!_notificationBackends.contains(backend)) {
173 _notificationBackends.append(backend);
174 instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
179 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend)
181 _notificationBackends.removeAll(backend);
185 void QtUi::unregisterAllNotificationBackends()
187 _notificationBackends.clear();
191 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends()
193 return _notificationBackends;
197 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString &sender, const QString &text)
199 static int notificationId = 0;
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;
209 void QtUi::closeNotification(uint notificationId)
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);
223 void QtUi::closeNotifications(BufferId bufferId)
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);
237 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications()
239 return _notifications;
243 void QtUi::notificationActivated(uint notificationId)
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;
251 Client::bufferModel()->switchToBuffer(bufId);
257 closeNotification(notificationId);
259 activateMainWidget();
263 void QtUi::bufferMarkedAsRead(BufferId bufferId)
265 if (bufferId.isValid()) {
266 closeNotifications(bufferId);
271 std::vector<std::pair<QString, QString>> QtUi::availableIconThemes() const
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") }
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);
296 QString QtUi::systemIconTheme() const
298 return _systemIconTheme;
302 void QtUi::setupIconTheme()
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;
313 themePaths << ":/icons";
314 QIcon::setThemeSearchPaths(themePaths);
320 void QtUi::refreshIconTheme()
322 // List of available fallback themes
323 QStringList availableThemes;
324 for (auto &&themePair : availableIconThemes()) {
325 availableThemes << themePair.first;
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.");
337 QString fallbackTheme{s.value("Icons/FallbackTheme").toString()};
339 if (fallbackTheme.isEmpty() || !availableThemes.contains(fallbackTheme)) {
340 if (availableThemes.contains(_systemIconTheme)) {
341 fallbackTheme = _systemIconTheme;
344 fallbackTheme = availableThemes.first();
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();
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
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();
371 // Add this to XDG_DATA_DIRS, otherwise KIconLoader complains
372 auto xdgDataDirs = qgetenv("XDG_DATA_DIRS");
373 if (!xdgDataDirs.isEmpty())
375 xdgDataDirs += _dummyThemeDir->path();
376 qputenv("XDG_DATA_DIRS", xdgDataDirs);
378 QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << _dummyThemeDir->path() + "/icons");
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();
389 // Write a dummy index file that is sufficient to make QIconLoader happy
390 auto indexContents = QString{
392 "Name=quassel-icon-proxy\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();
404 QIcon::setThemeName("quassel-icon-proxy");
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();