main.cpp 2.93 KB
Newer Older
longpanda's avatar
longpanda committed
1
2
3
4
5
6
#include "ventoy2diskwindow.h"

#include <QApplication>
#include <QMessageBox>
#include <QFileInfo>
#include <QStyle>
longpanda's avatar
longpanda committed
7
#include <QDir>
longpanda's avatar
longpanda committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <QDesktopWidget>
#include <QPixmap>
#include <unistd.h>
#include <sys/types.h>

extern "C" {
#include "ventoy_define.h"
#include "ventoy_util.h"
#include "ventoy_qt.h"
}

using namespace std;

char g_log_file[4096];
char g_ini_file[4096];

int main(int argc, char *argv[])
{
    int ret;
longpanda's avatar
longpanda committed
27
    long long size;
longpanda's avatar
longpanda committed
28
29
30
31
32
33
34
35
36
37
38
    QApplication a(argc, argv);
    Ventoy2DiskWindow w;

#ifdef QT_CHECK_EUID
    if (geteuid() != 0)
    {
        QMessageBox::critical(NULL, "Error", "Permission denied!\nPlease run with root privileges.");
        return 1;
    }
#endif

longpanda's avatar
longpanda committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    if (!QFileInfo::exists("./boot/boot.img"))
    {
        QString curdir = a.applicationDirPath();
        int index = curdir.indexOf("/tool/");

        if (index >= 0)
        {
            QDir::setCurrent(curdir.left(index));
        }
        else
        {
            QDir::setCurrent(curdir);        
        }        
    }

longpanda's avatar
longpanda committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    if (!QFileInfo::exists("./boot/boot.img"))
    {
        QMessageBox::critical(NULL, "Error", "Please run under the correct directory.");
        return 1;
    }

    ventoy_log_init();

    snprintf(g_log_file, sizeof(g_log_file), "./log.txt");
    snprintf(g_ini_file, sizeof(g_ini_file), "./Ventoy2Disk.ini");
    for (int i = 0; i < argc; i++)
    {
        if (argv[i] && argv[i + 1] && strcmp(argv[i], "-l") == 0)
        {
            snprintf(g_log_file, sizeof(g_log_file), "%s", argv[i + 1]);
        }
        else if (argv[i] && argv[i + 1] &&  strcmp(argv[i], "-i") == 0)
        {
            snprintf(g_ini_file, sizeof(g_ini_file), "%s", argv[i + 1]);
        }
    }

longpanda's avatar
longpanda committed
76
77
78
79
80
81
82
    QFileInfo Info(g_log_file);
    size = (long long)Info.size();
    if (size >= 4 * SIZE_1MB)
    {
        QFile::remove(g_log_file);
    }

longpanda's avatar
longpanda committed
83
84
85
    vlog("===================================================\n");
    vlog("===== Ventoy2Disk %s powered by QT %s =====\n", ventoy_get_local_version(), qVersion());
    vlog("===================================================\n");
longpanda's avatar
longpanda committed
86
87
    vlog("log file is <%s> lastsize:%lld\n", g_log_file, (long long)size);
    vlog("ini file is <%s>\n", g_ini_file);
longpanda's avatar
longpanda committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

    ventoy_disk_init();
    ventoy_http_init();

    w.setGeometry(QStyle::alignedRect(Qt::LeftToRight,
                                      Qt::AlignCenter,
                                      w.size(),
                                      a.desktop()->availableGeometry()));

    QPixmap pix;
    QIcon icon;
    int len;
    const uchar *data;

    data = (const uchar *)get_window_icon_raw_data(&len);
    pix.loadFromData(data, len);
    icon.addPixmap(pix);
    w.setWindowIcon(icon);

    w.show();
    w.setFixedSize(w.size());

    ret = a.exec();

    ventoy_disk_exit();
    ventoy_http_exit();
        
    vlog("######## Ventoy2Disk QT %s exit ########\n", ventoy_get_local_version());

    /* log exit must at the end */
    ventoy_log_exit();

    return ret;
}