Systray icon improvements
[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 "buffermodel.h"
26 #include "chatlinemodel.h"
27 #include "contextmenuactionprovider.h"
28 #include "mainwin.h"
29 #include "qtuimessageprocessor.h"
30 #include "qtuisettings.h"
31 #include "qtuistyle.h"
32 #include "toolbaractionprovider.h"
33 #include "types.h"
34 #include "util.h"
35
36 QHash<QString, ActionCollection *> QtUi::_actionCollections;
37
38 #ifdef Q_WS_X11
39 #  include <QX11Info>
40 #endif
41
42 QPointer<QtUi> QtUi::_instance = 0;
43 QPointer<MainWin> QtUi::_mainWin = 0;
44 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
45 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
46 QtUiStyle *QtUi::_style = 0;
47
48 QtUi::QtUi() : GraphicalUi() {
49   if(_instance != 0) {
50     qWarning() << "QtUi has been instantiated again!";
51     return;
52   }
53   _instance = this;
54
55   setContextMenuActionProvider(new ContextMenuActionProvider(this));
56   setToolBarActionProvider(new ToolBarActionProvider(this));
57
58   QtUiSettings uiSettings;
59   Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
60
61   _mainWin = new MainWin();
62   _style = new QtUiStyle;
63
64   connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
65   connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
66 }
67
68 QtUi::~QtUi() {
69   unregisterAllNotificationBackends();
70   delete _style;
71   delete _mainWin;
72 }
73
74 void QtUi::init() {
75   _mainWin->init();
76 }
77
78 ActionCollection *QtUi::actionCollection(const QString &category) {
79   if(_actionCollections.contains(category))
80     return _actionCollections.value(category);
81   ActionCollection *coll = new ActionCollection(mainWindow());
82   coll->addAssociatedWidget(mainWindow());
83   _actionCollections.insert(category, coll);
84   return coll;
85 }
86
87 MessageModel *QtUi::createMessageModel(QObject *parent) {
88   return new ChatLineModel(parent);
89 }
90
91 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent) {
92   return new QtUiMessageProcessor(parent);
93 }
94
95 void QtUi::connectedToCore() {
96   _mainWin->connectedToCore();
97 }
98
99 void QtUi::disconnectedFromCore() {
100   _mainWin->disconnectedFromCore();
101 }
102
103 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend) {
104   if(!_notificationBackends.contains(backend)) {
105     _notificationBackends.append(backend);
106     instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
107   }
108 }
109
110 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend) {
111   _notificationBackends.removeAll(backend);
112 }
113
114 void QtUi::unregisterAllNotificationBackends() {
115   _notificationBackends.clear();
116 }
117
118 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends() {
119   return _notificationBackends;
120 }
121
122 uint QtUi::invokeNotification(BufferId bufId, const QString &sender, const QString &text) {
123   static int notificationId = 0;
124   //notificationId++;
125   AbstractNotificationBackend::Notification notification(++notificationId, bufId, sender, text);
126   _notifications.append(notification);
127   foreach(AbstractNotificationBackend *backend, _notificationBackends)
128     backend->notify(notification);
129   return notificationId;
130 }
131
132 void QtUi::closeNotification(uint notificationId) {
133   QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
134   while(i != _notifications.end()) {
135     if((*i).notificationId == notificationId) {
136       foreach(AbstractNotificationBackend *backend, _notificationBackends)
137         backend->close(notificationId);
138       i = _notifications.erase(i);
139       break;
140     } else ++i;
141   }
142 }
143
144 void QtUi::closeNotifications(BufferId bufferId) {
145   QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
146   while(i != _notifications.end()) {
147     if(!bufferId.isValid() || (*i).bufferId == bufferId) {
148       foreach(AbstractNotificationBackend *backend, _notificationBackends)
149         backend->close((*i).notificationId);
150       i = _notifications.erase(i);
151     } else ++i;
152   }
153 }
154
155 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications() {
156   return _notifications;
157 }
158
159 void QtUi::notificationActivated(uint notificationId) {
160   if(notificationId != 0) {
161     QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
162     while(i != _notifications.end()) {
163       if((*i).notificationId == notificationId) {
164         BufferId bufId = (*i).bufferId;
165         if(bufId.isValid())
166           Client::bufferModel()->switchToBuffer(bufId);
167         foreach(AbstractNotificationBackend *backend, _notificationBackends)
168           backend->close(notificationId);
169         _notifications.erase(i);
170         break;
171       } else ++i;
172     }
173   }
174
175   mainWindow()->forceActivated();
176 }