Fix Quassel not rejoining newly joined channels
[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 "toolbaractionprovider.h"
32 #include "types.h"
33 #include "util.h"
34
35 #ifdef Q_WS_X11
36 #  include <QX11Info>
37 #endif
38
39 QPointer<QtUi> QtUi::_instance = 0;
40 QPointer<MainWin> QtUi::_mainWin = 0;
41 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
42 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
43
44 QtUi::QtUi() : GraphicalUi() {
45   if(_instance != 0) {
46     qWarning() << "QtUi has been instantiated again!";
47     return;
48   }
49   _instance = this;
50
51   QtUiSettings uiSettings;
52   Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
53
54   setContextMenuActionProvider(new ContextMenuActionProvider(this));
55   setToolBarActionProvider(new ToolBarActionProvider(this));
56
57   setUiStyle(new QtUiStyle(this));
58   _mainWin = new MainWin();
59
60   setMainWidget(_mainWin);
61
62   connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
63   connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
64 }
65
66 QtUi::~QtUi() {
67   unregisterAllNotificationBackends();
68   delete _mainWin;
69 }
70
71 void QtUi::init() {
72   _mainWin->init();
73 }
74
75 MessageModel *QtUi::createMessageModel(QObject *parent) {
76   return new ChatLineModel(parent);
77 }
78
79 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent) {
80   return new QtUiMessageProcessor(parent);
81 }
82
83 void QtUi::connectedToCore() {
84   _mainWin->connectedToCore();
85 }
86
87 void QtUi::disconnectedFromCore() {
88   _mainWin->disconnectedFromCore();
89 }
90
91 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend) {
92   if(!_notificationBackends.contains(backend)) {
93     _notificationBackends.append(backend);
94     instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
95   }
96 }
97
98 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend) {
99   _notificationBackends.removeAll(backend);
100 }
101
102 void QtUi::unregisterAllNotificationBackends() {
103   _notificationBackends.clear();
104 }
105
106 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends() {
107   return _notificationBackends;
108 }
109
110 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString &sender, const QString &text) {
111   static int notificationId = 0;
112
113   AbstractNotificationBackend::Notification notification(++notificationId, bufId, type, sender, text);
114   _notifications.append(notification);
115   foreach(AbstractNotificationBackend *backend, _notificationBackends)
116     backend->notify(notification);
117   return notificationId;
118 }
119
120 void QtUi::closeNotification(uint notificationId) {
121   QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
122   while(i != _notifications.end()) {
123     if(i->notificationId == notificationId) {
124       foreach(AbstractNotificationBackend *backend, _notificationBackends)
125         backend->close(notificationId);
126       i = _notifications.erase(i);
127     } else ++i;
128   }
129 }
130
131 void QtUi::closeNotifications(BufferId bufferId) {
132   QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
133   while(i != _notifications.end()) {
134     if(!bufferId.isValid() || i->bufferId == bufferId) {
135       foreach(AbstractNotificationBackend *backend, _notificationBackends)
136         backend->close(i->notificationId);
137       i = _notifications.erase(i);
138     } else ++i;
139   }
140 }
141
142 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications() {
143   return _notifications;
144 }
145
146 void QtUi::notificationActivated(uint notificationId) {
147   if(notificationId != 0) {
148     QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
149     while(i != _notifications.end()) {
150       if(i->notificationId == notificationId) {
151         BufferId bufId = i->bufferId;
152         if(bufId.isValid())
153           Client::bufferModel()->switchToBuffer(bufId);
154         break;
155       }
156       ++i;
157     }
158   }
159   closeNotification(notificationId);
160
161   mainWindow()->forceActivated();
162 }