NetherSwap::NetherSwap(QWidget *parent) : QMainWindow(parent), settings_(this), is_dragging_(false), is_second_instance_(false), tray_icon_(QIcon(":/NetherSwap/Resources/icons/netherswap-tray.ico"), this) { qRegisterMetaType<GameStatus>("GameStatus"); qRegisterMetaType<HotkeySequence>("HotkeySequence"); QLocalSocket single_instance_checker; single_instance_checker.connectToServer("netherswap_instance_check"); if(single_instance_checker.waitForConnected(250)) { // connected means a server is running elsewhere, exit this one single_instance_checker.abort(); QTimer::singleShot(0, this, SLOT(close())); is_second_instance_ = true; return; } single_instance_checker.abort(); single_instance_server_ = new QLocalServer(this); // running a second instance should bring up the first connect(single_instance_server_, SIGNAL(newConnection()), this, SLOT(Unminimize())); single_instance_server_->listen("netherswap_instance_check"); QThread* monitor_thread = new QThread(); GameMonitor* monitor = new GameMonitor("DOTA 2", "Valve001"); monitor->moveToThread(monitor_thread); connect(monitor_thread, SIGNAL(started()), monitor, SLOT(Start())); connect(monitor_thread, SIGNAL(finished()), monitor, SLOT(deleteLater())); connect(monitor_thread, SIGNAL(finished()), monitor_thread, SLOT(deleteLater())); connect(this, SIGNAL(destroyed()), monitor_thread, SLOT(quit())); connect(monitor, SIGNAL(StatusChanged(GameStatus)), this, SLOT(StatusChanged(GameStatus))); ui.setupUi(this); ReadSettings(); connect(ui.in_game_hotkey_btn, SIGNAL(HotkeySet(const HotkeySequence&)), this, SLOT(InGameHotkeyChanged(const HotkeySequence&))); connect(ui.out_of_game_hotkey_btn, SIGNAL(HotkeySet(const HotkeySequence&)), this, SLOT(OutOfGameHotkeyChanged(const HotkeySequence&))); setWindowFlags(Qt::FramelessWindowHint); connect(ui.minimize_btn, SIGNAL(clicked()), this, SLOT(hide())); connect(ui.close_btn, SIGNAL(clicked()), this, SLOT(close())); tray_icon_.show(); connect(&tray_icon_, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(IconActivated(QSystemTrayIcon::ActivationReason))); ui.version_label->setText(QString("v%1.%2").arg(NETHERSWAP_VERSION_MAJOR) .arg(NETHERSWAP_VERSION_MINOR)); connect(monitor, SIGNAL(StatusMessage(const QString&)), ui.status_label, SLOT(setText(const QString&))); monitor_thread->start(); }
TrayIcon::TrayIcon(MainWindow *window, QObject *parent) : QObject(parent), trayico(parent), eventtimer(parent) { hostwindow = window; traymenu.addAction( "Show Window", this, SLOT(MenuShowWindow()) ); traymenu.addAction( "E&xit", this, SLOT(MenuQuit()) ); trayico.setToolTip("If ... Then Do ..."); trayico.setIcon(hostwindow->windowIcon()); trayico.setContextMenu(&traymenu); trayico.show(); eventtimer.start( 1000 ); connect( &trayico, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(IconActivated(QSystemTrayIcon::ActivationReason)) ); connect( hostwindow, SIGNAL(iconChanged()), this, SLOT(IconUpdated()) ); connect( &eventtimer, SIGNAL(timeout()), this, SLOT(TimerTimeOut()) ); }