Warning, file /utilities/daykountdown/src/importexport.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002 * SPDX-FileCopyrightText: (C) 2021 Claudio Cambra <claudio.cambra@gmail.com>
0003 * 
0004 * SPDX-LicenseRef: GPL-3.0-or-later
0005 */
0006 
0007 #include "importexport.h"
0008 #include "kountdownmodel.h"
0009 
0010 // Constructor function
0011 ImportExport::ImportExport(QObject *parent) : QObject(parent)
0012 {
0013 
0014 }
0015 
0016 // Function extracts kountdowns from SQLite table and converts to JSON array
0017 QJsonDocument ImportExport::_createJson() {
0018     QJsonArray kountdownsJsonArr;
0019     
0020     // Select all rows from KountdownModel table
0021     QSqlQuery query("SELECT * FROM KountdownModel");
0022     // Query.next moves through each returned row
0023     while(query.next()) {
0024         // Object to add to JSON array
0025         QJsonObject kountdownToAdd {
0026             // Query.value(number) <- number is the column
0027             {"name", query.value(1).toString()},
0028             {"description", query.value(2).toString()},
0029             {"date", query.value(3).toString()},
0030             {"colour", query.value(5).toString()}
0031         };
0032         kountdownsJsonArr.append(kountdownToAdd);
0033     }
0034     // Create object with key-value pair of "kountdowns": kountdownsJsonArr
0035     QJsonObject mainObj {{"kountdowns", kountdownsJsonArr}};
0036     // Create JSON document from this object
0037     QJsonDocument exportingDoc(mainObj);
0038     return exportingDoc;
0039 }
0040 
0041 void ImportExport::exportFile() {
0042     // Open file dialog to get path for file-name to save
0043     QString fileName = QFileDialog::getSaveFileName(NULL, i18n("Save File As"), "exported_kountdowns.json", "JSON (*.json)");
0044     // Get the JSON file from the previous function to create
0045     QJsonDocument jsonDoc = _createJson();
0046     QSaveFile file(fileName);
0047     file.open(QIODevice::WriteOnly);
0048     file.write(jsonDoc.toJson());
0049     // Commit writes the changes to file
0050     file.commit();
0051 }
0052 
0053 void ImportExport::fetchKountdowns() {
0054     // _kountdownArray is going to hold all out kountdowns extracted from the JSON
0055     // We clear it so we don't import kountdowns from previously imported files
0056     _kountdownArray.clear();
0057     
0058     // Get QUrl for file to import
0059     QUrl filePath = QFileDialog::getOpenFileUrl(NULL, i18n("Import file"));
0060     // Returns the path of the QUrl formatted as a local gile path
0061     filePath = filePath.toLocalFile();
0062     
0063     QFile inFile(filePath.toString());
0064     if(inFile.exists()) {
0065         qDebug() << "Found kountdowns.json";
0066         inFile.open(QIODevice::ReadOnly | QIODevice::Text);
0067         // QByteArray is going to hold the data from the JSON file
0068         QByteArray data = inFile.readAll();
0069         inFile.close();
0070         
0071         // errorPtr is going to contain any errors we run into when parsing our JSON data
0072         QJsonParseError errorPtr;
0073         // Create internal JSON doc from data, with errorPtr address for errors
0074         QJsonDocument kountdownsDoc = QJsonDocument::fromJson(data, &errorPtr);
0075         if(kountdownsDoc.isNull())
0076             qDebug() << "Parse failed";
0077         // Create JSON object from root object of JSON file
0078         QJsonObject rootObj = kountdownsDoc.object();
0079         // Create array from the root object's value of "kountdowns" key (which is an array)
0080         QJsonArray kountdownsJsonArray = rootObj.value("kountdowns").toArray();
0081         
0082         /*
0083         * JSON Structure should be like so:
0084         * {
0085         *       "kountdowns": [
0086         *           {
0087         *               "name": "kountdown1",
0088         *               "description": "kountdown number one",
0089         *               "date": date string,
0090         *               "colour": "red"
0091         *           }
0092         *       ]
0093         * }
0094         * 
0095         */
0096         
0097         int i = 0;
0098         // For each kountdown in the JSON array...
0099         foreach(const QJsonValue & kountdownJson, kountdownsJsonArray) {
0100             // Create kountdown struct
0101             kountdown currKountdown;
0102             // Set values of this struct
0103             currKountdown.index = i;
0104             currKountdown.name = kountdownJson.toObject().value("name").toString();
0105             currKountdown.description = kountdownJson.toObject().value("description").toString();
0106             currKountdown.date = kountdownJson.toObject().value("date").toString();
0107             if(kountdownJson.toObject().contains("colour"))
0108                 currKountdown.colour = kountdownJson.toObject().value("colour").toString();
0109             else
0110                 // Default text colour defined in QML
0111                 currKountdown.colour = "palette.text";
0112             _kountdownArray.append(currKountdown);
0113             i++;
0114         }
0115     }
0116     else {
0117         qDebug() << "Didn't find kountdowns.json";
0118     }
0119 }
0120 
0121 QVariantList ImportExport::kountdownPopulator () {
0122     QVariantList kountdownsList;
0123     
0124     for(const kountdown & k : _kountdownArray) {
0125         kountdownsList << QVariant::fromValue(k);
0126     }
0127     
0128     return kountdownsList;
0129 }
0130 
0131