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