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 "bufferview.h"
24 #include "bufferviewconfig.h"
25 #include "bufferviewfilter.h"
26 #include "bufferviewmanager.h"
27 #include "channellistdlg.h"
28 #include "chatlinemodel.h"
29 #include "chatmonitorfilter.h"
32 #include "clientbacklogmanager.h"
33 #include "coreinfodlg.h"
34 #include "coreconnectdlg.h"
35 #include "networkmodel.h"
36 #include "buffermodel.h"
37 #include "nicklistwidget.h"
38 #include "settingsdlg.h"
39 #include "settingspagedlg.h"
40 #include "signalproxy.h"
41 #include "topicwidget.h"
42 #include "inputwidget.h"
43 #include "irclistmodel.h"
44 #include "verticaldock.h"
45 #include "uisettings.h"
47 #include "qtuisettings.h"
48 #include "jumpkeyhandler.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));
104 desktopNotifications = new org::freedesktop::Notifications(
105 "org.freedesktop.Notifications",
106 "/org/freedesktop/Notifications",
107 QDBusConnection::sessionBus(), this);
109 connect(desktopNotifications, SIGNAL(NotificationClosed(uint, uint)), this, SLOT(desktopNotificationClosed(uint, uint)));
110 connect(desktopNotifications, SIGNAL(ActionInvoked(uint, const QString&)), this, SLOT(desktopNotificationInvoked(uint, const QString&)));
114 void MainWin::init() {
116 if(s.value("MainWinSize").isValid())
117 resize(s.value("MainWinSize").toSize());
119 resize(QSize(800, 500));
121 Client::signalProxy()->attachSignal(this, SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)));
123 connect(QApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(saveLayout()));
125 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkCreated(NetworkId)));
126 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
130 statusBar()->showMessage(tr("Not connected to core."));
133 setDockNestingEnabled(true);
135 setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
136 setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
137 setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
138 setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
152 // restore mainwin state
153 restoreState(s.value("MainWinState").toByteArray());
155 // restore locked state of docks
156 ui.actionLockDockPositions->setChecked(s.value("LockDocks", false).toBool());
159 setDisconnectedState(); // Disable menus and stuff
160 showCoreConnectionDlg(true); // autoconnect if appropriate
162 // attach the BufferWidget to the BufferModel and the default selection
163 ui.bufferWidget->setModel(Client::bufferModel());
164 ui.bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
166 _titleSetter.setModel(Client::bufferModel());
167 _titleSetter.setSelectionModel(Client::bufferModel()->standardSelectionModel());
170 MainWin::~MainWin() {
172 s.setValue("MainWinSize", size());
173 s.setValue("MainWinPos", pos());
174 s.setValue("MainWinState", saveState());
177 void MainWin::setupMenus() {
178 connect(ui.actionConnectCore, SIGNAL(triggered()), this, SLOT(showCoreConnectionDlg()));
179 connect(ui.actionDisconnectCore, SIGNAL(triggered()), Client::instance(), SLOT(disconnectFromCore()));
180 connect(ui.actionCoreInfo, SIGNAL(triggered()), this, SLOT(showCoreInfoDlg()));
181 connect(ui.actionQuit, SIGNAL(triggered()), QCoreApplication::instance(), SLOT(quit()));
182 connect(ui.actionSettingsDlg, SIGNAL(triggered()), this, SLOT(showSettingsDlg()));
183 // connect(ui.actionDebug_Console, SIGNAL(triggered()), this, SLOT(showDebugConsole()));
184 connect(ui.actionAboutQuassel, SIGNAL(triggered()), this, SLOT(showAboutDlg()));
185 connect(ui.actionAboutQt, SIGNAL(triggered()), QApplication::instance(), SLOT(aboutQt()));
188 void MainWin::setupViews() {
192 void MainWin::addBufferView(int bufferViewConfigId) {
193 addBufferView(Client::bufferViewManager()->bufferViewConfig(bufferViewConfigId));
196 void MainWin::addBufferView(BufferViewConfig *config) {
197 BufferViewDock *dock;
199 dock = new BufferViewDock(config, this);
201 dock = new BufferViewDock(this);
203 //create the view and initialize it's filter
204 BufferView *view = new BufferView(dock);
205 view->setFilteredModel(Client::bufferModel(), config);
208 connect(&view->showChannelList, SIGNAL(triggered()), this, SLOT(showChannelList()));
210 Client::bufferModel()->synchronizeView(view);
212 dock->setWidget(view);
215 addDockWidget(Qt::LeftDockWidgetArea, dock);
216 ui.menuBufferViews->addAction(dock->toggleViewAction());
218 _netViews.append(dock);
221 void MainWin::removeBufferView(int bufferViewConfigId) {
223 BufferViewDock *dock;
224 foreach(QAction *action, ui.menuBufferViews->actions()) {
225 actionData = action->data();
226 if(!actionData.isValid())
229 dock = qobject_cast<BufferViewDock *>(action->parent());
230 if(dock && actionData.toInt() == bufferViewConfigId) {
231 removeAction(action);
237 void MainWin::setupSettingsDlg() {
238 //Category: Appearance
239 settingsDlg->registerSettingsPage(new ColorSettingsPage(settingsDlg));
240 settingsDlg->registerSettingsPage(new FontsSettingsPage(settingsDlg));
241 settingsDlg->registerSettingsPage(new AppearanceSettingsPage(settingsDlg)); //General
242 //Category: Behaviour
243 settingsDlg->registerSettingsPage(new GeneralSettingsPage(settingsDlg));
244 settingsDlg->registerSettingsPage(new HighlightSettingsPage(settingsDlg));
245 settingsDlg->registerSettingsPage(new AliasesSettingsPage(settingsDlg));
246 settingsDlg->registerSettingsPage(new NotificationsSettingsPage(settingsDlg));
248 settingsDlg->registerSettingsPage(new IdentitiesSettingsPage(settingsDlg));
249 settingsDlg->registerSettingsPage(new NetworksSettingsPage(settingsDlg));
250 settingsDlg->registerSettingsPage(new BufferViewSettingsPage(settingsDlg));
253 void MainWin::on_actionEditNetworks_triggered() {
254 SettingsPageDlg dlg(new NetworksSettingsPage(this), this);
258 void MainWin::on_actionManageViews_triggered() {
259 SettingsPageDlg dlg(new BufferViewSettingsPage(this), this);
263 void MainWin::on_actionLockDockPositions_toggled(bool lock) {
264 QList<VerticalDock *> docks = findChildren<VerticalDock *>();
265 foreach(VerticalDock *dock, docks) {
266 dock->showTitle(!lock);
268 QtUiSettings().setValue("LockDocks", lock);
271 void MainWin::setupNickWidget() {
273 NickListDock *nickDock = new NickListDock(tr("Nicks"), this);
274 nickDock->setObjectName("NickDock");
275 nickDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
277 nickListWidget = new NickListWidget(nickDock);
278 nickDock->setWidget(nickListWidget);
280 addDockWidget(Qt::RightDockWidgetArea, nickDock);
281 ui.menuViews->addAction(nickDock->toggleViewAction());
282 // See NickListDock::NickListDock();
283 // connect(nickDock->toggleViewAction(), SIGNAL(triggered(bool)), nickListWidget, SLOT(showWidget(bool)));
285 // attach the NickListWidget to the BufferModel and the default selection
286 nickListWidget->setModel(Client::bufferModel());
287 nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
290 void MainWin::setupChatMonitor() {
291 VerticalDock *dock = new VerticalDock(tr("Chat Monitor"), this);
292 dock->setObjectName("ChatMonitorDock");
294 ChatMonitorFilter *filter = new ChatMonitorFilter(Client::messageModel(), this);
295 ChatView *chatView = new ChatView(filter, this);
297 dock->setWidget(chatView);
300 addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
301 ui.menuViews->addAction(dock->toggleViewAction());
304 void MainWin::setupInputWidget() {
305 VerticalDock *dock = new VerticalDock(tr("Inputline"), this);
306 dock->setObjectName("InputDock");
308 InputWidget *inputWidget = new InputWidget(dock);
309 dock->setWidget(inputWidget);
311 addDockWidget(Qt::BottomDockWidgetArea, dock);
313 ui.menuViews->addAction(dock->toggleViewAction());
315 inputWidget->setModel(Client::bufferModel());
316 inputWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
318 ui.bufferWidget->setFocusProxy(inputWidget);
321 void MainWin::setupTopicWidget() {
322 VerticalDock *dock = new VerticalDock(tr("Topic"), this);
323 dock->setObjectName("TopicDock");
324 TopicWidget *topicwidget = new TopicWidget(dock);
325 connect(topicwidget, SIGNAL(topicChanged(const QString &)), this, SLOT(changeTopic(const QString &)));
327 dock->setWidget(topicwidget);
329 topicwidget->setModel(Client::bufferModel());
330 topicwidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
332 addDockWidget(Qt::TopDockWidgetArea, dock);
334 ui.menuViews->addAction(dock->toggleViewAction());
337 void MainWin::setupStatusBar() {
339 updateLagIndicator(0);
340 statusBar()->addPermanentWidget(coreLagLabel);
341 connect(Client::signalProxy(), SIGNAL(lagUpdated(int)), this, SLOT(updateLagIndicator(int)));
344 connect(Client::instance(), SIGNAL(securedConnection()), this, SLOT(securedConnection()));
345 sslLabel->setPixmap(QPixmap());
346 statusBar()->addPermanentWidget(sslLabel);
348 ui.menuViews->addSeparator();
349 QAction *showStatusbar = ui.menuViews->addAction(tr("Statusbar"));
350 showStatusbar->setCheckable(true);
352 UiSettings uiSettings;
354 bool enabled = uiSettings.value("ShowStatusBar", QVariant(true)).toBool();
355 showStatusbar->setChecked(enabled);
356 enabled ? statusBar()->show() : statusBar()->hide();
358 connect(showStatusbar, SIGNAL(toggled(bool)), statusBar(), SLOT(setVisible(bool)));
359 connect(showStatusbar, SIGNAL(toggled(bool)), this, SLOT(saveStatusBarStatus(bool)));
362 void MainWin::saveStatusBarStatus(bool enabled) {
363 UiSettings uiSettings;
364 uiSettings.setValue("ShowStatusBar", enabled);
367 void MainWin::setupSystray() {
368 connect(timer, SIGNAL(timeout()), this, SLOT(makeTrayIconBlink()));
369 connect(Client::messageModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
370 this, SLOT(messagesInserted(const QModelIndex &, int, int)));
372 systrayMenu = new QMenu(this);
373 systrayMenu->addAction(ui.actionAboutQuassel);
374 systrayMenu->addSeparator();
375 systrayMenu->addAction(ui.actionConnectCore);
376 systrayMenu->addAction(ui.actionDisconnectCore);
377 systrayMenu->addSeparator();
378 systrayMenu->addAction(ui.actionQuit);
380 systray->setContextMenu(systrayMenu);
383 if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
388 connect(systray, SIGNAL(activated( QSystemTrayIcon::ActivationReason )),
389 this, SLOT(systrayActivated( QSystemTrayIcon::ActivationReason )));
394 void MainWin::changeEvent(QEvent *event) {
395 if(event->type() == QEvent::WindowStateChange) {
396 if(windowState() & Qt::WindowMinimized) {
398 if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnMinimize").toBool()) {
406 // FIXME this should be made prettier...
407 void MainWin::changeTopic(const QString &topic) {
408 BufferId id = ui.bufferWidget->currentBuffer();
409 if(!id.isValid()) return;
410 Buffer *buffer = Client::buffer(id);
411 if(buffer) Client::userInput(buffer->bufferInfo(), QString("/topic %1").arg(topic));
414 void MainWin::connectedToCore() {
415 Q_CHECK_PTR(Client::bufferViewManager());
416 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
417 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigDeleted(int)), this, SLOT(removeBufferView(int)));
418 connect(Client::bufferViewManager(), SIGNAL(initDone()), this, SLOT(loadLayout()));
420 foreach(BufferInfo id, Client::allBufferInfos()) {
421 Client::backlogManager()->requestBacklog(id.bufferId(), 500, -1);
426 void MainWin::setConnectedState() {
427 //ui.menuCore->setEnabled(true);
428 ui.actionConnectCore->setEnabled(false);
429 ui.actionDisconnectCore->setEnabled(true);
430 ui.actionCoreInfo->setEnabled(true);
431 ui.menuViews->setEnabled(true);
432 ui.bufferWidget->show();
433 statusBar()->showMessage(tr("Connected to core."));
434 setWindowIcon(onlineTrayIcon);
435 qApp->setWindowIcon(onlineTrayIcon);
436 systray->setIcon(onlineTrayIcon);
437 if(sslLabel->width() == 0)
438 sslLabel->setPixmap(QPixmap::fromImage(QImage(":/16x16/status/no-ssl")));
441 void MainWin::loadLayout() {
443 int accountId = Client::currentCoreAccount().toInt();
444 restoreState(s.value(QString("MainWinState-%1").arg(accountId)).toByteArray(), accountId);
447 void MainWin::saveLayout() {
449 int accountId = Client::currentCoreAccount().toInt();
450 if(accountId > 0) s.setValue(QString("MainWinState-%1").arg(accountId) , saveState(accountId));
453 void MainWin::updateLagIndicator(int lag) {
454 coreLagLabel->setText(QString(tr("Core Lag: %1 msec")).arg(lag));
458 void MainWin::securedConnection() {
459 // todo: make status bar entry
460 sslLabel->setPixmap(QPixmap::fromImage(QImage(":/16x16/status/ssl")));
463 void MainWin::disconnectedFromCore() {
464 // save core specific layout and remove bufferviews;
467 BufferViewDock *dock;
468 foreach(QAction *action, ui.menuBufferViews->actions()) {
469 actionData = action->data();
470 if(!actionData.isValid())
473 dock = qobject_cast<BufferViewDock *>(action->parent());
474 if(dock && actionData.toInt() != -1) {
475 removeAction(action);
480 restoreState(s.value("MainWinState").toByteArray());
481 setDisconnectedState();
484 void MainWin::setDisconnectedState() {
485 //ui.menuCore->setEnabled(false);
486 ui.actionConnectCore->setEnabled(true);
487 ui.actionDisconnectCore->setEnabled(false);
488 ui.actionCoreInfo->setEnabled(false);
489 ui.menuViews->setEnabled(false);
490 ui.bufferWidget->hide();
491 statusBar()->showMessage(tr("Not connected to core."));
492 setWindowIcon(offlineTrayIcon);
493 qApp->setWindowIcon(offlineTrayIcon);
494 systray->setIcon(offlineTrayIcon);
495 sslLabel->setPixmap(QPixmap());
498 void MainWin::showCoreConnectionDlg(bool autoConnect) {
499 coreConnectDlg = new CoreConnectDlg(this, autoConnect);
500 connect(coreConnectDlg, SIGNAL(finished(int)), this, SLOT(coreConnectionDlgFinished(int)));
501 coreConnectDlg->setModal(true);
502 coreConnectDlg->show();
505 void MainWin::coreConnectionDlgFinished(int /*code*/) {
506 coreConnectDlg->close();
510 void MainWin::showChannelList(NetworkId netId) {
511 if(!netId.isValid()) {
512 QAction *action = qobject_cast<QAction *>(sender());
514 netId = action->data().value<NetworkId>();
516 channelListDlg->setNetwork(netId);
517 channelListDlg->show();
520 void MainWin::showCoreInfoDlg() {
521 CoreInfoDlg dlg(this);
525 void MainWin::showSettingsDlg() {
529 void MainWin::showDebugConsole() {
530 debugConsole->show();
533 void MainWin::showAboutDlg() {
538 void MainWin::closeEvent(QCloseEvent *event) {
540 if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool()) {
548 void MainWin::systrayActivated( QSystemTrayIcon::ActivationReason activationReason) {
549 if(activationReason == QSystemTrayIcon::Trigger) {
554 void MainWin::toggleVisibility() {
555 if(isHidden() /*|| !isActiveWindow()*/) {
566 // setFocus(); //Qt::ActiveWindowFocusReason
569 if(systray->isSystemTrayAvailable ()) {
572 if(!systray->isVisible()) {
581 void MainWin::messagesInserted(const QModelIndex &parent, int start, int end) {
583 if(QApplication::activeWindow() != 0)
589 for(int i = start; i <= end; i++) {
590 QModelIndex idx = Client::messageModel()->index(i, ChatLineModel::ContentsColumn);
592 qDebug() << "MainWin::messagesInserted(): Invalid model index!";
595 Message::Flags flags = (Message::Flags)idx.data(ChatLineModel::FlagsRole).toInt();
596 BufferId bufId = idx.data(ChatLineModel::BufferIdRole).value<BufferId>();
597 BufferInfo::Type bufType = Client::networkModel()->bufferType(bufId);
599 if(flags & Message::Highlight || bufType == BufferInfo::QueryBuffer) {
600 QString title = Client::networkModel()->networkName(bufId) + " - " + Client::networkModel()->bufferName(bufId);
602 // FIXME Don't instantiate this for every highlight...
603 UiSettings uiSettings;
605 bool displayBubble = uiSettings.value("NotificationBubble", QVariant(true)).toBool();
606 bool displayDesktop = uiSettings.value("NotificationDesktop", QVariant(true)).toBool();
607 if(displayBubble || displayDesktop) {
608 if(uiSettings.value("DisplayPopupMessages", QVariant(true)).toBool()) {
609 QString text = idx.data(ChatLineModel::DisplayRole).toString();
610 if(displayBubble) displayTrayIconMessage(title, text);
612 if(displayDesktop) sendDesktopNotification(title, text);
615 if(uiSettings.value("AnimateTrayIcon", QVariant(true)).toBool()) {
616 QApplication::alert(this);
617 setTrayIconActivity(true);
624 bool MainWin::event(QEvent *event) {
625 if(event->type() == QEvent::WindowActivate)
626 setTrayIconActivity(false);
627 return QMainWindow::event(event);
633 Using the notification-daemon from Freedesktop's Galago project
634 http://www.galago-project.org/specs/notification/0.9/x408.html#command-notify
636 void MainWin::sendDesktopNotification(const QString &title, const QString &message) {
638 QMap<QString, QVariant> hints;
639 UiSettings uiSettings;
641 hints["x"] = uiSettings.value("NotificationDesktopHintX", QVariant(0)).toInt(); // Standard hint: x location for the popup to show up
642 hints["y"] = uiSettings.value("NotificationDesktopHintY", QVariant(0)).toInt(); // Standard hint: y location for the popup to show up
644 actions << "click" << "Click Me!";
646 QDBusReply<uint> reply = desktopNotifications->Notify(
647 "Quassel", // Application name
648 notificationId, // ID of previous notification to replace
649 "", // Icon to display
650 title, // Summary / Header of the message to display
651 QString("%1: %2:\n%3").arg(QTime::currentTime().toString()).arg(title).arg(message), // Body of the message to display
652 actions, // Actions from which the user may choose
653 hints, // Hints to the server displaying the message
654 uiSettings.value("NotificationDesktopTimeout", QVariant(5000)).toInt() // Timeout in milliseconds
657 if(!reply.isValid()) {
659 // could also happen if no notification service runs, so... whatever :)
660 //qDebug() << "Error on sending notification..." << reply.error();
664 notificationId = reply.value();
666 // qDebug() << "ID: " << notificationId << " Time: " << QTime::currentTime().toString();
670 void MainWin::desktopNotificationClosed(uint id, uint reason) {
671 Q_UNUSED(id); Q_UNUSED(reason);
672 // qDebug() << "OID: " << notificationId << " ID: " << id << " Reason: " << reason << " Time: " << QTime::currentTime().toString();
677 void MainWin::desktopNotificationInvoked(uint id, const QString & action) {
678 Q_UNUSED(id); Q_UNUSED(action);
679 // qDebug() << "OID: " << notificationId << " ID: " << id << " Action: " << action << " Time: " << QTime::currentTime().toString();
682 #endif /* HAVE_DBUS */
684 void MainWin::displayTrayIconMessage(const QString &title, const QString &message) {
685 systray->showMessage(title, message);
688 void MainWin::setTrayIconActivity(bool active) {
690 if(!timer->isActive())
694 systray->setIcon(onlineTrayIcon);
698 void MainWin::makeTrayIconBlink() {
700 systray->setIcon(onlineTrayIcon);
701 trayIconActive = false;
703 systray->setIcon(activeTrayIcon);
704 trayIconActive = true;
708 void MainWin::clientNetworkCreated(NetworkId id) {
709 const Network *net = Client::network(id);
710 QAction *act = new QAction(net->networkName(), this);
711 act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
712 act->setData(QVariant::fromValue<NetworkId>(id));
713 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
714 connect(act, SIGNAL(triggered()), this, SLOT(connectOrDisconnectFromNet()));
716 QAction *beforeAction = 0;
717 foreach(QAction *action, ui.menuNetworks->actions()) {
718 if(action->isSeparator()) {
719 beforeAction = action;
722 if(net->networkName().localeAwareCompare(action->text()) < 0) {
723 beforeAction = action;
727 Q_CHECK_PTR(beforeAction);
728 ui.menuNetworks->insertAction(beforeAction, act);
731 void MainWin::clientNetworkUpdated() {
732 const Network *net = qobject_cast<const Network *>(sender());
736 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
740 action->setText(net->networkName());
742 switch(net->connectionState()) {
743 case Network::Initialized:
744 action->setIcon(QIcon(":/16x16/actions/network-connect"));
746 case Network::Disconnected:
747 action->setIcon(QIcon(":/16x16/actions/network-disconnect"));
750 action->setIcon(QIcon(":/16x16/actions/gear"));
754 void MainWin::clientNetworkRemoved(NetworkId id) {
755 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(id.toInt()));
759 action->deleteLater();
762 void MainWin::connectOrDisconnectFromNet() {
763 QAction *act = qobject_cast<QAction *>(sender());
765 const Network *net = Client::network(act->data().value<NetworkId>());
767 if(net->connectionState() == Network::Disconnected) net->requestConnect();
768 else net->requestDisconnect();
773 void MainWin::on_actionDebugNetworkModel_triggered(bool) {
774 QTreeView *view = new QTreeView;
775 view->setAttribute(Qt::WA_DeleteOnClose);
776 view->setWindowTitle("Debug NetworkModel View");
777 view->setModel(Client::networkModel());
778 view->setColumnWidth(0, 250);
779 view->setColumnWidth(1, 250);
780 view->setColumnWidth(2, 80);
781 view->resize(610, 300);