Add configuration option for a custom stylesheet
[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 "buffermodel.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 #ifdef Q_WS_X11
36 #  include <QX11Info>
37 #endif
38
39 QPointer<QtUi> QtUi::_instance = 0;
40 QPointer<MainWin> QtUi::_mainWin = 0;
41 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
42 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
43 QtUiStyle *QtUi::_style = 0;
44
45 QtUi::QtUi() : GraphicalUi() {
46   if(_instance != 0) {
47     qWarning() << "QtUi has been instantiated again!";
48     return;
49   }
50   _instance = this;
51
52   setContextMenuActionProvider(new ContextMenuActionProvider(this));
53   setToolBarActionProvider(new ToolBarActionProvider(this));
54
55   QtUiSettings uiSettings;
56   Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
57
58   _style = new QtUiStyle;
59   _mainWin = new MainWin();
60
61   setMainWidget(_mainWin);
62
63   connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
64   connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
65 }
66
67 QtUi::~QtUi() {
68   unregisterAllNotificationBackends();
69   delete _mainWin;
70   delete _style;
71 }
72
73 void QtUi::init() {
74   _mainWin->init();
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     instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
97   }
98 }
99
100 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend) {
101   _notificationBackends.removeAll(backend);
102 }
103
104 void QtUi::unregisterAllNotificationBackends() {
105   _notificationBackends.clear();
106 }
107
108 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends() {
109   return _notificationBackends;
110 }
111
112 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString &sender, const QString &text) {
113   static int notificationId = 0;
114   //notificationId++;
115   AbstractNotificationBackend::Notification notification(++notificationId, bufId, type, sender, text);
116   _notifications.append(notification);
117   foreach(AbstractNotificationBackend *backend, _notificationBackends)
118     backend->notify(notification);
119   return notificationId;
120 }
121
122 void QtUi::closeNotification(uint notificationId) {
123   QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
124   while(i != _notifications.end()) {
125     if((*i).notificationId == notificationId) {
126       foreach(AbstractNotificationBackend *backend, _notificationBackends)
127         backend->close(notificationId);
128       i = _notifications.erase(i);
129       break;
130     } else ++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       i = _notifications.erase(i);
141     } else ++i;
142   }
143 }
144
145 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications() {
146   return _notifications;
147 }
148
149 void QtUi::notificationActivated(uint notificationId) {
150   if(notificationId != 0) {
151     QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
152     while(i != _notifications.end()) {
153       if((*i).notificationId == notificationId) {
154         BufferId bufId = (*i).bufferId;
155         if(bufId.isValid())
156           Client::bufferModel()->switchToBuffer(bufId);
157         foreach(AbstractNotificationBackend *backend, _notificationBackends)
158           backend->close(notificationId);
159         _notifications.erase(i);
160         break;
161       } else ++i;
162     }
163   }
164
165   mainWindow()->forceActivated();
166 }