From 742e9dd3b4d802d4660b13f81a65c151484a225a Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Sat, 15 Oct 2022 07:32:42 -0500 Subject: [PATCH] adding backend of loading a file --- src/filemanager.cpp | 117 +++++++++++++++++++++++++++++--------------- src/filemanager.h | 1 + src/qml/main.qml | 37 ++++++++++++-- 3 files changed, 112 insertions(+), 43 deletions(-) diff --git a/src/filemanager.cpp b/src/filemanager.cpp index 356289c..7b929a5 100644 --- a/src/filemanager.cpp +++ b/src/filemanager.cpp @@ -1,6 +1,9 @@ #include "filemanager.h" #include #include +#include +#include +#include #include #include #include @@ -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,45 +105,83 @@ 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()); + + //let's add the backgrounds and audios to the archive + for (int i = 0; i < serviceList.size(); i++) { + QMap item = serviceList[i].toMap(); + QString background = item.value("background").toString(); + QString backgroundFile = background.right(background.size() - 5); + qDebug() << backgroundFile; + QString audio = item.value("audio").toString(); + QString audioFile = audio.right(audio.size() - 5); + qDebug() << audioFile; + + //here we need to cut off all the directories before + //adding into the archive + tar.addLocalFile(backgroundFile, + backgroundFile.right(backgroundFile.size() - + backgroundFile.lastIndexOf("/") - 1)); + tar.addLocalFile(audioFile, + audioFile.right(audioFile.size() - + audioFile.lastIndexOf("/") - 1)); + } + + //close the archive so that everything is done + tar.close(); + // dev.close(); + return true; } - //write our json data to the archive - tar.writeFile("servicelist.json",jsonText.toJson()); - - //let's add the backgrounds and audios to the archive - for (int i = 0; i < serviceList.size(); i++) { - QMap item = serviceList[i].toMap(); - QString background = item.value("background").toString(); - QString backgroundFile = background.right(background.size() - 5); - qDebug() << backgroundFile; - QString audio = item.value("audio").toString(); - QString audioFile = audio.right(audio.size() - 5); - qDebug() << audioFile; - - //here we need to cut off all the directories before - //adding into the archive - tar.addLocalFile(backgroundFile, - backgroundFile.right(backgroundFile.size() - - backgroundFile.lastIndexOf("/") - 1)); - tar.addLocalFile(audioFile, - audioFile.right(audioFile.size() - - audioFile.lastIndexOf("/") - 1)); - } - - //close the archive so that everything is done - tar.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(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; + } diff --git a/src/filemanager.h b/src/filemanager.h index dc6143c..1f26f7a 100644 --- a/src/filemanager.h +++ b/src/filemanager.h @@ -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); diff --git a/src/qml/main.qml b/src/qml/main.qml index 2912e6f..fdc251a 100644 --- a/src/qml/main.qml +++ b/src/qml/main.qml @@ -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"; */