| 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 | // This file is just Qt hooks that don't need to be seen. It should really do this for you anyway. |
| 19 | |
| 20 | #include "installer.h" |
| 21 | #include "ports.h" |
| 22 | |
| 23 | #define WRITE_QML(type, name, caps) \ |
| 24 | void InstallNet::caps(const type &var) { \ |
| 25 | if (var != _ ## name) { \ |
| 26 | _ ## name = var; \ |
| 27 | emit name ## Changed(); \ |
| 28 | } \ |
| 29 | } |
| 30 | |
| 31 | // _() |
| 32 | |
| 33 | WRITE_QML(bool, loginBlock, setLoginBlock) |
| 34 | WRITE_QML(int, possibleDevices, setPossibleDevices) |
| 35 | void InstallNet::setState(const int &state) { |
| 36 | if (!state) { |
| 37 | qSafeFree(manager); |
| 38 | qSafeFree(cookieJar); |
| 39 | } |
| 40 | _state = state; |
| 41 | emit stateChanged(); |
| 42 | } |
| 43 | |
| 44 | int InstallNet::dgMaxPos() const { |
| 45 | return _downgradeInfo.count(); |
| 46 | } |
| 47 | WRITE_QML(QString, curInstallName, setCurInstallName) |
| 48 | WRITE_QML(bool, installing, setInstalling) |
| 49 | WRITE_QML(bool, firmwareUpdate, setFirmwareUpdate) |
| 50 | WRITE_QML(bool, allowDowngrades, setAllowDowngrades) |
| 51 | |
| 52 | void InstallNet::setCurDGProgress(const int &curDGProgress) { |
| 53 | _curDGProgress = curDGProgress; |
| 54 | if (curDGProgress == -1) { |
| 55 | _dgProgress = -1; |
| 56 | } else { |
| 57 | qint64 curBytes = ((qint64)_curDGProgress * _dlTotal); |
| 58 | _dgProgress = (curBytes + _dlDoneBytes) / _dlOverallTotal; |
| 59 | } |
| 60 | emit curDGProgressChanged(); |
| 61 | } |
| 62 | |
| 63 | QQmlListProperty<Apps> InstallNet::appList() |
| 64 | { |
| 65 | return QQmlListProperty<Apps>(this, _appList); |
| 66 | } |
| 67 | |
| 68 | QQmlListProperty<Apps> InstallNet::backAppList() |
| 69 | { |
| 70 | return QQmlListProperty<Apps>(this, _back.apps); |
| 71 | } |
| 72 | |
| 73 | void InstallNet::setIp(const QString &ip) |
| 74 | { |
| 75 | _ip = ip; |
| 76 | emit ipChanged(); |
| 77 | QSettings settings("Qtness","Sachesi"); |
| 78 | if (settings.value("ip","169.254.0.1").toString() != ip) |
| 79 | settings.setValue("ip",ip); |
| 80 | } |
| 81 | |
| 82 | void InstallNet::setWrongPass(const bool &wrong) |
| 83 | { |
| 84 | _wrongPass = wrong; |
| 85 | if (wrong) { |
| 86 | setState(0); |
| 87 | setCompleted(false); |
| 88 | } |
| 89 | emit wrongPassChanged(); |
| 90 | } |
| 91 | |
| 92 | void InstallNet::setCompleted(const bool &exists) |
| 93 | { |
| 94 | _completed = exists; |
| 95 | if (_loginBlock && _completed) |
| 96 | setLoginBlock(false); |
| 97 | emit completedChanged(); |
| 98 | } |
| 99 | |
| 100 | void InstallNet::setNewLine(const QString &newLine) |
| 101 | { |
| 102 | // Prefix with date |
| 103 | QString newLog = QTime().currentTime().toString("[hh:mm:ss] ") + newLine; |
| 104 | |
| 105 | _newLine = ""; // Reset qproperty and notify |
| 106 | emit newLineChanged(); |
| 107 | _newLine = newLog + "<br>"; |
| 108 | emit newLineChanged(); |
| 109 | newLog.append("\n"); |
| 110 | logFile->write(newLog.toLatin1()); |
| 111 | logFile->flush(); // Update file so it can be viewed in real-time |
| 112 | emit hasLogChanged(); |
| 113 | } |
| 114 | |
| 115 | bool InstallNet::hasLog() { |
| 116 | return logFile->size() > 0; |
| 117 | } |
| 118 | |
| 119 | void InstallNet::openLog() { |
| 120 | if (logFile->fileName() != "") |
| 121 | openFile(logFile->fileName()); |
| 122 | } |
| 123 | |
| 124 | QString InstallNet::backStatus() const { |
| 125 | QString ret = _back.curMode(); |
| 126 | ret[0] = ret[0].toUpper(); |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | int InstallNet::backProgress() const { |
| 131 | return qMin((int)100, (int)((_back.size() + _back.curSize()) / _back.maxSize())); |
| 132 | } |
| 133 | |
| 134 | int InstallNet::backCurProgress() const { |
| 135 | return _back.progress(); |
| 136 | } |
| 137 | |
| 138 | QStringList InstallNet::backNames() const { |
| 139 | QStringList names; |
| 140 | foreach(BackupCategory* cat, _back.categories) |
| 141 | names.append(cat->name); |
| 142 | return names; |
| 143 | } |
| 144 | |
| 145 | QList<double> InstallNet::backSizes() const { |
| 146 | QList<double> sizes; |
| 147 | foreach(BackupCategory* cat, _back.categories) |
| 148 | sizes.append(cat->bytesize.toLongLong()); |
| 149 | return sizes; |
| 150 | } |
| 151 | |
| 152 | int InstallNet::backMethods() const { |
| 153 | return _back.numMethods(); |
| 154 | } |
| 155 | |
| 156 | void InstallNet::setBacking(const bool &backing) { |
| 157 | _backing = backing; |
| 158 | emit backingChanged(); |
| 159 | if (!backing) |
| 160 | { |
| 161 | _back.setMode(0); |
| 162 | for (int i = 0; i < _back.numMethods(); i++) |
| 163 | _back.setCurMaxSize(i, 1); |
| 164 | _back.setSize(0); |
| 165 | _back.setMaxSize(1); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void InstallNet::setRestoring(const bool &restoring) { |
| 170 | _restoring = restoring; |
| 171 | emit restoringChanged(); |
| 172 | if (!restoring) { |
| 173 | _back.setMode(0); |
| 174 | for (int i = 0; i < _back.numMethods(); i++) |
| 175 | _back.setCurMaxSize(i, 1); |
| 176 | _back.setSize(0); |
| 177 | _back.setMaxSize(1); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Note: not overly secure if user is specifically targeted. |
| 182 | void InstallNet::setPassword(const QString &password) { |
| 183 | _password = password; |
| 184 | emit newPassword(password); |
| 185 | QByteArray tmp = password.toLatin1(); |
| 186 | for (int i = 0; i < password.length(); i++) { |
| 187 | tmp[i] = tmp[i] ^ ((0x40 + 5 * i - password.length()) % 127); |
| 188 | } |
| 189 | QByteArray tmp2(1, tmp.length()); |
| 190 | QByteArray hashedPass = tmp.toBase64(); |
| 191 | hashedPass.prepend(tmp2.toBase64()); |
| 192 | QSettings settings("Qtness","Sachesi"); |
| 193 | settings.setValue("pass", hashedPass); |
| 194 | emit passwordChanged(); |
| 195 | } |