Files
TeacherSalaryMgmt_Frontend_Qt/main.cpp
2026-01-07 21:26:28 +08:00

90 lines
2.6 KiB
C++

#include "mainwindow.h"
#include "dialoglogin.h"
#include "Modules/inicpp.hpp"
#include "Modules/socketactions.h"
#include "Modules/Logger.h"
#include "Modules/global.h"
#include "Modules/methods.h"
#include <QApplication>
#include <iostream>
#include <string>
#include <winsock2.h>
#include <ws2tcpip.h>
using std::string;
using std::to_string;
SOCKET clientSock;
//static bool isRunning = true;
structUserInfo userInfo;
string serverAddr;
int serverPort;
string configPath = ".\\Config.ini";
string debug_configPath = ".\\debug.ini";
struct globalAppInfo {
string appName = "Simple Teacher Salary Management";
string version = "0.0.0.1";
int buildVer = 0;
string buildDate = "2025-12-22";
string developer = "Madobi Nanami";
string buildType = "debug";
} appInfo;
void clientInit_loadConfig();
int str_toint(const string& str); // Convert string to int
int main(int argc, char *argv[])
{
postLog(appInfo.appName + " Ver." + appInfo.version + "." + to_string(appInfo.buildVer) + "." + appInfo.buildType, 1);
postLog("Developed by " + appInfo.developer + " on " + appInfo.buildDate, 1);
clientInit_loadConfig();
QApplication a(argc, argv);
DialogLogin *p_DialogLogin = new DialogLogin;
MainWindow *p_MainWindow = nullptr;
p_DialogLogin->setAttribute(Qt::WA_DeleteOnClose);
p_DialogLogin->show();
QObject::connect(p_DialogLogin, &DialogLogin::loadMainWindow, [&](){
p_MainWindow = new MainWindow;
p_MainWindow->setAttribute(Qt::WA_DeleteOnClose);
p_MainWindow->show();
});
sockAct::sockInit();
postLog("[System] Trying to connect to " + serverAddr + ":" + to_string(serverPort), 1);
if(sockAct::connectToHost(serverAddr, serverPort)){
postLog("[Socket] Connected successfully to " + serverAddr + ":" + to_string(serverPort), 1);
p_DialogLogin->enableAll();
}
return a.exec();
}
void clientInit_loadConfig(){
if(is.debug == 1){
serverAddr = getKeyText(debug_configPath, "Main", "serverAddr");
serverPort = str_toint(getKeyText(debug_configPath, "Main", "serverPort"));
} else {
// <TODO>: Add formal serverAddr && serverPort
serverAddr = "localhost";
serverPort = 50010;
}
postLog("[System] Target server address has been set to: " + serverAddr + ":" + to_string(serverPort), 0);
userInfo.userName = getKeyText(configPath, "userInfo", "userName");
userInfo.passwd = getKeyText(configPath, "userInfo", "passwd");
}
int str_toint(const string& str) {
try {
return stoi(str);
}
catch (...) {
return 0; // or handle error as needed
}
}