1 /***************************************************************************
2 * Copyright (C) 2005-2012 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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() {
47 qWarning() << "QtUi has been instantiated again!";
52 QtUiSettings uiSettings;
53 Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
55 setContextMenuActionProvider(new ContextMenuActionProvider(this));
56 setToolBarActionProvider(new ToolBarActionProvider(this));
58 setUiStyle(new QtUiStyle(this));
59 _mainWin = new MainWin();
61 setMainWidget(_mainWin);
63 connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
64 connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
65 connect(Client::instance(), SIGNAL(bufferMarkedAsRead(BufferId)), SLOT(closeNotifications(BufferId)));
69 unregisterAllNotificationBackends();
77 QtUiSettings uiSettings;
78 uiSettings.initAndNotify("UseSystemTrayIcon", this, SLOT(useSystemTrayChanged(QVariant)), true);
80 GraphicalUi::init(); // needs to be called after the mainWin is initialized
83 MessageModel *QtUi::createMessageModel(QObject *parent) {
84 return new ChatLineModel(parent);
87 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent) {
88 return new QtUiMessageProcessor(parent);
91 void QtUi::connectedToCore() {
92 _mainWin->connectedToCore();
95 void QtUi::disconnectedFromCore() {
96 _mainWin->disconnectedFromCore();
97 GraphicalUi::disconnectedFromCore();
100 void QtUi::useSystemTrayChanged(const QVariant &v) {
101 _useSystemTray = v.toBool();
102 SystemTray *tray = mainWindow()->systemTray();
104 if(tray->isSystemTrayAvailable())
105 tray->setVisible(true);
107 if(tray->isSystemTrayAvailable() && mainWindow()->isVisible())
108 tray->setVisible(false);
112 bool QtUi::haveSystemTray() {
113 return mainWindow()->systemTray()->isSystemTrayAvailable() && instance()->_useSystemTray;
116 bool QtUi::isHidingMainWidgetAllowed() const {
117 return haveSystemTray();
120 void QtUi::minimizeRestore(bool show) {
121 SystemTray *tray = mainWindow()->systemTray();
123 if(tray && !_useSystemTray)
124 tray->setVisible(false);
126 if(tray && _useSystemTray)
127 tray->setVisible(true);
129 GraphicalUi::minimizeRestore(show);
132 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend) {
133 if(!_notificationBackends.contains(backend)) {
134 _notificationBackends.append(backend);
135 instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
139 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend) {
140 _notificationBackends.removeAll(backend);
143 void QtUi::unregisterAllNotificationBackends() {
144 _notificationBackends.clear();
147 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends() {
148 return _notificationBackends;
151 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString &sender, const QString &text) {
152 static int notificationId = 0;
154 AbstractNotificationBackend::Notification notification(++notificationId, bufId, type, sender, text);
155 _notifications.append(notification);
156 foreach(AbstractNotificationBackend *backend, _notificationBackends)
157 backend->notify(notification);
158 return notificationId;
161 void QtUi::closeNotification(uint notificationId) {
162 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
163 while(i != _notifications.end()) {
164 if(i->notificationId == notificationId) {
165 foreach(AbstractNotificationBackend *backend, _notificationBackends)
166 backend->close(notificationId);
167 i = _notifications.erase(i);
172 void QtUi::closeNotifications(BufferId bufferId) {
173 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
174 while(i != _notifications.end()) {
175 if(!bufferId.isValid() || i->bufferId == bufferId) {
176 foreach(AbstractNotificationBackend *backend, _notificationBackends)
177 backend->close(i->notificationId);
178 i = _notifications.erase(i);
183 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications() {
184 return _notifications;
187 void QtUi::notificationActivated(uint notificationId) {
188 if(notificationId != 0) {
189 QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
190 while(i != _notifications.end()) {
191 if(i->notificationId == notificationId) {
192 BufferId bufId = i->bufferId;
194 Client::bufferModel()->switchToBuffer(bufId);
200 closeNotification(notificationId);
202 activateMainWidget();
205 void QtUi::bufferMarkedAsRead(BufferId bufferId) {
206 if(bufferId.isValid()) {
207 closeNotifications(bufferId);