properly handling disconnects - this might even fix an antique bug with duplicate...
[quassel.git] / src / qtui / qtui.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 "actioncollection.h"
24 #include "chatlinemodel.h"
25 #include "mainwin.h"
26 #include "abstractnotificationbackend.h"
27 #include "qtuimessageprocessor.h"
28 #include "qtuisettings.h"
29 #include "qtuistyle.h"
30 #include "types.h"
31 #include "util.h"
32
33 QHash<QString, ActionCollection *> QtUi::_actionCollections;
34 MainWin *QtUi::_mainWin = 0;
35 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
36 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
37 QtUiStyle *QtUi::_style = 0;
38
39 QtUi::QtUi() : AbstractUi() {
40   if(_style != 0) {
41     qWarning() << "QtUi has been instantiated again!";
42     return;
43   }
44
45   QtUiSettings uiSettings;
46   loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
47
48   _mainWin = new MainWin();
49   _style = new QtUiStyle;
50
51   connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
52   connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
53 }
54
55 QtUi::~QtUi() {
56   unregisterAllNotificationBackends();
57   delete _style;
58   delete _mainWin;
59 }
60
61 void QtUi::init() {
62   _mainWin->init();
63 }
64
65 ActionCollection *QtUi::actionCollection(const QString &category) {
66   if(_actionCollections.contains(category))
67     return _actionCollections.value(category);
68   ActionCollection *coll = new ActionCollection(mainWindow());
69   coll->addAssociatedWidget(mainWindow());
70   _actionCollections.insert(category, coll);
71   return coll;
72 }
73
74 MessageModel *QtUi::createMessageModel(QObject *parent) {
75   return new ChatLineModel(parent);
76 }
77
78 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent) {
79   return new QtUiMessageProcessor(parent);
80 }
81
82 void QtUi::connectedToCore() {
83   _mainWin->connectedToCore();
84 }
85
86 void QtUi::disconnectedFromCore() {
87   _mainWin->disconnectedFromCore();
88 }
89
90 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend) {
91   if(!_notificationBackends.contains(backend)) {
92     _notificationBackends.append(backend);
93   }
94 }
95
96 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend) {
97   _notificationBackends.removeAll(backend);
98 }
99
100 void QtUi::unregisterAllNotificationBackends() {
101   _notificationBackends.clear();
102 }
103
104 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends() {
105   return _notificationBackends;
106 }
107
108 uint QtUi::invokeNotification(BufferId bufId, const QString &sender, const QString &text) {
109   static int notificationId = 0;
110   //notificationId++;
111   AbstractNotificationBackend::Notification notification(++notificationId, bufId, sender, text);
112   _notifications.append(notification);
113   foreach(AbstractNotificationBackend *backend, _notificationBackends)
114     backend->notify(notification);
115   return notificationId;
116 }
117
118 void QtUi::closeNotification(uint notificationId) {
119   QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
120   while(i != _notifications.end()) {
121     if((*i).notificationId == notificationId) {
122       foreach(AbstractNotificationBackend *backend, _notificationBackends)
123         backend->close(notificationId);
124       _notifications.erase(i);
125       break;
126     }
127     ++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       _notifications.erase(i);
138     }
139     ++i;
140   }
141 }
142
143 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications() {
144   return _notifications;
145 }
146