1 /***************************************************************************
2 * Copyright (C) 2005-2016 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 "abstractnotificationbackend.h"
24 #include "buffermodel.h"
25 #include "chatlinemodel.h"
26 #include "contextmenuactionprovider.h"
28 #include "qtuimessageprocessor.h"
29 #include "qtuisettings.h"
30 #include "qtuistyle.h"
31 #include "systemtray.h"
32 #include "toolbaractionprovider.h"
36 QtUi *QtUi::_instance = 0;
37 MainWin *QtUi::_mainWin = 0;
38 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
39 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
41 QtUi::QtUi() : GraphicalUi()
44 qWarning() << "QtUi has been instantiated again!";
49 QtUiSettings uiSettings;
50 Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
52 setContextMenuActionProvider(new ContextMenuActionProvider(this));
53 setToolBarActionProvider(new ToolBarActionProvider(this));
55 setUiStyle(new QtUiStyle(this));
56 _mainWin = new MainWin();
58 setMainWidget(_mainWin);
60 connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
61 connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
62 connect(Client::instance(), SIGNAL(bufferMarkedAsRead(BufferId)), SLOT(closeNotifications(BufferId)));
68 unregisterAllNotificationBackends();
78 QtUiSettings uiSettings;
79 uiSettings.initAndNotify("UseSystemTrayIcon", this, SLOT(useSystemTrayChanged(QVariant)), true);
81 GraphicalUi::init(); // needs to be called after the mainWin is initialized
85 MessageModel *QtUi::createMessageModel(QObject *parent)
87 return new ChatLineModel(parent);
91 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent)
93 return new QtUiMessageProcessor(parent);
97 void QtUi::connectedToCore()
99 _mainWin->connectedToCore();
103 void QtUi::disconnectedFromCore()
105 _mainWin->disconnectedFromCore();
106 GraphicalUi::disconnectedFromCore();
110 void QtUi::useSystemTrayChanged(const QVariant &v)
112 _useSystemTray = v.toBool();
113 SystemTray *tray = mainWindow()->systemTray();
114 if (_useSystemTray) {
115 if (tray->isSystemTrayAvailable())
116 tray->setVisible(true);
119 if (tray->isSystemTrayAvailable() && mainWindow()->isVisible())
120 tray->setVisible(false);
125 bool QtUi::haveSystemTray()
127 return mainWindow()->systemTray()->isSystemTrayAvailable() && instance()->_useSystemTray;
131 bool QtUi::isHidingMainWidgetAllowed() const
133 return haveSystemTray();
137 void QtUi::minimizeRestore(bool show)
139 SystemTray *tray = mainWindow()->systemTray();
141 if (tray && !_useSystemTray)
142 tray->setVisible(false);
145 if (tray && _useSystemTray)
146 tray->setVisible(true);
148 GraphicalUi::minimizeRestore(show);
152 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend)
154 if (!_notificationBackends.contains(backend)) {
155 _notificationBackends.append(backend);
156 instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
161 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend)
163 _notificationBackends.removeAll(backend);
167 void QtUi::unregisterAllNotificationBackends()
169 _notificationBackends.clear();
173 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends()
175 return _notificationBackends;
179 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString &sender, const QString &text)
181 static int notificationId = 0;
183 AbstractNotificationBackend::Notification notification(++notificationId, bufId, type, sender, text);
184 _notifications.append(notification);
185 foreach(AbstractNotificationBackend *backend, _notificationBackends)
186 backend->notify(notification);
187 return notificationId;
191 void QtUi::closeNotification(uint notificationId)
193 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
194 while (i != _notifications.end()) {
195 if (i->notificationId == notificationId) {
196 foreach(AbstractNotificationBackend *backend, _notificationBackends)
197 backend->close(notificationId);
198 i = _notifications.erase(i);
205 void QtUi::closeNotifications(BufferId bufferId)
207 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
208 while (i != _notifications.end()) {
209 if (!bufferId.isValid() || i->bufferId == bufferId) {
210 foreach(AbstractNotificationBackend *backend, _notificationBackends)
211 backend->close(i->notificationId);
212 i = _notifications.erase(i);
219 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications()
221 return _notifications;
225 void QtUi::notificationActivated(uint notificationId)
227 if (notificationId != 0) {
228 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
229 while (i != _notifications.end()) {
230 if (i->notificationId == notificationId) {
231 BufferId bufId = i->bufferId;
233 Client::bufferModel()->switchToBuffer(bufId);
239 closeNotification(notificationId);
241 activateMainWidget();
245 void QtUi::bufferMarkedAsRead(BufferId bufferId)
247 if (bufferId.isValid()) {
248 closeNotifications(bufferId);