| 1 | #include "list_of_job_options.h" |
| 2 | #include <QDataStream> |
| 3 | #include <qdir.h> |
| 4 | #include <qlogging.h> |
| 5 | #include <qstandardpaths.h> |
| 6 | #include <utils.h> |
| 7 | |
| 8 | static QDataStream &operator>>(QDataStream &dataStream, JobOptions &jo); |
| 9 | static QDataStream &operator<<(QDataStream &dataStream, JobOptions &jo); |
| 10 | static QDataStream &operator>>(QDataStream &in, JobOptions::Operation &e); |
| 11 | static QDataStream &operator>>(QDataStream &in, JobOptions::SyncTiming &e); |
| 12 | static QDataStream &operator>>(QDataStream &in, JobOptions::CompareOption &e); |
| 13 | static QDataStream &operator>>(QDataStream &in, JobOptions::JobType &e); |
| 14 | |
| 15 | ListOfJobOptions *ListOfJobOptions::SavedJobOptions = nullptr; |
| 16 | const QString ListOfJobOptions::persistenceFileName = "tasks.bin"; |
| 17 | |
| 18 | ListOfJobOptions::ListOfJobOptions() {} |
| 19 | |
| 20 | ListOfJobOptions *ListOfJobOptions::getInstance() { |
| 21 | if (SavedJobOptions == nullptr) { |
| 22 | SavedJobOptions = new ListOfJobOptions(); |
| 23 | RestoreFromUserData(*SavedJobOptions); |
| 24 | } |
| 25 | return SavedJobOptions; |
| 26 | } |
| 27 | |
| 28 | bool ListOfJobOptions::Persist(JobOptions *jo) { |
| 29 | bool isNew = !this->tasks.contains(jo); |
| 30 | if (isNew) |
| 31 | this->tasks.append(jo); |
| 32 | else { |
| 33 | // int ix = tasks.indexOf(jo); |
| 34 | // JobOptions *old = tasks[ix]; |
| 35 | // qDebug() << QString("old [%1] New [%2]") |
| 36 | // .arg(old->description) |
| 37 | // .arg(jo->description); |
| 38 | } |
| 39 | PersistToUserData(); |
| 40 | return isNew; |
| 41 | } |
| 42 | |
| 43 | bool ListOfJobOptions::Forget(JobOptions *jo) { |
| 44 | bool isKnown = this->tasks.contains(jo); |
| 45 | if (!isKnown) |
| 46 | return false; |
| 47 | int ix = tasks.indexOf(jo); |
| 48 | tasks.removeAt(ix); |
| 49 | // qDebug() << QString("removed [%1]").arg(jo->description); |
| 50 | PersistToUserData(); |
| 51 | return isKnown; |
| 52 | } |
| 53 | |
| 54 | QFile *ListOfJobOptions::GetPersistenceFile(QIODevice::OpenModeFlag mode) { |
| 55 | |
| 56 | QDir outputDir; |
| 57 | |
| 58 | if (IsPortableMode()) { |
| 59 | // in portable mode tasks' file will be saved in the same folder as |
| 60 | // excecutable |
| 61 | #ifdef Q_OS_MACOS |
| 62 | // on macOS excecutable file is located in |
| 63 | // ./rclone-browser.app/Contents/MasOS/ |
| 64 | // to get actual bundle folder we have |
| 65 | // to traverse three levels up |
| 66 | outputDir = QDir(qApp->applicationDirPath() + "/../../.."); |
| 67 | #else |
| 68 | #ifdef Q_OS_WIN |
| 69 | // not macOS |
| 70 | outputDir = QDir(qApp->applicationDirPath()); |
| 71 | #else |
| 72 | QString xdg_config_home = qgetenv("XDG_CONFIG_HOME"); |
| 73 | outputDir = QDir(xdg_config_home + "/rclone-browser"); |
| 74 | #endif |
| 75 | #endif |
| 76 | |
| 77 | } else { |
| 78 | |
| 79 | // get data location folder from Qt - OS dependend |
| 80 | outputDir = |
| 81 | QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation)); |
| 82 | } |
| 83 | |
| 84 | if (!outputDir.exists()) { |
| 85 | outputDir.mkpath("."); |
| 86 | } |
| 87 | QString filePath = outputDir.absoluteFilePath(persistenceFileName); |
| 88 | QFile *file = new QFile(filePath); |
| 89 | |
| 90 | if (!file->open(mode)) { |
| 91 | // qDebug() << QString("Could not open ") << file->fileName(); |
| 92 | delete file; |
| 93 | file = nullptr; |
| 94 | } |
| 95 | return file; |
| 96 | } |
| 97 | |
| 98 | bool ListOfJobOptions::RestoreFromUserData(ListOfJobOptions &dataIn) { |
| 99 | QFile *file = GetPersistenceFile(QIODevice::ReadOnly); |
| 100 | if (file == nullptr) |
| 101 | return false; |
| 102 | QDataStream instream(file); |
| 103 | instream.setVersion(QDataStream::Qt_5_2); |
| 104 | |
| 105 | while (!instream.atEnd()) { |
| 106 | try { |
| 107 | JobOptions *jo = new JobOptions(); |
| 108 | instream >> *jo; |
| 109 | dataIn.tasks.append(jo); |
| 110 | } catch (SerializationException &ex) { |
| 111 | // qDebug() << QString("failed to restore tasks: ") << ex.Message; |
| 112 | file->close(); |
| 113 | delete file; |
| 114 | return false; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | file->close(); |
| 119 | delete file; |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | bool ListOfJobOptions::PersistToUserData() { |
| 125 | QFile *file = GetPersistenceFile( |
| 126 | QIODevice::WriteOnly); // note this mode implies Truncate also |
| 127 | if (file == nullptr) |
| 128 | return false; |
| 129 | QDataStream outstream(file); |
| 130 | outstream.setVersion(QDataStream::Qt_5_2); |
| 131 | |
| 132 | for (JobOptions *it : tasks) { |
| 133 | outstream << *it; |
| 134 | } |
| 135 | |
| 136 | file->flush(); |
| 137 | file->close(); |
| 138 | |
| 139 | emit tasksListUpdated(); |
| 140 | |
| 141 | delete file; |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | QDataStream &operator<<(QDataStream &stream, JobOptions &jo) { |
| 147 | stream << jo.myName() << JobOptions::classVersion << jo.description |
| 148 | << jo.jobType << jo.operation << /* jo.dryRun <<*/ jo.sync |
| 149 | << jo.syncTiming << jo.skipNewer << jo.skipExisting << jo.compare |
| 150 | << jo.compareOption << jo.verbose << jo.sameFilesystem |
| 151 | << jo.dontUpdateModified << jo.transfers << jo.checkers << jo.bandwidth |
| 152 | << jo.minSize << jo.minAge << jo.maxAge << jo.maxDepth |
| 153 | << jo.connectTimeout << jo.idleTimeout << jo.retries |
| 154 | << jo.lowLevelRetries << jo.deleteExcluded << jo.excluded << jo.extra |
| 155 | << jo.DriveSharedWithMe << jo.source << jo.dest << jo.isFolder |
| 156 | << jo.uniqueId; |
| 157 | |
| 158 | return stream; |
| 159 | } |
| 160 | |
| 161 | QDataStream &operator>>(QDataStream &stream, JobOptions &jo) { |
| 162 | QString actualName; |
| 163 | qint32 actualVersion; |
| 164 | |
| 165 | stream >> actualName; |
| 166 | if (QString::compare(actualName, jo.myName()) != 0) |
| 167 | throw SerializationException("incorrect class"); |
| 168 | |
| 169 | stream >> actualVersion; |
| 170 | if (actualVersion > JobOptions::classVersion) |
| 171 | throw SerializationException("stored version is newer"); |
| 172 | |
| 173 | stream >> jo.description >> jo.jobType >> jo.operation >> |
| 174 | /* jo.dryRun >> */ jo.sync >> jo.syncTiming >> jo.skipNewer >> |
| 175 | jo.skipExisting >> jo.compare >> jo.compareOption >> jo.verbose >> |
| 176 | jo.sameFilesystem >> jo.dontUpdateModified >> jo.transfers >> |
| 177 | jo.checkers >> jo.bandwidth >> jo.minSize >> jo.minAge >> jo.maxAge >> |
| 178 | jo.maxDepth >> jo.connectTimeout >> jo.idleTimeout >> jo.retries >> |
| 179 | jo.lowLevelRetries >> jo.deleteExcluded >> jo.excluded >> jo.extra >> |
| 180 | jo.DriveSharedWithMe >> jo.source >> jo.dest; |
| 181 | |
| 182 | // as fields are added in later revisions, check actualVersion here and |
| 183 | // conditionally extract any new fields iff they are expected based on the |
| 184 | // stream value |
| 185 | if (actualVersion >= 2) { |
| 186 | stream >> jo.isFolder; |
| 187 | if (actualVersion >= 3) { |
| 188 | stream >> jo.uniqueId; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return stream; |
| 193 | } |
| 194 | |
| 195 | QDataStream &operator>>(QDataStream &in, JobOptions::Operation &e) { |
| 196 | in >> (quint32 &)e; |
| 197 | return in; |
| 198 | } |
| 199 | |
| 200 | QDataStream &operator>>(QDataStream &in, JobOptions::SyncTiming &e) { |
| 201 | in >> (quint32 &)e; |
| 202 | return in; |
| 203 | } |
| 204 | |
| 205 | QDataStream &operator>>(QDataStream &in, JobOptions::CompareOption &e) { |
| 206 | in >> (quint32 &)e; |
| 207 | return in; |
| 208 | } |
| 209 | |
| 210 | QDataStream &operator>>(QDataStream &in, JobOptions::JobType &e) { |
| 211 | in >> (quint32 &)e; |
| 212 | return in; |
| 213 | } |