| 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 "backupinfo.h" |
| 19 | |
| 20 | BackupInfo::BackupInfo() : |
| 21 | _progress(0), _size(0), _maxSize(1), |
| 22 | _mode(0), _curMode(0), _numMethods(0), |
| 23 | _rev(2), _appMode(0) |
| 24 | { |
| 25 | _numMethods = 3; |
| 26 | _appMode = 7; // 1 + 2 + 3 represents system + bin + data |
| 27 | categories.append(new BackupCategory("app", "Application Data")); |
| 28 | categories.append(new BackupCategory("media", "Media")); |
| 29 | categories.append(new BackupCategory("settings", "Device Settings and Local Contacts/Calendar Data")); |
| 30 | _curSize.append(QList<qint64>() << 0 << 0 << 0); |
| 31 | _curMaxSize.append(QList<qint64>() << 1 << 1 << 1); |
| 32 | } |
| 33 | |
| 34 | void BackupInfo::clearModes() { |
| 35 | _curMode = 0; |
| 36 | _numMethods = 0; |
| 37 | categories.clear(); |
| 38 | _curSize.clear(); |
| 39 | _curMaxSize.clear(); |
| 40 | foreach(Apps* app, apps) |
| 41 | app->deleteLater(); |
| 42 | apps.clear(); |
| 43 | emit curModeChanged(); |
| 44 | emit numMethodsChanged(); |
| 45 | } |
| 46 | |
| 47 | void BackupInfo::addMode(QXmlStreamAttributes cat) { |
| 48 | categories.append(new BackupCategory(cat)); |
| 49 | _numMethods++; |
| 50 | emit numMethodsChanged(); |
| 51 | _curSize.append(0); |
| 52 | _curMaxSize.append(1); |
| 53 | } |
| 54 | |
| 55 | void BackupInfo::addApp(QXmlStreamAttributes cat) { |
| 56 | Apps* newApp = new Apps(); |
| 57 | foreach(QXmlStreamAttribute attr, cat) |
| 58 | { |
| 59 | QString att = attr.name().toString(); |
| 60 | QString val = attr.value().toString(); |
| 61 | if (att == "pkgid") |
| 62 | newApp->setPackageId(val); |
| 63 | else if (att == "name") |
| 64 | newApp->setFriendlyName(val); |
| 65 | else if (att == "bytesize") |
| 66 | newApp->setSize(val.toInt()); |
| 67 | else if (att == "version") |
| 68 | newApp->setVersion(val); |
| 69 | else if (att == "type") |
| 70 | newApp->setType(val); |
| 71 | } |
| 72 | newApp->setIsMarked(true); |
| 73 | apps.append(newApp); |
| 74 | std::sort(apps.begin(), apps.end(), |
| 75 | [=](const Apps* i, const Apps* j) { |
| 76 | if (i->type() == "system" && j->type() != "system") |
| 77 | return true; |
| 78 | if (j->type() == "bin" && i->type() != "bin") |
| 79 | return true; |
| 80 | return false; |
| 81 | } |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | void BackupInfo::sortApps() { |
| 86 | |
| 87 | } |
| 88 | |
| 89 | QString BackupInfo::modeString() |
| 90 | { |
| 91 | QString ret = ""; |
| 92 | for (int i = 0; i < _numMethods; i++) |
| 93 | { |
| 94 | if (_mode & (1 << i)) { |
| 95 | if (ret != "") ret += "_"; |
| 96 | ret += categories.at(i)->id; |
| 97 | } |
| 98 | } |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | QString BackupInfo::stringFromMode(int mode) { |
| 103 | if (mode >= 0 && mode < _numMethods) |
| 104 | return categories.at(mode)->id; |
| 105 | if (mode == _numMethods) |
| 106 | return "complete"; |
| 107 | return ""; |
| 108 | } |
| 109 | |
| 110 | int BackupInfo::progress() const { |
| 111 | return _progress; |
| 112 | } |
| 113 | |
| 114 | void BackupInfo::setProgress(const int &progress) { |
| 115 | _progress = progress; |
| 116 | emit progressChanged(); |
| 117 | } |
| 118 | |
| 119 | int BackupInfo::mode() const { |
| 120 | return _mode; |
| 121 | } |
| 122 | |
| 123 | int BackupInfo::appMode() const { |
| 124 | return _appMode; |
| 125 | } |
| 126 | |
| 127 | void BackupInfo::setMode(const int &mode) { |
| 128 | _mode = mode; |
| 129 | emit modeChanged(); |
| 130 | } |
| 131 | |
| 132 | qint64 BackupInfo::setAppMode(QString mode) { |
| 133 | qint64 totalSize = 0; |
| 134 | int type = 0; |
| 135 | if (mode == "system") |
| 136 | type = 1; |
| 137 | else if (mode == "data") |
| 138 | type = 2; |
| 139 | else if (mode == "bin") |
| 140 | type = 4; |
| 141 | |
| 142 | if (type == 0) |
| 143 | return 0; |
| 144 | |
| 145 | // Toggle mode based on string |
| 146 | _appMode ^= type; |
| 147 | |
| 148 | // Are we enabling this mode or disabling it? |
| 149 | bool enable = _appMode & type; |
| 150 | for (int i = 0; i < apps.count(); i++) { |
| 151 | if (apps.at(i)->type() == mode && apps.at(i)->isMarked() != enable) { |
| 152 | apps.at(i)->setIsMarked(enable); |
| 153 | totalSize += (enable ? 1 : -1) * apps.at(i)->size(); |
| 154 | } |
| 155 | } |
| 156 | return totalSize; |
| 157 | } |
| 158 | |
| 159 | QString BackupInfo::curMode() const { |
| 160 | if (_curMode == _numMethods) |
| 161 | return "complete"; |
| 162 | return categories.at(_curMode)->id; |
| 163 | } |
| 164 | |
| 165 | void BackupInfo::setCurMode(int increment) { |
| 166 | if (increment) { |
| 167 | if (_numMethods > _curMode && _curSize[_curMode]) |
| 168 | _size += _curSize[_curMode]; |
| 169 | _curMode++; |
| 170 | } else { |
| 171 | _curMode = 0; |
| 172 | } |
| 173 | while (!(_mode & (1 << _curMode)) && _curMode < _numMethods) |
| 174 | _curMode++; |
| 175 | if (_curMode < _numMethods) |
| 176 | _curSize[_curMode] = 0; |
| 177 | emit curModeChanged(); |
| 178 | emit curSizeChanged(); |
| 179 | emit sizeChanged(); |
| 180 | } |
| 181 | |
| 182 | qint64 BackupInfo::size() const { |
| 183 | return _size; |
| 184 | } |
| 185 | |
| 186 | void BackupInfo::setSize(const qint64 &val) { |
| 187 | _size = val; |
| 188 | emit sizeChanged(); |
| 189 | } |
| 190 | |
| 191 | qint64 BackupInfo::maxSize() const { |
| 192 | return _maxSize; |
| 193 | } |
| 194 | |
| 195 | void BackupInfo::setMaxSize(const qint64 &val) { |
| 196 | _maxSize = val; |
| 197 | emit maxSizeChanged(); |
| 198 | } |
| 199 | |
| 200 | qint64 BackupInfo::curSize() const { |
| 201 | if (_curMode == _numMethods) |
| 202 | return 1; |
| 203 | if (_numMethods > _curMode) |
| 204 | return _curSize[_curMode]; |
| 205 | else |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | void BackupInfo::setCurSize(const qint64 &val) { |
| 210 | if (_numMethods > _curMode) |
| 211 | _curSize[_curMode] = val; |
| 212 | emit curSizeChanged(); |
| 213 | } |
| 214 | |
| 215 | qint64 BackupInfo::curMaxSize() const { |
| 216 | if (_curMode == _numMethods || _numMethods <= _curMode) |
| 217 | return 1; |
| 218 | |
| 219 | return _curMaxSize[_curMode]; |
| 220 | } |
| 221 | |
| 222 | void BackupInfo::setCurMaxSize(const qint64 &val) { |
| 223 | if (_numMethods > _curMode) |
| 224 | _curMaxSize[_curMode] = val; |
| 225 | emit curMaxSizeChanged(); |
| 226 | } |
| 227 | |
| 228 | void BackupInfo::setCurMaxSize(int index, const qint64 &val) { |
| 229 | _curMaxSize[index] = val; |
| 230 | emit curMaxSizeChanged(); |
| 231 | } |
| 232 | |
| 233 | int BackupInfo::numMethods() const { |
| 234 | return categories.size(); |
| 235 | } |
| 236 | |
| 237 | int BackupInfo::rev() const { |
| 238 | return _rev; |
| 239 | } |