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