Warning, /multimedia/kid3/src/qml/script/ExportPlaylist.qml is written in an unsupported language. File is not indexed.
0001 /**
0002 * \file ExportPlaylistFolder.qml
0003 * Export files found in playlist to a new folder.
0004 *
0005 * \b Project: Kid3
0006 * \author Urs Fleisch
0007 * \date 07 Sep 2018
0008 *
0009 * Copyright (C) 2018 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.1
0025
0026 Kid3Script {
0027 onRun: {
0028 var playlistItems, exportItems, exportPlaylistPath, itemNr
0029
0030 function getPlaylistPath() {
0031 var paths = isStandalone()
0032 ? getArguments().slice(1) : app.getSelectedFilePaths(false)
0033 for (var i = 0, len = paths.length; i < len; i++) {
0034 var path = paths[i]
0035 var dotPos = path.lastIndexOf(".")
0036 if (dotPos !== -1) {
0037 var ext = path.substring(dotPos)
0038 if ([".m3u", ".pls", ".xspf"].indexOf(ext) !== -1) {
0039 return path
0040 }
0041 }
0042 }
0043 return null
0044 }
0045
0046 function getFileName(path) {
0047 var slashPos = path.lastIndexOf("/")
0048 return slashPos !== -1 ? path.substring(slashPos + 1) : path
0049 }
0050
0051 function createPathLists() {
0052 var playlistPath = getPlaylistPath()
0053 if (!playlistPath) {
0054 console.error("No playlist file selected")
0055 return false
0056 }
0057 console.log("Reading playlist", playlistPath)
0058 playlistItems = app.getPlaylistItems(playlistPath)
0059 var numPlaylistItems = playlistItems.length
0060 if (numPlaylistItems <= 0) {
0061 return false
0062 }
0063 var exportPath = getArguments()[0]
0064 if (!exportPath) {
0065 exportPath = app.selectDirName(
0066 "Select Destination Folder", app.dirName)
0067 }
0068 if (!exportPath) {
0069 return false
0070 }
0071 if (exportPath[exportPath.length - 1] !== "/") {
0072 exportPath += "/"
0073 }
0074
0075 // Create path to playlist in export directory.
0076 exportPlaylistPath = exportPath + getFileName(playlistPath)
0077 if (script.fileExists(exportPlaylistPath)) {
0078 console.error("File already exists:", exportPlaylistPath)
0079 return false
0080 }
0081
0082 // Create the paths of the files in the exported playlist.
0083 exportItems = []
0084 var numDigits = Math.max(
0085 2, Math.floor(Math.log(exportItems.length) / Math.log(10)) + 1)
0086 var zeros = ""
0087 for (var i = 0; i < numDigits; i++) {
0088 zeros += "0"
0089 }
0090 for (i = 0; i < numPlaylistItems; i++) {
0091 // Make sure that the file name starts with the number
0092 // of the position in the playlist.
0093 var nr = (zeros + (i + 1)).slice(-numDigits)
0094 var fileName = getFileName(playlistItems[i])
0095 fileName = fileName.replace(/^\d+/, nr)
0096 if (fileName.substring(0, numDigits) !== nr) {
0097 fileName = nr + " " + fileName
0098 }
0099
0100 var filePath = exportPath + fileName
0101 if (script.fileExists(filePath)) {
0102 console.error("File already exists:", filePath)
0103 return false
0104 }
0105
0106 exportItems.push(filePath)
0107 }
0108 return true
0109 }
0110
0111 function doWork() {
0112 if (itemNr < playlistItems.length) {
0113 // Copy the next file.
0114 var src = playlistItems[itemNr]
0115 var dst = exportItems[itemNr]
0116 console.log("Copy " + src + " to " + dst)
0117 if (!script.copyFile(src, dst)) {
0118 console.error("Failed to copy")
0119 Qt.quit()
0120 } else {
0121 ++itemNr
0122 setTimeout(doWork, 1)
0123 }
0124 } else {
0125 // Finally create a playlist in the export folder.
0126 app.setPlaylistItems(exportPlaylistPath, exportItems)
0127 Qt.quit()
0128 }
0129 }
0130
0131 if (createPathLists()) {
0132 itemNr = 0
0133 doWork()
0134 } else {
0135 Qt.quit()
0136 }
0137 }
0138 }