Cavoke  1.1.0
A Platform for creating and hosting multiplayer turn-based board games
Loading...
Searching...
No Matches
cache_manager.cpp
1#include <cache_manager.h>
2#include <kzip.h>
3#include <QtDebug>
4
5QUrl cache_manager::get_cached_app_path(const QString &gameId) {
6 QDir cur_app_dir =
7 QDir(APPS_DIR.filePath(gameId + "/client")); // Seems bad
8 if (!cur_app_dir.exists()) {
9 return QString();
10 }
11 QFile qml_file(cur_app_dir.filePath("app.qml"));
12 if (!qml_file.exists()) {
13 qDebug() << "Can not find app.qml in directory " << cur_app_dir << '\n';
14 return QString();
15 }
16 return QUrl::fromUserInput(qml_file.fileName());
17}
18
19QUrl cache_manager::save_zip_to_cache(const QFile *archive_file,
20 const QString &gameId) {
21 QFileInfo archiveFileInfo(archive_file->fileName());
22 QDir app_dir(APPS_DIR.filePath(gameId));
23
24 if (app_dir.exists()) {
25 app_dir.removeRecursively();
26 }
27
28 unzip_to_folder(*archive_file, app_dir);
29
30 return QUrl::fromUserInput(app_dir.filePath("app.qml"));
31}
32
33void cache_manager::unzip_to_folder(const QFile &archive_file,
34 const QDir &dest_dir) {
35 if (!archive_file.exists()) {
36 qDebug() << "Can not unpack the archive: " << archive_file.fileName()
37 << " does not exist\n";
38 return;
39 }
40 dest_dir.mkpath(".");
41
42 KZip archive(archive_file.fileName());
43 if (!archive.open(QIODevice::ReadOnly)) {
44 qDebug() << "Cannot open " << archive_file.fileName()
45 << " Is it a valid zip file?\n";
46 return;
47 }
48
49 const KArchiveDirectory *archive_root = archive.directory();
50 archive_root->copyTo(dest_dir.path(), true);
51 archive.close();
52 qDebug() << "Unpacked " << archive_file.fileName() << " to "
53 << dest_dir.path() << '\n';
54 archive.close();
55}