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 "uisettings.h"
50 #include "selectionmodelsynchronizer.h"
51 #include "mappedselectionmodel.h"
53 #include "settingspages/aliasessettingspage.h"
54 #include "settingspages/appearancesettingspage.h"
55 #include "settingspages/bufferviewsettingspage.h"
56 #include "settingspages/colorsettingspage.h"
57 #include "settingspages/fontssettingspage.h"
58 #include "settingspages/generalsettingspage.h"
59 #include "settingspages/highlightsettingspage.h"
60 #include "settingspages/identitiessettingspage.h"
61 #include "settingspages/networkssettingspage.h"
62 #include "settingspages/notificationssettingspage.h"
64 #include "debugconsole.h"
66 #include "qtuistyle.h"
68 MainWin::MainWin(QtUi *_gui, QWidget *parent)
69 : QMainWindow(parent),
71 coreLagLabel(new QLabel()),
72 sslLabel(new QLabel()),
74 systray(new QSystemTrayIcon(this)),
75 activeTrayIcon(":/icons/quassel-icon-active.png"),
76 onlineTrayIcon(":/icons/quassel-icon.png"),
77 offlineTrayIcon(":/icons/quassel-icon-offline.png"),
78 trayIconActive(false),
79 timer(new QTimer(this)),
80 channelListDlg(new ChannelListDlg(this)),
81 settingsDlg(new SettingsDlg(this)),
82 debugConsole(new DebugConsole(this))
84 UiSettings uiSettings;
85 loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
87 QString style = uiSettings.value("Style", QString("")).toString();
89 QApplication::setStyle(style);
93 setWindowTitle("Quassel IRC");
94 setWindowIcon(offlineTrayIcon);
95 qApp->setWindowIcon(offlineTrayIcon);
96 systray->setIcon(offlineTrayIcon);
97 setWindowIconText("Quassel IRC");
99 statusBar()->showMessage(tr("Waiting for core..."));
101 installEventFilter(new JumpKeyHandler(this));
103 UiSettings uiSettings;
104 QString style = uiSettings.value("Style", QString("")).toString();
106 QApplication::setStyle(style);
110 desktopNotifications = new org::freedesktop::Notifications(
111 "org.freedesktop.Notifications",
112 "/org/freedesktop/Notifications",
113 QDBusConnection::sessionBus(), this);
115 connect(desktopNotifications, SIGNAL(NotificationClosed(uint, uint)), this, SLOT(desktopNotificationClosed(uint, uint)));
116 connect(desktopNotifications, SIGNAL(ActionInvoked(uint, const QString&)), this, SLOT(desktopNotificationInvoked(uint, const QString&)));
120 void MainWin::init() {
122 if(s.value("MainWinSize").isValid())
123 resize(s.value("MainWinSize").toSize());
125 resize(QSize(800, 500));
127 Client::signalProxy()->attachSignal(this, SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)));
129 connect(QApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(saveLayout()));
131 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkCreated(NetworkId)));
132 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
136 statusBar()->showMessage(tr("Not connected to core."));
139 setDockNestingEnabled(true);
141 setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
142 setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
143 setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
144 setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
158 // restore mainwin state
159 restoreState(s.value("MainWinState").toByteArray());
161 // restore locked state of docks
162 ui.actionLockDockPositions->setChecked(s.value("LockDocks", false).toBool());
165 setDisconnectedState(); // Disable menus and stuff
166 showCoreConnectionDlg(true); // autoconnect if appropriate
168 // attach the BufferWidget to the BufferModel and the default selection
169 ui.bufferWidget->setModel(Client::bufferModel());
170 ui.bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
172 _titleSetter.setModel(Client::bufferModel());
173 _titleSetter.setSelectionModel(Client::bufferModel()->standardSelectionModel());
176 MainWin::~MainWin() {
178 s.setValue("MainWinSize", size());
179 s.setValue("MainWinPos", pos());
180 s.setValue("MainWinState", saveState());
183 void MainWin::setupMenus() {
184 connect(ui.actionConnectCore, SIGNAL(triggered()), this, SLOT(showCoreConnectionDlg()));
185 connect(ui.actionDisconnectCore, SIGNAL(triggered()), Client::instance(), SLOT(disconnectFromCore()));
186 connect(ui.actionCoreInfo, SIGNAL(triggered()), this, SLOT(showCoreInfoDlg()));
187 connect(ui.actionQuit, SIGNAL(triggered()), QCoreApplication::instance(), SLOT(quit()));
188 connect(ui.actionSettingsDlg, SIGNAL(triggered()), this, SLOT(showSettingsDlg()));
189 // connect(ui.actionDebug_Console, SIGNAL(triggered()), this, SLOT(showDebugConsole()));
190 connect(ui.actionAboutQuassel, SIGNAL(triggered()), this, SLOT(showAboutDlg()));
191 connect(ui.actionAboutQt, SIGNAL(triggered()), QApplication::instance(), SLOT(aboutQt()));
194 void MainWin::setupViews() {
198 void MainWin::addBufferView(int bufferViewConfigId) {
199 addBufferView(Client::bufferViewManager()->bufferViewConfig(bufferViewConfigId));
202 void MainWin::addBufferView(BufferViewConfig *config) {
203 BufferViewDock *dock;
205 dock = new BufferViewDock(config, this);
207 dock = new BufferViewDock(this);
209 //create the view and initialize it's filter
210 BufferView *view = new BufferView(dock);
211 view->setFilteredModel(Client::bufferModel(), config);
214 connect(&view->showChannelList, SIGNAL(triggered()), this, SLOT(showChannelList()));
216 Client::bufferModel()->synchronizeView(view);
218 dock->setWidget(view);
221 addDockWidget(Qt::LeftDockWidgetArea, dock);
222 ui.menuBufferViews->addAction(dock->toggleViewAction());
224 _netViews.append(dock);
227 void MainWin::removeBufferView(int bufferViewConfigId) {
229 BufferViewDock *dock;
230 foreach(QAction *action, ui.menuBufferViews->actions()) {
231 actionData = action->data();
232 if(!actionData.isValid())
235 dock = qobject_cast<BufferViewDock *>(action->parent());
236 if(dock && actionData.toInt() == bufferViewConfigId) {
237 removeAction(action);
243 void MainWin::setupSettingsDlg() {
244 //Category: Appearance
245 settingsDlg->registerSettingsPage(new ColorSettingsPage(settingsDlg));
246 settingsDlg->registerSettingsPage(new FontsSettingsPage(settingsDlg));
247 settingsDlg->registerSettingsPage(new AppearanceSettingsPage(settingsDlg)); //General
248 //Category: Behaviour
249 settingsDlg->registerSettingsPage(new GeneralSettingsPage(settingsDlg));
250 settingsDlg->registerSettingsPage(new HighlightSettingsPage(settingsDlg));
251 settingsDlg->registerSettingsPage(new AliasesSettingsPage(settingsDlg));
252 settingsDlg->registerSettingsPage(new NotificationsSettingsPage(settingsDlg));
254 settingsDlg->registerSettingsPage(new IdentitiesSettingsPage(settingsDlg));
255 settingsDlg->registerSettingsPage(new NetworksSettingsPage(settingsDlg));
256 settingsDlg->registerSettingsPage(new BufferViewSettingsPage(settingsDlg));
259 void MainWin::on_actionEditNetworks_triggered() {
260 SettingsPageDlg dlg(new NetworksSettingsPage(this), this);
264 void MainWin::on_actionManageViews_triggered() {
265 SettingsPageDlg dlg(new BufferViewSettingsPage(this), this);
269 void MainWin::on_actionLockDockPositions_toggled(bool lock) {
270 QList<VerticalDock *> docks = findChildren<VerticalDock *>();
271 foreach(VerticalDock *dock, docks) {
272 dock->showTitle(!lock);
274 QtUiSettings().setValue("LockDocks", lock);
277 void MainWin::setupNickWidget() {
279 NickListDock *nickDock = new NickListDock(tr("Nicks"), this);
280 nickDock->setObjectName("NickDock");
281 nickDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
283 nickListWidget = new NickListWidget(nickDock);
284 nickDock->setWidget(nickListWidget);
286 addDockWidget(Qt::RightDockWidgetArea, nickDock);
287 ui.menuViews->addAction(nickDock->toggleViewAction());
288 // See NickListDock::NickListDock();
289 // connect(nickDock->toggleViewAction(), SIGNAL(triggered(bool)), nickListWidget, SLOT(showWidget(bool)));
291 // attach the NickListWidget to the BufferModel and the default selection
292 nickListWidget->setModel(Client::bufferModel());
293 nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
296 void MainWin::setupChatMonitor() {
298 VerticalDock *dock = new VerticalDock(tr("Chat Monitor"), this);
299 dock->setObjectName("ChatMonitorDock");
301 ChatWidget *chatWidget = new ChatWidget(0, this);
303 dock->setWidget(chatWidget);
306 Buffer *buf = Client::monitorBuffer();
310 chatWidget->setContents(buf->contents());
311 connect(buf, SIGNAL(msgAppended(AbstractUiMsg *)), chatWidget, SLOT(appendMsg(AbstractUiMsg *)));
312 connect(buf, SIGNAL(msgPrepended(AbstractUiMsg *)), chatWidget, SLOT(prependMsg(AbstractUiMsg *)));
314 addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
315 ui.menuViews->addAction(dock->toggleViewAction());
319 void MainWin::setupInputWidget() {
320 VerticalDock *dock = new VerticalDock(tr("Inputline"), this);
321 dock->setObjectName("InputDock");
323 InputWidget *inputWidget = new InputWidget(dock);
324 dock->setWidget(inputWidget);
326 addDockWidget(Qt::BottomDockWidgetArea, dock);
328 ui.menuViews->addAction(dock->toggleViewAction());
330 inputWidget->setModel(Client::bufferModel());
331 inputWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
333 ui.bufferWidget->setFocusProxy(inputWidget);
336 void MainWin::setupTopicWidget() {
337 VerticalDock *dock = new VerticalDock(tr("Topic"), this);
338 dock->setObjectName("TopicDock");
339 TopicWidget *topicwidget = new TopicWidget(dock);
340 connect(topicwidget, SIGNAL(topicChanged(const QString &)), this, SLOT(changeTopic(const QString &)));
342 dock->setWidget(topicwidget);
344 topicwidget->setModel(Client::bufferModel());
345 topicwidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
347 addDockWidget(Qt::TopDockWidgetArea, dock);
349 ui.menuViews->addAction(dock->toggleViewAction());
352 void MainWin::setupStatusBar() {
354 updateLagIndicator(0);
355 statusBar()->addPermanentWidget(coreLagLabel);
356 connect(Client::signalProxy(), SIGNAL(lagUpdated(int)), this, SLOT(updateLagIndicator(int)));
359 connect(Client::instance(), SIGNAL(securedConnection()), this, SLOT(securedConnection()));
360 sslLabel->setPixmap(QPixmap());
361 statusBar()->addPermanentWidget(sslLabel);
363 ui.menuViews->addSeparator();
364 QAction *showStatusbar = ui.menuViews->addAction(tr("Statusbar"));
365 showStatusbar->setCheckable(true);
367 UiSettings uiSettings;
369 bool enabled = uiSettings.value("ShowStatusBar", QVariant(true)).toBool();
370 showStatusbar->setChecked(enabled);
371 enabled ? statusBar()->show() : statusBar()->hide();
373 connect(showStatusbar, SIGNAL(toggled(bool)), statusBar(), SLOT(setVisible(bool)));
374 connect(showStatusbar, SIGNAL(toggled(bool)), this, SLOT(saveStatusBarStatus(bool)));
377 void MainWin::saveStatusBarStatus(bool enabled) {
378 UiSettings uiSettings;
379 uiSettings.setValue("ShowStatusBar", enabled);
382 void MainWin::setupSystray() {
383 connect(timer, SIGNAL(timeout()), this, SLOT(makeTrayIconBlink()));
384 connect(Client::instance(), SIGNAL(messageReceived(const Message &)), this, SLOT(receiveMessage(const Message &)));
386 systrayMenu = new QMenu(this);
387 systrayMenu->addAction(ui.actionAboutQuassel);
388 systrayMenu->addSeparator();
389 systrayMenu->addAction(ui.actionConnectCore);
390 systrayMenu->addAction(ui.actionDisconnectCore);
391 systrayMenu->addSeparator();
392 systrayMenu->addAction(ui.actionQuit);
394 systray->setContextMenu(systrayMenu);
397 if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
402 connect(systray, SIGNAL(activated( QSystemTrayIcon::ActivationReason )),
403 this, SLOT(systrayActivated( QSystemTrayIcon::ActivationReason )));
408 void MainWin::changeEvent(QEvent *event) {
409 if(event->type() == QEvent::WindowStateChange) {
410 if(windowState() & Qt::WindowMinimized) {
412 if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnMinimize").toBool()) {
420 // FIXME this should be made prettier...
421 void MainWin::changeTopic(const QString &topic) {
422 BufferId id = ui.bufferWidget->currentBuffer();
423 if(!id.isValid()) return;
424 Buffer *buffer = Client::buffer(id);
425 if(buffer) Client::userInput(buffer->bufferInfo(), QString("/topic %1").arg(topic));
428 void MainWin::connectedToCore() {
429 Q_CHECK_PTR(Client::bufferViewManager());
430 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
431 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigDeleted(int)), this, SLOT(removeBufferView(int)));
432 connect(Client::bufferViewManager(), SIGNAL(initDone()), this, SLOT(loadLayout()));
434 foreach(BufferInfo id, Client::allBufferInfos()) {
435 Client::backlogManager()->requestBacklog(id.bufferId(), 500, -1);
440 void MainWin::setConnectedState() {
441 //ui.menuCore->setEnabled(true);
442 ui.actionConnectCore->setEnabled(false);
443 ui.actionDisconnectCore->setEnabled(true);
444 ui.actionCoreInfo->setEnabled(true);
445 ui.menuViews->setEnabled(true);
446 ui.bufferWidget->show();
447 statusBar()->showMessage(tr("Connected to core."));
448 setWindowIcon(onlineTrayIcon);
449 qApp->setWindowIcon(onlineTrayIcon);
450 systray->setIcon(onlineTrayIcon);
451 if(sslLabel->width() == 0)
452 sslLabel->setPixmap(QPixmap::fromImage(QImage(":/16x16/status/no-ssl")));
455 void MainWin::loadLayout() {
457 int accountId = Client::currentCoreAccount().toInt();
458 restoreState(s.value(QString("MainWinState-%1").arg(accountId)).toByteArray(), accountId);
461 void MainWin::saveLayout() {
463 int accountId = Client::currentCoreAccount().toInt();
464 if(accountId > 0) s.setValue(QString("MainWinState-%1").arg(accountId) , saveState(accountId));
467 void MainWin::updateLagIndicator(int lag) {
468 coreLagLabel->setText(QString("Core Lag: %1 msec").arg(lag));
472 void MainWin::securedConnection() {
473 // todo: make status bar entry
474 sslLabel->setPixmap(QPixmap::fromImage(QImage(":/16x16/status/ssl")));
477 void MainWin::disconnectedFromCore() {
478 // save core specific layout and remove bufferviews;
481 BufferViewDock *dock;
482 foreach(QAction *action, ui.menuBufferViews->actions()) {
483 actionData = action->data();
484 if(!actionData.isValid())
487 dock = qobject_cast<BufferViewDock *>(action->parent());
488 if(dock && actionData.toInt() != -1) {
489 removeAction(action);
494 restoreState(s.value("MainWinState").toByteArray());
495 setDisconnectedState();
498 void MainWin::setDisconnectedState() {
499 //ui.menuCore->setEnabled(false);
500 ui.actionConnectCore->setEnabled(true);
501 ui.actionDisconnectCore->setEnabled(false);
502 ui.actionCoreInfo->setEnabled(false);
503 ui.menuViews->setEnabled(false);
504 ui.bufferWidget->hide();
505 statusBar()->showMessage(tr("Not connected to core."));
506 setWindowIcon(offlineTrayIcon);
507 qApp->setWindowIcon(offlineTrayIcon);
508 systray->setIcon(offlineTrayIcon);
509 sslLabel->setPixmap(QPixmap());
512 void MainWin::showCoreConnectionDlg(bool autoConnect) {
513 coreConnectDlg = new CoreConnectDlg(this, autoConnect);
514 connect(coreConnectDlg, SIGNAL(finished(int)), this, SLOT(coreConnectionDlgFinished(int)));
515 coreConnectDlg->setModal(true);
516 coreConnectDlg->show();
519 void MainWin::coreConnectionDlgFinished(int /*code*/) {
520 coreConnectDlg->close();
524 void MainWin::showChannelList(NetworkId netId) {
525 if(!netId.isValid()) {
526 QAction *action = qobject_cast<QAction *>(sender());
528 netId = action->data().value<NetworkId>();
530 channelListDlg->setNetwork(netId);
531 channelListDlg->show();
534 void MainWin::showCoreInfoDlg() {
535 CoreInfoDlg dlg(this);
539 void MainWin::showSettingsDlg() {
543 void MainWin::showDebugConsole() {
544 debugConsole->show();
547 void MainWin::showAboutDlg() {
552 void MainWin::closeEvent(QCloseEvent *event) {
554 if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool()) {
562 void MainWin::systrayActivated( QSystemTrayIcon::ActivationReason activationReason) {
563 if(activationReason == QSystemTrayIcon::Trigger) {
568 void MainWin::toggleVisibility() {
569 if(isHidden() /*|| !isActiveWindow()*/) {
580 // setFocus(); //Qt::ActiveWindowFocusReason
583 if(systray->isSystemTrayAvailable ()) {
586 if(!systray->isVisible()) {
595 void MainWin::receiveMessage(const Message &msg) {
596 if(QApplication::activeWindow() != 0)
599 if(msg.flags() & Message::Highlight || msg.bufferInfo().type() == BufferInfo::QueryBuffer) {
600 QString title = msg.bufferInfo().bufferName();;
601 if(msg.bufferInfo().type() != BufferInfo::QueryBuffer) {
602 QString sender = msg.sender();
603 int i = sender.indexOf("!");
605 sender = sender.left(i);
606 title += QString(" - %1").arg(sender);
609 UiSettings uiSettings;
612 bool displayBubble = uiSettings.value("NotificationBubble", QVariant(true)).toBool();
613 bool displayDesktop = uiSettings.value("NotificationDesktop", QVariant(true)).toBool();
614 if(displayBubble || displayDesktop) {
615 // FIXME don't invoke style engine for this!
616 QString text = QtUi::style()->styleString(Message::mircToInternal(msg.contents())).plainText;
617 if(displayBubble) displayTrayIconMessage(title, text);
619 if(displayDesktop) sendDesktopNotification(title, text);
623 if(uiSettings.value("AnimateTrayIcon", QVariant(true)).toBool()) {
624 QApplication::alert(this);
625 setTrayIconActivity(true);
630 bool MainWin::event(QEvent *event) {
631 if(event->type() == QEvent::WindowActivate)
632 setTrayIconActivity(false);
633 return QMainWindow::event(event);
639 Using the notification-daemon from Freedesktop's Galago project
640 http://www.galago-project.org/specs/notification/0.9/x408.html#command-notify
642 void MainWin::sendDesktopNotification(const QString &title, const QString &message) {
644 QMap<QString, QVariant> hints;
645 UiSettings uiSettings;
647 hints["x"] = uiSettings.value("NotificationDesktopHintX", QVariant(0)).toInt(); // Standard hint: x location for the popup to show up
648 hints["y"] = uiSettings.value("NotificationDesktopHintY", QVariant(0)).toInt(); // Standard hint: y location for the popup to show up
650 actions << "click" << "Click Me!";
652 QDBusReply<uint> reply = desktopNotifications->Notify(
653 "Quassel", // Application name
654 notificationId, // ID of previous notification to replace
655 "", // Icon to display
656 title, // Summary / Header of the message to display
657 QString("%1: %2:\n%3").arg(QTime::currentTime().toString()).arg(title).arg(message), // Body of the message to display
658 actions, // Actions from which the user may choose
659 hints, // Hints to the server displaying the message
660 uiSettings.value("NotificationDesktopTimeout", QVariant(5000)).toInt() // Timeout in milliseconds
663 if(!reply.isValid()) {
665 qDebug() << "Error on sending notification..." << reply;
669 notificationId = reply.value();
671 qDebug() << "ID: " << notificationId << " Time: " << QTime::currentTime().toString();
675 void MainWin::desktopNotificationClosed(uint id, uint reason) {
676 qDebug() << "OID: " << notificationId << " ID: " << id << " Reason: " << reason << " Time: " << QTime::currentTime().toString();
681 void MainWin::desktopNotificationInvoked(uint id, const QString & action) {
682 qDebug() << "OID: " << notificationId << " ID: " << id << " Action: " << action << " Time: " << QTime::currentTime().toString();
685 #else /* HAVE_DBUS */
687 void MainWin::desktopNotificationClosed(uint, uint) {}
688 void MainWin::desktopNotificationInvoked(uint, const QString &) {}
690 #endif /* HAVE_DBUS */
692 void MainWin::displayTrayIconMessage(const QString &title, const QString &message) {
693 systray->showMessage(title, message);
696 void MainWin::setTrayIconActivity(bool active) {
698 if(!timer->isActive())
702 systray->setIcon(onlineTrayIcon);
706 void MainWin::makeTrayIconBlink() {
708 systray->setIcon(onlineTrayIcon);
709 trayIconActive = false;
711 systray->setIcon(activeTrayIcon);
712 trayIconActive = true;
716 void MainWin::clientNetworkCreated(NetworkId id) {
717 const Network *net = Client::network(id);
718 QAction *act = new QAction(net->networkName(), this);
719 act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
720 act->setData(QVariant::fromValue<NetworkId>(id));
721 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
722 connect(act, SIGNAL(triggered()), this, SLOT(connectOrDisconnectFromNet()));
724 QAction *beforeAction = 0;
725 foreach(QAction *action, ui.menuNetworks->actions()) {
726 if(action->isSeparator()) {
727 beforeAction = action;
730 if(net->networkName().localeAwareCompare(action->text()) < 0) {
731 beforeAction = action;
735 Q_CHECK_PTR(beforeAction);
736 ui.menuNetworks->insertAction(beforeAction, act);
739 void MainWin::clientNetworkUpdated() {
740 const Network *net = qobject_cast<const Network *>(sender());
744 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
748 action->setText(net->networkName());
750 switch(net->connectionState()) {
751 case Network::Initialized:
752 action->setIcon(QIcon(":/16x16/actions/network-connect"));
754 case Network::Disconnected:
755 action->setIcon(QIcon(":/16x16/actions/network-disconnect"));
758 action->setIcon(QIcon(":/16x16/actions/gear"));
762 void MainWin::clientNetworkRemoved(NetworkId id) {
763 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(id.toInt()));
767 action->deleteLater();
770 void MainWin::connectOrDisconnectFromNet() {
771 QAction *act = qobject_cast<QAction *>(sender());
773 const Network *net = Client::network(act->data().value<NetworkId>());
775 if(net->connectionState() == Network::Disconnected) net->requestConnect();
776 else net->requestDisconnect();