Skip to content

Commit

Permalink
feat: first time setup
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Apr 10, 2023
1 parent 36924cc commit 1f25b07
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ set(PROJECT_SOURCES
ui/dialog_hotkey.h
ui/dialog_hotkey.ui

ui/dialog_first_setup.cpp
ui/dialog_first_setup.h
ui/dialog_first_setup.ui

ui/widget/ProxyItem.cpp
ui/widget/ProxyItem.h
ui/widget/ProxyItem.ui
Expand Down
61 changes: 47 additions & 14 deletions main/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "ui/mainwindow_interface.h"

#include <csignal>

#include <QApplication>
Expand All @@ -13,6 +11,9 @@
#include "3rdparty/RunGuard.hpp"
#include "main/NekoRay.hpp"

#include "ui/mainwindow_interface.h"
#include "ui/dialog_first_setup.h"

#ifdef Q_OS_WIN
#include "sys/windows/MiniDump.h"
#endif
Expand All @@ -24,9 +25,32 @@ void signal_handler(int signum) {
}
}

QTranslator* trans = nullptr;
QTranslator* trans_qt = nullptr;

void loadTranslate(const QString& locale) {
if (trans != nullptr) {
trans->deleteLater();
}
if (trans_qt != nullptr) {
trans_qt->deleteLater();
}
//
trans = new QTranslator;
trans_qt = new QTranslator;
QLocale::setDefault(QLocale(locale));
//
if (trans->load(":/translations/" + locale + ".qm")) {
QCoreApplication::installTranslator(trans);
}
if (trans_qt->load(QApplication::applicationDirPath() + "/qtbase_" + locale + ".qm")) {
QCoreApplication::installTranslator(trans_qt);
}
}

#define LOCAL_SERVER_PREFIX "nekoraylocalserver-"

int main(int argc, char *argv[]) {
int main(int argc, char* argv[]) {
// Core dump
#ifdef Q_OS_WIN
Windows_SetCrashHandler();
Expand Down Expand Up @@ -121,7 +145,25 @@ int main(int argc, char *argv[]) {
}

// Load coreType
NekoRay::coreType = ReadFileText("groups/coreType").toInt(); // default to 0
auto coreLoaded = ReadFileText("groups/coreType");
if (coreLoaded.isEmpty()) {
loadTranslate(QLocale().name());
auto dialogFirstSetup = new DialogFirstSetup;
auto coreSelected = dialogFirstSetup->exec();
dialogFirstSetup->deleteLater();
if (coreSelected < 0) {
return 0;
} else {
NekoRay::coreType = coreSelected;
QFile file;
file.setFileName("groups/coreType");
file.open(QIODevice::ReadWrite | QIODevice::Truncate);
file.write(Int2String(coreSelected).toUtf8());
file.close();
}
} else {
NekoRay::coreType = coreLoaded.toInt();
}

// Dir
QDir dir;
Expand Down Expand Up @@ -182,16 +224,7 @@ int main(int argc, char *argv[]) {
locale = QLocale().name();
}
QGuiApplication::tr("QT_LAYOUT_DIRECTION");
QLocale::setDefault(QLocale(locale));
//
QTranslator trans;
if (trans.load(":/translations/" + locale + ".qm")) {
QCoreApplication::installTranslator(&trans);
}
QTranslator trans_qt;
if (trans_qt.load(QApplication::applicationDirPath() + "/qtbase_" + locale + ".qm")) {
QCoreApplication::installTranslator(&trans_qt);
}
loadTranslate(locale);

// Signals
signal(SIGTERM, signal_handler);
Expand Down
21 changes: 21 additions & 0 deletions translations/fa_IR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,27 @@
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>DialogFirstSetup</name>
<message>
<source>First time setup</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>It looks like you are using this software for the first time, please select a core.

These settings can be changed later.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Exit</source>
<translation type="obsolete">خروج</translation>
</message>
<message>
<source>Quit</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>DialogHotkey</name>
<message>
Expand Down
19 changes: 19 additions & 0 deletions translations/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,25 @@
<translation>自定义 (%1 完整配置)</translation>
</message>
</context>
<context>
<name>DialogFirstSetup</name>
<message>
<source>First time setup</source>
<translation>首次设置</translation>
</message>
<message>
<source>It looks like you are using this software for the first time, please select a core.

These settings can be changed later.</source>
<translation>看来您是第一次使用该软件,请选择内核。

这些设置可以稍后更改。</translation>
</message>
<message>
<source>Quit</source>
<translation>退出</translation>
</message>
</context>
<context>
<name>DialogHotkey</name>
<message>
Expand Down
23 changes: 23 additions & 0 deletions ui/dialog_first_setup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "dialog_first_setup.h"
#include "ui_dialog_first_setup.h"

#include "main/NekoRay.hpp"

DialogFirstSetup::DialogFirstSetup(QWidget *parent) : QDialog(parent), ui(new Ui::DialogFirstSetup) {
ui->setupUi(this);
}

DialogFirstSetup::~DialogFirstSetup() {
delete ui;
}

void DialogFirstSetup::onButtonClicked() {
auto s = sender();
if (s == ui->v2ray) {
done(NekoRay::CoreType::V2RAY);
} else if (s == ui->singbox) {
done(NekoRay::CoreType::SING_BOX);
} else {
done(-1);
}
}
23 changes: 23 additions & 0 deletions ui/dialog_first_setup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <QDialog>

QT_BEGIN_NAMESPACE
namespace Ui {
class DialogFirstSetup;
}
QT_END_NAMESPACE

class DialogFirstSetup : public QDialog {
Q_OBJECT

public:
explicit DialogFirstSetup(QWidget *parent = nullptr);
~DialogFirstSetup() override;

private:
Ui::DialogFirstSetup *ui;

private slots:
void onButtonClicked();
};
106 changes: 106 additions & 0 deletions ui/dialog_first_setup.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DialogFirstSetup</class>
<widget class="QDialog" name="DialogFirstSetup">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>First time setup</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>It looks like you are using this software for the first time, please select a core.

These settings can be changed later.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="v2ray">
<property name="text">
<string notr="true">V2Ray</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="singbox">
<property name="text">
<string notr="true">sing-box</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="quit">
<property name="text">
<string>Quit</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>v2ray</sender>
<signal>clicked()</signal>
<receiver>DialogFirstSetup</receiver>
<slot>onButtonClicked()</slot>
<hints>
<hint type="sourcelabel">
<x>199</x>
<y>196</y>
</hint>
<hint type="destinationlabel">
<x>199</x>
<y>149</y>
</hint>
</hints>
</connection>
<connection>
<sender>singbox</sender>
<signal>clicked()</signal>
<receiver>DialogFirstSetup</receiver>
<slot>onButtonClicked()</slot>
<hints>
<hint type="sourcelabel">
<x>199</x>
<y>236</y>
</hint>
<hint type="destinationlabel">
<x>199</x>
<y>149</y>
</hint>
</hints>
</connection>
<connection>
<sender>quit</sender>
<signal>clicked()</signal>
<receiver>DialogFirstSetup</receiver>
<slot>onButtonClicked()</slot>
<hints>
<hint type="sourcelabel">
<x>199</x>
<y>276</y>
</hint>
<hint type="destinationlabel">
<x>199</x>
<y>149</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>onButtonClicked()</slot>
</slots>
</ui>

0 comments on commit 1f25b07

Please sign in to comment.