Warning, /multimedia/kid3/src/qml/script/ImportJson.qml is written in an unsupported language. File is not indexed.

0001 /**
0002  * \file ImportJson.qml
0003  * Import all tags of all files from a JSON file.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 29 Oct 2021
0008  *
0009  * Copyright (C) 2021  Urs Fleisch
0010  *
0011  * This program is free software; you can redistribute it and/or modify
0012  * it under the terms of the GNU Lesser General Public License as published by
0013  * the Free Software Foundation; version 3.
0014  *
0015  * This program is distributed in the hope that it will be useful,
0016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  * GNU Lesser General Public License for more details.
0019  *
0020  * You should have received a copy of the GNU Lesser General Public License
0021  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  */
0023 
0024 import Kid3 1.0
0025 
0026 Kid3Script {
0027   onRun: {
0028     var obj, files = {}
0029     var numRowsImported = 0
0030     var round = 0
0031 
0032     function doWork() {
0033       var rowNr
0034       if (files) {
0035         var filePath = app.selectionInfo.filePath
0036         rowNr = files[filePath]
0037       } else {
0038         rowNr = numRowsImported
0039       }
0040 
0041       if (typeof rowNr !== "undefined" && rowNr >= 0 && rowNr < obj.data.length) {
0042         var row = obj.data[rowNr]
0043         for (var frameName in row) {
0044           var frameValue = row[frameName]
0045           if (frameName !== "File Path" && frameName !== "Duration" &&
0046               frameValue !== "") {
0047             if (frameName.substr(0, 2) === "v1") {
0048               frameName = frameName.substr(2)
0049               app.setFrame(tagv1, frameName, frameValue)
0050             } else if (frameName.substr(0, 2) === "v3") {
0051               frameName = frameName.substr(2)
0052               app.setFrame(Frame.TagV3, frameName, frameValue)
0053             } else {
0054               app.setFrame(tagv2, frameName, frameValue)
0055             }
0056           }
0057         }
0058         ++numRowsImported
0059       } else if (filePath) {
0060         console.log("No data for " + filePath)
0061       }
0062       if (!app.nextFile()) {
0063         console.log("Imported tags for %1 files".arg(numRowsImported))
0064         if (isStandalone()) {
0065           // Save the changes if the script is started stand-alone, not from Kid3.
0066           app.saveDirectory()
0067         } else if (numRowsImported === 0 && round === 0) {
0068           console.log("No files found, importing unconditionally.")
0069           files = undefined
0070           ++round;
0071           app.firstFile()
0072           doWork()
0073         }
0074         Qt.quit()
0075       } else {
0076         setTimeout(doWork, 1)
0077       }
0078     }
0079 
0080     function startWork() {
0081       app.expandFileListFinished.disconnect(startWork)
0082       console.log("Setting tags")
0083       app.firstFile()
0084       doWork()
0085     }
0086 
0087     var importPath = getArguments()[0]
0088     if (!importPath) {
0089       importPath = app.selectFileName(
0090         "Import", app.dirName, "JSON files (*.json);;All files (*)", false)
0091       if (!importPath) {
0092         Qt.quit()
0093         return
0094       }
0095     }
0096     var txt = "" + script.readFile(importPath)
0097     obj = JSON.parse(txt);
0098     if (obj && obj.data && obj.data.length > 0) {
0099       console.log("Read tags for %1 files from %2".
0100                   arg(obj.data.length).arg(importPath))
0101       for (var i = 0; i < obj.data.length; ++i) {
0102         var filePath = obj.data[i]["File Path"]
0103         if (filePath) {
0104           files[filePath] = i
0105         }
0106       }
0107       if (Object.keys(files).length === 0) {
0108         console.log("No File Path column found, importing unconditionally.")
0109         files = undefined
0110       }
0111 
0112       if (!isStandalone() && app.hasGui()) {
0113         console.log("Expanding file list")
0114         app.expandFileListFinished.connect(startWork)
0115         app.requestExpandFileList()
0116       } else {
0117         startWork()
0118       }
0119     } else {
0120       console.log("No data found in %1".arg(importPath))
0121     }
0122   }
0123 }