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