Properly clean up after disconnecting
[quassel.git] / src / qtui / qtui.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 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 QtUi *QtUi::_instance = 0;
41 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   _mainWin = 0;
71   _instance = 0;
72 }
73
74 void QtUi::init() {
75   _mainWin->init();
76   QtUiSettings uiSettings;
77   uiSettings.initAndNotify("UseSystemTrayIcon", this, SLOT(useSystemTrayChanged(QVariant)), true);
78
79   GraphicalUi::init(); // needs to be called after the mainWin is initialized
80 }
81
82 MessageModel *QtUi::createMessageModel(QObject *parent) {
83   return new ChatLineModel(parent);
84 }
85
86 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent) {
87   return new QtUiMessageProcessor(parent);
88 }
89
90 void QtUi::connectedToCore() {
91   _mainWin->connectedToCore();
92 }
93
94 void QtUi::disconnectedFromCore() {
95   _mainWin->disconnectedFromCore();
96   GraphicalUi::disconnectedFromCore();
97 }
98
99 void QtUi::useSystemTrayChanged(const QVariant &v) {
100   _useSystemTray = v.toBool();
101   SystemTray *tray = mainWindow()->systemTray();
102   if(_useSystemTray) {
103     if(tray->isSystemTrayAvailable())
104       tray->setVisible(true);
105   } else {
106     if(tray->isSystemTrayAvailable() && mainWindow()->isVisible())
107       tray->setVisible(false);
108   }
109 }
110
111 bool QtUi::haveSystemTray() {
112   return mainWindow()->systemTray()->isSystemTrayAvailable() && instance()->_useSystemTray;
113 }
114
115 bool QtUi::isHidingMainWidgetAllowed() const {
116   return haveSystemTray();
117 }
118
119 void QtUi::minimizeRestore(bool show) {
120   SystemTray *tray = mainWindow()->systemTray();
121   if(show) {
122     if(tray && !_useSystemTray)
123       tray->setVisible(false);
124   } else {
125     if(tray && _useSystemTray)
126       tray->setVisible(true);
127   }
128   GraphicalUi::minimizeRestore(show);
129 }
130
131 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend) {
132   if(!_notificationBackends.contains(backend)) {
133     _notificationBackends.append(backend);
134     instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
135   }
136 }
137
138 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend) {
139   _notificationBackends.removeAll(backend);
140 }
141
142 void QtUi::unregisterAllNotificationBackends() {
143   _notificationBackends.clear();
144 }
145
146 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends() {
147   return _notificationBackends;
148 }
149
150 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString &sender, const QString &text) {
151   static int notificationId = 0;
152
153   AbstractNotificationBackend::Notification notification(++notificationId, bufId, type, sender, text);
154   _notifications.append(notification);
155   foreach(AbstractNotificationBackend *backend, _notificationBackends)
156     backend->notify(notification);
157   return notificationId;
158 }
159
160 void QtUi::closeNotification(uint notificationId) {
161   QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
162   while(i != _notifications.end()) {
163     if(i->notificationId == notificationId) {
164       foreach(AbstractNotificationBackend *backend, _notificationBackends)
165         backend->close(notificationId);
166       i = _notifications.erase(i);
167     } else ++i;
168   }
169 }
170
171 void QtUi::closeNotifications(BufferId bufferId) {
172   QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
173   while(i != _notifications.end()) {
174     if(!bufferId.isValid() || i->bufferId == bufferId) {
175       foreach(AbstractNotificationBackend *backend, _notificationBackends)
176         backend->close(i->notificationId);
177       i = _notifications.erase(i);
178     } else ++i;
179   }
180 }
181
182 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications() {
183   return _notifications;
184 }
185
186 void QtUi::notificationActivated(uint notificationId) {
187   if(notificationId != 0) {
188     QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
189     while(i != _notifications.end()) {
190       if(i->notificationId == notificationId) {
191         BufferId bufId = i->bufferId;
192         if(bufId.isValid())
193           Client::bufferModel()->switchToBuffer(bufId);
194         break;
195       }
196       ++i;
197     }
198   }
199   closeNotification(notificationId);
200
201   activateMainWidget();
202 }