adding backend of loading a file
This commit is contained in:
parent
9ba3f447a3
commit
742e9dd3b4
3 changed files with 112 additions and 43 deletions
|
@ -1,6 +1,9 @@
|
|||
#include "filemanager.h"
|
||||
#include <ktar.h>
|
||||
#include <KCompressionDevice>
|
||||
#include <KArchiveDirectory>
|
||||
#include <KArchiveFile>
|
||||
#include <KArchiveEntry>
|
||||
#include <QDebug>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
|
@ -70,11 +73,11 @@ bool File::save(QUrl file, QVariantList serviceList) {
|
|||
qDebug() << "AUDIO IS: " << item.value("audio").toString();
|
||||
QFileInfo audioFile = item.value("audio").toString();
|
||||
qDebug() << audioFile.fileName();
|
||||
item["audio"] = audioFile.fileName();
|
||||
item["flatAudio"] = audioFile.fileName();
|
||||
qDebug() << "AUDIO IS NOW: " << item.value("audio").toString();
|
||||
|
||||
QFileInfo backgroundFile = item.value("background").toString();
|
||||
item["background"] = backgroundFile.fileName();
|
||||
item["flatBackground"] = backgroundFile.fileName();
|
||||
qDebug() << "BACKGRUOND IS: " << item.value("background").toString();
|
||||
// qDebug() << serviceList[i].value();
|
||||
QJsonObject obj = QJsonObject::fromVariantMap(item);
|
||||
|
@ -83,11 +86,7 @@ bool File::save(QUrl file, QVariantList serviceList) {
|
|||
}
|
||||
|
||||
qDebug() << jsonData;
|
||||
|
||||
QJsonDocument jsonText(jsonData);
|
||||
|
||||
QDir dir;
|
||||
dir.mkpath("/tmp/presenter");
|
||||
QTemporaryFile jsonFile;
|
||||
|
||||
if (!jsonFile.exists())
|
||||
|
@ -106,20 +105,19 @@ bool File::save(QUrl file, QVariantList serviceList) {
|
|||
QString filename = file.toString().right(file.toString().size() - 7);
|
||||
qDebug() << filename;
|
||||
|
||||
KCompressionDevice dev(filename, KCompressionDevice::Zstd);
|
||||
if (!dev.open(QIODevice::WriteOnly)) {
|
||||
qDebug() << dev.isOpen();
|
||||
return false;
|
||||
}
|
||||
// KCompressionDevice dev(filename, KCompressionDevice::Zstd);
|
||||
// if (!dev.open(QIODevice::WriteOnly)) {
|
||||
// qDebug() << dev.isOpen();
|
||||
// return false;
|
||||
// }
|
||||
KTar tar(filename, "application/zstd");
|
||||
|
||||
if (!tar.open(QIODevice::WriteOnly)) {
|
||||
if (tar.open(QIODevice::WriteOnly)) {
|
||||
qDebug() << tar.isOpen();
|
||||
return false;
|
||||
}
|
||||
|
||||
//write our json data to the archive
|
||||
tar.writeFile("servicelist.json",jsonText.toJson());
|
||||
tar.writeFile("servicelist.json",
|
||||
jsonText.toJson());
|
||||
|
||||
//let's add the backgrounds and audios to the archive
|
||||
for (int i = 0; i < serviceList.size(); i++) {
|
||||
|
@ -143,8 +141,47 @@ bool File::save(QUrl file, QVariantList serviceList) {
|
|||
|
||||
//close the archive so that everything is done
|
||||
tar.close();
|
||||
dev.close();
|
||||
|
||||
|
||||
// dev.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
QVariantList File::load(QUrl file) {
|
||||
qDebug() << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
|
||||
qDebug() << "Loading...";
|
||||
qDebug() << "File path is: " << file.toString();
|
||||
qDebug() << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
|
||||
|
||||
|
||||
QString fileUrl = file.toString().right(file.toString().size() - 7);
|
||||
KTar tar(fileUrl);
|
||||
|
||||
if (tar.open(QIODevice::ReadOnly)){
|
||||
qDebug() << tar.isOpen();
|
||||
const KArchiveDirectory *dir = tar.directory();
|
||||
|
||||
const KArchiveEntry *e = dir->entry("servicelist.json");
|
||||
if (!e) {
|
||||
qDebug() << "File not found!";
|
||||
}
|
||||
const KArchiveFile *f = static_cast<const KArchiveFile *>(e);
|
||||
QByteArray arr(f->data());
|
||||
QJsonDocument jsonText = QJsonDocument::fromJson(arr);
|
||||
qDebug() << jsonText; // the file contents
|
||||
|
||||
QJsonArray array = jsonText.array();
|
||||
|
||||
QVariantList serviceList = array.toVariantList();
|
||||
qDebug() << serviceList;
|
||||
return serviceList;
|
||||
|
||||
}
|
||||
|
||||
QVariantList blankList;
|
||||
return blankList;
|
||||
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ public:
|
|||
Q_INVOKABLE void setFilePath(QString filePath);
|
||||
|
||||
Q_INVOKABLE bool save(QUrl file, QVariantList serviceList);
|
||||
Q_INVOKABLE QVariantList load(QUrl file);
|
||||
|
||||
signals:
|
||||
Q_INVOKABLE void nameChanged(QString name);
|
||||
|
|
|
@ -35,7 +35,10 @@ Kirigami.ApplicationWindow {
|
|||
title: qsTr("File")
|
||||
Controls.MenuItem { text: qsTr("New...") }
|
||||
Controls.MenuItem { text: qsTr("Open...") }
|
||||
Controls.MenuItem { text: qsTr("Save") }
|
||||
Controls.MenuItem {
|
||||
text: qsTr("Save")
|
||||
onTriggered: saveFileDialog.open()
|
||||
}
|
||||
Controls.MenuItem { text: qsTr("Save As...") }
|
||||
Controls.MenuSeparator { }
|
||||
Controls.MenuItem { text: qsTr("Quit") }
|
||||
|
@ -67,7 +70,11 @@ Kirigami.ApplicationWindow {
|
|||
Labs.Menu {
|
||||
title: qsTr("File")
|
||||
Labs.MenuItem { text: qsTr("New...") }
|
||||
Labs.MenuItem { text: qsTr("Open...") }
|
||||
Labs.MenuItem {
|
||||
text: qsTr("Open...")
|
||||
shortcut: "Ctrl+O"
|
||||
onTriggered: loadFileDialog.open()
|
||||
}
|
||||
Labs.MenuItem {
|
||||
text: qsTr("Save")
|
||||
shortcut: "Ctrl+S"
|
||||
|
@ -118,6 +125,21 @@ Kirigami.ApplicationWindow {
|
|||
}
|
||||
}
|
||||
|
||||
FileDialog {
|
||||
id: loadFileDialog
|
||||
title: "Load"
|
||||
folder: shortcuts.home
|
||||
/* fileMode: FileDialog.SaveFile */
|
||||
defaultSuffix: ".pres"
|
||||
selectExisting: true
|
||||
onAccepted: {
|
||||
load(loadFileDialog.fileUrl);
|
||||
}
|
||||
onRejected: {
|
||||
print("Canceled")
|
||||
}
|
||||
}
|
||||
|
||||
function toggleEditMode() {
|
||||
editMode = !editMode;
|
||||
mainPage.editSwitch();
|
||||
|
@ -138,13 +160,22 @@ Kirigami.ApplicationWindow {
|
|||
|
||||
function save(file) {
|
||||
const saved = FileManager.save(file, mainPage.serviceItems.getItems());
|
||||
saved ? showPassiveNotification("SAVED! " + file) : showPassiveNotification("FAILED!");
|
||||
saved ? showPassiveNotification("SAVED! " + file)
|
||||
: showPassiveNotification("FAILED!");
|
||||
}
|
||||
|
||||
function saveAs() {
|
||||
|
||||
}
|
||||
|
||||
function load(file) {
|
||||
const loaded = FileManager.load(file);
|
||||
loaded ? showPassiveNotification("Loaded: " + file)
|
||||
: showPassiveNotification("FAILED!");
|
||||
print("Number of items: " + loaded.length);
|
||||
print(loaded[0].audio);
|
||||
}
|
||||
|
||||
Component.onCompleted: {
|
||||
/* showPassiveNotification(Kirigami.Settings.style); */
|
||||
/* Kirigami.Settings.style = "Plasma"; */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue