1 /***************************************************************************
2 * Copyright (C) 2005-08 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 "chatwidget.h"
24 #include "bufferview.h"
25 #include "bufferviewconfig.h"
26 #include "bufferviewfilter.h"
27 #include "bufferviewmanager.h"
28 #include "channellistdlg.h"
30 #include "clientbacklogmanager.h"
31 #include "coreinfodlg.h"
32 #include "coreconnectdlg.h"
33 #include "networkmodel.h"
34 #include "buffermodel.h"
35 #include "nicklistwidget.h"
36 #include "settingsdlg.h"
37 #include "settingspagedlg.h"
38 #include "signalproxy.h"
39 #include "topicwidget.h"
40 #include "inputwidget.h"
41 #include "irclistmodel.h"
42 #include "verticaldock.h"
43 #include "uisettings.h"
45 #include "qtuisettings.h"
46 #include "jumpkeyhandler.h"
48 #include "selectionmodelsynchronizer.h"
49 #include "mappedselectionmodel.h"
51 #include "settingspages/aliasessettingspage.h"
52 #include "settingspages/appearancesettingspage.h"
53 #include "settingspages/bufferviewsettingspage.h"
54 #include "settingspages/colorsettingspage.h"
55 #include "settingspages/fontssettingspage.h"
56 #include "settingspages/generalsettingspage.h"
57 #include "settingspages/highlightsettingspage.h"
58 #include "settingspages/identitiessettingspage.h"
59 #include "settingspages/networkssettingspage.h"
60 #include "settingspages/notificationssettingspage.h"
62 #include "debugconsole.h"
64 #include "qtuistyle.h"
66 MainWin::MainWin(QtUi *_gui, QWidget *parent)
67 : QMainWindow(parent),
69 coreLagLabel(new QLabel()),
70 sslLabel(new QLabel()),
72 systray(new QSystemTrayIcon(this)),
73 activeTrayIcon(":/icons/quassel-icon-active.png"),
74 onlineTrayIcon(":/icons/quassel-icon.png"),
75 offlineTrayIcon(":/icons/quassel-icon-offline.png"),
76 trayIconActive(false),
77 timer(new QTimer(this)),
78 channelListDlg(new ChannelListDlg(this)),
79 settingsDlg(new SettingsDlg(this)),
80 debugConsole(new DebugConsole(this))
82 UiSettings uiSettings;
83 loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
85 QString style = uiSettings.value("Style", QString("")).toString();
87 QApplication::setStyle(style);
91 setWindowTitle("Quassel IRC");
92 setWindowIcon(offlineTrayIcon);
93 qApp->setWindowIcon(offlineTrayIcon);
94 systray->setIcon(offlineTrayIcon);
95 setWindowIconText("Quassel IRC");
97 statusBar()->showMessage(tr("Waiting for core..."));
99 installEventFilter(new JumpKeyHandler(this));
102 desktopNotifications = new org::freedesktop::Notifications(
103 "org.freedesktop.Notifications",
104 "/org/freedesktop/Notifications",
105 QDBusConnection::sessionBus(), this);
107 connect(desktopNotifications, SIGNAL(NotificationClosed(uint, uint)), this, SLOT(desktopNotificationClosed(uint, uint)));
108 connect(desktopNotifications, SIGNAL(ActionInvoked(uint, const QString&)), this, SLOT(desktopNotificationInvoked(uint, const QString&)));
112 void MainWin::init() {
114 if(s.value("MainWinSize").isValid())
115 resize(s.value("MainWinSize").toSize());
117 resize(QSize(800, 500));
119 Client::signalProxy()->attachSignal(this, SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)));
121 connect(QApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(saveLayout()));
123 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkCreated(NetworkId)));
124 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
128 statusBar()->showMessage(tr("Not connected to core."));
131 setDockNestingEnabled(true);
133 setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
134 setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
135 setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
136 setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
150 // restore mainwin state
151 restoreState(s.value("MainWinState").toByteArray());
153 // restore locked state of docks
154 ui.actionLockDockPositions->setChecked(s.value("LockDocks", false).toBool());
157 setDisconnectedState(); // Disable menus and stuff
158 showCoreConnectionDlg(true); // autoconnect if appropriate
160 // attach the BufferWidget to the BufferModel and the default selection
161 ui.bufferWidget->setModel(Client::bufferModel());
162 ui.bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
164 _titleSetter.setModel(Client::bufferModel());
165 _titleSetter.setSelectionModel(Client::bufferModel()->standardSelectionModel());
168 MainWin::~MainWin() {
170 s.setValue("MainWinSize", size());
171 s.setValue("MainWinPos", pos());
172 s.setValue("MainWinState", saveState());
175 void MainWin::setupMenus() {
176 connect(ui.actionConnectCore, SIGNAL(triggered()), this, SLOT(showCoreConnectionDlg()));
177 connect(ui.actionDisconnectCore, SIGNAL(triggered()), Client::instance(), SLOT(disconnectFromCore()));
178 connect(ui.actionCoreInfo, SIGNAL(triggered()), this, SLOT(showCoreInfoDlg()));
179 connect(ui.actionQuit, SIGNAL(triggered()), QCoreApplication::instance(), SLOT(quit()));
180 connect(ui.actionSettingsDlg, SIGNAL(triggered()), this, SLOT(showSettingsDlg()));
181 // connect(ui.actionDebug_Console, SIGNAL(triggered()), this, SLOT(showDebugConsole()));
182 connect(ui.actionAboutQuassel, SIGNAL(triggered()), this, SLOT(showAboutDlg()));
183 connect(ui.actionAboutQt, SIGNAL(triggered()), QApplication::instance(), SLOT(aboutQt()));
186 void MainWin::setupViews() {
190 void MainWin::addBufferView(int bufferViewConfigId) {
191 addBufferView(Client::bufferViewManager()->bufferViewConfig(bufferViewConfigId));
194 void MainWin::addBufferView(BufferViewConfig *config) {
195 BufferViewDock *dock;
197 dock = new BufferViewDock(config, this);
199 dock = new BufferViewDock(this);
201 //create the view and initialize it's filter
202 BufferView *view = new BufferView(dock);
203 view->setFilteredModel(Client::bufferModel(), config);
206 connect(&view->showChannelList, SIGNAL(triggered()), this, SLOT(showChannelList()));
208 Client::bufferModel()->synchronizeView(view);
210 dock->setWidget(view);
213 addDockWidget(Qt::LeftDockWidgetArea, dock);
214 ui.menuBufferViews->addAction(dock->toggleViewAction());
216 _netViews.append(dock);
219 void MainWin::removeBufferView(int bufferViewConfigId) {
221 BufferViewDock *dock;
222 foreach(QAction *action, ui.menuBufferViews->actions()) {
223 actionData = action->data();
224 if(!actionData.isValid())
227 dock = qobject_cast<BufferViewDock *>(action->parent());
228 if(dock && actionData.toInt() == bufferViewConfigId) {
229 removeAction(action);
235 void MainWin::setupSettingsDlg() {
236 //Category: Appearance
237 settingsDlg->registerSettingsPage(new ColorSettingsPage(settingsDlg));
238 settingsDlg->registerSettingsPage(new FontsSettingsPage(settingsDlg));
239 settingsDlg->registerSettingsPage(new AppearanceSettingsPage(settingsDlg)); //General
240 //Category: Behaviour
241 settingsDlg->registerSettingsPage(new GeneralSettingsPage(settingsDlg));
242 settingsDlg->registerSettingsPage(new HighlightSettingsPage(settingsDlg));
243 settingsDlg->registerSettingsPage(new AliasesSettingsPage(settingsDlg));
244 settingsDlg->registerSettingsPage(new NotificationsSettingsPage(settingsDlg));
246 settingsDlg->registerSettingsPage(new IdentitiesSettingsPage(settingsDlg));
247 settingsDlg->registerSettingsPage(new NetworksSettingsPage(settingsDlg));
248 settingsDlg->registerSettingsPage(new BufferViewSettingsPage(settingsDlg));
251 void MainWin::on_actionEditNetworks_triggered() {
252 SettingsPageDlg dlg(new NetworksSettingsPage(this), this);
256 void MainWin::on_actionManageViews_triggered() {
257 SettingsPageDlg dlg(new BufferViewSettingsPage(this), this);
261 void MainWin::on_actionLockDockPositions_toggled(bool lock) {
262 QList<VerticalDock *> docks = findChildren<VerticalDock *>();
263 foreach(VerticalDock *dock, docks) {
264 dock->showTitle(!lock);
266 QtUiSettings().setValue("LockDocks", lock);
269 void MainWin::setupNickWidget() {
271 NickListDock *nickDock = new NickListDock(tr("Nicks"), this);
272 nickDock->setObjectName("NickDock");
273 nickDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
275 nickListWidget = new NickListWidget(nickDock);
276 nickDock->setWidget(nickListWidget);
278 addDockWidget(Qt::RightDockWidgetArea, nickDock);
279 ui.menuViews->addAction(nickDock->toggleViewAction());
280 // See NickListDock::NickListDock();
281 // connect(nickDock->toggleViewAction(), SIGNAL(triggered(bool)), nickListWidget, SLOT(showWidget(bool)));
283 // attach the NickListWidget to the BufferModel and the default selection
284 nickListWidget->setModel(Client::bufferModel());
285 nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
288 void MainWin::setupChatMonitor() {
290 VerticalDock *dock = new VerticalDock(tr("Chat Monitor"), this);
291 dock->setObjectName("ChatMonitorDock");
293 ChatWidget *chatWidget = new ChatWidget(0, this);
295 dock->setWidget(chatWidget);
298 Buffer *buf = Client::monitorBuffer();
302 chatWidget->setContents(buf->contents());
303 connect(buf, SIGNAL(msgAppended(AbstractUiMsg *)), chatWidget, SLOT(appendMsg(AbstractUiMsg *)));
304 connect(buf, SIGNAL(msgPrepended(AbstractUiMsg *)), chatWidget, SLOT(prependMsg(AbstractUiMsg *)));
306 addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
307 ui.menuViews->addAction(dock->toggleViewAction());
311 void MainWin::setupInputWidget() {
312 VerticalDock *dock = new VerticalDock(tr("Inputline"), this);
313 dock->setObjectName("InputDock");
315 InputWidget *inputWidget = new InputWidget(dock);
316 dock->setWidget(inputWidget);
318 addDockWidget(Qt::BottomDockWidgetArea, dock);
320 ui.menuViews->addAction(dock->toggleViewAction());
322 inputWidget->setModel(Client::bufferModel());
323 inputWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
325 ui.bufferWidget->setFocusProxy(inputWidget);
328 void MainWin::setupTopicWidget() {
329 VerticalDock *dock = new VerticalDock(tr("Topic"), this);
330 dock->setObjectName("TopicDock");
331 TopicWidget *topicwidget = new TopicWidget(dock);
332 connect(topicwidget, SIGNAL(topicChanged(const QString &)), this, SLOT(changeTopic(const QString &)));
334 dock->setWidget(topicwidget);
336 topicwidget->setModel(Client::bufferModel());
337 topicwidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
339 addDockWidget(Qt::TopDockWidgetArea, dock);
341 ui.menuViews->addAction(dock->toggleViewAction());
344 void MainWin::setupStatusBar() {
346 updateLagIndicator(0);
347 statusBar()->addPermanentWidget(coreLagLabel);
348 connect(Client::signalProxy(), SIGNAL(lagUpdated(int)), this, SLOT(updateLagIndicator(int)));
351 connect(Client::instance(), SIGNAL(securedConnection()), this, SLOT(securedConnection()));
352 sslLabel->setPixmap(QPixmap());
353 statusBar()->addPermanentWidget(sslLabel);
355 ui.menuViews->addSeparator();
356 QAction *showStatusbar = ui.menuViews->addAction(tr("Statusbar"));
357 showStatusbar->setCheckable(true);
359 UiSettings uiSettings;
361 bool enabled = uiSettings.value("ShowStatusBar", QVariant(true)).toBool();
362 showStatusbar->setChecked(enabled);
363 enabled ? statusBar()->show() : statusBar()->hide();
365 connect(showStatusbar, SIGNAL(toggled(bool)), statusBar(), SLOT(setVisible(bool)));
366 connect(showStatusbar, SIGNAL(toggled(bool)), this, SLOT(saveStatusBarStatus(bool)));
369 void MainWin::saveStatusBarStatus(bool enabled) {
370 UiSettings uiSettings;
371 uiSettings.setValue("ShowStatusBar", enabled);
374 void MainWin::setupSystray() {
375 connect(timer, SIGNAL(timeout()), this, SLOT(makeTrayIconBlink()));
376 connect(Client::instance(), SIGNAL(messageReceived(const Message &)), this, SLOT(receiveMessage(const Message &)));
378 systrayMenu = new QMenu(this);
379 systrayMenu->addAction(ui.actionAboutQuassel);
380 systrayMenu->addSeparator();
381 systrayMenu->addAction(ui.actionConnectCore);
382 systrayMenu->addAction(ui.actionDisconnectCore);
383 systrayMenu->addSeparator();
384 systrayMenu->addAction(ui.actionQuit);
386 systray->setContextMenu(systrayMenu);
389 if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
394 connect(systray, SIGNAL(activated( QSystemTrayIcon::ActivationReason )),
395 this, SLOT(systrayActivated( QSystemTrayIcon::ActivationReason )));
400 void MainWin::changeEvent(QEvent *event) {
401 if(event->type() == QEvent::WindowStateChange) {
402 if(windowState() & Qt::WindowMinimized) {
404 if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnMinimize").toBool()) {
412 // FIXME this should be made prettier...
413 void MainWin::changeTopic(const QString &topic) {
414 BufferId id = ui.bufferWidget->currentBuffer();
415 if(!id.isValid()) return;
416 Buffer *buffer = Client::buffer(id);
417 if(buffer) Client::userInput(buffer->bufferInfo(), QString("/topic %1").arg(topic));
420 void MainWin::connectedToCore() {
421 Q_CHECK_PTR(Client::bufferViewManager());
422 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
423 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigDeleted(int)), this, SLOT(removeBufferView(int)));
424 connect(Client::bufferViewManager(), SIGNAL(initDone()), this, SLOT(loadLayout()));
426 foreach(BufferInfo id, Client::allBufferInfos()) {
427 Client::backlogManager()->requestBacklog(id.bufferId(), 500, -1);
432 void MainWin::setConnectedState() {
433 //ui.menuCore->setEnabled(true);
434 ui.actionConnectCore->setEnabled(false);
435 ui.actionDisconnectCore->setEnabled(true);
436 ui.actionCoreInfo->setEnabled(true);
437 ui.menuViews->setEnabled(true);
438 ui.bufferWidget->show();
439 statusBar()->showMessage(tr("Connected to core."));
440 setWindowIcon(onlineTrayIcon);
441 qApp->setWindowIcon(onlineTrayIcon);
442 systray->setIcon(onlineTrayIcon);
443 if(sslLabel->width() == 0)
444 sslLabel->setPixmap(QPixmap::fromImage(QImage(":/16x16/status/no-ssl")));
447 void MainWin::loadLayout() {
449 int accountId = Client::currentCoreAccount().toInt();
450 restoreState(s.value(QString("MainWinState-%1").arg(accountId)).toByteArray(), accountId);
453 void MainWin::saveLayout() {
455 int accountId = Client::currentCoreAccount().toInt();
456 if(accountId > 0) s.setValue(QString("MainWinState-%1").arg(accountId) , saveState(accountId));
459 void MainWin::updateLagIndicator(int lag) {
460 coreLagLabel->setText(QString("Core Lag: %1 msec").arg(lag));
464 void MainWin::securedConnection() {
465 // todo: make status bar entry
466 sslLabel->setPixmap(QPixmap::fromImage(QImage(":/16x16/status/ssl")));
469 void MainWin::disconnectedFromCore() {
470 // save core specific layout and remove bufferviews;
473 BufferViewDock *dock;
474 foreach(QAction *action, ui.menuBufferViews->actions()) {
475 actionData = action->data();
476 if(!actionData.isValid())
479 dock = qobject_cast<BufferViewDock *>(action->parent());
480 if(dock && actionData.toInt() != -1) {
481 removeAction(action);
486 restoreState(s.value("MainWinState").toByteArray());
487 setDisconnectedState();
490 void MainWin::setDisconnectedState() {
491 //ui.menuCore->setEnabled(false);
492 ui.actionConnectCore->setEnabled(true);
493 ui.actionDisconnectCore->setEnabled(false);
494 ui.actionCoreInfo->setEnabled(false);
495 ui.menuViews->setEnabled(false);
496 ui.bufferWidget->hide();
497 statusBar()->showMessage(tr("Not connected to core."));
498 setWindowIcon(offlineTrayIcon);
499 qApp->setWindowIcon(offlineTrayIcon);
500 systray->setIcon(offlineTrayIcon);
501 sslLabel->setPixmap(QPixmap());
504 void MainWin::showCoreConnectionDlg(bool autoConnect) {
505 coreConnectDlg = new CoreConnectDlg(this, autoConnect);
506 connect(coreConnectDlg, SIGNAL(finished(int)), this, SLOT(coreConnectionDlgFinished(int)));
507 coreConnectDlg->setModal(true);
508 coreConnectDlg->show();
511 void MainWin::coreConnectionDlgFinished(int /*code*/) {
512 coreConnectDlg->close();
516 void MainWin::showChannelList(NetworkId netId) {
517 if(!netId.isValid()) {
518 QAction *action = qobject_cast<QAction *>(sender());
520 netId = action->data().value<NetworkId>();
522 channelListDlg->setNetwork(netId);
523 channelListDlg->show();
526 void MainWin::showCoreInfoDlg() {
527 CoreInfoDlg dlg(this);
531 void MainWin::showSettingsDlg() {
535 void MainWin::showDebugConsole() {
536 debugConsole->show();
539 void MainWin::showAboutDlg() {
544 void MainWin::closeEvent(QCloseEvent *event) {
546 if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool()) {
554 void MainWin::systrayActivated( QSystemTrayIcon::ActivationReason activationReason) {
555 if(activationReason == QSystemTrayIcon::Trigger) {
560 void MainWin::toggleVisibility() {
561 if(isHidden() /*|| !isActiveWindow()*/) {
572 // setFocus(); //Qt::ActiveWindowFocusReason
575 if(systray->isSystemTrayAvailable ()) {
578 if(!systray->isVisible()) {
587 void MainWin::receiveMessage(const Message &msg) {
588 if(QApplication::activeWindow() != 0)
591 if(msg.flags() & Message::Highlight || msg.bufferInfo().type() == BufferInfo::QueryBuffer) {
592 QString title = msg.bufferInfo().bufferName();;
593 if(msg.bufferInfo().type() != BufferInfo::QueryBuffer) {
594 QString sender = msg.sender();
595 int i = sender.indexOf("!");
597 sender = sender.left(i);
598 title += QString(" - %1").arg(sender);
601 UiSettings uiSettings;
604 bool displayBubble = uiSettings.value("NotificationBubble", QVariant(true)).toBool();
605 bool displayDesktop = uiSettings.value("NotificationDesktop", QVariant(true)).toBool();
606 if(displayBubble || displayDesktop) {
607 // FIXME don't invoke style engine for this!
608 QString text = QtUi::style()->styleString(Message::mircToInternal(msg.contents())).plainText;
609 if(displayBubble) displayTrayIconMessage(title, text);
611 if(displayDesktop) sendDesktopNotification(title, text);
615 if(uiSettings.value("AnimateTrayIcon", QVariant(true)).toBool()) {
616 QApplication::alert(this);
617 setTrayIconActivity(true);
622 bool MainWin::event(QEvent *event) {
623 if(event->type() == QEvent::WindowActivate)
624 setTrayIconActivity(false);
625 return QMainWindow::event(event);
631 Using the notification-daemon from Freedesktop's Galago project
632 http://www.galago-project.org/specs/notification/0.9/x408.html#command-notify
634 void MainWin::sendDesktopNotification(const QString &title, const QString &message) {
636 QMap<QString, QVariant> hints;
637 UiSettings uiSettings;
639 hints["x"] = uiSettings.value("NotificationDesktopHintX", QVariant(0)).toInt(); // Standard hint: x location for the popup to show up
640 hints["y"] = uiSettings.value("NotificationDesktopHintY", QVariant(0)).toInt(); // Standard hint: y location for the popup to show up
642 actions << "click" << "Click Me!";
644 QDBusReply<uint> reply = desktopNotifications->Notify(
645 "Quassel", // Application name
646 notificationId, // ID of previous notification to replace
647 "", // Icon to display
648 title, // Summary / Header of the message to display
649 QString("%1: %2:\n%3").arg(QTime::currentTime().toString()).arg(title).arg(message), // Body of the message to display
650 actions, // Actions from which the user may choose
651 hints, // Hints to the server displaying the message
652 uiSettings.value("NotificationDesktopTimeout", QVariant(5000)).toInt() // Timeout in milliseconds
655 if(!reply.isValid()) {
657 // could also happen if no notification service runs, so... whatever :)
658 //qDebug() << "Error on sending notification..." << reply.error();
662 notificationId = reply.value();
664 // qDebug() << "ID: " << notificationId << " Time: " << QTime::currentTime().toString();
668 void MainWin::desktopNotificationClosed(uint id, uint reason) {
669 Q_UNUSED(id); Q_UNUSED(reason);
670 // qDebug() << "OID: " << notificationId << " ID: " << id << " Reason: " << reason << " Time: " << QTime::currentTime().toString();
675 void MainWin::desktopNotificationInvoked(uint id, const QString & action) {
676 Q_UNUSED(id); Q_UNUSED(action);
677 // qDebug() << "OID: " << notificationId << " ID: " << id << " Action: " << action << " Time: " << QTime::currentTime().toString();
680 #endif /* HAVE_DBUS */
682 void MainWin::displayTrayIconMessage(const QString &title, const QString &message) {
683 systray->showMessage(title, message);
686 void MainWin::setTrayIconActivity(bool active) {
688 if(!timer->isActive())
692 systray->setIcon(onlineTrayIcon);
696 void MainWin::makeTrayIconBlink() {
698 systray->setIcon(onlineTrayIcon);
699 trayIconActive = false;
701 systray->setIcon(activeTrayIcon);
702 trayIconActive = true;
706 void MainWin::clientNetworkCreated(NetworkId id) {
707 const Network *net = Client::network(id);
708 QAction *act = new QAction(net->networkName(), this);
709 act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
710 act->setData(QVariant::fromValue<NetworkId>(id));
711 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
712 connect(act, SIGNAL(triggered()), this, SLOT(connectOrDisconnectFromNet()));
714 QAction *beforeAction = 0;
715 foreach(QAction *action, ui.menuNetworks->actions()) {
716 if(action->isSeparator()) {
717 beforeAction = action;
720 if(net->networkName().localeAwareCompare(action->text()) < 0) {
721 beforeAction = action;
725 Q_CHECK_PTR(beforeAction);
726 ui.menuNetworks->insertAction(beforeAction, act);
729 void MainWin::clientNetworkUpdated() {
730 const Network *net = qobject_cast<const Network *>(sender());
734 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
738 action->setText(net->networkName());
740 switch(net->connectionState()) {
741 case Network::Initialized:
742 action->setIcon(QIcon(":/16x16/actions/network-connect"));
744 case Network::Disconnected:
745 action->setIcon(QIcon(":/16x16/actions/network-disconnect"));
748 action->setIcon(QIcon(":/16x16/actions/gear"));
752 void MainWin::clientNetworkRemoved(NetworkId id) {
753 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(id.toInt()));
757 action->deleteLater();
760 void MainWin::connectOrDisconnectFromNet() {
761 QAction *act = qobject_cast<QAction *>(sender());
763 const Network *net = Client::network(act->data().value<NetworkId>());
765 if(net->connectionState() == Network::Disconnected) net->requestConnect();
766 else net->requestDisconnect();
771 void MainWin::on_actionDebugNetworkModel_triggered(bool) {
772 QTreeView *view = new QTreeView;
773 view->setAttribute(Qt::WA_DeleteOnClose);
774 view->setModel(Client::networkModel());