Move ActionCollection handling from QtUi to GraphicalUi
[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 "chatlinemodel.h"
25 #include "contextmenuactionprovider.h"
26 #include "mainwin.h"
27 #include "qtuimessageprocessor.h"
28 #include "qtuisettings.h"
29 #include "qtuistyle.h"
30 #include "toolbaractionprovider.h"
31 #include "types.h"
32 #include "util.h"
33
34 QPointer<QtUi> QtUi::_instance = 0;
35 QPointer<MainWin> QtUi::_mainWin = 0;
36 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
37 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
38 QtUiStyle *QtUi::_style = 0;
39
40 QtUi::QtUi() : GraphicalUi() {
41   if(_instance != 0) {
42     qWarning() << "QtUi has been instantiated again!";
43     return;
44   }
45   _instance = this;
46
47   setContextMenuActionProvider(new ContextMenuActionProvider(this));
48   setToolBarActionProvider(new ToolBarActionProvider(this));
49
50   QtUiSettings uiSettings;
51   Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
52
53   _mainWin = new MainWin();
54   _style = new QtUiStyle;
55
56   setMainWidget(_mainWin);
57
58   connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
59   connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
60 }
61
62 QtUi::~QtUi() {
63   unregisterAllNotificationBackends();
64   delete _style;
65   delete _mainWin;
66 }
67
68 void QtUi::init() {
69   _mainWin->init();
70 }
71
72 MessageModel *QtUi::createMessageModel(QObject *parent) {
73   return new ChatLineModel(parent);
74 }
75
76 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent) {
77   return new QtUiMessageProcessor(parent);
78 }
79
80 void QtUi::connectedToCore() {
81   _mainWin->connectedToCore();
82 }
83
84 void QtUi::disconnectedFromCore() {
85   _mainWin->disconnectedFromCore();
86 }
87
88 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend) {
89   if(!_notificationBackends.contains(backend)) {
90     _notificationBackends.append(backend);
91     instance()->connect(backend, SIGNAL(activated()), SLOT(notificationActivated()));
92   }
93 }
94
95 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend) {
96   _notificationBackends.removeAll(backend);
97 }
98
99 void QtUi::unregisterAllNotificationBackends() {
100   _notificationBackends.clear();
101 }
102
103 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends() {
104   return _notificationBackends;
105 }
106
107 uint QtUi::invokeNotification(BufferId bufId, const QString &sender, const QString &text) {
108   static int notificationId = 0;
109   //notificationId++;
110   AbstractNotificationBackend::Notification notification(++notificationId, bufId, sender, text);
111   _notifications.append(notification);
112   foreach(AbstractNotificationBackend *backend, _notificationBackends)
113     backend->notify(notification);
114   return notificationId;
115 }
116
117 void QtUi::closeNotification(uint notificationId) {
118   QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
119   while(i != _notifications.end()) {
120     if((*i).notificationId == notificationId) {
121       foreach(AbstractNotificationBackend *backend, _notificationBackends)
122         backend->close(notificationId);
123       i = _notifications.erase(i);
124       break;
125     } else {
126       ++i;
127     }
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 {
139       ++i;
140     }
141   }
142 }
143
144 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications() {
145   return _notifications;
146 }
147
148 void QtUi::notificationActivated() {
149   // this might not work with some window managers
150   _mainWin->raise();
151   _mainWin->activateWindow();
152 }