1 #include "library/libraryfeature.h"
2
3 #include <QStandardPaths>
4
5 #include "library/library.h"
6 #include "library/parserm3u.h"
7 #include "library/parserpls.h"
8 #include "moc_libraryfeature.cpp"
9 #include "util/logger.h"
10
11 // KEEP THIS cpp file to tell scons that moc should be called on the class!!!
12 // The reason for this is that LibraryFeature uses slots/signals and for this
13 // to work the code has to be precompiles by moc
14
15 namespace {
16
17 const mixxx::Logger kLogger("LibraryFeature");
18
19 } // anonymous namespace
20
LibraryFeature(Library * pLibrary,UserSettingsPointer pConfig)21 LibraryFeature::LibraryFeature(
22 Library* pLibrary,
23 UserSettingsPointer pConfig)
24 : QObject(pLibrary),
25 m_pLibrary(pLibrary),
26 m_pConfig(pConfig) {
27 }
28
getPlaylistFiles(QFileDialog::FileMode mode) const29 QStringList LibraryFeature::getPlaylistFiles(QFileDialog::FileMode mode) const {
30 QString lastPlaylistDirectory = m_pConfig->getValue(
31 ConfigKey("[Library]", "LastImportExportPlaylistDirectory"),
32 QStandardPaths::writableLocation(QStandardPaths::MusicLocation));
33
34 QFileDialog dialog(nullptr,
35 tr("Import Playlist"),
36 lastPlaylistDirectory,
37 tr("Playlist Files (*.m3u *.m3u8 *.pls *.csv)"));
38 dialog.setAcceptMode(QFileDialog::AcceptOpen);
39 dialog.setFileMode(mode);
40 dialog.setModal(true);
41
42 // If the user refuses return
43 if (!dialog.exec()) {
44 return QStringList();
45 }
46 return dialog.selectedFiles();
47 }
48
exportPlaylistItemsIntoFile(QString playlistFilePath,const QList<QString> & playlistItemLocations,bool useRelativePath)49 bool LibraryFeature::exportPlaylistItemsIntoFile(
50 QString playlistFilePath,
51 const QList<QString>& playlistItemLocations,
52 bool useRelativePath) {
53 if (playlistFilePath.endsWith(
54 QStringLiteral(".pls"),
55 Qt::CaseInsensitive)) {
56 return ParserPls::writePLSFile(
57 playlistFilePath,
58 playlistItemLocations,
59 useRelativePath);
60 } else if (playlistFilePath.endsWith(
61 QStringLiteral(".m3u8"),
62 Qt::CaseInsensitive)) {
63 return ParserM3u::writeM3U8File(
64 playlistFilePath,
65 playlistItemLocations,
66 useRelativePath);
67 } else {
68 //default export to M3U if file extension is missing
69 if (!playlistFilePath.endsWith(
70 QStringLiteral(".m3u"),
71 Qt::CaseInsensitive)) {
72 kLogger.debug()
73 << "No valid file extension for playlist export specified."
74 << "Appending .m3u and exporting to M3U.";
75 playlistFilePath.append(QStringLiteral(".m3u"));
76 if (QFileInfo::exists(playlistFilePath)) {
77 auto overwrite = QMessageBox::question(
78 nullptr,
79 tr("Overwrite File?"),
80 tr("A playlist file with the name \"%1\" already exists.\n"
81 "The default \"m3u\" extension was added because none was specified.\n\n"
82 "Do you really want to overwrite it?")
83 .arg(playlistFilePath));
84 if (overwrite != QMessageBox::StandardButton::Yes) {
85 return false;
86 }
87 }
88 }
89 return ParserM3u::writeM3UFile(
90 playlistFilePath,
91 playlistItemLocations,
92 useRelativePath);
93 }
94 }
95