From 81bdfeda27eb84e798869ced2307dbbfc4175368 Mon Sep 17 00:00:00 2001 From: w-mj Date: Thu, 5 Jul 2018 09:12:56 +0800 Subject: [PATCH] add auto start for linux --- main.cpp | 2 +- maintray.cpp | 54 ++++++++++++++++++++++++++++++++--------------- maintray.h | 2 ++ netcontroller.cpp | 2 +- netcontroller.h | 4 ++-- optionswindow.cpp | 4 ++-- 6 files changed, 45 insertions(+), 23 deletions(-) diff --git a/main.cpp b/main.cpp index 47fe87e..4d97406 100644 --- a/main.cpp +++ b/main.cpp @@ -14,7 +14,7 @@ int main(int argc, char *argv[]) a.setWindowIcon(QIcon(QObject::tr(":/icon/favicon.ico"))); a.setFont(QFont("Microsoft YaHei", 9)); MainTray *tray = new MainTray(); - QObject::connect(tray, MainTray::exit, &a, QApplication::quit); + QObject::connect(tray, &MainTray::exit, &a, &QApplication::quit); tray->show(); return a.exec(); } diff --git a/maintray.cpp b/maintray.cpp index 2727e98..8dfddab 100644 --- a/maintray.cpp +++ b/maintray.cpp @@ -20,7 +20,7 @@ MainTray::MainTray(QObject *parent) : QSystemTrayIcon(parent), setIcon(QIcon(olIconPath)); netctrl = new NetController(user, passwd, this); //passwd为解密后密码 - connect(netctrl, NetController::sendLog, this, writeLog); + connect(netctrl, &NetController::sendLog, this, &MainTray::writeLog); totalTraffic = settings.value("total traffic", 60).toInt(); netctrl->setUsername(user); netctrl->setPassword(passwd); @@ -28,16 +28,16 @@ MainTray::MainTray(QObject *parent) : QSystemTrayIcon(parent), writeLog(tr("Load username[") + user + tr("] traffic[") + QString::number(totalTraffic) + tr("].")); - connect(&opWindow, OptionsWindow::saveSettings, this, updateUserInfo); + connect(&opWindow, &OptionsWindow::saveSettings, this, &MainTray::updateUserInfo); //连接controller状态改变信号 - connect(netctrl, NetController::sendState, this, handleState); + connect(netctrl, &NetController::sendState, this, &MainTray::handleState); - connect(this, QSystemTrayIcon::messageClicked, this, [this]() { + connect(this, &QSystemTrayIcon::messageClicked, this, [this]() { if (currentState == NetController::WrongPass) opWindow.show(); }); - connect(netctrl, NetController::sendInfo, this, handleInfo); + connect(netctrl, &NetController::sendInfo, this, &MainTray::handleInfo); //菜单Action loginAction = new QAction(tr("连接网络"), this); @@ -54,7 +54,7 @@ MainTray::MainTray(QObject *parent) : QSystemTrayIcon(parent), balanceAction = new QAction(tr("账户余额: "), this); ipAction = new QAction(tr("IP地址: "), this); - connect(ipAction, QAction::triggered, this, [this]() { + connect(ipAction, &QAction::triggered, this, [this]() { QStringList ipList = ipAction->text().split('\t'); if (ipList.size() > 1) { @@ -70,7 +70,7 @@ MainTray::MainTray(QObject *parent) : QSystemTrayIcon(parent), muteAction->setToolTip(tr("勿扰模式下不会发出通知")); - connect(loginAction, QAction::triggered, this, [this]() { + connect(loginAction, &QAction::triggered, this, [this]() { writeLog(tr("Login triggered.")); isForceLogin = true; netctrl->sendLogoutRequest(); @@ -78,29 +78,29 @@ MainTray::MainTray(QObject *parent) : QSystemTrayIcon(parent), netctrl->sendLoginRequest(); isForceLogout = false; }); - connect(logoutAction, QAction::triggered, this, [this]() { + connect(logoutAction, &QAction::triggered, this, [this]() { writeLog(tr("Logout triggered.")); netctrl->sendLogoutRequest(); isForceLogout = true; }); - connect(autoLogin, QAction::toggled, this, [this](bool set) { + connect(autoLogin, &QAction::toggled, this, [this](bool set) { writeLog(tr("Autologin turn ") + (set ? tr("[on].") : tr("[off]."))); settings.setValue("isAutoLogin", set); isForceLogout = false; showToolTip(currentState); }); - connect(muteAction, QAction::toggled, this, [this](bool set) { + connect(muteAction, &QAction::toggled, this, [this](bool set) { writeLog(tr("Mute mode turn ") + (set ? tr("[on].") : tr("[off]."))); settings.setValue("isMute", set); }); - connect(bootAction, QAction::toggled, this, [this](bool set) { + connect(bootAction, &QAction::toggled, this, [this](bool set) { writeLog(tr("Boot with system turn ") + (set ? tr("[on].") : tr("[off]."))); settings.setValue("isOnBoot", set); setAutoStart(set); }); - connect(optionsAction, QAction::triggered, this, [this]() { showOptions(); }); - connect(aboutAction, QAction::triggered, this, showAbout); - connect(quitAction, QAction::triggered, this, [this]() { + connect(optionsAction, &QAction::triggered, this, [this]() { showOptions(); }); + connect(aboutAction, &QAction::triggered, this, &MainTray::showAbout); + connect(quitAction, &QAction::triggered, this, [this]() { writeLog(tr("Exit.\n\n")); emit exit(); }); @@ -129,13 +129,13 @@ MainTray::MainTray(QObject *parent) : QSystemTrayIcon(parent), setContextMenu(menu); - connect(this, activated, this, handleActivated); - connect(this, exit, this, [this]() { setVisible(false); }); + connect(this, &MainTray::activated, this, &MainTray::handleActivated); + connect(this, &MainTray::exit, this, [this]() { setVisible(false); }); autoLoginTimer = new QTimer(this); autoLoginTimer->setInterval(checkInterval); autoLoginTimer->start(); - connect(autoLoginTimer, QTimer::timeout, this, [this]() { + connect(autoLoginTimer, &QTimer::timeout, this, [this]() { netctrl->checkState(); }); @@ -266,6 +266,26 @@ void MainTray::setAutoStart(bool set) #endif #ifdef Q_OS_LINUX + QString path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/autostart"; + QFile file(path + "/ipgw.desktop"); + if(!file.open(QIODevice::ReadWrite | QIODevice::Text)) { + showMessage(tr("设置失败"), tr("读写/home/.config/autostart/失败"), this->icon(), msgDur); + return; + } + if (set) { + QTextStream in(&file); + in << "[Desktop Entry]\n" << + "Type=Application\n" << + "Version=1.0\n" << + "Name=NEU-Monitor\n" << + "Comment=NEU-Monitor startup script\n"<< + "Exec=" << qApp->applicationFilePath() << " --hidden\n"<< + "StartupNotify=false\n"<< + "Terminal=false\n"; + file.close(); + } else { + file.remove(); + } #endif } diff --git a/maintray.h b/maintray.h index 3781b95..93906a0 100644 --- a/maintray.h +++ b/maintray.h @@ -14,6 +14,8 @@ #include #include #include +#include +#include class MainTray : public QSystemTrayIcon { diff --git a/netcontroller.cpp b/netcontroller.cpp index 8531bd8..31c5579 100644 --- a/netcontroller.cpp +++ b/netcontroller.cpp @@ -17,7 +17,7 @@ NetController::NetController(QByteArray id, QByteArray passwd, QObject *parent) request.setRawHeader("Accept-Encoding", "gzip, deflate"); request.setRawHeader("Accept-Language", "zh-CN,zh;q=0.9"); - connect(&manager, QNetworkAccessManager::finished, this, handleResponse); + connect(&manager, &QNetworkAccessManager::finished, this, &NetController::handleResponse); } void NetController::checkState() diff --git a/netcontroller.h b/netcontroller.h index 4fe65bf..3d2a397 100644 --- a/netcontroller.h +++ b/netcontroller.h @@ -32,8 +32,8 @@ class NetController : public QObject }; private: - QString checkUrl = "https://ipgw.neu.edu.cn/include/auth_action.php"; - QString loginUrl = "https://ipgw.neu.edu.cn/srun_portal_pc.php?ac_id=1&"; + QString checkUrl = "http://ipgw.neu.edu.cn/include/auth_action.php"; + QString loginUrl = "http://ipgw.neu.edu.cn/srun_portal_pc.php?ac_id=1&"; QString offlineString = "not_online"; QByteArray checkParam, username, password; int traffic; diff --git a/optionswindow.cpp b/optionswindow.cpp index 86a262e..5340e71 100644 --- a/optionswindow.cpp +++ b/optionswindow.cpp @@ -49,10 +49,10 @@ OptionsWindow::OptionsWindow(QByteArray id, QByteArray pass, int traffic, QWidge tabWidget->addTab(page1, tr("常规")); setCentralWidget(tabWidget); - connect(saveButton, QPushButton::clicked, this, [this]() { + connect(saveButton, &QPushButton::clicked, this, [this]() { emit saveSettings(idEdit->text().toUtf8(), passwordEdit->text().toUtf8(), trafficSpin->value()); }); - connect(cancleButton, QPushButton::clicked, this, hide); + connect(cancleButton, &QPushButton::clicked, this, &OptionsWindow::hide); } -- 2.54.0