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 ***************************************************************************/
25 #include <QApplication>
29 #include <QStringList>
31 #include "abstractnotificationbackend.h"
32 #include "buffermodel.h"
33 #include "chatlinemodel.h"
34 #include "contextmenuactionprovider.h"
37 #include "qtuimessageprocessor.h"
38 #include "qtuisettings.h"
39 #include "qtuistyle.h"
40 #include "systemtray.h"
41 #include "toolbaractionprovider.h"
45 QList<AbstractNotificationBackend*> QtUi::_notificationBackends;
46 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
48 QtUi* QtUi::instance()
50 return static_cast<QtUi*>(GraphicalUi::instance());
55 , _systemIconTheme{QIcon::themeName()}
57 QtUiSettings uiSettings;
58 Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
60 if (Quassel::isOptionSet("icontheme")) {
61 _systemIconTheme = Quassel::optionValue("icontheme");
62 QIcon::setThemeName(_systemIconTheme);
65 QApplication::setWindowIcon(icon::get("quassel"));
67 setUiStyle(new QtUiStyle(this));
72 unregisterAllNotificationBackends();
77 setContextMenuActionProvider(new ContextMenuActionProvider(this));
78 setToolBarActionProvider(new ToolBarActionProvider(this));
80 _mainWin = std::make_unique<MainWin>();
81 setMainWidget(_mainWin.get());
83 connect(_mainWin.get(), &MainWin::connectToCore, this, &QtUi::connectToCore);
84 connect(_mainWin.get(), &MainWin::disconnectFromCore, this, &QtUi::disconnectFromCore);
85 connect(Client::instance(), &Client::bufferMarkedAsRead, this, &QtUi::closeNotifications);
89 QtUiSettings uiSettings;
90 uiSettings.initAndNotify("UseSystemTrayIcon", this, &QtUi::useSystemTrayChanged, true);
92 GraphicalUi::init(); // needs to be called after the mainWin is initialized
95 MessageModel* QtUi::createMessageModel(QObject* parent)
97 return new ChatLineModel(parent);
100 AbstractMessageProcessor* QtUi::createMessageProcessor(QObject* parent)
102 return new QtUiMessageProcessor(parent);
105 void QtUi::connectedToCore()
107 _mainWin->connectedToCore();
110 void QtUi::disconnectedFromCore()
112 _mainWin->disconnectedFromCore();
113 GraphicalUi::disconnectedFromCore();
116 void QtUi::useSystemTrayChanged(const QVariant& v)
118 _useSystemTray = v.toBool();
119 SystemTray* tray = mainWindow()->systemTray();
120 if (_useSystemTray) {
121 if (tray->isSystemTrayAvailable())
122 tray->setVisible(true);
125 if (tray->isSystemTrayAvailable() && mainWindow()->isVisible())
126 tray->setVisible(false);
130 bool QtUi::haveSystemTray()
132 return mainWindow()->systemTray()->isSystemTrayAvailable() && instance()->_useSystemTray;
135 bool QtUi::isHidingMainWidgetAllowed() const
137 return haveSystemTray();
140 void QtUi::minimizeRestore(bool show)
142 SystemTray* tray = mainWindow()->systemTray();
144 if (tray && !_useSystemTray)
145 tray->setVisible(false);
148 if (tray && _useSystemTray)
149 tray->setVisible(true);
151 GraphicalUi::minimizeRestore(show);
154 void QtUi::registerNotificationBackend(AbstractNotificationBackend* backend)
156 if (!_notificationBackends.contains(backend)) {
157 _notificationBackends.append(backend);
158 connect(backend, &AbstractNotificationBackend::activated, instance(), &QtUi::notificationActivated);
162 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend* backend)
164 _notificationBackends.removeAll(backend);
167 void QtUi::unregisterAllNotificationBackends()
169 _notificationBackends.clear();
172 const QList<AbstractNotificationBackend*>& QtUi::notificationBackends()
174 return _notificationBackends;
177 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString& sender, const QString& text)
179 static int notificationId = 0;
181 AbstractNotificationBackend::Notification notification(++notificationId, bufId, type, sender, text);
182 _notifications.append(notification);
183 foreach (AbstractNotificationBackend* backend, _notificationBackends)
184 backend->notify(notification);
185 return notificationId;
188 void QtUi::closeNotification(uint notificationId)
190 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
191 while (i != _notifications.end()) {
192 if (i->notificationId == notificationId) {
193 foreach (AbstractNotificationBackend* backend, _notificationBackends)
194 backend->close(notificationId);
195 i = _notifications.erase(i);
202 void QtUi::closeNotifications(BufferId bufferId)
204 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
205 while (i != _notifications.end()) {
206 if (!bufferId.isValid() || i->bufferId == bufferId) {
207 foreach (AbstractNotificationBackend* backend, _notificationBackends)
208 backend->close(i->notificationId);
209 i = _notifications.erase(i);
216 const QList<AbstractNotificationBackend::Notification>& QtUi::activeNotifications()
218 return _notifications;
221 void QtUi::notificationActivated(uint notificationId)
223 if (notificationId != 0) {
224 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
225 while (i != _notifications.end()) {
226 if (i->notificationId == notificationId) {
227 BufferId bufId = i->bufferId;
229 Client::bufferModel()->switchToBuffer(bufId);
235 closeNotification(notificationId);
237 activateMainWidget();
240 void QtUi::bufferMarkedAsRead(BufferId bufferId)
242 if (bufferId.isValid()) {
243 closeNotifications(bufferId);
247 std::vector<std::pair<QString, QString>> QtUi::availableIconThemes() const
249 //: Supported icon theme names
250 static const std::vector<std::pair<QString, QString>> supported{{"breeze", tr("Breeze")},
251 {"breeze-dark", tr("Breeze Dark")},
252 #ifdef WITH_OXYGEN_ICONS
253 {"oxygen", tr("Oxygen")}
257 std::vector<std::pair<QString, QString>> result;
258 for (auto&& themePair : supported) {
259 for (auto&& dir : QIcon::themeSearchPaths()) {
260 if (QFileInfo{dir + "/" + themePair.first + "/index.theme"}.exists()) {
261 result.push_back(themePair);
270 QString QtUi::systemIconTheme() const
272 return _systemIconTheme;
275 void QtUi::setupIconTheme()
277 // Add paths to our own icon sets to the theme search paths
278 QStringList themePaths = QIcon::themeSearchPaths();
279 themePaths.removeAll(":/icons"); // this should come last
280 for (auto&& dataDir : Quassel::dataDirPaths()) {
281 QString iconDir{dataDir + "icons"};
282 if (QFileInfo{iconDir}.isDir()) {
283 themePaths << iconDir;
286 themePaths << ":/icons";
287 QIcon::setThemeSearchPaths(themePaths);
292 void QtUi::refreshIconTheme()
294 // List of available fallback themes
295 QStringList availableThemes;
296 for (auto&& themePair : availableIconThemes()) {
297 availableThemes << themePair.first;
300 if (availableThemes.isEmpty()) {
301 // We could probably introduce a more sophisticated fallback handling, such as putting the "most important" icons into hicolor,
302 // but this just gets complex for no good reason. We really rely on a supported theme to be installed, if not system-wide, then
303 // as part of the Quassel installation (which is enabled by default anyway).
305 "No supported icon theme installed, you'll lack icons! Supported are the KDE/Plasma themes Breeze, Breeze Dark and Oxygen.");
310 QString fallbackTheme{s.value("Icons/FallbackTheme").toString()};
312 if (fallbackTheme.isEmpty() || !availableThemes.contains(fallbackTheme)) {
313 if (availableThemes.contains(_systemIconTheme)) {
314 fallbackTheme = _systemIconTheme;
317 fallbackTheme = availableThemes.first();
321 if (_systemIconTheme.isEmpty() || _systemIconTheme == fallbackTheme || s.value("Icons/OverrideSystemTheme", true).toBool()) {
322 // We have a valid fallback theme and want to override the system theme (if it's even defined), so we're basically done
323 QIcon::setThemeName(fallbackTheme);
324 emit iconThemeRefreshed();
328 // At this point, we have a system theme that we don't want to override, but that may not contain all
330 // We create a dummy theme that inherits first from the system theme, then from the supported fallback.
331 // This rather ugly hack allows us to inject the fallback into the inheritance chain, so non-standard
332 // icons missing in the system theme will be filled in by the fallback.
333 // Since we can't get notified when the system theme changes, this means that a restart may be required
334 // to apply a theme change... but you can't have everything, I guess.
335 if (!_dummyThemeDir) {
336 _dummyThemeDir = std::make_unique<QTemporaryDir>();
337 if (!_dummyThemeDir->isValid() || !QDir{_dummyThemeDir->path()}.mkpath("icons/quassel-icon-proxy/apps/32")) {
338 qWarning() << "Could not create temporary directory for proxying the system icon theme, using fallback";
339 QIcon::setThemeName(fallbackTheme);
340 emit iconThemeRefreshed();
343 // Add this to XDG_DATA_DIRS, otherwise KIconLoader complains
344 auto xdgDataDirs = qgetenv("XDG_DATA_DIRS");
345 if (!xdgDataDirs.isEmpty())
347 xdgDataDirs += _dummyThemeDir->path();
348 qputenv("XDG_DATA_DIRS", xdgDataDirs);
350 QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << _dummyThemeDir->path() + "/icons");
353 QFile indexFile{_dummyThemeDir->path() + "/icons/quassel-icon-proxy/index.theme"};
354 if (!indexFile.open(QFile::WriteOnly | QFile::Truncate)) {
355 qWarning() << "Could not create index file for proxying the system icon theme, using fallback";
356 QIcon::setThemeName(fallbackTheme);
357 emit iconThemeRefreshed();
361 // Write a dummy index file that is sufficient to make QIconLoader happy
362 auto indexContents = QString{"[Icon Theme]\n"
363 "Name=quassel-icon-proxy\n"
365 "Directories=apps/32\n"
366 "[apps/32]\nSize=32\nType=Fixed\n"}
367 .arg(_systemIconTheme, fallbackTheme);
368 if (indexFile.write(indexContents.toLatin1()) < 0) {
369 qWarning() << "Could not write index file for proxying the system icon theme, using fallback";
370 QIcon::setThemeName(fallbackTheme);
371 emit iconThemeRefreshed();
375 QIcon::setThemeName("quassel-icon-proxy");