[Change] New Connection config UI and removed SQLite

This commit is contained in:
Leroy.Haoyu.Liu 2019-06-19 00:20:08 +08:00
parent db5c902da1
commit b57651784d
No known key found for this signature in database
GPG Key ID: 6AC1673B587DC37D
12 changed files with 998 additions and 459 deletions

View File

@ -4,7 +4,7 @@
#
#-------------------------------------------------
QT += core gui sql
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
@ -33,7 +33,6 @@ SOURCES += \
importconf.cpp \
inbound_settings.cpp \
vinteract.cpp \
db.cpp \
import_vmess.cpp \
utils.cpp \
runguard.cpp
@ -44,7 +43,6 @@ HEADERS += \
importconf.h \
inbound_settings.h \
vinteract.h \
db.h \
import_vmess.h \
utils.h \
runguard.h

View File

@ -1,11 +1,8 @@
#include "confedit.h"
#include "ui_confedit.h"
#include <iostream>
#include <QSqlDatabase>
#include <QSqlQuery>
#include "mainwindow.h"
#include <QDebug>
#include <QSqlError>
#include <QFile>
#include "db.h"
#include <QIntValidator>
@ -15,7 +12,6 @@ ConfEdit::ConfEdit(QWidget *parent) :
ui(new Ui::ConfEdit)
{
ui->setupUi(this);
connect(this, SIGNAL(updateConfTable()), parentWidget(), SLOT(updateConfTable()));
ui->portLineEdit->setValidator(new QIntValidator());
ui->alterLineEdit->setValidator(new QIntValidator());
}
@ -24,48 +20,10 @@ ConfEdit::~ConfEdit()
{
delete ui;
}
vConfig *vConfig::query(int id)
{
QString selectQuery = "select * from confs where id = " + QString::number(id) + ";";
SQLiteDB myDb = SQLiteDB();
myDb.DoQuery(selectQuery);
myDb.myQuery.first();
this->host = myDb.myQuery.value(1).toString();
this->port = myDb.myQuery.value(2).toString();
this->alias = myDb.myQuery.value(3).toString();
this->uuid = myDb.myQuery.value(4).toString();
this->alterid = myDb.myQuery.value(5).toString();
this->security = myDb.myQuery.value(6).toString();
this->isCustom = myDb.myQuery.value(7).toInt();
return this;
}
int vConfig::save()
{
QSqlDatabase database;
if (QSqlDatabase::contains("qt_sql_default_connection")) {
database = QSqlDatabase::database("qt_sql_default_connection");
} else {
database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName(confDatabase);
}
if (!database.open()) {
qDebug() << "Failed to open database while saving.";
return -1;
}
QSqlQuery myQuery(database);
myQuery.prepare("insert into confs (host, port, alias, uuid, alterid, security, isCustom, selected) values(:host, :port, :alias, :uuid, :alterid, :security, :isCustom, :selected)");
myQuery.bindValue(":host", this->host);
myQuery.bindValue(":port", this->port);
myQuery.bindValue(":alias", this->alias);
myQuery.bindValue(":uuid", this->uuid);
myQuery.bindValue(":alterid", this->alterid);
myQuery.bindValue(":security", this->security);
myQuery.bindValue(":isCustom", this->isCustom);
myQuery.bindValue(":selected", 0);
myQuery.exec();
myQuery.exec("select last_insert_rowid();");
myQuery.first();
return myQuery.value(0).toInt();
return -1;
}
void vConfig::getConfigFromDialog(Ui::ConfEdit *ui)
@ -78,10 +36,3 @@ void vConfig::getConfigFromDialog(Ui::ConfEdit *ui)
this->security = ui->securityCombo->currentText();
this->isCustom = 0;
}
void ConfEdit::on_ConfEdit_accepted()
{
vConfig newConf;
newConf.getConfigFromDialog(this->ui);
newConf.save();
emit updateConfTable();
}

View File

@ -18,7 +18,6 @@ public:
QString alterid;
QString security;
int isCustom;
vConfig *query(int id);
int save();
void getConfigFromDialog(Ui::ConfEdit *ui);
private:
@ -32,11 +31,6 @@ class ConfEdit : public QDialog
public:
explicit ConfEdit(QWidget *parent = nullptr);
~ConfEdit();
signals:
void updateConfTable();
private slots:
void on_ConfEdit_accepted();
private:
Ui::ConfEdit *ui;

