Make TX volatge for simu more flexible (#7124)
[opentx.git] / companion / src / process_sync.h
blob67a1397914e0c3b68e95456e7ffe3a5c4d33cf25
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef PROCESS_SYNC_H
22 #define PROCESS_SYNC_H
24 #include <QObject>
25 #include <QDateTime>
26 #include <QDebug>
27 #include <QDir>
28 #include <QDirIterator>
29 #include <QReadWriteLock>
30 #include <QRegExp>
31 #include <QVector>
33 class SyncProcess : public QObject
35 Q_OBJECT
37 public:
38 enum SyncDirection {
39 SYNC_A2B_B2A = 0,
40 SYNC_B2A_A2B,
41 SYNC_A2B,
42 SYNC_B2A
44 Q_ENUM(SyncDirection)
46 enum SyncCompareType {
47 OVERWR_NEWER_IF_DIFF = 0,
48 OVERWR_NEWER_ALWAYS,
49 OVERWR_IF_DIFF,
50 OVERWR_ALWAYS
52 Q_ENUM(SyncCompareType)
54 enum SyncOptionFlag {
55 OPT_NONE = 0,
56 OPT_DRY_RUN = 0x01,
57 OPT_SKIP_EMPTY_DIR = 0x02,
58 OPT_RECURSIVE = 0x04,
59 OPT_SKIP_DIR_LINKS = 0x08
61 Q_DECLARE_FLAGS(SyncOptionFlags, SyncOptionFlag)
62 Q_FLAG(SyncOptionFlags)
64 // TODO: perhaps move this to AppData/saved settings once that refactoring is merged.
65 struct SyncOptions {
66 explicit SyncOptions() { reset(); }
68 QString folderA;
69 QString folderB;
70 QString excludeFilter; // CSV list of glob pattern(s)
71 QString includeFilter; // CSV list of glob pattern(s)
72 qint64 maxFileSize; // Bytes
73 int direction; // SyncDirection
74 int compareType; // SyncCompareType
75 int flags; // SyncOptionFlags
76 int dirFilterFlags; // QDir::Filters
77 int logLevel; // QtMsgType (see ProgressWidget::addMessage())
79 void reset()
81 excludeFilter = QStringLiteral(".*,System Volume*");
82 includeFilter.clear();
83 direction = SYNC_A2B_B2A;
84 compareType = OVERWR_NEWER_IF_DIFF;
85 maxFileSize = 2 * 1024 * 1024;
86 logLevel = QtWarningMsg;
87 flags = OPT_RECURSIVE;
88 dirFilterFlags = QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Readable;
91 friend inline QDebug operator<<(QDebug debug, const SyncOptions &o) {
92 return debug << "Incl:" << o.includeFilter << "Excl:" << o.excludeFilter << SyncDirection(o.direction) << SyncCompareType(o.compareType) << SyncOptionFlags(o.flags) << QDir::Filters(o.dirFilterFlags);
96 struct SyncStatus {
97 int index;
98 int count;
99 int created;
100 int updated;
101 int skipped;
102 int errored;
103 void clear() { memset(this, 0, sizeof(SyncStatus)); }
106 explicit SyncProcess(const SyncOptions & options);
107 ~SyncProcess();
109 public slots:
110 void run();
111 void stop();
113 signals:
114 void started();
115 void finished();
116 void fileCountChanged(int count);
117 void statusUpdate(const SyncProcess::SyncStatus & status);
118 void statusMessage(const QString & text, const int & type = QtInfoMsg);
119 void progressMessage(const QString & text, const int & type = QtInfoMsg, bool richText = false);
121 protected:
122 enum FileFilterResult { FILE_ALLOW, FILE_OVERSIZE, FILE_EXCLUDE, FILE_LINK_IGNORE };
124 bool isStopRequsted();
125 void finish();
126 FileFilterResult fileFilter(const QFileInfo & fileInfo);
127 QFileInfoList dirInfoList(const QString & directory);
128 int getFilesCount(const QString & directory);
129 void updateDir(const QString & source, const QString & destination);
130 void pushDirEntries(const QFileInfo & fi, QMutableListIterator<QFileInfo> &it);
131 bool updateEntry(const QString & entry, const QDir & source, const QDir & destination);
132 void pause();
133 void emitProgressMessage(const QString &text, int type);
135 SyncOptions m_options;
136 SyncStatus m_stat;
137 QReadWriteLock stopReqMutex;
138 QString testRunStr;
139 QVector<QRegExp> m_excludeFilters;
140 QStringList m_dirIteratorFilters;
141 QDir::Filters m_dirFilters;
142 QDateTime m_startTime;
143 unsigned long m_pauseTime;
144 bool stopping;
147 Q_DECLARE_METATYPE(SyncProcess::SyncOptions)
148 Q_DECLARE_TYPEINFO(SyncProcess::SyncOptions, Q_MOVABLE_TYPE);
149 Q_DECLARE_METATYPE(SyncProcess::SyncStatus)
150 Q_DECLARE_TYPEINFO(SyncProcess::SyncStatus, Q_PRIMITIVE_TYPE);
152 #endif // PROCESS_SYNC_H