This repository was archived by the owner on Feb 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathornbackup.cpp
More file actions
314 lines (273 loc) · 8.94 KB
/
ornbackup.cpp
File metadata and controls
314 lines (273 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "ornbackup.h"
#include "ornzypp.h"
#include "ornversion.h"
#include "ornclient.h"
#include <QFileInfo>
#include <QSettings>
#include <QtConcurrent/QtConcurrent>
#include <QFutureWatcher>
#include <QtDBus/QDBusMessage>
#include <QtDBus/QDBusConnection>
#include <QDebug>
#define BR_CREATED QStringLiteral("created")
#define BR_REPO_ALL QStringLiteral("repos/all")
#define BR_REPO_DISABLED QStringLiteral("repos/disabled")
#define BR_INSTALLED QStringLiteral("packages/installed")
#define BR_BOOKMARKS QStringLiteral("packages/bookmarks")
OrnBackup::OrnBackup(QObject *parent) :
QObject(parent),
mZypp(OrnZypp::instance()),
mStatus(Idle)
{
}
OrnBackup::Status OrnBackup::status() const
{
return mStatus;
}
void OrnBackup::setStatus(const Status &status)
{
if (mStatus != status)
{
mStatus = status;
emit this->statusChanged();
}
}
QVariantMap OrnBackup::details(const QString &path)
{
Q_ASSERT_X(QFileInfo(path).isFile(), Q_FUNC_INFO, "Backup file does not exist");
QVariantMap res;
QSettings file(path, QSettings::IniFormat);
res.insert(QLatin1String("created"), file.value(BR_CREATED).toDateTime().toLocalTime());
res.insert(QLatin1String("repos"), file.value(BR_REPO_ALL).toStringList().size());
res.insert(QLatin1String("packages"), file.value(BR_INSTALLED).toStringList().size());
res.insert(QLatin1String("bookmarks"), file.value(BR_BOOKMARKS).toStringList().size());
return res;
}
void OrnBackup::backup(const QString &filePath)
{
Q_ASSERT_X(!filePath.isEmpty(), Q_FUNC_INFO, "A file path must be provided");
Q_ASSERT_X(!QFileInfo(filePath).isFile(), Q_FUNC_INFO, "Backup file already exists");
if (mStatus != Idle)
{
qWarning() << this << "is already" << mStatus;
return;
}
mFilePath = filePath;
auto dir = QFileInfo(mFilePath).dir();
if (!dir.exists() && !dir.mkpath(QChar('.')))
{
qCritical() << "Failed to create directory" << dir.absolutePath();
emit this->backupError(DirectoryError);
}
qDebug() << mFilePath;
QtConcurrent::run(this, &OrnBackup::pBackup);
}
void OrnBackup::restore(const QString &filePath)
{
Q_ASSERT_X(!filePath.isEmpty(), Q_FUNC_INFO, "A file path must be set");
Q_ASSERT_X(QFileInfo(filePath).isFile(), Q_FUNC_INFO, "Backup file does not exist");
if (mStatus != Idle)
{
qWarning() << this << "is already" << mStatus;
return;
}
mFilePath = filePath;
auto watcher = new QFutureWatcher<void>();
connect(watcher, &QFutureWatcher<void>::finished, this, &OrnBackup::pRefreshRepos);
watcher->setFuture(QtConcurrent::run(this, &OrnBackup::pRestore));
}
QStringList OrnBackup::notFound() const
{
QStringList names;
for (const auto &name : mPackagesToInstall.keys())
{
if (!mNamesToSearch.contains(name))
{
names << name;
}
}
return names;
}
bool OrnBackup::removeFile(const QString &filePath)
{
Q_ASSERT_X(!QFileInfo(filePath).isDir(), Q_FUNC_INFO, "Path must be a file");
return QFile(filePath).remove();
}
void OrnBackup::pSearchPackages()
{
qDebug() << "Searching packages";
this->setStatus(SearchingPackages);
// Delete future watcher and prepare variables
this->sender()->deleteLater();
mPackagesToInstall.clear();
mSearchIndex = 0;
auto t = PackageKit::Daemon::searchNames(mNamesToSearch[mSearchIndex]);
connect(t, &PackageKit::Transaction::errorCode, mZypp, &OrnZypp::pkError);
connect(t, &PackageKit::Transaction::package, this, &OrnBackup::pAddPackage);
// It seems that the PackageKit::Transaction::searchNames(QStringList, ...)
// does searches only for the first name so we use this hack
connect(t, &PackageKit::Transaction::finished, [this, t]()
{
/* ++mSearchIndex;
if (mSearchIndex < mNamesToSearch.size())
{
delete t;
auto t = PackageKit::Daemon::searchNames(mNamesToSearch[mSearchIndex]);
}
else
{
*/
t->deleteLater();
this->pInstallPackages();
});
}
void OrnBackup::pAddPackage(int info, const QString &packageId, const QString &summary)
{
Q_UNUSED(info)
Q_UNUSED(summary)
auto idParts = packageId.split(QChar(';'));
auto name = idParts.first();
if (mNamesToSearch.contains(name))
{
auto repo = idParts.last();
// Process only packages from OpenRepos
if (repo.startsWith(OrnZypp::repoNamePrefix))
{
// We will filter the newest versions later
mPackagesToInstall.insert(name, packageId);
}
else if (repo == QStringLiteral("installed"))
{
mInstalled.insert(name, idParts[1]);
}
}
}
void OrnBackup::pInstallPackages()
{
qDebug() << "Installing packages";
this->setStatus(InstallingPackages);
QStringList ids;
for (const auto &pname : mPackagesToInstall.uniqueKeys())
{
const auto &pids = mPackagesToInstall.values(pname);
QString newestId;
OrnVersion newestVersion;
for (const auto &pid : pids)
{
OrnVersion v(PackageKit::Transaction::packageVersion(pid));
if (v > newestVersion)
{
newestVersion = v;
newestId = pid;
}
}
// Skip packages that are already installed
if (!mInstalled.contains(pname) || OrnVersion(mInstalled[pname]) < newestVersion)
{
ids << newestId;
}
}
if (ids.isEmpty())
{
this->pFinishRestore();
}
else
{
auto t = PackageKit::Daemon::installPackages(ids);
connect(t, &PackageKit::Transaction::finished, this, &OrnBackup::pFinishRestore);
}
}
void OrnBackup::pFinishRestore()
{
qDebug() << "Finished restoring";
mFilePath.clear();
this->setStatus(Idle);
emit this->restored();
}
void OrnBackup::pBackup()
{
qDebug() << "Starting backing up";
this->setStatus(BackingUp);
QSettings file(mFilePath, QSettings::IniFormat);
QStringList repos;
QStringList disabled;
QSet<QString> ornPackages;
for (auto it = mZypp->mRepos.cbegin(); it != mZypp->mRepos.cend(); ++it)
{
auto author = it.key().mid(OrnZypp::repoNamePrefixLength);
repos << author;
auto repo = it.value();
if (!repo.enabled)
{
disabled << author;
}
for (const auto &package : repo.packages)
{
ornPackages.insert(package);
}
}
qDebug() << "Backing up repos";
file.setValue(BR_REPO_ALL, repos);
file.setValue(BR_REPO_DISABLED, disabled);
qDebug() << "Backing up installed packages";
QStringList installed;
for (const auto &name : mZypp->mInstalledPackages.uniqueKeys())
{
if (ornPackages.contains(name))
{
installed << name;
}
}
file.setValue(BR_INSTALLED, installed);
qDebug() << "Backing up bookmarks";
QVariantList bookmarks;
for (const auto &b : OrnClient::instance()->mBookmarks)
{
bookmarks << b;
}
file.setValue(BR_BOOKMARKS, bookmarks);
file.setValue(BR_CREATED, QDateTime::currentDateTime().toUTC());
qDebug() << "Finished backing up";
mFilePath.clear();
this->setStatus(Idle);
emit this->backedUp();
}
void OrnBackup::pRestore()
{
QSettings file(mFilePath, QSettings::IniFormat);
qDebug() << "Restoring bookmarks";
this->setStatus(RestoringBookmarks);
auto client = OrnClient::instance();
auto oldBookmarks = client->mBookmarks;
for (const auto &b : file.value(BR_BOOKMARKS).toList())
{
client->mBookmarks.insert(b.toUInt());
}
if (oldBookmarks != client->mBookmarks)
{
emit client->bookmarksChanged();
}
qDebug() << "Restoring repos";
this->setStatus(RestoringRepos);
auto repos = file.value(BR_REPO_ALL).toStringList();
auto disabled = file.value(BR_REPO_DISABLED).toStringList().toSet();
mNamesToSearch = file.value(BR_INSTALLED).toStringList();
for (const auto &author : repos)
{
auto alias = OrnZypp::repoNamePrefix + author;
auto call = QDBusMessage::createMethodCall(OrnZypp::ssuInterface, OrnZypp::ssuPath,
OrnZypp::ssuInterface, OrnZypp::ssuAddRepo);
call.setArguments(QVariantList{ alias, OrnZypp::repoBaseUrl.arg(author) });
QDBusConnection::systemBus().call(call, QDBus::Block);
mZypp->mRepos.insert(alias, OrnZypp::RepoMeta(!disabled.contains(author)));
}
}
void OrnBackup::pRefreshRepos()
{
qDebug() << "Refreshing repos";
this->setStatus(RefreshingRepos);
auto t = PackageKit::Daemon::refreshCache(false);
connect(t, &PackageKit::Transaction::finished, t, &PackageKit::Transaction::deleteLater);
connect(t, &PackageKit::Transaction::errorCode, mZypp, &OrnZypp::pkError);
connect(t, &PackageKit::Transaction::finished, this, &OrnBackup::pSearchPackages);
}