2022-01-24 02:23:08 +00:00
|
|
|
#include "qmlbackend.h"
|
2023-01-03 15:37:14 +00:00
|
|
|
#ifndef Q_OS_WASM
|
2022-01-24 02:23:08 +00:00
|
|
|
#include "server.h"
|
2023-01-03 15:37:14 +00:00
|
|
|
#endif
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2022-12-18 04:52:52 +00:00
|
|
|
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
|
|
|
|
#include "shell.h"
|
|
|
|
#endif
|
|
|
|
|
2022-12-18 07:08:01 +00:00
|
|
|
#if defined(Q_OS_WIN32)
|
|
|
|
#include "applink.c"
|
|
|
|
#endif
|
|
|
|
|
2022-12-18 04:52:52 +00:00
|
|
|
#include <QSplashScreen>
|
|
|
|
#include <QScreen>
|
2023-01-03 15:37:14 +00:00
|
|
|
#include <QFileDialog>
|
2022-12-18 04:52:52 +00:00
|
|
|
|
2023-01-03 15:37:14 +00:00
|
|
|
#if defined(Q_OS_ANDROID) || defined(Q_OS_WASM)
|
2022-09-14 05:01:10 +00:00
|
|
|
static bool copyPath(const QString &srcFilePath, const QString &tgtFilePath)
|
|
|
|
{
|
|
|
|
QFileInfo srcFileInfo(srcFilePath);
|
|
|
|
if (srcFileInfo.isDir()) {
|
|
|
|
QDir targetDir(tgtFilePath);
|
|
|
|
if (!targetDir.exists()) {
|
|
|
|
targetDir.cdUp();
|
|
|
|
if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QDir sourceDir(srcFilePath);
|
|
|
|
QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
|
|
|
|
foreach (const QString &fileName, fileNames) {
|
|
|
|
const QString newSrcFilePath
|
|
|
|
= srcFilePath + QLatin1Char('/') + fileName;
|
|
|
|
const QString newTgtFilePath
|
|
|
|
= tgtFilePath + QLatin1Char('/') + fileName;
|
|
|
|
if (!copyPath(newSrcFilePath, newTgtFilePath))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QFile::remove(tgtFilePath);
|
|
|
|
if (!QFile::copy(srcFilePath, tgtFilePath))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-12-18 04:52:52 +00:00
|
|
|
void fkMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
|
|
|
|
fprintf(stderr, "\r[%s] ", QTime::currentTime().toString("hh:mm:ss").toLatin1().constData());
|
2022-12-18 13:19:35 +00:00
|
|
|
auto localMsg = msg.toUtf8();
|
2022-12-18 04:52:52 +00:00
|
|
|
auto threadName = QThread::currentThread()->objectName().toLatin1().constData();
|
|
|
|
switch (type) {
|
|
|
|
case QtDebugMsg:
|
2023-01-03 15:37:14 +00:00
|
|
|
fprintf(stderr, "[%s/DEBUG] %s\n", threadName, localMsg.constData());
|
2022-12-18 04:52:52 +00:00
|
|
|
break;
|
|
|
|
case QtInfoMsg:
|
2023-01-03 15:37:14 +00:00
|
|
|
fprintf(stderr, "[%s/INFO] %s\n", threadName, localMsg.constData());
|
2022-12-18 04:52:52 +00:00
|
|
|
break;
|
|
|
|
case QtWarningMsg:
|
2023-01-03 15:37:14 +00:00
|
|
|
fprintf(stderr, "[%s/WARNING] %s\n", threadName, localMsg.constData());
|
2022-12-18 04:52:52 +00:00
|
|
|
break;
|
|
|
|
case QtCriticalMsg:
|
2023-01-03 15:37:14 +00:00
|
|
|
fprintf(stderr, "[%s/CRITICAL] %s\n", threadName, localMsg.constData());
|
2022-12-18 04:52:52 +00:00
|
|
|
break;
|
|
|
|
case QtFatalMsg:
|
2023-01-03 15:37:14 +00:00
|
|
|
fprintf(stderr, "[%s/FATAL] %s\n", threadName, localMsg.constData());
|
2022-12-18 04:52:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 02:23:08 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2022-12-18 04:52:52 +00:00
|
|
|
QThread::currentThread()->setObjectName("Main");
|
|
|
|
qInstallMessageHandler(fkMsgHandler);
|
2022-04-30 07:27:56 +00:00
|
|
|
QCoreApplication *app;
|
|
|
|
QCoreApplication::setApplicationName("FreeKill");
|
|
|
|
QCoreApplication::setApplicationVersion("Alpha 0.0.1");
|
|
|
|
|
2023-01-03 15:37:14 +00:00
|
|
|
#ifndef Q_OS_WASM
|
2022-04-30 07:27:56 +00:00
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.setApplicationDescription("FreeKill server");
|
|
|
|
parser.addHelpOption();
|
|
|
|
parser.addVersionOption();
|
|
|
|
parser.addOption({{"s", "server"}, "start server at <port>", "port"});
|
|
|
|
QStringList cliOptions;
|
|
|
|
for (int i = 0; i < argc; i++)
|
|
|
|
cliOptions << argv[i];
|
|
|
|
|
|
|
|
parser.parse(cliOptions);
|
|
|
|
|
|
|
|
bool startServer = parser.isSet("server");
|
|
|
|
ushort serverPort = 9527;
|
|
|
|
|
|
|
|
if (startServer) {
|
|
|
|
app = new QCoreApplication(argc, argv);
|
|
|
|
bool ok = false;
|
|
|
|
if (parser.value("server").toInt(&ok) && ok)
|
|
|
|
serverPort = parser.value("server").toInt();
|
|
|
|
Server *server = new Server;
|
|
|
|
if (!server->listen(QHostAddress::Any, serverPort)) {
|
2022-12-18 04:52:52 +00:00
|
|
|
qFatal("cannot listen on port %d!\n", serverPort);
|
2022-04-30 07:27:56 +00:00
|
|
|
app->exit(1);
|
2022-12-18 04:52:52 +00:00
|
|
|
} else {
|
|
|
|
qInfo("Server is listening on port %d", serverPort);
|
|
|
|
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
|
|
|
|
auto shell = new Shell;
|
|
|
|
shell->start();
|
|
|
|
#endif
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
2022-04-30 07:27:56 +00:00
|
|
|
return app->exec();
|
|
|
|
}
|
2023-01-03 15:37:14 +00:00
|
|
|
#else
|
|
|
|
copyPath(":/", QDir::currentPath());
|
|
|
|
#endif
|
2022-04-30 07:27:56 +00:00
|
|
|
|
2022-12-18 04:52:52 +00:00
|
|
|
app = new QApplication(argc, argv);
|
|
|
|
|
|
|
|
#define SHOW_SPLASH_MSG(msg) \
|
|
|
|
splash.showMessage(msg, Qt::AlignHCenter | Qt::AlignBottom);
|
|
|
|
|
|
|
|
#ifdef Q_OS_ANDROID
|
|
|
|
QScreen *screen = qobject_cast<QApplication *>(app)->primaryScreen();
|
|
|
|
QRect screenGeometry = screen->geometry();
|
|
|
|
int screenWidth = screenGeometry.width();
|
|
|
|
int screenHeight = screenGeometry.height();
|
|
|
|
QSplashScreen splash(QPixmap("assets:/res/image/splash.jpg").scaled(screenWidth, screenHeight));
|
|
|
|
splash.showFullScreen();
|
|
|
|
SHOW_SPLASH_MSG("Copying resources...");
|
|
|
|
copyPath("assets:/res", QDir::currentPath());
|
|
|
|
#else
|
|
|
|
QSplashScreen splash(QPixmap("image/splash.jpg"));
|
|
|
|
splash.show();
|
|
|
|
#endif
|
2022-04-30 07:27:56 +00:00
|
|
|
|
2022-12-18 04:52:52 +00:00
|
|
|
SHOW_SPLASH_MSG("Loading qml files...");
|
2022-04-30 07:27:56 +00:00
|
|
|
QQmlApplicationEngine *engine = new QQmlApplicationEngine;
|
|
|
|
|
|
|
|
QmlBackend backend;
|
|
|
|
backend.setEngine(engine);
|
|
|
|
|
|
|
|
engine->rootContext()->setContextProperty("Backend", &backend);
|
2023-01-03 15:37:14 +00:00
|
|
|
|
2022-03-23 11:40:28 +00:00
|
|
|
#ifdef QT_DEBUG
|
2022-04-30 07:27:56 +00:00
|
|
|
bool debugging = true;
|
2022-03-23 11:40:28 +00:00
|
|
|
#else
|
2022-04-30 07:27:56 +00:00
|
|
|
bool debugging = false;
|
2022-03-23 11:40:28 +00:00
|
|
|
#endif
|
2022-04-30 07:27:56 +00:00
|
|
|
engine->rootContext()->setContextProperty("Debugging", debugging);
|
2023-01-03 15:37:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
QString system;
|
|
|
|
#if defined(Q_OS_ANDROID)
|
|
|
|
system = "Android";
|
|
|
|
#elif defined(Q_OS_WASM)
|
|
|
|
system = "Web";
|
|
|
|
#elif defined(Q_OS_WIN32)
|
|
|
|
system = "Win";
|
|
|
|
#elif defined(Q_OS_LINUX)
|
|
|
|
system = "Linux";
|
2022-12-18 04:52:52 +00:00
|
|
|
#else
|
2023-01-03 15:37:14 +00:00
|
|
|
system = "Other";
|
2022-12-18 04:52:52 +00:00
|
|
|
#endif
|
2023-01-03 15:37:14 +00:00
|
|
|
engine->rootContext()->setContextProperty("OS", system);
|
|
|
|
|
|
|
|
engine->rootContext()->setContextProperty("AppPath", QUrl::fromLocalFile(QDir::currentPath()));
|
2022-04-30 07:27:56 +00:00
|
|
|
engine->load("qml/main.qml");
|
2023-01-03 15:37:14 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
if (engine->rootObjects().isEmpty())
|
|
|
|
return -1;
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2022-12-18 04:52:52 +00:00
|
|
|
splash.close();
|
2022-04-30 07:27:56 +00:00
|
|
|
int ret = app->exec();
|
2022-03-27 12:00:29 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
// delete the engine first
|
|
|
|
// to avoid "TypeError: Cannot read property 'xxx' of null"
|
|
|
|
delete engine;
|
2022-03-27 12:00:29 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
return ret;
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|