Debian Codegraph @

src/main.cpp

raw symbols SCIP
1 #include "main_window.h"
2 #include "utils.h"
3
4 int main(int argc, char *argv[]) {
5
6 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
7 static const char ENV_VAR_QT_DEVICE_PIXEL_RATIO[] = "QT_DEVICE_PIXEL_RATIO";
8 if (!qEnvironmentVariableIsSet(ENV_VAR_QT_DEVICE_PIXEL_RATIO) &&
9 !qEnvironmentVariableIsSet("QT_AUTO_SCREEN_SCALE_FACTOR") &&
10 !qEnvironmentVariableIsSet("QT_SCALE_FACTOR") &&
11 !qEnvironmentVariableIsSet("QT_SCREEN_SCALE_FACTORS")) {
12 QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
13 }
14 #endif
15
16 QApplication app(argc, argv);
17
18 // app.setApplicationDisplayName("Rclone Browser");
19 app.setApplicationName("rclone-browser");
20 app.setOrganizationName("rclone-browser");
21 app.setWindowIcon(QIcon(":/icons/icon.png"));
22
23 // initialize SSL libraries
24 // see: https://github.com/linuxdeploy/linuxdeploy-plugin-qt/issues/57
25 #if defined(Q_OS_LINUX)
26 QString currentDir = QDir::currentPath();
27 QDir::setCurrent(QCoreApplication::applicationDirPath());
28 QSslSocket::supportsSsl();
29 QDir::setCurrent(currentDir);
30 #endif
31
32 auto settings = GetSettings();
33
34 // initialize proxy settings
35 if (!(settings->contains("Settings/useProxy"))) {
36 settings->setValue("Settings/useProxy", "false");
37 };
38 if (!(settings->contains("Settings/http_proxy"))) {
39 settings->setValue("Settings/http_proxy", "");
40 };
41 if (!(settings->contains("Settings/https_proxy"))) {
42 settings->setValue("Settings/https_proxy", "");
43 };
44 if (!(settings->contains("Settings/no_proxy"))) {
45 settings->setValue("Settings/no_proxy", "");
46 };
47
48 if (settings->value("Settings/useProxy").toBool()) {
49 qputenv("HTTP_PROXY", settings->value("Settings/http_proxy").toByteArray());
50 qputenv("http_proxy", settings->value("Settings/http_proxy").toByteArray());
51 qputenv("HTTPS_PROXY",
52 settings->value("Settings/https_proxy").toByteArray());
53 qputenv("https_proxy",
54 settings->value("Settings/https_proxy").toByteArray());
55 qputenv("NO_PROXY", settings->value("Settings/no_proxy").toByteArray());
56 qputenv("no_proxy", settings->value("Settings/no_proxy").toByteArray());
57 }
58
59 // remmber darkMode state on app startup
60 // during first run the darkModeIni key might not exist
61 if (!(settings->contains("Settings/darkModeIni"))) {
62 // if darkModeIni does not exist create new key
63 settings->setValue("Settings/darkModeIni", "false");
64 };
65
66 // during first run the darkMode key might not exist
67 if (!(settings->contains("Settings/darkMode"))) {
68 // if darkMode does not exist create new key
69 settings->setValue("Settings/darkMode", "false");
70 };
71
72 bool darkMode = settings->value("Settings/darkMode").toBool();
73
74 settings->setValue("Settings/darkModeIni", darkMode);
75
76 // during first run the iconSize key might not exist
77 if (!(settings->contains("Settings/iconSize"))) {
78 // if iconSize does not exist create new key
79 settings->setValue("Settings/iconSize", "medium");
80 };
81
82 // enforce one instance of Rclone Browser per user
83 QString tmpDir;
84 QString applicationNameBase;
85 QFileInfo applicationPath;
86 QFileInfo applicationUserPath;
87
88 // QString xdg_config_home = qgetenv("XDG_CONFIG_HOME");
89 // qDebug() << QString("main.cpp $XDG_CONFIG_HOME: " + xdg_config_home);
90
91 // QString APPIMAGE = qgetenv("APPIMAGE");
92 // qDebug() << QString("main.cpp $APPIMAGE: " + APPIMAGE);
93
94 QFileInfo appBundlePath;
95
96 if (IsPortableMode()) {
97
98 // qDebug() << QString("isPortable is true");
99 // applicationPath = qApp->applicationFilePath();
100 #ifdef Q_OS_MACOS
101 // on macOS excecutable file is located in
102 // ./rclone-browser.app/Contents/MasOS/
103 // to get actual bundle folder we have
104 // to traverse three levels up
105 applicationPath = qApp->applicationFilePath();
106 tmpDir = applicationPath.absolutePath() + "/../../..";
107
108 // get bundle name
109 QFileInfo MacOSPath = applicationPath.dir().path();
110 QFileInfo ContentsPath = MacOSPath.dir().path();
111 appBundlePath = ContentsPath.dir().path();
112
113 #else
114 // not macOS
115 #ifdef Q_OS_WIN
116 applicationPath = qApp->applicationFilePath();
117 tmpDir = applicationPath.absolutePath();
118 #else
119 QString xdg_config_home = qgetenv("XDG_CONFIG_HOME");
120 tmpDir = xdg_config_home + "/rclone-browser";
121 // create ./rclone-browser folder
122 if (!QDir(tmpDir).exists()) {
123 QDir().mkdir(tmpDir);
124 }
125 #endif
126 #endif
127 } else {
128 // not portable mode
129 // get tmp folder from Qt - OS dependend
130 tmpDir = QDir::tempPath();
131 }
132
133 // check if tmpDir writable
134 // as isWritable does weird things on Windows
135 // we do this old fashioned way by creating temp file
136 QTemporaryFile tempfile(tmpDir + "/rclone-browserXXXXXX.test");
137
138 if (tempfile.open()) {
139 tempfile.close();
140 tempfile.remove();
141 } else {
142 // folder has no write access
143 if (IsPortableMode()) {
144 QMessageBox msgBox;
145 msgBox.setIcon(QMessageBox::Warning);
146 msgBox.setText("You need write "
147 "access to this folder:\n\n"
148 #ifdef Q_OS_MACOS
149 + appBundlePath.absolutePath() +
150 #else
151 #ifdef Q_OS_WIN
152 + tmpDir +
153 #else
154 + tmpDir.left(tmpDir.length() - 15) +
155 #endif
156 #endif
157 "\n\n"
158 #ifdef Q_OS_MACOS
159 "Or remove file:\n\n" +
160 appBundlePath.baseName() +
161 ".ini \n\nfrom the above folder "
162 #else
163 #ifdef Q_OS_WIN
164 "Or remove file:\n\n" +
165 applicationPath.baseName() +
166 ".ini \n\nfrom the above folder "
167 #else
168 "Or remove folder:\n\n" +
169 tmpDir.left(tmpDir.length() - 15) +
170 "\n\n"
171 #endif
172 #endif
173 "to disable portable mode.");
174 msgBox.exec();
175 } else {
176 QMessageBox msgBox;
177 msgBox.setIcon(QMessageBox::Warning);
178 msgBox.setText("You need write "
179 "access to this folder: \n\n"
180 #ifdef Q_OS_MACOS
181 + tmpDir
182 #else
183
184 #ifdef Q_OS_WIN
185 + tmpDir
186 #else
187
188 + tmpDir.left(tmpDir.length() - 15)
189 #endif
190 #endif
191 );
192 msgBox.exec();
193 }
194 return static_cast<int>(
195 0x80004004); // exit immediately if folder not writable
196 }
197
198 // qDebug() << QString("main.cpp tmpDir: " + tmpDir);
199
200 // not most elegant as fixed name but in reality not big deal
201 QLockFile lockFile(tmpDir + "/.RcloneBrowser_4q6RgLs2RpbJA.lock");
202
203 if (!lockFile.tryLock(100)) {
204 // if already running display warning and quit
205 QMessageBox msgBox;
206 msgBox.setIcon(QMessageBox::Warning);
207 msgBox.setText("Rclone Browser is already running."
208 "\r\n\nOnly one instance is allowed.");
209 msgBox.exec();
210 return static_cast<int>(
211 0x80004004); // exit immediately if another instance is running
212 }
213
214 MainWindow w;
215 w.show();
216
217 return app.exec();
218 }