File indexing completed on 2024-05-19 04:49:24
0001 /**************************************************************************************** 0002 * Copyright (c) 2007 Ian Monroe <ian@monroe.nu> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify it under * 0005 * the terms of the GNU General Public License as published by the Free Software * 0006 * Foundation; either version 2 of the License, or (at your option) version 3 or * 0007 * any later version accepted by the membership of KDE e.V. (or its successor approved * 0008 * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of * 0009 * version 3 of the license. * 0010 * * 0011 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0012 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0013 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License along with * 0016 * this program. If not, see <http://www.gnu.org/licenses/>. * 0017 ****************************************************************************************/ 0018 0019 #include "core/playlists/PlaylistFormat.h" 0020 #include "core/logger/Logger.h" 0021 #include "core/support/Components.h" 0022 #include "core/support/Amarok.h" 0023 #include "core-impl/playlists/types/file/PlaylistFileSupport.h" 0024 #include "core/support/Debug.h" 0025 #include "core-impl/playlists/types/file/asx/ASXPlaylist.h" 0026 #include "core-impl/playlists/types/file/xspf/XSPFPlaylist.h" 0027 #include "core-impl/playlists/types/file/pls/PLSPlaylist.h" 0028 #include "core-impl/playlists/types/file/m3u/M3UPlaylist.h" 0029 #include "playlistmanager/file/PlaylistFileProvider.h" 0030 0031 #include "amarokconfig.h" 0032 0033 #include <KLocalizedString> 0034 #include <KMessageBox> 0035 0036 #include <QFile> 0037 #include <QFileInfo> 0038 #include <QUrl> 0039 0040 using namespace Playlists; 0041 0042 PlaylistFilePtr 0043 Playlists::loadPlaylistFile( const QUrl &url, PlaylistFileProvider *provider ) 0044 { 0045 // note: this function can be called from out of process, so don't do any 0046 // UI stuff from this thread. 0047 if( !url.isValid() ) 0048 { 0049 error() << "url is not valid!"; 0050 return PlaylistFilePtr(); 0051 } 0052 0053 if( url.isLocalFile() ) 0054 { 0055 if( !QFileInfo( url.toLocalFile() ).exists() ) 0056 { 0057 error() << QStringLiteral("Could not load local playlist file %1!").arg( url.toLocalFile() ); 0058 return PlaylistFilePtr(); 0059 } 0060 } 0061 0062 PlaylistFormat format = Playlists::getFormat( url ); 0063 PlaylistFilePtr playlist; 0064 switch( format ) 0065 { 0066 case ASX: 0067 playlist = new ASXPlaylist( url, provider ); 0068 break; 0069 case PLS: 0070 playlist = new PLSPlaylist( url, provider ); 0071 break; 0072 case M3U: 0073 playlist = new M3UPlaylist( url, provider ); 0074 break; 0075 case XSPF: 0076 playlist = new XSPFPlaylist( url, provider ); 0077 break; 0078 default: 0079 debug() << "Could not load playlist file " << url; 0080 break; 0081 } 0082 0083 return playlist; 0084 } 0085 0086 bool 0087 Playlists::exportPlaylistFile( const Meta::TrackList &list, const QUrl &url, bool relative, 0088 const QList<int> &queued ) 0089 { 0090 PlaylistFormat format = Playlists::getFormat( url ); 0091 bool result = false; 0092 PlaylistFilePtr playlist; 0093 0094 switch( format ) 0095 { 0096 case ASX: 0097 playlist = new ASXPlaylist( url ); 0098 break; 0099 case PLS: 0100 playlist = new PLSPlaylist( url ); 0101 break; 0102 case M3U: 0103 playlist = new M3UPlaylist( url ); 0104 break; 0105 case XSPF: 0106 playlist = new XSPFPlaylist( url ); 0107 break; 0108 default: 0109 debug() << "Could not export playlist file " << url; 0110 break; 0111 } 0112 0113 if( playlist ) 0114 { 0115 playlist->addTracks( list ); 0116 playlist->setQueue( queued ); 0117 result = playlist->save( relative ); 0118 } 0119 else 0120 { 0121 KMessageBox::error( nullptr, 0122 i18n( "The used file extension is not valid for playlists." ), 0123 i18n( "Unknown playlist format" ) ); 0124 } 0125 0126 return result; 0127 } 0128 0129 bool 0130 Playlists::canExpand( Meta::TrackPtr track ) 0131 { 0132 if( !track ) 0133 return false; 0134 0135 return Playlists::getFormat( QUrl::fromUserInput(track->uidUrl()) ) != Playlists::NotPlaylist; 0136 } 0137 0138 PlaylistPtr 0139 Playlists::expand( Meta::TrackPtr track ) 0140 { 0141 return Playlists::PlaylistPtr::dynamicCast( loadPlaylistFile( QUrl::fromUserInput(track->uidUrl()) ) ); 0142 } 0143 0144 QUrl 0145 Playlists::newPlaylistFilePath( const QString &fileExtension ) 0146 { 0147 int trailingNumber = 1; 0148 KLocalizedString fileName = ki18n("Playlist_%1"); 0149 QUrl url = QUrl::fromLocalFile( Amarok::saveLocation( QStringLiteral("playlists") ) ); 0150 url = url.adjusted(QUrl::StripTrailingSlash); 0151 url.setPath(url.path() + QLatin1Char('/') + ( fileName.subs( trailingNumber ).toString() )); 0152 0153 while( QFileInfo( url.path() ).exists() ) 0154 { 0155 url = url.adjusted(QUrl::RemoveFilename); 0156 url.setPath(url.path() + fileName.subs( ++trailingNumber ).toString() ); 0157 } 0158 0159 return QUrl::fromLocalFile( QStringLiteral( "%1.%2" ).arg( url.path(), fileExtension ) ); 0160 }