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