File diff suppressed because it is too large Load Diff

View File

@ -1,27 +0,0 @@
#include "db.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include "mainwindow.h"
#include <QDebug>
SQLiteDB::SQLiteDB()
{
QSqlDatabase database;
if (QSqlDatabase::contains("qt_sql_default_connection")) {
database = QSqlDatabase::database("qt_sql_default_connection");
} else {
database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName(confDatabase);
}
if (!database.open()) {
qDebug() << "Failed to open database while querying.";
} else {
this->myQuery = QSqlQuery(database);
}
}
void SQLiteDB::DoQuery(QString queryString)
{
this->myQuery.prepare(queryString);
this->myQuery.exec();
}

View File

@ -1,14 +0,0 @@
#ifndef DB_H
#define DB_H
#include <QSqlQuery>
class SQLiteDB
{
public:
SQLiteDB();
QSqlQuery myQuery;
void DoQuery(QString queryString);
};
#endif // DB_H

View File

@ -2,9 +2,6 @@
#include <QDir>
#include <iostream>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>
@ -23,28 +20,6 @@ void firstRunCheck()
qDebug() << "Config directory created.";
}
QFileInfo confdb("conf/conf.db");
if (!confdb.exists()) {
QSqlDatabase database;
if (QSqlDatabase::contains("qt_sql_default_connection")) {
database = QSqlDatabase::database("qt_sql_default_connection");
} else {
database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName("conf/conf.db");
if(!database.open()) {
qDebug() << "Failed to open database while creating.";
}
}
QSqlQuery query(database);
bool bsuccess =
query.exec("create table confs(id INTEGER primary key AUTOINCREMENT, host char(50), port char(5), "
"alias char(80), uuid char(36), alterid char(5), security char(12), isCustom int, selected int);");
if(!bsuccess) {
qDebug() << "Failed to create table.";
}
}
QFileInfo hvConfInfo("conf/Hv2ray.config.json");
// First Run?
@ -81,12 +56,13 @@ void firstRunCheck()
confFile.close();
}
}
int main(int argc, char *argv[])
{
QApplication _qApp(argc, argv);
RunGuard guard("Hv2ray");
if(!guard.tryToRun()) {
alterMessage("Already running", "Another instance of Hv2ray is already running!");
if(!guard.isSingleInstance()) {
alterMessage("Hv2Ray", "Another instance of Hv2ray is already running!");
return -1;
}

View File

@ -12,7 +12,7 @@
#include "import_vmess.h"
#include "inbound_settings.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <ui_mainwindow.h>
#include "confedit.h"
#include "importconf.h"
#include "vinteract.h"
@ -25,8 +25,8 @@ MainWindow::MainWindow(QWidget *parent) :
this->setWindowIcon(QIcon("Himeki.ico"));
ui->setupUi(this);
updateConfTable();
ui->configTable->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->configTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
// ui->configTable->setContextMenuPolicy(Qt::CustomContextMenu);
// connect(ui->configTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
this->v2Inst = new v2Instance();
hTray = new QSystemTrayIcon();
hTray->setToolTip("Hv2ray");
@ -36,17 +36,17 @@ MainWindow::MainWindow(QWidget *parent) :
if(QFileInfo("config.json").exists()) {
v2Inst->start(this);
}
QAction *select = new QAction("Select", ui->configTable);
QAction *del = new QAction("Delete", ui->configTable);
QAction *rename = new QAction("Rename", ui->configTable);
popMenu->addAction(select);
popMenu->addAction(del);
popMenu->addAction(rename);
connect(select, SIGNAL(triggered()), this, SLOT(select_triggered()));
connect(del, SIGNAL(triggered()), this, SLOT(delConf()));
connect(rename, SIGNAL(triggered()), this, SLOT(renameRow()));
connect(ui->logText, SIGNAL(textChanged()), this, SLOT(scrollToBottom()));
bar = ui->logText->verticalScrollBar();
// QAction *select = new QAction("Select", ui->configTable);
// QAction *del = new QAction("Delete", ui->configTable);
// QAction *rename = new QAction("Rename", ui->configTable);
// popMenu->addAction(select);
// popMenu->addAction(del);
// popMenu->addAction(rename);
// connect(select, SIGNAL(triggered()), this, SLOT(select_triggered()));
// connect(del, SIGNAL(triggered()), this, SLOT(delConf()));
// connect(rename, SIGNAL(triggered()), this, SLOT(renameRow()));
// connect(ui->logText, SIGNAL(textChanged()), this, SLOT(scrollToBottom()));
// bar = ui->logText->verticalScrollBar();
}
MainWindow::~MainWindow()
@ -70,73 +70,33 @@ void MainWindow::on_actionExisting_config_triggered()
}
void MainWindow::showMenu(QPoint pos)
{
if(ui->configTable->indexAt(pos).column() != -1) {
popMenu->move(cursor().pos());
popMenu->show();
}
// if(ui->configTable->indexAt(pos).column() != -1) {
// popMenu->move(cursor().pos());
// popMenu->show();
// }
}
void MainWindow::select_triggered()
{
int row = ui->configTable->selectionModel()->currentIndex().row();
int idIntable = ui->configTable->model()->data(ui->configTable->model()->index(row, 4)).toInt();
this->geneConf(idIntable);
if(this->v2Inst->v2Process->state() == QProcess::Running) {
this->on_restartButton_clicked();
}
// int row = ui->configTable->selectionModel()->currentIndex().row();
// int idIntable = ui->configTable->model()->data(ui->configTable->model()->index(row, 4)).toInt();
// this->geneConf(idIntable);
// if(this->v2Inst->v2Process->state() == QProcess::Running) {
// this->on_restartButton_clicked();
// }
}
void MainWindow::delConf()
{
int row = ui->configTable->selectionModel()->currentIndex().row();
int idIntable = ui->configTable->model()->data(ui->configTable->model()->index(row, 4)).toInt();
QString queryString = "delete from confs where id = " + QString::number(idIntable);
SQLiteDB myDb;
myDb.DoQuery(queryString);
QString rmFile = "conf/" + QString::number(idIntable) + ".conf";
QFile::remove(rmFile);
emit updateConfTable();
}
void MainWindow::updateConfTable()
{
SQLiteDB myDb;
myDb.DoQuery("select COUNT(*) from confs;");
myDb.myQuery.first();
int rows = myDb.myQuery.value(0).toInt();
QStandardItemModel* model = new QStandardItemModel(rows, 5);
ui->configTable->setModel(model);
model->setHeaderData(0, Qt::Horizontal, "Alias");
model->setHeaderData(1, Qt::Horizontal, "Host");
model->setHeaderData(2, Qt::Horizontal, "Port");
model->setHeaderData(3, Qt::Horizontal, "Checked");
model->setHeaderData(4, Qt::Horizontal, "idInTable");
ui->configTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->configTable->setColumnHidden(4, true);
ui->configTable->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->configTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
myDb.DoQuery("select * from confs");
myDb.myQuery.first();
for (int i = 0; i < rows; ++i) {
model->setItem(i, 0, new QStandardItem(myDb.myQuery.value(3).toString()));
model->setItem(i, 1, new QStandardItem(myDb.myQuery.value(1).toString()));
model->setItem(i, 2, new QStandardItem(myDb.myQuery.value(2).toString()));
model->setItem(i, 4, new QStandardItem(myDb.myQuery.value(0).toString()));
if (myDb.myQuery.value(8).toInt() == 1) {
model->setItem(i, 3, new QStandardItem(""));
}
if (i < rows - 1) {
myDb.myQuery.next();
}
}
}
void MainWindow::geneConf(int idIntable)
void MainWindow::generateConfig(int idIntable)
{
vConfig tmpConf;
SQLiteDB myDb;
myDb.DoQuery("update confs set selected = 0");
QString queryString = "update confs set selected = 1 where id = " + QString::number(idIntable);
myDb.DoQuery(queryString);
emit updateConfTable();
tmpConf.query(idIntable);
if (tmpConf.isCustom == 1) {
QString src = "conf/" + QString::number(idIntable) + ".conf";
overrideInbounds(src);
@ -281,13 +241,13 @@ void MainWindow::on_actionExit_triggered()
void MainWindow::renameRow()
{
QString text = QInputDialog::getText(this, "Rename config", "New name:", QLineEdit::Normal);
int row = ui->configTable->currentIndex().row();
int idIntable = ui->configTable->model()->data(ui->configTable->model()->index(row, 4)).toInt();
SQLiteDB mydb;
QString updateString = "update confs set alias = '" + text + "' where id = " + QString::number(idIntable);
mydb.DoQuery(updateString);
emit updateConfTable();
// QString text = QInputDialog::getText(this, "Rename config", "New name:", QLineEdit::Normal);
// int row = ui->configTable->currentIndex().row();
// int idIntable = ui->configTable->model()->data(ui->configTable->model()->index(row, 4)).toInt();
// SQLiteDB mydb;
// QString updateString = "update confs set alias = '" + text + "' where id = " + QString::number(idIntable);
// mydb.DoQuery(updateString);
// emit updateConfTable();
}
void MainWindow::scrollToBottom()

View File

@ -42,7 +42,7 @@ private slots:
void select_triggered();
void on_clbutton_clicked();
void on_rtButton_clicked();
void geneConf(int idIntable);
void generateConfig(int idIntable);
void on_actionVmess_triggered();
void on_activatedTray(QSystemTrayIcon::ActivationReason reason);
void toggleMainWindowVisibility();

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>1060</width>
<height>643</height>
<width>867</width>
<height>519</height>
</rect>
</property>
<property name="windowTitle">
@ -20,11 +20,11 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout" rowstretch="0,2,0,0" columnstretch="2,1">
<item row="0" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,1,1,1,1,4">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<layout class="QVBoxLayout" name="verticalLayout" stretch="1,9,0">
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,1,1,1,0,0,1,4">
<item>
<widget class="QPushButton" name="startButton">
<property name="text">
@ -44,6 +44,9 @@
<property name="text">
<string>Restart</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
@ -54,14 +57,7 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="rtButton">
<property name="text">
<string>Refresh Table</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
@ -73,35 +69,118 @@
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Log Level</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>138</width>
<height>0</height>
</size>
</property>
<item>
<property name="text">
<string>debug</string>
</property>
</item>
<item>
<property name="text">
<string>info</string>
</property>
</item>
<item>
<property name="text">
<string>warning</string>
</property>
</item>
<item>
<property name="text">
<string>error</string>
</property>
</item>
<item>
<property name="text">
<string>none</string>
</property>
</item>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QTableView" name="configTable">
<property name="showGrid">
<bool>true</bool>
</property>
</widget>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3" stretch="1,1">
<item>
<widget class="QListWidget" name="listWidget"/>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Config Detail</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>30</x>
<y>60</y>
<width>85</width>
<height>33</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QToolButton" name="toolButton">
<property name="geometry">
<rect>
<x>180</x>
<y>60</y>
<width>111</width>
<height>33</height>
</rect>
</property>
<property name="text">
<string>Restart To</string>
</property>
<property name="popupMode">
<enum>QToolButton::DelayedPopup</enum>
</property>
</widget>
</widget>
</item>
</layout>
</item>
<item row="1" column="1">
<widget class="QGraphicsView" name="QR"/>
</item>
<item row="2" column="0" colspan="2">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="3" column="0">
<item>
<widget class="QTextBrowser" name="logText"/>
</item>
<item row="3" column="1">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
@ -111,7 +190,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>1060</width>
<width>867</width>
<height>29</height>
</rect>
</property>
@ -135,9 +214,16 @@
</widget>
<addaction name="menuNew"/>
<addaction name="actionPreferences"/>
<addaction name="separator"/>
<addaction name="actionExit"/>
</widget>
<widget class="QMenu" name="menuAbout">
<property name="title">
<string>About</string>
</property>
</widget>
<addaction name="menuFile"/>
<addaction name="menuAbout"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionEdit">

View File

@ -52,7 +52,7 @@ bool RunGuard::isAnotherRunning()
return isRunning;
}
bool RunGuard::tryToRun()
bool RunGuard::isSingleInstance()
{
if ( isAnotherRunning() ) { // Extra check
return false;

View File

@ -14,7 +14,7 @@ public:
~RunGuard();
bool isAnotherRunning();
bool tryToRun();
bool isSingleInstance();
void release();
private: