Commit 0b60dcfc by yunpeng.song

初始化

parents
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFile>
#include <QSettings>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//通过qprocess启动cmd
_exe.setProgram("cmd");
connect(&_exe,&QProcess::readyRead,this,&MainWindow::on_readStandardContent);
_exe.start();
QString fileName = qApp->applicationDirPath()+QString("/config.ini");
_setting=new QSettings(fileName,QSettings::IniFormat);
QString path = _setting->value("info/vpnPath").toString();
//输入cmd命令启动vpncli.exe
QByteArray command= QString("cd /d %1 \r\n").arg(path).toLocal8Bit();
_exe.write(command);
_isFirst = true;
_getVpnStatus();
m_tray.show();
connect(&m_tray,&SysTray::doShow,this,&MainWindow::showNormal);
connect(&m_tray,&SysTray::doConnect,this,&MainWindow::on_btn_connect_clicked);
connect(&m_tray,&SysTray::doDisconnect,this,&MainWindow::on_btn_disconnect_clicked);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btn_connect_clicked()
{
_isFirst = true;
_getVpnStatus();
if(!_vpnStatus)
{
QString file = qApp->applicationDirPath()+"/"+_setting->value("info/fileName").toString();
qDebug()<<file;
QByteArray command= QString("vpncli.exe -s <%1\n").arg(file).toLocal8Bit();
qDebug()<<command;
//_exe.write("vpncli.exe -s < f:\\myfile.dat\n");
_exe.write(command);
}
}
void MainWindow::on_btn_disconnect_clicked()
{
_isFirst = true;
_getVpnStatus();
if(_vpnStatus)
{
_exe.write("vpncli.exe disconnect\n");
}
}
void MainWindow::on_readStandardContent()
{
if(_isFirst)
ui->textEdit->clear();
QString content = QString::fromLocal8Bit(_exe.readAllStandardOutput());
content.replace("\n","\r");
QStringList list =content.split('\r');
foreach(QString str,list)
{
if(str.isEmpty())
continue;
ui->textEdit->append(str);
}
if(content.contains("state: Disconnected"))
{
ui->label->setText("当前VPN状态:断开");
_vpnStatus = false;
}
if(content.contains("state: Connected"))
{
ui->label->setText("当前VPN状态:连接");
_vpnStatus = true;
}
_isFirst = false;
}
void MainWindow::_getVpnStatus()
{
_exe.write("vpncli.exe state\n");
}
void MainWindow::on_btn_query_clicked()
{
_isFirst = true;
_getVpnStatus();
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProcess>
#include <windows.h>
#include <QSettings>
#include "sysTray.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
//连接vpn
void on_btn_connect_clicked();
//断开vpn
void on_btn_disconnect_clicked();
//读取cmd的标准输出
void on_readStandardContent();
//查询状态
void on_btn_query_clicked();
private:
bool _isFirst;
SysTray m_tray;
Ui::MainWindow *ui;
QProcess _exe;
QSettings* _setting;
bool _vpnStatus;
//获取vpn状态
void _getVpnStatus();
};
#endif // MAINWINDOW_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>606</width>
<height>536</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="btn_connect">
<property name="geometry">
<rect>
<x>30</x>
<y>60</y>
<width>111</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>启动vpn</string>
</property>
</widget>
<widget class="QPushButton" name="btn_disconnect">
<property name="geometry">
<rect>
<x>30</x>
<y>120</y>
<width>111</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>断开vpn</string>
</property>
</widget>
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>210</x>
<y>90</y>
<width>381</width>
<height>361</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>210</x>
<y>30</y>
<width>381</width>
<height>41</height>
</rect>
</property>
<property name="font">
<font>
<family>Agency FB</family>
<pointsize>22</pointsize>
</font>
</property>
<property name="text">
<string>当前VPN状态</string>
</property>
</widget>
<widget class="QPushButton" name="btn_query">
<property name="geometry">
<rect>
<x>30</x>
<y>180</y>
<width>111</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>查询状态</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>606</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
<RCC>
<qresource prefix="/">
<file>resource/config.ini</file>
<file>resource/fm.ico</file>
<file>resource/myfile.dat</file>
</qresource>
</RCC>
[Info]
vpnPath = C:/Program Files (x86)/Cisco/Cisco AnyConnect Secure Mobility Client
fileName = myfile.dat
File added
connect VPN_MCD
8
cn-hexinghe
Password123
\ No newline at end of file
#include "SysTray.h"
#include <QApplication>
#include <QThread>
extern QThread workThread;
SysTray::SysTray(QObject *parent) :
QSystemTrayIcon(parent)
{
_CreatMenu();
setIcon(QIcon(":/resource/fm.ico"));
}
void SysTray::_CreatMenu()
{
m_menu = new QMenu();
m_menu->setObjectName("trayMenu");
QAction *acQuit = new QAction(QString("退出"), m_menu);
QAction *acConnect = new QAction(QString("连接"), m_menu);
QAction *acDisconnect = new QAction(QString("断开"), m_menu);
QAction *acShow = new QAction(QString("显示"), m_menu);
connect(acShow, &QAction::triggered, this, &SysTray::doShow);
connect(acConnect, &QAction::triggered, this, &SysTray::doConnect);
connect(acDisconnect, &QAction::triggered, this, &SysTray::doDisconnect);
connect(acQuit, &QAction::triggered, this, &SysTray::onActionQuitTriggered);
connect(this, &SysTray::activated, this, &SysTray::doShow);
m_menu->addAction(acConnect);
m_menu->addAction(acDisconnect);
m_menu->addAction(acShow);
m_menu->addAction(acQuit);
setContextMenu(m_menu);
}
void SysTray::onActionQuitTriggered()
{
qApp->exit(-1);
}
void SysTray::onACtivited(QSystemTrayIcon::ActivationReason reason)
{
if(reason == QSystemTrayIcon::Trigger)
{
return;
}
if(reason == QSystemTrayIcon::DoubleClick)
{
emit doShow();
}
}
#ifndef SYSTRAY_H
#define SYSTRAY_H
#include <QSystemTrayIcon>
#include <QAction>
#include <QMenu>
class SysTray : public QSystemTrayIcon
{
Q_OBJECT
public:
explicit SysTray(QObject *parent = 0);
signals:
void doShow();
void doConnect();
void doDisconnect();
private:
QMenu *m_menu;
/* 功能:创建菜单
* 参数:[1]token
* 返回:NULL
* */
void _CreatMenu();
private slots:
/* 功能:处理退出点击动作
* 参数:NULL
* 返回:NULL
* */
void onActionQuitTriggered();
void onACtivited(QSystemTrayIcon::ActivationReason reason);
};
#endif // SYSTRAY_H
#-------------------------------------------------
#
# Project created by QtCreator 2018-06-28T10:07:01
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = vpnConnect
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp \
sysTray.cpp
HEADERS += \
mainwindow.h \
sysTray.h
FORMS += \
mainwindow.ui
RESOURCES += \
resource.qrc
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment