Browse Source

Added FileSystem static class, prepairing flexible ui scale tools

Ivan Arkhipov 6 years ago
parent
commit
4570f287f3
11 changed files with 103 additions and 59 deletions
  1. 6 2
      Legacy.pro
  2. 6 5
      config.cpp
  3. 2 1
      downloadmanager.cpp
  4. 34 0
      filesystem.cpp
  5. 26 0
      filesystem.h
  6. 8 35
      helper.cpp
  7. 0 3
      helper.h
  8. 12 6
      main.cpp
  9. 4 3
      mainwindow.cpp
  10. 2 1
      network.cpp
  11. 3 3
      ui_mainwindow.h

+ 6 - 2
Legacy.pro

@@ -33,7 +33,8 @@ SOURCES += \
     network.cpp \
     helper.cpp \
     downloadmanager.cpp \
-    gui.cpp
+    gui.cpp \
+    filesystem.cpp
 
 HEADERS += \
         mainwindow.h \
@@ -42,11 +43,14 @@ HEADERS += \
     network.h \
     helper.h \
     downloadmanager.h \
-    gui.h
+    gui.h \
+    filesystem.h
 
 FORMS += \
         mainwindow.ui
 
+LIBS += -luser32 -lshell32 -lgdi32
+
 RESOURCES += \
     res.qrc \
     res.qrc

+ 6 - 5
config.cpp

@@ -1,8 +1,9 @@
 #include "app.h"
-
+#include "filesystem.h"
 
 AConfig::AConfig() {
     conffile = QApplication::applicationDirPath() + "/settings.ini";
+    //conffile = "../settings.ini";
 }
 
 AConfig::AConfig(const AConfig&) {
@@ -92,7 +93,7 @@ QString AConfig::getSkinFile(){
     App *app = &App::getInstance();
     QString current_theme = QApplication::applicationDirPath() + "/styles/" + app->config->getValue("Global", "current_theme") + "/skin.ini";
     QString default_theme = QApplication::applicationDirPath() + "/styles/" + app->config->getValue("Global", "default_theme") + "/skin.ini";
-    QString skinfile = app->helper->fileExists(current_theme) ? current_theme : default_theme;
+    QString skinfile = FileSystem::fileExists(current_theme) ? current_theme : default_theme;
     return skinfile;
 }
 
@@ -101,7 +102,7 @@ QString AConfig::getSkinName(){
     QString current_theme = app->config->getValue("Global", "current_theme");
     QString default_theme = app->config->getValue("Global", "default_theme");
     QString path = QApplication::applicationDirPath() + "/styles/" + current_theme + "/skin.ini";
-    QString skinname = app->helper->fileExists(path) ? current_theme : default_theme;
+    QString skinname = FileSystem::fileExists(path) ? current_theme : default_theme;
     return skinname;
 }
 
@@ -239,7 +240,7 @@ QStringList AConfig::getLotroPath(){
     // Windows 7
     QSettings m("HKEY_CLASSES_ROOT\\Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache", QSettings::NativeFormat);
     foreach (QString key, m.allKeys()) {
-      if(key.contains("TurbineLauncher.exe") && app->helper->fileExists(key)){
+      if(key.contains("TurbineLauncher.exe") && FileSystem::fileExists(key)){
           paths.append(key.replace("/TurbineLauncher.exe", ""));
       }
     }
@@ -250,7 +251,7 @@ QStringList AConfig::getLotroPath(){
         qDebug() << key;
         if(key.contains("InstallLocation") || key.contains("installlocation")){
             QString folder = n.value(key).toString().replace("\\", "/").replace("/TurbineLauncher.exe", "").replace("\"", "");
-            if(app->helper->fileExists(folder + "/TurbineLauncher.exe")) paths.append(folder);
+            if(FileSystem::fileExists(folder + "/TurbineLauncher.exe")) paths.append(folder);
         }
     }
 

+ 2 - 1
downloadmanager.cpp

@@ -1,4 +1,5 @@
 #include "app.h"
+#include "filesystem.h"
 
 #include <QFileInfo>
 #include <QApplication>
@@ -49,7 +50,7 @@ void DownloadManager::startNextDownload()
     output.setFileName(QApplication::applicationDirPath() + "/data/" + filename);
     qDebug("%s:%i: %s%s", __FILE__, __LINE__, "Начата загрузка файла: ", app->helper->stringToChar(filename));
     // Проверяем целостность файла и игнорируем в случае если он цел
-    QString hash = app->helper->fileHash(QApplication::applicationDirPath() + "/data/" + filename, QCryptographicHash::Md5);
+    QString hash = FileSystem::fileHash(QApplication::applicationDirPath() + "/data/" + filename, QCryptographicHash::Md5);
     QStringList pname = output.fileName().split("/");
     QStringList ptype = pname.last().split("_");
     qDebug() << ptype.first();

+ 34 - 0
filesystem.cpp

@@ -0,0 +1,34 @@
+#include "filesystem.h"
+#include <QDebug>
+
+bool FileSystem::fileExists(QString path) {
+    QFileInfo check_file(path);
+    bool exists = check_file.exists() && check_file.isFile();
+    if (exists == false)
+        qWarning("%s:%i: %s%s", __FILE__, __LINE__, "Файл не найден: ", path.toStdString().c_str());
+    return exists;
+}
+
+QString FileSystem::fileHash(const QString &fileName, QCryptographicHash::Algorithm hashAlgorithm){
+    QFile file(fileName);
+    if (file.open(QIODevice::ReadOnly)) {
+        QByteArray fileData = file.readAll();
+        QByteArray hashData = QCryptographicHash::hash(fileData, hashAlgorithm);
+        return hashData.toHex();
+    }
+    return QByteArray();
+}
+
+void FileSystem::clearFolder(QDir &dir){
+    //Получаем список файлов
+    QStringList lstFiles = dir.entryList(QDir::Files);
+
+    //Удаляем файлы
+    foreach (QString entry, lstFiles){
+        QString entryAbsPath = dir.absolutePath() + "/" + entry;
+        //QFile::setPermissions(entryAbsPath, QFile::ReadOwner | QFile::WriteOwner);
+        qDebug() << dir.absolutePath();
+        QFile::remove(entryAbsPath);
+    }
+}
+

+ 26 - 0
filesystem.h

@@ -0,0 +1,26 @@
+#ifndef FILESYSTEM_H
+#define FILESYSTEM_H
+
+#include <QObject>
+#include <QFile>
+#include <QDir>
+#include <QFileInfo>
+#include <QCryptographicHash>
+
+class FileSystem : public QObject
+{
+    Q_OBJECT
+private:
+    explicit FileSystem(QObject *parent = nullptr);
+
+public:
+    static bool fileExists(QString path);
+    static QString fileHash(const QString &fileName, QCryptographicHash::Algorithm hashAlgorithm);
+    static void clearFolder(QDir &dir);
+
+signals:
+
+public slots:
+};
+
+#endif // FILESYSTEM_H

+ 8 - 35
helper.cpp

@@ -1,19 +1,11 @@
 #include "app.h"
+#include "filesystem.h"
 
 Helper::Helper()
 {
 
 }
 
-QString Helper::fileHash(const QString &fileName, QCryptographicHash::Algorithm hashAlgorithm){
-    QFile file(fileName);
-    if (file.open(QIODevice::ReadOnly)) {
-        QByteArray fileData = file.readAll();
-        QByteArray hashData = QCryptographicHash::hash(fileData, hashAlgorithm);
-        return hashData.toHex();
-    }
-    return QByteArray();
-}
 
 int Helper::getVersion(QString string) {
     std::string str(string.toLatin1().data());
@@ -182,13 +174,7 @@ QString Helper::getSkinName() {
     return skin;
 }
 
-bool Helper::fileExists(QString path) {
-    App *app = &App::getInstance();
-    QFileInfo check_file(path);
-    bool exists = check_file.exists() && check_file.isFile();
-    if(exists == false) qWarning("%s:%i: %s%s", __FILE__, __LINE__, "Файл не найден: ", app->helper->stringToChar(path));
-    return exists;
-}
+
 
 void Helper::applyShadow(QWidget * obj, QString color, int blur, int offset_x, int offset_y){
     QGraphicsDropShadowEffect *pLabelTextShadowEffect = new QGraphicsDropShadowEffect(obj);
@@ -228,7 +214,7 @@ void Helper::myDialogBox(QString text, QString info, QString okText, QString can
 
     QString skin = app->helper->getSkinName();
     QString iconpath = "";
-    iconpath = app->helper->fileExists("styles/" + skin + "/" + image) ? "styles/" + skin + "/" + image : ":/res/styles/" + skin + "/" + image;
+    iconpath = FileSystem::fileExists("styles/" + skin + "/" + image) ? "styles/" + skin + "/" + image : ":/res/styles/" + skin + "/" + image;
     QPixmap icon(iconpath);
 
     app->window->ui->dialogImage->setPixmap(icon.scaled(app->window->ui->dialogImage->width(), app->window->ui->dialogImage->height()));
@@ -332,7 +318,7 @@ void Helper::installMicroPatch(){
         app->helper->prepareMicroPatch();
         app->helper->applyMicroPatch();
         QDir dir("/data/micro");
-        app->helper->clearFolder(dir);
+        FileSystem::clearFolder(dir);
     });
     app->window->ui->hintLabel->setText("Установка завершена");
     app->window->ui->exthintLabel->setText("Обновления успешно установлены");
@@ -361,7 +347,7 @@ void Helper::prepareMicroPatch(){
 
     qInfo("%s:%i: %s", __FILE__, __LINE__, "Опция активна. Начинаем загрузку обновлений");
     app->network->micropatch = QApplication::applicationDirPath() + "/data/micro/" + QString::number(timestamp) + ".db";
-    if(!app->helper->fileExists(app->network->micropatch)){
+    if(!FileSystem::fileExists(app->network->micropatch)){
         qInfo("%s:%i: %s%s", __FILE__, __LINE__, "Файл патча: ", app->helper->stringToChar(app->network->micropatch));
         app->network->micropatch = app->network->getMicroPath(timestamp);
     }
@@ -463,7 +449,7 @@ void Helper::saveLocale(int locale, QPushButton * button){
     if(locale == 0) file = "client_local_English.dat";
     if(locale == 1) file = "client_local_DE.dat";
     if(locale == 2) file = "client_local_FR.dat";
-    if(app->helper->fileExists(app->config->getValue("Local", "folder") + "/" + file)){
+    if(FileSystem::fileExists(app->config->getValue("Local", "folder") + "/" + file)){
         app->window->ui->enButton->setEnabled(true);
         app->window->ui->deButton->setEnabled(true);
         app->window->ui->frButton->setEnabled(true);
@@ -519,7 +505,7 @@ void Helper::startGame(){
 
     QFile f(app->config->getValue("Local", "folder") + "/TurbineLauncher.exe");
     QProcess process;
-    if(fileExists(f.fileName())){
+    if(FileSystem::fileExists(f.fileName())){
         if(f.fileName().contains(" ")) f.setFileName("\"" + f.fileName() + "\"");
         process.startDetached(f.fileName(), args);
         process.waitForFinished(-1);
@@ -679,7 +665,7 @@ bool Helper::isDatReady(){
     QString file = app->config->getValue("Local", "file");
     QFile fl(dir+"/" + file);
     qDebug() << fl.fileName();
-    if(app->helper->fileExists(fl.fileName())){
+    if(FileSystem::fileExists(fl.fileName())){
         QDir game_dir(dir);
         free = game_dir.rename(fl.fileName(), fl.fileName() + "99");
         game_dir.rename(fl.fileName() + "99", fl.fileName());
@@ -767,16 +753,3 @@ int Helper::checkDatFile(){
     }
     return 0;
 }
-
-void Helper::clearFolder(QDir &dir){
-    //Получаем список файлов
-    QStringList lstFiles = dir.entryList(QDir::Files);
-
-    //Удаляем файлы
-    foreach (QString entry, lstFiles){
-        QString entryAbsPath = dir.absolutePath() + "/" + entry;
-        //QFile::setPermissions(entryAbsPath, QFile::ReadOwner | QFile::WriteOwner);
-        qDebug() << dir.absolutePath();
-        QFile::remove(entryAbsPath);
-    }
-}

+ 0 - 3
helper.h

@@ -21,7 +21,6 @@ class Helper:public QObject {
     QMap <QString, QString> htmlColors;
 
     void checkTab(QString tabname);
-    bool fileExists(QString path);
     void loadNews();
     void loadHelp();
     void hideAll();
@@ -34,7 +33,6 @@ class Helper:public QObject {
     void applyShadow(QWidget *obj, QString color, int blur, int offset_x, int offset_y);
     void myDialogBox(QString text, QString info, QString okText, QString cancelText, QString image,
                    QString okAnswer, QString cancelAnswer, int w, int h, bool textshadow, bool is_cancel);
-    QString fileHash(const QString &fileName, QCryptographicHash::Algorithm hashAlgorithm);
     int dateToTimestamp(QString customDateString, QString format);
     void addComboItem(QComboBox* combo, QString itemName, QIcon* icon);
     bool setGameLocale(QString locale);
@@ -52,7 +50,6 @@ class Helper:public QObject {
     void installMicroPatch();
     void applyLoadscreens();
     int checkDatFile();
-    void clearFolder(QDir &dir);
     QByteArray ba;
     QString dialog_ok_answer;
     QString dialog_cancel_answer;

+ 12 - 6
main.cpp

@@ -37,13 +37,19 @@ int main(int argc, char *argv[]){
     //if (argc <= 1 || (argc > 1 && std::string(argv[1]) != std::string("-S") && std::string(argv[1]) != "-prelaunched"))
     //    return 0;
 
-    freopen("log.txt", "w", stdout);
-    freopen("log.txt", "w", stderr);
+//    if (app->config->getValue("General", "dpiscale") == "-1") {
+//        QApplication a(argc, argv);
+//        qDebug() << "No value. Detected " << ((QGuiApplication*)QCoreApplication::instance())->primaryScreen()->physicalDotsPerInch();
+//        app->config->setValue("General", "dpiscale", QString::number(((QGuiApplication*)QCoreApplication::instance())->primaryScreen()->physicalDotsPerInch()));
+//        a.exit(0);
+//        return 0;
+//    } else {
+//        qDebug() << app->config->getValue("general", "dpiscale").toLatin1();
+//        qputenv("QT_SCALE_FACTOR", app->config->getValue("general", "dpiscale").toLatin1());
+//    }
 
-    // if it's later than 25.02.2018 - return. That's for temporary version
-    long long cur_time = time(0);
-    if (cur_time >= 1523331670)
-        return 0;
+    freopen("log.txt", "w", stdout);
+    freopen("log.txt", "a", stderr);
 
     setbuf(stdout, NULL);
     setbuf(stderr, NULL);

+ 4 - 3
mainwindow.cpp

@@ -5,13 +5,14 @@ App *App::instance;
 #include <QBitmap>
 #include <QFutureWatcher>
 
+#include "filesystem.h"
 MainWindow::MainWindow( QMainWindow* parent ) :
-    QMainWindow( parent, Qt::Window | Qt::FramelessWindowHint ),
+    QMainWindow( parent, Qt::Window | Qt::FramelessWindowHint),
     ui( new Ui::MainWindow ), dx( 0 ), dy( 0 ) {
     ui->setupUi(this);
 
     App *app = &App::getInstance();
-    if(!app->helper->fileExists(QApplication::applicationDirPath() + "/settings.ini")) app->config->saveConfig();
+    if(!FileSystem::fileExists(QApplication::applicationDirPath() + "/settings.ini")) app->config->saveConfig();
     QString skin = app->helper->getSkinName();
     setWindowIcon(QIcon(":/res/styles/" + skin + "/icon6.png"));
     QCoreApplication::setOrganizationName(ORGANIZATION_NAME);
@@ -349,7 +350,7 @@ void MainWindow::on_checkMicro_stateChanged(int arg1){
 void MainWindow::on_lotropathButton_clicked(){
     App *app = &App::getInstance();
     QString path = QFileDialog::getExistingDirectory(0, "Выберите папку с игрой");
-    if(path != "" && app->helper->fileExists(path + "/TurbineLauncher.exe")){
+    if(path != "" && FileSystem::fileExists(path + "/TurbineLauncher.exe")){
         app->window->ui->lotropathLabel->setText(path);
         app->config->setValue("Local", "folder", path);
     } else {

+ 2 - 1
network.cpp

@@ -1,4 +1,5 @@
 #include "app.h"
+#include "filesystem.h"
 
 #include <QtNetwork/QNetworkAccessManager>
 #include <QUrl>
@@ -189,7 +190,7 @@ void ANetwork::getPaths(){
 
             if(!paths.empty()) {
                  version = QString::number(app->helper->getVersion(paths.first()));
-                 QString hash = app->helper->fileHash(datafolder + "/" + paths.first(), QCryptographicHash::Md5);
+                 QString hash = FileSystem::fileHash(datafolder + "/" + paths.first(), QCryptographicHash::Md5);
                  if(hash != app->config->getValue("Hashes", s + "s")){
                      qDebug("%s:%i: %s%s", __FILE__, __LINE__, "Эталон хэша отсутствует (удаляю файл): ", app->helper->stringToChar(paths.first()));
                      QFile base(datafolder + "/" + paths.first());

+ 3 - 3
ui_mainwindow.h

@@ -151,7 +151,7 @@ public:
     {
         if (MainWindow->objectName().isEmpty())
             MainWindow->setObjectName(QStringLiteral("MainWindow"));
-        MainWindow->resize(508, 307);
+        MainWindow->resize(448, 303);
         MainWindow->setStyleSheet(QStringLiteral(""));
         centralWidget = new QWidget(MainWindow);
         centralWidget->setObjectName(QStringLiteral("centralWidget"));
@@ -645,7 +645,7 @@ public:
         fontsStatus->setText(QApplication::translate("MainWindow", "\320\236\321\200\320\270\320\263\320\270\320\275\320\260\320\273", Q_NULLPTR));
         videosStatus->setText(QApplication::translate("MainWindow", "\320\241\320\272\320\276\321\200\320\276 \320\277\320\276\321\217\320\262\320\270\321\202\321\201\321\217", Q_NULLPTR));
         label_3->setText(QApplication::translate("MainWindow", "\320\237\320\265\321\200\320\265\320\262\320\276\320\264 \321\202\320\265\320\272\321\201\321\202\320\276\320\262 \320\276\321\202 \320\272\320\276\320\274\320\260\320\275\320\264\321\213 \320\235\320\260\321\201\320\273\320\265\320\264\320\270\321\217", Q_NULLPTR));
-        label->setText(QApplication::translate("MainWindow", "\320\240\321\203\321\201\320\270\321\204\320\270\321\206\320\270\321\200\320\276\320\262\320\260\320\275\320\275\321\213\320\265 \320\267\320\262\321\203\320\272\320\276\320\262\321\213\320\265 \321\204\320\260\320\271\320\273\321\213", Q_NULLPTR));
+        label->setText(QApplication::translate("MainWindow", "\320\240\321\203\321\201\321\201\320\272\320\270\320\265 \320\267\320\262\321\203\320\272\320\276\320\262\321\213\320\265 \321\204\320\260\320\271\320\273\321\213 \320\276\321\202 Mail.ru", Q_NULLPTR));
         label_4->setText(QApplication::translate("MainWindow", "\320\240\321\203\321\201\321\201\320\272\320\270\320\265 \320\272\320\260\321\200\321\202\321\213 \320\276\321\202 \320\275\320\260\321\210\320\265\320\271 \320\272\320\276\320\274\320\260\320\275\320\264\321\213", Q_NULLPTR));
         label_8->setText(QApplication::translate("MainWindow", "\320\235\320\260\321\210\320\260 \320\262\320\265\321\200\321\201\320\270\321\217 \320\267\320\260\321\201\321\202\320\260\320\262\320\276\321\207\320\275\321\213\321\205 \321\215\320\272\321\200\320\260\320\275\320\276\320\262", Q_NULLPTR));
         label_6->setText(QApplication::translate("MainWindow", "\320\222\321\200\320\265\320\274\320\265\320\275\320\275\320\260\321\217 \320\262\320\265\321\200\321\201\320\270\321\217 \321\210\321\200\320\270\321\204\321\202\320\276\320\262 \320\264\320\273\321\217 U22", Q_NULLPTR));
@@ -730,7 +730,7 @@ public:
         goTitle->setText(QApplication::translate("MainWindow", "\320\222\320\276 \321\207\321\202\320\276 \320\261\321\203\320\264\320\265\320\274 \320\270\320\263\321\200\320\260\321\202\321\214 \321\201\320\265\320\263\320\276\320\264\320\275\321\217?", Q_NULLPTR));
         goHeader->setText(QApplication::translate("MainWindow", "\320\227\320\260\320\277\321\203\321\201\320\272 \320\270\320\263\321\200\321\213", Q_NULLPTR));
         goHint->setText(QApplication::translate("MainWindow", "\320\237\320\276\320\264\320\263\320\276\321\202\320\276\320\262\320\272\320\260 \321\204\320\260\320\271\320\273\320\276\320\262 \320\273\320\276\320\272\320\260\320\273\320\270\320\267\320\260\321\206\320\270\320\270 ...", Q_NULLPTR));
-        labelVersion->setText(QApplication::translate("MainWindow", "\320\235\320\260\321\201\320\273\320\265\320\264\320\270\320\265: v1.0.1", Q_NULLPTR));
+        labelVersion->setText(QApplication::translate("MainWindow", "\320\235\320\260\321\201\320\273\320\265\320\264\320\270\320\265: v1.0.0", Q_NULLPTR));
         designButton->setText(QApplication::translate("MainWindow", "\320\236\320\261\320\275\320\276\320\262\320\270\321\202\321\214", Q_NULLPTR));
         mainTitle->setText(QApplication::translate("MainWindow", "\320\222\320\273\320\260\321\201\321\202\320\265\320\273\320\270\320\275 \320\272\320\276\320\273\320\265\321\206 \320\236\320\275\320\273\320\260\320\271\320\275: \320\235\320\260\321\201\320\273\320\265\320\264\320\270\320\265", Q_NULLPTR));
     } // retranslateUi