+ QApplication::quit();
+ }
+}
+
+void MainWin::changeEvent(QEvent *event) {
+ if(event->type() == QEvent::WindowStateChange) {
+ if(windowState() & Qt::WindowMinimized) {
+ QtUiSettings s;
+ if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnMinimize").toBool()) {
+ hideToTray();
+ event->accept();
+ return;
+ }
+ }
+ }
+
+#ifdef Q_WS_WIN
+ else if(event->type() == QEvent::ActivationChange)
+ dwTickCount = GetTickCount(); // needed for toggleMinimizedToTray()
+#endif
+
+ event->ignore();
+}
+
+void MainWin::hideToTray() {
+ if(!systemTray()->isSystemTrayAvailable()) {
+ qWarning() << Q_FUNC_INFO << "was called with no SystemTray available!";
+ return;
+ }
+ hide();
+ systemTray()->setIconVisible();
+ _isHidden = true;
+}
+
+void MainWin::toggleMinimizedToTray() {
+#ifdef Q_WS_WIN
+ // the problem is that we lose focus when the systray icon is activated
+ // and we don't know the former active window
+ // therefore we watch for activation event and use our stopwatch :)
+ // courtesy: KSystemTrayIcon
+ if(GetTickCount() - dwTickCount >= 300)
+ // we weren't active in the last 300ms -> activate
+ forceActivated();
+ else
+ hideToTray();
+
+#else
+
+ if(!isVisible() || isMinimized())
+ // restore
+ forceActivated();
+ else
+ hideToTray();
+
+#endif
+}
+
+void MainWin::forceActivated() {
+#ifdef Q_WS_X11
+ // Bypass focus stealing prevention
+ QX11Info::setAppUserTime(QX11Info::appTime());
+#endif
+
+ if(windowState() & Qt::WindowMinimized) {
+ // restore
+ setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
+ }
+
+ move(frameGeometry().topLeft()); // avoid placement policies
+ show();
+ raise();
+ activateWindow();
+ _isHidden = false;
+}
+
+void MainWin::messagesInserted(const QModelIndex &parent, int start, int end) {
+ Q_UNUSED(parent);
+
+ bool hasFocus = QApplication::activeWindow() != 0;
+
+ for(int i = start; i <= end; i++) {
+ QModelIndex idx = Client::messageModel()->index(i, ChatLineModel::ContentsColumn);
+ if(!idx.isValid()) {
+ qDebug() << "MainWin::messagesInserted(): Invalid model index!";
+ continue;
+ }
+ Message::Flags flags = (Message::Flags)idx.data(ChatLineModel::FlagsRole).toInt();
+ if(flags.testFlag(Message::Backlog) || flags.testFlag(Message::Self))
+ continue;
+ flags |= Message::Backlog; // we only want to trigger a highlight once!
+ Client::messageModel()->setData(idx, (int)flags, ChatLineModel::FlagsRole);
+
+ BufferId bufId = idx.data(ChatLineModel::BufferIdRole).value<BufferId>();
+ BufferInfo::Type bufType = Client::networkModel()->bufferType(bufId);
+
+ if(hasFocus && bufId == _bufferWidget->currentBuffer())
+ continue;
+
+ if(flags & Message::Highlight || bufType == BufferInfo::QueryBuffer) {
+ QModelIndex senderIdx = Client::messageModel()->index(i, ChatLineModel::SenderColumn);
+ QString sender = senderIdx.data(ChatLineModel::EditRole).toString();
+ QString contents = idx.data(ChatLineModel::DisplayRole).toString();
+ AbstractNotificationBackend::NotificationType type;
+
+ if(bufType == BufferInfo::QueryBuffer && !hasFocus)
+ type = AbstractNotificationBackend::PrivMsg;
+ else if(bufType == BufferInfo::QueryBuffer && hasFocus)
+ type = AbstractNotificationBackend::PrivMsgFocused;
+ else if(flags & Message::Highlight && !hasFocus)
+ type = AbstractNotificationBackend::Highlight;
+ else
+ type = AbstractNotificationBackend::HighlightFocused;
+
+ QtUi::invokeNotification(bufId, type, sender, contents);
+ }
+ }
+}
+
+void MainWin::clientNetworkCreated(NetworkId id) {
+ const Network *net = Client::network(id);
+ QAction *act = new QAction(net->networkName(), this);
+ act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
+ act->setData(QVariant::fromValue<NetworkId>(id));
+ connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
+ connect(act, SIGNAL(triggered()), this, SLOT(connectOrDisconnectFromNet()));
+
+ QAction *beforeAction = 0;
+ foreach(QAction *action, _networksMenu->actions()) {
+ if(!action->data().isValid()) // ignore stock actions
+ continue;
+ if(net->networkName().localeAwareCompare(action->text()) < 0) {
+ beforeAction = action;
+ break;
+ }
+ }
+ _networksMenu->insertAction(beforeAction, act);
+}
+
+void MainWin::clientNetworkUpdated() {
+ const Network *net = qobject_cast<const Network *>(sender());
+ if(!net)
+ return;
+
+ QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
+ if(!action)
+ return;
+
+ action->setText(net->networkName());
+
+ switch(net->connectionState()) {
+ case Network::Initialized:
+ action->setIcon(SmallIcon("network-connect"));
+ break;
+ case Network::Disconnected:
+ action->setIcon(SmallIcon("network-disconnect"));
+ break;
+ default:
+ action->setIcon(SmallIcon("network-wired"));
+ }
+}
+
+void MainWin::clientNetworkRemoved(NetworkId id) {
+ QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(id.toInt()));
+ if(!action)
+ return;
+
+ action->deleteLater();
+}
+
+void MainWin::connectOrDisconnectFromNet() {
+ QAction *act = qobject_cast<QAction *>(sender());
+ if(!act) return;
+ const Network *net = Client::network(act->data().value<NetworkId>());
+ if(!net) return;
+ if(net->connectionState() == Network::Disconnected) net->requestConnect();
+ else net->requestDisconnect();
+}
+
+void MainWin::on_actionDebugNetworkModel_triggered() {
+ QTreeView *view = new QTreeView;
+ view->setAttribute(Qt::WA_DeleteOnClose);
+ view->setWindowTitle("Debug NetworkModel View");
+ view->setModel(Client::networkModel());
+ view->setColumnWidth(0, 250);
+ view->setColumnWidth(1, 250);
+ view->setColumnWidth(2, 80);
+ view->resize(610, 300);
+ view->show();
+}
+
+void MainWin::on_actionDebugBufferViewOverlay_triggered() {
+ DebugBufferViewOverlay *overlay = new DebugBufferViewOverlay(0);
+ overlay->setAttribute(Qt::WA_DeleteOnClose);
+ overlay->show();
+}
+
+void MainWin::on_actionDebugMessageModel_triggered() {
+ QTableView *view = new QTableView(0);
+ DebugMessageModelFilter *filter = new DebugMessageModelFilter(view);
+ filter->setSourceModel(Client::messageModel());
+ view->setModel(filter);
+ view->setAttribute(Qt::WA_DeleteOnClose, true);
+ view->verticalHeader()->hide();
+ view->horizontalHeader()->setStretchLastSection(true);
+ view->show();
+}
+
+void MainWin::on_actionDebugLog_triggered() {
+ DebugLogWidget *logWidget = new DebugLogWidget(0);
+ logWidget->show();
+}
+
+// FIXME
+void MainWin::saveStateToSession(const QString &sessionId) {
+ return;
+ SessionSettings s(sessionId);
+
+ s.setValue("MainWinSize", size());
+ s.setValue("MainWinPos", pos());
+ s.setValue("MainWinState", saveState());
+}
+
+// FIXME
+void MainWin::saveStateToSessionSettings(SessionSettings & s)
+{
+ s.setValue("MainWinSize", size());
+ s.setValue("MainWinPos", pos());
+ s.setValue("MainWinState", saveState());
+}
+
+void MainWin::showStatusBarMessage(const QString &message) {
+ statusBar()->showMessage(message, 10000);