Instantiate the QmlChatView instead of the QGV-based ChatView
[quassel.git] / src / qtui / qtui.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 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 "systemtray.h"
32 #include "toolbaractionprovider.h"
33 #include "types.h"
34 #include "util.h"
35
36 #ifdef HAVE_QML
37 #  include "qmlmessagemodel.h"
38 #endif
39
40 #ifdef Q_WS_X11
41 #  include <QX11Info>
42 #endif
43
44 QtUi *QtUi::_instance = 0;
45 MainWin *QtUi::_mainWin = 0;
46 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
47 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
48
49 QtUi::QtUi() : GraphicalUi() {
50   if(_instance != 0) {
51     qWarning() << "QtUi has been instantiated again!";
52     return;
53   }
54   _instance = this;
55
56   QtUiSettings uiSettings;
57   Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
58
59   setContextMenuActionProvider(new ContextMenuActionProvider(this));
60   setToolBarActionProvider(new ToolBarActionProvider(this));
61
62   setUiStyle(new QtUiStyle(this));
63   _mainWin = new MainWin();
64
65   setMainWidget(_mainWin);
66
67   connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
68   connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
69   connect(Client::instance(), SIGNAL(bufferMarkedAsRead(BufferId)), SLOT(closeNotifications(BufferId)));
70 }
71
72 QtUi::~QtUi() {
73   unregisterAllNotificationBackends();
74   delete _mainWin;
75   _mainWin = 0;
76   _instance = 0;
77 }
78
79 void QtUi::init() {
80   _mainWin->init();
81   QtUiSettings uiSettings;
82   uiSettings.initAndNotify("UseSystemTrayIcon", this, SLOT(useSystemTrayChanged(QVariant)), true);
83
84   GraphicalUi::init(); // needs to be called after the mainWin is initialized
85 }
86
87 MessageModel *QtUi::createMessageModel(QObject *parent) {
88 #ifdef HAVE_QML
89   return new QmlMessageModel(parent);
90 #else
91   return new ChatLineModel(parent);
92 #endif
93 }
94
95 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent) {
96   return new QtUiMessageProcessor(parent);
97 }
98
99 void QtUi::connectedToCore() {
100   _mainWin->connectedToCore();
101 }
102
103 void QtUi::disconnectedFromCore() {
104   _mainWin->disconnectedFromCore();
105   GraphicalUi::disconnectedFromCore();
106 }
107
108 void QtUi::useSystemTrayChanged(const QVariant &v) {
109   _useSystemTray = v.toBool();
110   SystemTray *tray = mainWindow()->systemTray();
111   if(_useSystemTray) {
112     if(tray->isSystemTrayAvailable())
113       tray->setVisible(true);
114   } else {
115     if(tray->isSystemTrayAvailable() && mainWindow()->isVisible())
116       tray->setVisible(false);
117   }
118 }
119
120 bool QtUi::haveSystemTray() {
121   return mainWindow()->systemTray()->isSystemTrayAvailable() && instance()->_useSystemTray;
122 }
123
124 bool QtUi::isHidingMainWidgetAllowed() const {
125   return haveSystemTray();
126 }
127
128 void QtUi::minimizeRestore(bool show) {
129   SystemTray *tray = mainWindow()->systemTray();
130   if(show) {
131     if(tray && !_useSystemTray)
132       tray->setVisible(false);
133   } else {
134     if(tray && _useSystemTray)
135       tray->setVisible(true);
136   }
137   GraphicalUi::minimizeRestore(show);
138 }
139
140 void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend) {
141   if(!_notificationBackends.contains(backend)) {
142     _notificationBackends.append(backend);
143     instance()->connect(backend, SIGNAL(activated(uint)), SLOT(notificationActivated(uint)));
144   }
145 }
146
147 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend) {
148   _notificationBackends.removeAll(backend);
149 }
150
151 void QtUi::unregisterAllNotificationBackends() {
152   _notificationBackends.clear();
153 }
154
155 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends() {
156   return _notificationBackends;
157 }
158
159 uint QtUi::invokeNotification(BufferId bufId, AbstractNotificationBackend::NotificationType type, const QString &sender, const QString &text) {
160   static int notificationId = 0;
161
162   AbstractNotificationBackend::Notification notification(++notificationId, bufId, type, sender, text);
163   _notifications.append(notification);
164   foreach(AbstractNotificationBackend *backend, _notificationBackends)
165     backend->notify(notification);
166   return notificationId;
167 }
168
169 void QtUi::closeNotification(uint notificationId) {
170   QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
171   while(i != _notifications.end()) {
172     if(i->notificationId == notificationId) {
173       foreach(AbstractNotificationBackend *backend, _notificationBackends)
174         backend->close(notificationId);
175       i = _notifications.erase(i);
176     } else ++i;
177   }
178 }
179
180 void QtUi::closeNotifications(BufferId bufferId) {
181   QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
182   while(i != _notifications.end()) {
183     if(!bufferId.isValid() || i->bufferId == bufferId) {
184       foreach(AbstractNotificationBackend *backend, _notificationBackends)
185         backend->close(i->notificationId);
186       i = _notifications.erase(i);
187     } else ++i;
188   }
189 }
190
191 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications() {
192   return _notifications;
193 }
194
195 void QtUi::notificationActivated(uint notificationId) {
196   if(notificationId != 0) {
197     QList<AbstractNotificationBackend::Notification>::iterator i = _notifications.begin();
198     while(i != _notifications.end()) {
199       if(i->notificationId == notificationId) {
200         BufferId bufId = i->bufferId;
201         if(bufId.isValid())
202           Client::bufferModel()->switchToBuffer(bufId);
203         break;
204       }
205       ++i;
206     }
207   }
208   closeNotification(notificationId);
209
210   activateMainWidget();
211 }
212
213 void QtUi::bufferMarkedAsRead(BufferId bufferId) {
214   if(bufferId.isValid()) {
215     closeNotifications(bufferId);
216   }
217 }