1/**
2 * \file ExportPlaylistFolder.qml
3 * Export files found in playlist to a new folder.
4 *
5 * \b Project: Kid3
6 * \author Urs Fleisch
7 * \date 07 Sep 2018
8 *
9 * Copyright (C) 2018  Urs Fleisch
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; version 3.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24import Kid3 1.1
25
26Kid3Script {
27  onRun: {
28    var playlistItems, exportItems, exportPlaylistPath, itemNr
29
30    function getPlaylistPath() {
31      var paths = isStandalone()
32          ? getArguments().slice(1) : app.getSelectedFilePaths(false)
33      for (var i = 0, len = paths.length; i < len; i++) {
34        var path = paths[i]
35        var dotPos = path.lastIndexOf(".")
36        if (dotPos !== -1) {
37          var ext = path.substring(dotPos)
38          if ([".m3u", ".pls", ".xspf"].indexOf(ext) !== -1) {
39            return path
40          }
41        }
42      }
43      return null
44    }
45
46    function getFileName(path) {
47      var slashPos = path.lastIndexOf("/")
48      return slashPos !== -1 ? path.substring(slashPos + 1) : path
49    }
50
51    function createPathLists() {
52      var playlistPath = getPlaylistPath()
53      if (!playlistPath) {
54        console.error("No playlist file selected")
55        return false
56      }
57      console.log("Reading playlist", playlistPath)
58      playlistItems = app.getPlaylistItems(playlistPath)
59      var numPlaylistItems = playlistItems.length
60      if (numPlaylistItems <= 0) {
61        return false
62      }
63      var exportPath = getArguments()[0]
64      if (!exportPath) {
65        exportPath = app.selectDirName(
66          "Select Destination Folder", app.dirName)
67      }
68      if (!exportPath) {
69        return false
70      }
71      if (exportPath[exportPath.length - 1] !== "/") {
72        exportPath += "/"
73      }
74
75      // Create path to playlist in export directory.
76      exportPlaylistPath = exportPath + getFileName(playlistPath)
77      if (script.fileExists(exportPlaylistPath)) {
78        console.error("File already exists:", exportPlaylistPath)
79        return false
80      }
81
82      // Create the paths of the files in the exported playlist.
83      exportItems = []
84      var numDigits = Math.max(
85          2, Math.floor(Math.log(exportItems.length) / Math.log(10)) + 1)
86      var zeros = ""
87      for (var i = 0; i < numDigits; i++) {
88        zeros += "0"
89      }
90      for (i = 0; i < numPlaylistItems; i++) {
91        // Make sure that the file name starts with the number
92        // of the position in the playlist.
93        var nr = (zeros + (i + 1)).slice(-numDigits)
94        var fileName = getFileName(playlistItems[i])
95        fileName = fileName.replace(/^\d+/, nr)
96        if (fileName.substring(0, numDigits) !== nr) {
97          fileName = nr + " " + fileName
98        }
99
100        var filePath = exportPath + fileName
101        if (script.fileExists(filePath)) {
102          console.error("File already exists:", filePath)
103          return false
104        }
105
106        exportItems.push(filePath)
107      }
108      return true
109    }
110
111    function doWork() {
112      if (itemNr < playlistItems.length) {
113        // Copy the next file.
114        var src = playlistItems[itemNr]
115        var dst = exportItems[itemNr]
116        console.log("Copy " + src + " to " + dst)
117        if (!script.copyFile(src, dst)) {
118          console.error("Failed to copy")
119          Qt.quit()
120        } else {
121          ++itemNr
122          setTimeout(doWork, 1)
123        }
124      } else {
125        // Finally create a playlist in the export folder.
126        app.setPlaylistItems(exportPlaylistPath, exportItems)
127        Qt.quit()
128      }
129    }
130
131    if (createPathLists()) {
132      itemNr = 0
133      doWork()
134    } else {
135      Qt.quit()
136    }
137  }
138}
139