| 1 | // Copyright (C) 2014 Sacha Refshauge |
| 2 | |
| 3 | // This program is free software: you can redistribute it and/or modify |
| 4 | // it under the terms of the GNU General Public License as published by |
| 5 | // the Free Software Foundation, version 3.0. |
| 6 | |
| 7 | // This program is distributed in the hope that it will be useful, |
| 8 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | // GNU General Public License 3.0 for more details. |
| 11 | |
| 12 | // A copy of the GPL 3.0 should have been included with the program. |
| 13 | // If not, see http://www.gnu.org/licenses/ |
| 14 | |
| 15 | // Official GIT repository and contact information can be found at |
| 16 | // http://github.com/xsacha/Sachesi |
| 17 | |
| 18 | #include "ports.h" |
| 19 | // For portability between platforms and Qt versions. |
| 20 | // Clears up the code in the more important files. |
| 21 | |
| 22 | #ifndef BLACKBERRY |
| 23 | #include <QApplication> |
| 24 | #include <QMessageBox> |
| 25 | #endif |
| 26 | #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) |
| 27 | #include <QStandardPaths> |
| 28 | #endif |
| 29 | #include <QDir> |
| 30 | |
| 31 | QPair<QString,QString> getFamilyFromDevice(int device, bool specialQ30) { |
| 32 | QPair<QString,QString> ret = {"", ""}; |
| 33 | switch(device) { |
| 34 | case Z30Family: |
| 35 | ret = {"qc8960.factory_sfi", "qc8960.wtr5"}; |
| 36 | break; |
| 37 | case OMAPFamily: |
| 38 | ret = {"winchester.factory_sfi", "m5730"}; |
| 39 | break; |
| 40 | case Z10Family: |
| 41 | ret = {"qc8960.factory_sfi", "qc8960"}; |
| 42 | break; |
| 43 | case Z3Family: |
| 44 | ret = {"qc8960.factory_sfi_hybrid_qc8x30", "qc8930.wtr5"}; |
| 45 | break; |
| 46 | case Q30Family: |
| 47 | if (specialQ30) |
| 48 | ret = {"qc8974.factory_sfi", "qc8974.wtr2"}; |
| 49 | else |
| 50 | ret = {"qc8960.factory_sfi_hybrid_qc8974", "qc8974.wtr2"}; |
| 51 | break; |
| 52 | case Q10Family: |
| 53 | ret = {"qc8960.factory_sfi", "qc8960.wtr"}; |
| 54 | break; |
| 55 | } |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | QString capPath(bool temp) { |
| 60 | #ifndef BLACKBERRY |
| 61 | QSettings ini(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName()); |
| 62 | QString capPath = QFileInfo(ini.fileName()).absolutePath(); |
| 63 | if (temp) { |
| 64 | // On Windows this folder may not exist even if the .ini is meant to be there! So make it when writing temp file. |
| 65 | QDir(capPath).mkdir("."); |
| 66 | return capPath + "/.cap.exe"; |
| 67 | } |
| 68 | return capPath + "/cap.exe"; |
| 69 | #else |
| 70 | return "cap.exe"; |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | bool isVersionNewer(QString first, QString second, bool orSame) { |
| 75 | QStringList firstVer = first.split('.'); |
| 76 | QStringList secondVer = second.split('.'); |
| 77 | if (firstVer.count() < 4) |
| 78 | return false; |
| 79 | for (int i = 0; i < 4; i++) { |
| 80 | int newBuild = firstVer.at(i).toInt(); |
| 81 | int oldBuild = secondVer.at(i).toInt(); |
| 82 | if (newBuild > oldBuild) |
| 83 | return true; |
| 84 | if (newBuild < oldBuild) |
| 85 | return false; |
| 86 | } |
| 87 | return orSame; |
| 88 | } |
| 89 | |
| 90 | #ifndef BLACKBERRY |
| 91 | QFileDialog* selectFiles(QString title, QString dir, QString nameString, QString nameExt) { |
| 92 | QFileDialog* finder = new QFileDialog(); |
| 93 | finder->setWindowTitle(title); |
| 94 | finder->setDirectory(dir); |
| 95 | finder->setNameFilter(nameString + "(" + nameExt + ")"); |
| 96 | QListView *l = finder->findChild<QListView*>("listView"); |
| 97 | if (l) |
| 98 | l->setSelectionMode(QAbstractItemView::ExtendedSelection); |
| 99 | QTreeView *t = finder->findChild<QTreeView*>(); |
| 100 | if (t) |
| 101 | t->setSelectionMode(QAbstractItemView::ExtendedSelection); |
| 102 | return finder; |
| 103 | } |
| 104 | #endif |
| 105 | |
| 106 | QString getSaveDir() { |
| 107 | #ifdef BLACKBERRY |
| 108 | #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) |
| 109 | return "/accounts/1000/shared/misc/Sachesi/"; |
| 110 | #else |
| 111 | return QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation).first() + "/Sachesi/"; |
| 112 | #endif |
| 113 | #else |
| 114 | QString writable = QDir::currentPath() + "/"; |
| 115 | |
| 116 | if (QFileInfo(writable).isWritable()) |
| 117 | return writable; |
| 118 | |
| 119 | return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); |
| 120 | #endif |
| 121 | } |
| 122 | |
| 123 | bool checkCurPath() |
| 124 | { |
| 125 | QDir dir; |
| 126 | #ifdef BLACKBERRY |
| 127 | #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) |
| 128 | QString path = "/accounts/1000/shared/misc/Sachesi/"; |
| 129 | #else |
| 130 | QString path = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation).first() + "/Sachesi/"; |
| 131 | #endif |
| 132 | dir.mkpath(path); |
| 133 | QDir::setCurrent(path); |
| 134 | #elif defined(ANDROID) |
| 135 | // Iterate through removable to local |
| 136 | QString sdcard = "/sdcard1/Sachesi/"; |
| 137 | dir.mkpath(sdcard); |
| 138 | if (!dir.exists(sdcard)) { |
| 139 | sdcard = "/sdcard/Sachesi"; |
| 140 | dir.mkpath(sdcard); |
| 141 | } |
| 142 | |
| 143 | QDir::setCurrent(sdcard); |
| 144 | #else |
| 145 | QDir::setCurrent(QApplication::applicationDirPath()); |
| 146 | #if defined(__APPLE__) |
| 147 | dir = QDir(QDir::currentPath()); |
| 148 | // Use .app path instead of binary path. Should really use a different method. |
| 149 | if (dir.absolutePath().endsWith("Contents/MacOS")) { |
| 150 | while (!dir.absolutePath().endsWith(".app")) |
| 151 | dir.cdUp(); |
| 152 | dir.cdUp(); |
| 153 | } |
| 154 | QDir::setCurrent(dir.absolutePath()); |
| 155 | #endif |
| 156 | #endif |
| 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | void openFile(QString name) { |
| 162 | QDesktopServices::openUrl(QUrl::fromLocalFile(name)); |
| 163 | } |
| 164 | |
| 165 | void writeDisplayFile(QString type, QString writeText) { |
| 166 | QDir(getSaveDir()).mkpath("."); |
| 167 | QFile displayFile(getSaveDir() + "/" + type + ".txt"); |
| 168 | |
| 169 | if (!displayFile.open(QIODevice::WriteOnly | QIODevice::Text)) |
| 170 | return; |
| 171 | displayFile.write(writeText.toUtf8()); |
| 172 | openFile(displayFile.fileName()); |
| 173 | displayFile.close(); |
| 174 | } |
| 175 |