1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
23 #include "abstractnotificationbackend.h"
24 #include "actioncollection.h"
25 #include "buffermodel.h"
26 #include "chatlinemodel.h"
27 #include "contextmenuactionprovider.h"
29 #include "qtuimessageprocessor.h"
30 #include "qtuisettings.h"
31 #include "qtuistyle.h"
32 #include "toolbaractionprovider.h"
36 QHash<QString, ActionCollection *> QtUi::_actionCollections;
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;
48 QtUi::QtUi() : GraphicalUi() {
50 qWarning() << "QtUi has been instantiated again!";
55 setContextMenuActionProvider(new ContextMenuActionProvider(this));
56 setToolBarActionProvider(new ToolBarActionProvider(this));
58 QtUiSettings uiSettings;
59 Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
61 _mainWin = new MainWin();
62 _style = new QtUiStyle;
64 connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
65 connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
69 unregisterAllNotificationBackends();
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);
87 MessageModel *QtUi::createMessageModel(QObject *parent) {
88 return new ChatLineModel(parent);
91 AbstractMessageProcessor *QtUi::createMessageProcessor(QObject *parent) {
92 return new QtUiMessageProcessor(parent);
95 void QtUi::connectedToCore() {
96 _mainWin->connectedToCore();
99 void QtUi::disconnectedFromCore() {
100 _mainWin->disconnectedFromCore();
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)));
110 void QtUi::unregisterNotificationBackend(AbstractNotificationBackend *backend) {
111 _notificationBackends.removeAll(backend);
114 void QtUi::unregisterAllNotificationBackends() {
115 _notificationBackends.clear();
118 const QList<AbstractNotificationBackend *> &QtUi::notificationBackends() {
119 return _notificationBackends;
122 uint QtUi::invokeNotification(BufferId bufId, const QString &sender, const QString &text) {
123 static int notificationId = 0;
125 AbstractNotificationBackend::Notification notification(++notificationId, bufId, sender, text);
126 _notifications.append(notification);
127 foreach(AbstractNotificationBackend *backend, _notificationBackends)
128 backend->notify(notification);
129 return notificationId;
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);
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);
155 const QList<AbstractNotificationBackend::Notification> &QtUi::activeNotifications() {
156 return _notifications;
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;
166 Client::bufferModel()->switchToBuffer(bufId);
167 _notifications.erase(i);
174 // Bypass focus stealing prevention
175 QX11Info::setAppUserTime(QX11Info::appTime());
178 if(_mainWin->windowState() & Qt::WindowMinimized) {
180 _mainWin->setWindowState((_mainWin->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
184 _mainWin->activateWindow();