1 /***************************************************************************
2 * Copyright (C) 2005-2014 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"
40 QtUi *QtUi::_instance = 0;
41 MainWin *QtUi::_mainWin = 0;
42 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
43 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
45 QtUi::QtUi() : GraphicalUi()
48 qWarning() << "QtUi has been instantiated again!";
53 QtUiSettings uiSettings;
54 Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
56 setContextMenuActionProvider(new ContextMenuActionProvider(this));
57 setToolBarActionProvider(new ToolBarActionProvider(this));
59 setUiStyle(new QtUiStyle(this));
60 _mainWin = new MainWin();
62 setMainWidget(_mainWin);
64 connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
65 connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
66 connect(Client::instance(), SIGNAL(bufferMarkedAsRead(BufferId)), SLOT(closeNotifications(BufferId)));
72 unregisterAllNotificationBackends();
82 QtUiSettings uiSettings;
83 uiSettings.initAndNotify("UseSystemTrayIcon", this, SLOT(useSystemTrayChanged(QVariant)), true);
85 GraphicalUi::init(); // needs to be called after the mainWin is initialized
89 MessageModel *QtUi::createMessageModel(QObject *parent)
91 return new ChatLineModel(parent);
95 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent)
97 return new QtUiMessageProcessor(parent);
101 void QtUi::connectedToCore()
103 _mainWin->connectedToCore();
107 void QtUi::disconnectedFromCore()
109 _mainWin->disconnectedFromCore();
110 GraphicalUi::disconnectedFromCore();
114 void QtUi::useSystemTrayChanged(const QVariant &v)
116 _useSystemTray = v.toBool();
117 SystemTray *tray = mainWindow()->systemTray();
118 if (_useSystemTray) {
119 if (tray->isSystemTrayAvailable())
120 tray->setVisible(true);
123 if (tray->isSystemTrayAvailable() && mainWindow()->isVisible())
124 tray->setVisible(false);
129 bool QtUi::haveSystemTray()
131 return mainWindow()->systemTray()->isSystemTrayAvailable() && instance()->_useSystemTray;
135 bool QtUi::isHidingMainWidgetAllowed() const
137 return haveSystemTray();
141 void QtUi::minimizeRestore(bool show)
143 SystemTray *tray = mainWindow()->systemTray();
145 if (tray && !_useSystemTray)
146 tray->setVisible(false);
149 if (tray && _useSystemTray)
150 tray->setVisible(true);
152 GraphicalUi::minimizeRestore(show);
156 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend)
158 if (!_notificationBackends.contains(backend)) {
159 _notificationBackends.append(backend);
160 instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
165 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend)
167 _notificationBackends.removeAll(backend);
171 void QtUi::unregisterAllNotificationBackends()
173 _notificationBackends.clear();
177 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends()
179 return _notificationBackends;
183 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString &sender, const QString &text)
185 static int notificationId = 0;
187 AbstractNotificationBackend::Notification notification(++notificationId, bufId, type, sender, text);
188 _notifications.append(notification);
189 foreach(AbstractNotificationBackend *backend, _notificationBackends)
190 backend->notify(notification);
191 return notificationId;
195 void QtUi::closeNotification(uint notificationId)
197 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
198 while (i != _notifications.end()) {
199 if (i->notificationId == notificationId) {
200 foreach(AbstractNotificationBackend *backend, _notificationBackends)
201 backend->close(notificationId);
202 i = _notifications.erase(i);
209 void QtUi::closeNotifications(BufferId bufferId)
211 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
212 while (i != _notifications.end()) {
213 if (!bufferId.isValid() || i->bufferId == bufferId) {
214 foreach(AbstractNotificationBackend *backend, _notificationBackends)
215 backend->close(i->notificationId);
216 i = _notifications.erase(i);
223 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications()
225 return _notifications;
229 void QtUi::notificationActivated(uint notificationId)
231 if (notificationId != 0) {
232 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
233 while (i != _notifications.end()) {
234 if (i->notificationId == notificationId) {
235 BufferId bufId = i->bufferId;
237 Client::bufferModel()->switchToBuffer(bufId);
243 closeNotification(notificationId);
245 activateMainWidget();
249 void QtUi::bufferMarkedAsRead(BufferId bufferId)
251 if (bufferId.isValid()) {
252 closeNotifications(bufferId);