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