9dbc60afb05b54fe5939ffeff373d2d7ca9b4dee
[quassel.git] / src / qtui / qtui.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "qtui.h"
22
23 #include "abstractnotificationbackend.h"
24 #include "buffermodel.h"
25 #include "chatlinemodel.h"
26 #include "contextmenuactionprovider.h"
27 #include "mainwin.h"
28 #include "qtuimessageprocessor.h"
29 #include "qtuisettings.h"
30 #include "qtuistyle.h"
31 #include "systemtray.h"
32 #include "toolbaractionprovider.h"
33 #include "types.h"
34 #include "util.h"
35
36 #ifdef Q_WS_X11
37 #  include <QX11Info>
38 #endif
39
40 QPointer<QtUi> QtUi::_instance = 0;
41 QPointer<MainWin> QtUi::_mainWin = 0;
42 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
43 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
44
45 QtUi::QtUi() : GraphicalUi() {
46   if(_instance != 0) {
47     qWarning() << "QtUi has been instantiated again!";
48     return;
49   }
50   _instance = this;
51
52   QtUiSettings uiSettings;
53   Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
54
55   setContextMenuActionProvider(new ContextMenuActionProvider(this));
56   setToolBarActionProvider(new ToolBarActionProvider(this));
57
58   setUiStyle(new QtUiStyle(this));
59   _mainWin = new MainWin();
60
61   setMainWidget(_mainWin);
62
63   connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
64   connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
65 }
66
67 QtUi::~QtUi() {
68   unregisterAllNotificationBackends();
69   delete _mainWin;
70 }
71
72 void QtUi::init() {
73   _mainWin->init();
74   QtUiSettings uiSettings;
75   uiSettings.initAndNotify("UseSystemTrayIcon", this, SLOT(useSystemTrayChanged(QVariant)), true);
76
77   GraphicalUi::init(); // needs to be called after the mainWin is initialized
78 }
79
80 MessageModel *QtUi::createMessageModel(QObject *parent) {
81   return new ChatLineModel(parent);
82 }
83
84 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent) {
85   return new QtUiMessageProcessor(parent);
86 }
87
88 void QtUi::connectedToCore() {
89   _mainWin->connectedToCore();
90 }
91
92 void QtUi::disconnectedFromCore() {
93   _mainWin->disconnectedFromCore();
94 }
95
96 void QtUi::useSystemTrayChanged(const QVariant &v) {
97   _useSystemTray = v.toBool();
98   SystemTray *tray = mainWindow()->systemTray();
99   if(_useSystemTray) {
100     if(tray->isSystemTrayAvailable())
101       tray->setVisible(true);
102   } else {
103     if(tray->isSystemTrayAvailable() && mainWindow()->isVisible())
104       tray->setVisible(false);
105   }
106 }
107
108 bool QtUi::haveSystemTray() {
109   return mainWindow()->systemTray()->isSystemTrayAvailable() && instance()->_useSystemTray;
110 }
111
112 bool QtUi::isHidingMainWidgetAllowed() const {
113   return haveSystemTray();
114 }
115
116 void QtUi::minimizeRestore(bool show) {
117   SystemTray *tray = mainWindow()->systemTray();
118   if(show) {
119     if(tray && !_useSystemTray)
120       tray->setVisible(false);
121   } else {
122     if(tray && _useSystemTray)
123       tray->setVisible(true);
124   }
125   GraphicalUi::minimizeRestore(show);
126 }
127
128 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend) {
129   if(!_notificationBackends.contains(backend)) {
130     _notificationBackends.append(backend);
131     instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
132   }
133 }
134
135 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend) {
136   _notificationBackends.removeAll(backend);
137 }
138
139 void QtUi::unregisterAllNotificationBackends() {
140   _notificationBackends.clear();
141 }
142
143 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends() {
144   return _notificationBackends;
145 }
146
147 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString &sender, const QString &text) {
148   static int notificationId = 0;
149
150   AbstractNotificationBackend::Notification notification(++notificationId, bufId, type, sender, text);
151   _notifications.append(notification);
152   foreach(AbstractNotificationBackend *backend, _notificationBackends)
153     backend->notify(notification);
154   return notificationId;
155 }
156
157 void QtUi::closeNotification(uint notificationId) {
158   QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
159   while(i != _notifications.end()) {
160     if(i->notificationId == notificationId) {
161       foreach(AbstractNotificationBackend *backend, _notificationBackends)
162         backend->close(notificationId);
163       i = _notifications.erase(i);
164     } else ++i;
165   }
166 }
167
168 void QtUi::closeNotifications(BufferId bufferId) {
169   QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
170   while(i != _notifications.end()) {
171     if(!bufferId.isValid() || i->bufferId == bufferId) {
172       foreach(AbstractNotificationBackend *backend, _notificationBackends)
173         backend->close(i->notificationId);
174       i = _notifications.erase(i);
175     } else ++i;
176   }
177 }
178
179 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications() {
180   return _notifications;
181 }
182
183 void QtUi::notificationActivated(uint notificationId) {
184   if(notificationId != 0) {
185     QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
186     while(i != _notifications.end()) {
187       if(i->notificationId == notificationId) {
188         BufferId bufId = i->bufferId;
189         if(bufId.isValid())
190           Client::bufferModel()->switchToBuffer(bufId);
191         break;
192       }
193       ++i;
194     }
195   }
196   closeNotification(notificationId);
197
198   activateMainWidget();
199 }