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>
30 #include "abstractnotificationbackend.h"
31 #include "buffermodel.h"
32 #include "chatlinemodel.h"
33 #include "contextmenuactionprovider.h"
36 #include "qtuimessageprocessor.h"
37 #include "qtuisettings.h"
38 #include "qtuistyle.h"
39 #include "systemtray.h"
40 #include "toolbaractionprovider.h"
44 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
45 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
48 QtUi *QtUi::instance()
50 return static_cast<QtUi*>(GraphicalUi::instance());
56 , _systemIconTheme{QIcon::themeName()}
58 QtUiSettings uiSettings;
59 Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
61 if (Quassel::isOptionSet("icontheme")) {
62 _systemIconTheme = Quassel::optionValue("icontheme");
63 QIcon::setThemeName(_systemIconTheme);
66 QApplication::setWindowIcon(icon::get("quassel"));
68 setUiStyle(new QtUiStyle(this));
74 unregisterAllNotificationBackends();
80 setContextMenuActionProvider(new ContextMenuActionProvider(this));
81 setToolBarActionProvider(new ToolBarActionProvider(this));
83 _mainWin = std::make_unique<MainWin>();
84 setMainWidget(_mainWin.get());
86 connect(_mainWin.get(), &MainWin::connectToCore, this, &QtUi::connectToCore);
87 connect(_mainWin.get(), &MainWin::disconnectFromCore, this, &QtUi::disconnectFromCore);
88 connect(Client::instance(), &Client::bufferMarkedAsRead, this, &QtUi::closeNotifications);
92 QtUiSettings uiSettings;
93 uiSettings.initAndNotify("UseSystemTrayIcon", this, SLOT(useSystemTrayChanged(QVariant)), true);
95 GraphicalUi::init(); // needs to be called after the mainWin is initialized
99 MessageModel *QtUi::createMessageModel(QObject *parent)
101 return new ChatLineModel(parent);
105 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent)
107 return new QtUiMessageProcessor(parent);
111 void QtUi::connectedToCore()
113 _mainWin->connectedToCore();
117 void QtUi::disconnectedFromCore()
119 _mainWin->disconnectedFromCore();
120 GraphicalUi::disconnectedFromCore();
124 void QtUi::useSystemTrayChanged(const QVariant &v)
126 _useSystemTray = v.toBool();
127 SystemTray *tray = mainWindow()->systemTray();
128 if (_useSystemTray) {
129 if (tray->isSystemTrayAvailable())
130 tray->setVisible(true);
133 if (tray->isSystemTrayAvailable() && mainWindow()->isVisible())
134 tray->setVisible(false);
139 bool QtUi::haveSystemTray()
141 return mainWindow()->systemTray()->isSystemTrayAvailable() && instance()->_useSystemTray;
145 bool QtUi::isHidingMainWidgetAllowed() const
147 return haveSystemTray();
151 void QtUi::minimizeRestore(bool show)
153 SystemTray *tray = mainWindow()->systemTray();
155 if (tray && !_useSystemTray)
156 tray->setVisible(false);
159 if (tray && _useSystemTray)
160 tray->setVisible(true);
162 GraphicalUi::minimizeRestore(show);
166 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend)
168 if (!_notificationBackends.contains(backend)) {
169 _notificationBackends.append(backend);
170 instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
175 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend)
177 _notificationBackends.removeAll(backend);
181 void QtUi::unregisterAllNotificationBackends()
183 _notificationBackends.clear();
187 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends()
189 return _notificationBackends;
193 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString &sender, const QString &text)
195 static int notificationId = 0;
197 AbstractNotificationBackend::Notification notification(++notificationId, bufId, type, sender, text);
198 _notifications.append(notification);
199 foreach(AbstractNotificationBackend *backend, _notificationBackends)
200 backend->notify(notification);
201 return notificationId;
205 void QtUi::closeNotification(uint notificationId)
207 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
208 while (i != _notifications.end()) {
209 if (i->notificationId == notificationId) {
210 foreach(AbstractNotificationBackend *backend, _notificationBackends)
211 backend->close(notificationId);
212 i = _notifications.erase(i);
219 void QtUi::closeNotifications(BufferId bufferId)
221 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
222 while (i != _notifications.end()) {
223 if (!bufferId.isValid() || i->bufferId == bufferId) {
224 foreach(AbstractNotificationBackend *backend, _notificationBackends)
225 backend->close(i->notificationId);
226 i = _notifications.erase(i);
233 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications()
235 return _notifications;
239 void QtUi::notificationActivated(uint notificationId)
241 if (notificationId != 0) {
242 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
243 while (i != _notifications.end()) {
244 if (i->notificationId == notificationId) {
245 BufferId bufId = i->bufferId;
247 Client::bufferModel()->switchToBuffer(bufId);
253 closeNotification(notificationId);
255 activateMainWidget();
259 void QtUi::bufferMarkedAsRead(BufferId bufferId)
261 if (bufferId.isValid()) {
262 closeNotifications(bufferId);
267 std::vector<std::pair<QString, QString>> QtUi::availableIconThemes() const
269 //: Supported icon theme names
270 static const std::vector<std::pair<QString, QString>> supported {
271 { "breeze", tr("Breeze") },
272 { "breeze-dark", tr("Breeze Dark") },
273 #ifdef WITH_OXYGEN_ICONS
274 { "oxygen", tr("Oxygen") }
278 std::vector<std::pair<QString, QString>> result;
279 for (auto &&themePair : supported) {
280 for (auto &&dir : QIcon::themeSearchPaths()) {
281 if (QFileInfo{dir + "/" + themePair.first + "/index.theme"}.exists()) {
282 result.push_back(themePair);
292 QString QtUi::systemIconTheme() const
294 return _systemIconTheme;
298 void QtUi::setupIconTheme()
300 // Add paths to our own icon sets to the theme search paths
301 QStringList themePaths = QIcon::themeSearchPaths();
302 themePaths.removeAll(":/icons"); // this should come last
303 for (auto &&dataDir : Quassel::dataDirPaths()) {
304 QString iconDir{dataDir + "icons"};
305 if (QFileInfo{iconDir}.isDir()) {
306 themePaths << iconDir;
309 themePaths << ":/icons";
310 QIcon::setThemeSearchPaths(themePaths);
316 void QtUi::refreshIconTheme()
318 // List of available fallback themes
319 QStringList availableThemes;
320 for (auto &&themePair : availableIconThemes()) {
321 availableThemes << themePair.first;
324 if (availableThemes.isEmpty()) {
325 // We could probably introduce a more sophisticated fallback handling, such as putting the "most important" icons into hicolor,
326 // but this just gets complex for no good reason. We really rely on a supported theme to be installed, if not system-wide, then
327 // as part of the Quassel installation (which is enabled by default anyway).
328 qWarning() << tr("No supported icon theme installed, you'll lack icons! Supported are the KDE/Plasma themes Breeze, Breeze Dark and Oxygen.");
333 QString fallbackTheme{s.value("Icons/FallbackTheme").toString()};
335 if (fallbackTheme.isEmpty() || !availableThemes.contains(fallbackTheme)) {
336 if (availableThemes.contains(_systemIconTheme)) {
337 fallbackTheme = _systemIconTheme;
340 fallbackTheme = availableThemes.first();
344 if (_systemIconTheme.isEmpty() || _systemIconTheme == fallbackTheme || s.value("Icons/OverrideSystemTheme", true).toBool()) {
345 // We have a valid fallback theme and want to override the system theme (if it's even defined), so we're basically done
346 QIcon::setThemeName(fallbackTheme);
347 emit iconThemeRefreshed();
351 // At this point, we have a system theme that we don't want to override, but that may not contain all
353 // We create a dummy theme that inherits first from the system theme, then from the supported fallback.
354 // This rather ugly hack allows us to inject the fallback into the inheritance chain, so non-standard
355 // icons missing in the system theme will be filled in by the fallback.
356 // Since we can't get notified when the system theme changes, this means that a restart may be required
357 // to apply a theme change... but you can't have everything, I guess.
358 if (!_dummyThemeDir) {
359 _dummyThemeDir = std::make_unique<QTemporaryDir>();
360 if (!_dummyThemeDir->isValid() || !QDir{_dummyThemeDir->path()}.mkpath("icons/quassel-icon-proxy/apps/32")) {
361 qWarning() << "Could not create temporary directory for proxying the system icon theme, using fallback";
362 QIcon::setThemeName(fallbackTheme);
363 emit iconThemeRefreshed();
366 // Add this to XDG_DATA_DIRS, otherwise KIconLoader complains
367 auto xdgDataDirs = qgetenv("XDG_DATA_DIRS");
368 if (!xdgDataDirs.isEmpty())
370 xdgDataDirs += _dummyThemeDir->path();
371 qputenv("XDG_DATA_DIRS", xdgDataDirs);
373 QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << _dummyThemeDir->path() + "/icons");
376 QFile indexFile{_dummyThemeDir->path() + "/icons/quassel-icon-proxy/index.theme"};
377 if (!indexFile.open(QFile::WriteOnly|QFile::Truncate)) {
378 qWarning() << "Could not create index file for proxying the system icon theme, using fallback";
379 QIcon::setThemeName(fallbackTheme);
380 emit iconThemeRefreshed();
384 // Write a dummy index file that is sufficient to make QIconLoader happy
385 auto indexContents = QString{
387 "Name=quassel-icon-proxy\n"
389 "Directories=apps/32\n"
390 "[apps/32]\nSize=32\nType=Fixed\n"
391 }.arg(_systemIconTheme, fallbackTheme);
392 if (indexFile.write(indexContents.toLatin1()) < 0) {
393 qWarning() << "Could not write index file for proxying the system icon theme, using fallback";
394 QIcon::setThemeName(fallbackTheme);
395 emit iconThemeRefreshed();
399 QIcon::setThemeName("quassel-icon-proxy");