| 1 | #include "utils.h" |
| 2 | |
| 3 | static QString gRclone; |
| 4 | static QString gRcloneConf; |
| 5 | static QString gRclonePassword; |
| 6 | |
| 7 | // Software versions comparison |
| 8 | // source: https://helloacm.com/how-to-compare-version-numbers-in-c/ |
| 9 | std::vector<std::string> split(const std::string &s, char d) { |
| 10 | std::vector<std::string> r; |
| 11 | int j = 0; |
| 12 | for (unsigned int i = 0; i < s.length(); i++) { |
| 13 | if (s[i] == d) { |
| 14 | r.push_back(s.substr(j, i - j)); |
| 15 | j = i + 1; |
| 16 | } |
| 17 | } |
| 18 | r.push_back(s.substr(j)); |
| 19 | return r; |
| 20 | } |
| 21 | |
| 22 | unsigned int compareVersion(std::string version1, std::string version2) { |
| 23 | auto v1 = split(version1, '.'); |
| 24 | auto v2 = split(version2, '.'); |
| 25 | unsigned int max = v1.size() > v2.size() ? v1.size() : v2.size(); |
| 26 | // pad the shorter version string |
| 27 | if (v1.size() != max) { |
| 28 | for (unsigned int i = max - v1.size(); i--;) { |
| 29 | v1.push_back("0"); |
| 30 | } |
| 31 | } else { |
| 32 | for (unsigned int i = max - v2.size(); i--;) { |
| 33 | v2.push_back("0"); |
| 34 | } |
| 35 | } |
| 36 | for (unsigned int i = 0; i < max; i++) { |
| 37 | unsigned int n1 = stoi(v1[i]); |
| 38 | unsigned int n2 = stoi(v2[i]); |
| 39 | if (n1 > n2) { |
| 40 | // version1 is higher than version2 |
| 41 | return 1; |
| 42 | } else if (n1 < n2) { |
| 43 | // version2 is higher than version1 |
| 44 | return 2; |
| 45 | } |
| 46 | } |
| 47 | // the same versions |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static QString GetIniFilename() { |
| 52 | #ifdef Q_OS_MACOS |
| 53 | QFileInfo applicationPath = qApp->applicationFilePath(); |
| 54 | // qDebug() << QString(applicationPath.absolutePath()); |
| 55 | // on macOS excecutable file is located in |
| 56 | // ./rclone-browser.app/Contents/MasOS/ to get actual bundle folder we have to |
| 57 | // traverse three levels up |
| 58 | QFileInfo MacOSPath = applicationPath.dir().path(); |
| 59 | QFileInfo ContentsPath = MacOSPath.dir().path(); |
| 60 | QFileInfo appBundlePath = ContentsPath.dir().path(); |
| 61 | // qDebug() << QString("utils.cpp appBundle.absolutePath: " + |
| 62 | // appBundlePath.absolutePath()); |
| 63 | // qDebug() << QString( |
| 64 | // "utils.cpp ini file:" + |
| 65 | // appBundlePath.dir().filePath(appBundlePath.baseName() + ".ini")); |
| 66 | return appBundlePath.dir().filePath(appBundlePath.baseName() + ".ini"); |
| 67 | #else |
| 68 | #ifdef Q_OS_WIN |
| 69 | QFileInfo applicationPath = qApp->applicationFilePath(); |
| 70 | return applicationPath.dir().filePath(applicationPath.baseName() + ".ini"); |
| 71 | #else |
| 72 | QString xdg_config_home = qgetenv("XDG_CONFIG_HOME"); |
| 73 | return xdg_config_home + "/rclone-browser/rclone-browser.ini"; |
| 74 | #endif |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | bool IsPortableMode() { |
| 79 | QString ini = GetIniFilename(); |
| 80 | QString xdg_config_home = qgetenv("XDG_CONFIG_HOME"); |
| 81 | // qDebug() << QString("utils.cpp $XDG_CONFIG_HOME: " + xdg_config_home); |
| 82 | QString appimage = qgetenv("APPIMAGE"); |
| 83 | // qDebug() << QString("utils.cpp $APPIMAGE: " + appimage); |
| 84 | |
| 85 | // cat ".config" from $XDG_CONFIG_HOME |
| 86 | // it should be the same as appimage if run from AppImage |
| 87 | xdg_config_home = xdg_config_home.left(xdg_config_home.length() - 7); |
| 88 | // qDebug() << QString("utils.cpp $XDG_CONFIG_HOME-7: " + xdg_config_home); |
| 89 | |
| 90 | if (!xdg_config_home.isEmpty() && !appimage.isEmpty() && |
| 91 | xdg_config_home == appimage) { |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | if (QFileInfo(ini).exists()) { |
| 97 | |
| 98 | return true; |
| 99 | } else { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | // return QFileInfo(ini).exists(); |
| 104 | } |
| 105 | |
| 106 | std::unique_ptr<QSettings> GetSettings() { |
| 107 | if (IsPortableMode()) { |
| 108 | return std::unique_ptr<QSettings>( |
| 109 | new QSettings(GetIniFilename(), QSettings::IniFormat)); |
| 110 | } |
| 111 | return std::unique_ptr<QSettings>(new QSettings); |
| 112 | } |
| 113 | |
| 114 | void ReadSettings(QSettings *settings, QObject *widget) { |
| 115 | QString name = widget->objectName(); |
| 116 | if (!name.isEmpty() && settings->contains(name)) { |
| 117 | if (QRadioButton *obj = qobject_cast<QRadioButton *>(widget)) { |
| 118 | obj->setChecked(settings->value(name).toBool()); |
| 119 | return; |
| 120 | } |
| 121 | if (QCheckBox *obj = qobject_cast<QCheckBox *>(widget)) { |
| 122 | obj->setChecked(settings->value(name).toBool()); |
| 123 | return; |
| 124 | } |
| 125 | if (QComboBox *obj = qobject_cast<QComboBox *>(widget)) { |
| 126 | obj->setCurrentIndex(settings->value(name).toInt()); |
| 127 | return; |
| 128 | } |
| 129 | if (QSpinBox *obj = qobject_cast<QSpinBox *>(widget)) { |
| 130 | obj->setValue(settings->value(name).toInt()); |
| 131 | return; |
| 132 | } |
| 133 | if (QLineEdit *obj = qobject_cast<QLineEdit *>(widget)) { |
| 134 | obj->setText(settings->value(name).toString()); |
| 135 | return; |
| 136 | } |
| 137 | if (QPlainTextEdit *obj = qobject_cast<QPlainTextEdit *>(widget)) { |
| 138 | int count = settings->beginReadArray(name); |
| 139 | QStringList lines; |
| 140 | lines.reserve(count); |
| 141 | for (int i = 0; i < count; i++) { |
| 142 | settings->setArrayIndex(i); |
| 143 | lines.append(settings->value("value").toString()); |
| 144 | } |
| 145 | settings->endArray(); |
| 146 | |
| 147 | obj->setPlainText(lines.join('\n')); |
| 148 | return; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | for (auto child : widget->children()) { |
| 153 | ReadSettings(settings, child); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void WriteSettings(QSettings *settings, QObject *widget) { |
| 158 | QString name = widget->objectName(); |
| 159 | if (QCheckBox *obj = qobject_cast<QCheckBox *>(widget)) { |
| 160 | settings->setValue(name, obj->isChecked()); |
| 161 | return; |
| 162 | } |
| 163 | if (QComboBox *obj = qobject_cast<QComboBox *>(widget)) { |
| 164 | settings->setValue(name, obj->currentIndex()); |
| 165 | return; |
| 166 | } |
| 167 | if (QSpinBox *obj = qobject_cast<QSpinBox *>(widget)) { |
| 168 | settings->setValue(name, obj->value()); |
| 169 | return; |
| 170 | } |
| 171 | if (QLineEdit *obj = qobject_cast<QLineEdit *>(widget)) { |
| 172 | if (obj->text().isEmpty()) { |
| 173 | settings->remove(name); |
| 174 | } else { |
| 175 | settings->setValue(name, obj->text()); |
| 176 | } |
| 177 | return; |
| 178 | } |
| 179 | if (QPlainTextEdit *obj = qobject_cast<QPlainTextEdit *>(widget)) { |
| 180 | QString text = obj->toPlainText().trimmed(); |
| 181 | if (!text.isEmpty()) { |
| 182 | QStringList lines = text.split('\n'); |
| 183 | settings->beginWriteArray(name, lines.size()); |
| 184 | for (int i = 0; i < lines.count(); i++) { |
| 185 | settings->setArrayIndex(i); |
| 186 | settings->setValue("value", lines[i]); |
| 187 | } |
| 188 | settings->endArray(); |
| 189 | } |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | for (auto child : widget->children()) { |
| 194 | WriteSettings(settings, child); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | QStringList GetRcloneConf() { |
| 199 | if (gRcloneConf.isEmpty()) { |
| 200 | return QStringList(); |
| 201 | } |
| 202 | |
| 203 | QString conf = gRcloneConf; |
| 204 | if (IsPortableMode() && QFileInfo(conf).isRelative()) { |
| 205 | #ifdef Q_OS_MACOS |
| 206 | // on macOS excecutable file is located in |
| 207 | // ./rclone-browser.app/Contents/MasOS/rclone-browser to get actual bundle |
| 208 | // folder we have to traverse three levels up |
| 209 | conf = QDir(qApp->applicationDirPath() + "/../../..").filePath(conf); |
| 210 | #else |
| 211 | #ifdef Q_OS_WIN |
| 212 | conf = QDir(qApp->applicationDirPath()).filePath(conf); |
| 213 | #else |
| 214 | QString xdg_config_home = qgetenv("XDG_CONFIG_HOME"); |
| 215 | conf = QDir(xdg_config_home + "/..").filePath(conf); |
| 216 | #endif |
| 217 | #endif |
| 218 | // qDebug() << QString("utils.cpp conf: " + conf); |
| 219 | } |
| 220 | return QStringList() << "--config" << conf; |
| 221 | } |
| 222 | |
| 223 | void SetRcloneConf(const QString &rcloneConf) { gRcloneConf = rcloneConf; } |
| 224 | |
| 225 | QString GetRclone() { |
| 226 | QString rclone = gRclone; |
| 227 | if (IsPortableMode() && QFileInfo(rclone).isRelative()) { |
| 228 | #ifdef Q_OS_MACOS |
| 229 | // on macOS excecutable file is located in |
| 230 | // ./rclone-browser.app/Contents/MasOS/rclone-browser to get actual bundle |
| 231 | // folder we have to traverse three levels up |
| 232 | rclone = QDir(qApp->applicationDirPath() + "/../../..").filePath(rclone); |
| 233 | #else |
| 234 | #ifdef Q_OS_WIN |
| 235 | rclone = QDir(qApp->applicationDirPath()).filePath(rclone); |
| 236 | #else |
| 237 | QString xdg_config_home = qgetenv("XDG_CONFIG_HOME"); |
| 238 | rclone = QDir(xdg_config_home + "/..").filePath(rclone); |
| 239 | #endif |
| 240 | #endif |
| 241 | // qDebug() << QString("utils.cpp rclone portable: " + rclone); |
| 242 | } |
| 243 | |
| 244 | return rclone; |
| 245 | } |
| 246 | |
| 247 | void SetRclone(const QString &rclone) { gRclone = rclone.trimmed(); } |
| 248 | |
| 249 | void UseRclonePassword(QProcess *process) { |
| 250 | if (!gRclonePassword.isEmpty()) { |
| 251 | QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); |
| 252 | env.insert("RCLONE_CONFIG_PASS", gRclonePassword); |
| 253 | process->setProcessEnvironment(env); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void SetRclonePassword(const QString &rclonePassword) { |
| 258 | gRclonePassword = rclonePassword; |
| 259 | } |
| 260 | |
| 261 | QStringList GetDriveSharedWithMe() { |
| 262 | auto settings = GetSettings(); |
| 263 | bool driveShared = settings->value("Settings/driveShared", false).toBool(); |
| 264 | QStringList driveSharedOption; |
| 265 | if (driveShared) { |
| 266 | driveSharedOption << "--drive-shared-with-me"; |
| 267 | } |
| 268 | return driveSharedOption; |
| 269 | } |
| 270 | |
| 271 | QStringList GetDefaultRcloneOptionsList() { |
| 272 | auto settings = GetSettings(); |
| 273 | QString defaultRcloneOptions = |
| 274 | settings->value("Settings/defaultRcloneOptions").toString(); |
| 275 | QStringList defaultRcloneOptionsList; |
| 276 | if (!defaultRcloneOptions.isEmpty()) { |
| 277 | for (auto arg : defaultRcloneOptions.split(' ')) { |
| 278 | defaultRcloneOptionsList << arg; |
| 279 | } |
| 280 | } |
| 281 | return defaultRcloneOptionsList; |
| 282 | } |
| 283 | |
| 284 | QStringList GetShowHidden() { |
| 285 | auto settings = GetSettings(); |
| 286 | bool showHidden = settings->value("Settings/showHidden", true).toBool(); |
| 287 | QStringList showHiddenOption; |
| 288 | if (!showHidden) { |
| 289 | showHiddenOption << "--exclude" |
| 290 | << ".*/**" |
| 291 | << "--exclude" |
| 292 | << ".*"; |
| 293 | } |
| 294 | return showHiddenOption; |
| 295 | } |