adding backend of loading a file

This commit is contained in:
Chris Cochrun 2022-10-15 07:32:42 -05:00
parent 9ba3f447a3
commit 742e9dd3b4
3 changed files with 112 additions and 43 deletions

View file

@ -1,6 +1,9 @@
#include "filemanager.h" #include "filemanager.h"
#include <ktar.h> #include <ktar.h>
#include <KCompressionDevice> #include <KCompressionDevice>
#include <KArchiveDirectory>
#include <KArchiveFile>
#include <KArchiveEntry>
#include <QDebug> #include <QDebug>
#include <QJsonArray> #include <QJsonArray>
#include <QJsonObject> #include <QJsonObject>
@ -70,11 +73,11 @@ bool File::save(QUrl file, QVariantList serviceList) {
qDebug() << "AUDIO IS: " << item.value("audio").toString(); qDebug() << "AUDIO IS: " << item.value("audio").toString();
QFileInfo audioFile = item.value("audio").toString(); QFileInfo audioFile = item.value("audio").toString();
qDebug() << audioFile.fileName(); qDebug() << audioFile.fileName();
item["audio"] = audioFile.fileName(); item["flatAudio"] = audioFile.fileName();
qDebug() << "AUDIO IS NOW: " << item.value("audio").toString(); qDebug() << "AUDIO IS NOW: " << item.value("audio").toString();
QFileInfo backgroundFile = item.value("background").toString(); QFileInfo backgroundFile = item.value("background").toString();
item["background"] = backgroundFile.fileName(); item["flatBackground"] = backgroundFile.fileName();
qDebug() << "BACKGRUOND IS: " << item.value("background").toString(); qDebug() << "BACKGRUOND IS: " << item.value("background").toString();
// qDebug() << serviceList[i].value(); // qDebug() << serviceList[i].value();
QJsonObject obj = QJsonObject::fromVariantMap(item); QJsonObject obj = QJsonObject::fromVariantMap(item);
@ -83,11 +86,7 @@ bool File::save(QUrl file, QVariantList serviceList) {
} }
qDebug() << jsonData; qDebug() << jsonData;
QJsonDocument jsonText(jsonData); QJsonDocument jsonText(jsonData);
QDir dir;
dir.mkpath("/tmp/presenter");
QTemporaryFile jsonFile; QTemporaryFile jsonFile;
if (!jsonFile.exists()) if (!jsonFile.exists())
@ -106,45 +105,83 @@ bool File::save(QUrl file, QVariantList serviceList) {
QString filename = file.toString().right(file.toString().size() - 7); QString filename = file.toString().right(file.toString().size() - 7);
qDebug() << filename; qDebug() << filename;
KCompressionDevice dev(filename, KCompressionDevice::Zstd); // KCompressionDevice dev(filename, KCompressionDevice::Zstd);
if (!dev.open(QIODevice::WriteOnly)) { // if (!dev.open(QIODevice::WriteOnly)) {
qDebug() << dev.isOpen(); // qDebug() << dev.isOpen();
return false; // return false;
} // }
KTar tar(filename, "application/zstd"); KTar tar(filename, "application/zstd");
if (!tar.open(QIODevice::WriteOnly)) { if (tar.open(QIODevice::WriteOnly)) {
qDebug() << tar.isOpen(); 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<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;
} }

View file

@ -25,6 +25,7 @@ public:
Q_INVOKABLE void setFilePath(QString filePath); Q_INVOKABLE void setFilePath(QString filePath);
Q_INVOKABLE bool save(QUrl file, QVariantList serviceList); Q_INVOKABLE bool save(QUrl file, QVariantList serviceList);
Q_INVOKABLE QVariantList load(QUrl file);
signals: signals:
Q_INVOKABLE void nameChanged(QString name); Q_INVOKABLE void nameChanged(QString name);

View file

@ -35,7 +35,10 @@ Kirigami.ApplicationWindow {
title: qsTr("File") title: qsTr("File")
Controls.MenuItem { text: qsTr("New...") } Controls.MenuItem { text: qsTr("New...") }
Controls.MenuItem { text: qsTr("Open...") } 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.MenuItem { text: qsTr("Save As...") }
Controls.MenuSeparator { } Controls.MenuSeparator { }
Controls.MenuItem { text: qsTr("Quit") } Controls.MenuItem { text: qsTr("Quit") }
@ -67,7 +70,11 @@ Kirigami.ApplicationWindow {
Labs.Menu { Labs.Menu {
title: qsTr("File") title: qsTr("File")
Labs.MenuItem { text: qsTr("New...") } Labs.MenuItem { text: qsTr("New...") }
Labs.MenuItem { text: qsTr("Open...") } Labs.MenuItem {
text: qsTr("Open...")
shortcut: "Ctrl+O"
onTriggered: loadFileDialog.open()
}
Labs.MenuItem { Labs.MenuItem {
text: qsTr("Save") text: qsTr("Save")
shortcut: "Ctrl+S" 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() { function toggleEditMode() {
editMode = !editMode; editMode = !editMode;
mainPage.editSwitch(); mainPage.editSwitch();
@ -138,13 +160,22 @@ Kirigami.ApplicationWindow {
function save(file) { function save(file) {
const saved = FileManager.save(file, mainPage.serviceItems.getItems()); const saved = FileManager.save(file, mainPage.serviceItems.getItems());
saved ? showPassiveNotification("SAVED! " + file) : showPassiveNotification("FAILED!"); saved ? showPassiveNotification("SAVED! " + file)
: showPassiveNotification("FAILED!");
} }
function saveAs() { 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: { Component.onCompleted: {
/* showPassiveNotification(Kirigami.Settings.style); */ /* showPassiveNotification(Kirigami.Settings.style); */
/* Kirigami.Settings.style = "Plasma"; */ /* Kirigami.Settings.style = "Plasma"; */