Debian Codegraph @

src/playlistmanager/file/PlaylistFileProvider.cpp

raw
1 /****************************************************************************************
2 * Copyright (c) 2007 Bart Cerneels <[email protected]> *
3 * *
4 * This program is free software; you can redistribute it and/or modify it under *
5 * the terms of the GNU General Public License as published by the Free Software *
6 * Foundation; either version 2 of the License, or (at your option) any later *
7 * version. *
8 * *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
11 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
12 * *
13 * You should have received a copy of the GNU General Public License along with *
14 * this program. If not, see <http://www.gnu.org/licenses/>. *
15 ****************************************************************************************/
16
17 #include "PlaylistFileProvider.h"
18 #include "App.h"
19 #include "core-impl/playlists/types/file/PlaylistFileSupport.h"
20 #include "core/support/Amarok.h"
21 #include "core/support/Debug.h"
22 #include "core/support/Components.h"
23 #include "core/logger/Logger.h"
24 #include "core-impl/playlists/types/file/asx/ASXPlaylist.h"
25 #include "core-impl/playlists/types/file/m3u/M3UPlaylist.h"
26 #include "core-impl/playlists/types/file/pls/PLSPlaylist.h"
27 #include "core-impl/playlists/types/file/xspf/XSPFPlaylist.h"
28 #include "playlistmanager/PlaylistManager.h"
29
30 #include <QDir>
31 #include <QString>
32 #include <QTimer>
33 #include <QUrl>
34
35 #include <KIO/Global>
36 #include <KLocalizedString>
37
38 namespace Playlists {
39
40 PlaylistFileProvider::PlaylistFileProvider()
41 : UserPlaylistProvider()
42 , m_playlistsLoaded( false )
43 , m_saveLaterTimer( nullptr )
44 {
45 //playlists are lazy loaded but we can count how many we'll load already
46 QStringList keys = loadedPlaylistsConfig().keyList();
47 for( const QString &key : keys )
48 {
49 QUrl url( key );
50 //Don't load these from the config file, they are read from the directory anyway
51 if( KIO::upUrl(url).matches( QUrl::fromUserInput(Amarok::saveLocation(QStringLiteral("playlists"))), QUrl::StripTrailingSlash ) )
52 continue;
53 m_urlsToLoad << url;
54 }
55 //also add all files in the $KDEHOME/share/apps/amarok/playlists
56 QDir playlistDir = QDir( Amarok::saveLocation( QStringLiteral("playlists") ), QLatin1String(""),
57 QDir::Name,
58 QDir::Files | QDir::Readable );
59 for( const QString &file : playlistDir.entryList() )
60 {
61 QUrl url( playlistDir.path() );
62 url = url.adjusted(QUrl::StripTrailingSlash);
63 url.setPath(url.path() + QLatin1Char('/') + ( file ));
64 if( Playlists::isPlaylist( url ) )
65 m_urlsToLoad << url;
66 }
67 }
68
69 PlaylistFileProvider::~PlaylistFileProvider()
70 {
71 DEBUG_BLOCK
72 //remove all, well add them again soon
73 loadedPlaylistsConfig().deleteGroup();
74 //Write loaded playlists to config file
75 for( Playlists::PlaylistFilePtr playlistFile : m_playlists )
76 {
77 QUrl url = playlistFile->uidUrl();
78 //only save files NOT in "playlists", those are automatically loaded.
79 if( KIO::upUrl(url).matches( QUrl::fromUserInput(Amarok::saveLocation( QStringLiteral("playlists") )), QUrl::StripTrailingSlash ) )
80 continue;
81
82 //debug() << "storing to rc-file: " << url.url();
83
84 loadedPlaylistsConfig().writeEntry( url.url(), playlistFile->groups() );
85 }
86 loadedPlaylistsConfig().sync();
87 }
88
89 QString
90 PlaylistFileProvider::prettyName() const
91 {
92 return i18n( "Playlist Files on Disk" );
93 }
94
95 QIcon PlaylistFileProvider::icon() const
96 {
97 return QIcon::fromTheme( QStringLiteral("folder-documents") );
98 }
99
100 int
101 PlaylistFileProvider::playlistCount() const
102 {
103 return m_playlists.count() + m_urlsToLoad.count();
104 }
105
106 Playlists::PlaylistList
107 PlaylistFileProvider::playlists()
108 {
109 Playlists::PlaylistList playlists;
110
111 if( !m_playlistsLoaded )
112 {
113 //trigger a lazy load the playlists
114 QTimer::singleShot(0, this, &PlaylistFileProvider::loadPlaylists );
115 return playlists;
116 }
117
118 for( const Playlists::PlaylistFilePtr &playlistFile : m_playlists )
119 {
120 Playlists::PlaylistPtr playlist = Playlists::PlaylistPtr::dynamicCast( playlistFile );
121 if( !playlist.isNull() )
122 playlists << playlist;
123 }
124 return playlists;
125 }
126
127 Playlists::PlaylistPtr
128 PlaylistFileProvider::save( const Meta::TrackList &tracks, const QString &name )
129 {
130 DEBUG_BLOCK
131
132 QString filename = name.isEmpty() ? QDateTime::currentDateTime().toString( QStringLiteral("ddd MMMM d yy hh-mm")) : name;
133 filename.replace( QLatin1Char('/'), QLatin1Char('-') );
134 filename.replace( QLatin1Char('\\'), QLatin1Char('-') );
135
136 Playlists::PlaylistFormat format = Playlists::getFormat( QUrl::fromUserInput( filename, QStringLiteral("./"), QUrl::AssumeLocalFile ) );
137 if( format == Playlists::Unknown ) // maybe the name just had a dot in it. We just add .xspf
138 {
139 format = Playlists::XSPF;
140 filename.append( QLatin1String( ".xspf" ) );
141 }
142
143 QUrl path = QUrl::fromLocalFile( Amarok::saveLocation( QStringLiteral("playlists") ) );
144 path = path.adjusted(QUrl::StripTrailingSlash);
145 path.setPath(path.path() + QLatin1Char('/') + ( Amarok::vfatPath( filename ) ));
146 if( QFileInfo( path.toLocalFile() ).exists() )
147 {
148 //TODO:request overwrite
149 return Playlists::PlaylistPtr();
150 }
151
152 Playlists::PlaylistFile *playlistFile = nullptr;
153 switch( format )
154 {
155 case Playlists::ASX:
156 playlistFile = new Playlists::ASXPlaylist( path, this );
157 break;
158 case Playlists::PLS:
159 playlistFile = new Playlists::PLSPlaylist( path, this );
160 break;
161 case Playlists::M3U:
162 playlistFile = new Playlists::M3UPlaylist( path, this );
163 break;
164 case Playlists::XSPF:
165 playlistFile = new Playlists::XSPFPlaylist( path, this );
166 break;
167 case Playlists::XML:
168 case Playlists::RAM:
169 case Playlists::SMIL:
170 case Playlists::Unknown:
171 // this should not happen since we set the format to XSPF above.
172 return Playlists::PlaylistPtr();
173 }
174 playlistFile->setName( filename );
175 playlistFile->addTracks( tracks );
176 playlistFile->save( true );
177
178 Playlists::PlaylistFilePtr playlistPtr( playlistFile );
179 m_playlists << playlistPtr;
180 //just in case there wasn't one loaded before.
181 m_playlistsLoaded = true;
182
183 Playlists::PlaylistPtr playlist( playlistFile );
184 Q_EMIT playlistAdded( playlist );
185 return playlist;
186 }
187
188 bool
189 PlaylistFileProvider::import( const QUrl &path )
190 {
191 DEBUG_BLOCK
192 if( !path.isValid() )
193 {
194 error() << "path is not valid!";
195 return false;
196 }
197
198 for( Playlists::PlaylistFilePtr playlistFile : m_playlists )
199 {
200 if( !playlistFile )
201 {
202 error() << "Could not cast down.";
203 error() << "m_playlists got corrupted! " << __FILE__ << ":" << __LINE__;
204 continue;
205 }
206 if( playlistFile->uidUrl() == path )
207 {
208 debug() << "Playlist " << path.path() << " was already imported";
209 return false;
210 }
211 }
212
213 debug() << "Importing playlist file " << path;
214 if( path == QUrl::fromLocalFile(Amarok::defaultPlaylistPath()) )
215 {
216 error() << "trying to load saved session playlist at %s" << path.path();
217 return false;
218 }
219
220 Playlists::PlaylistFilePtr playlistFile = Playlists::loadPlaylistFile( path, this );
221 if( !playlistFile )
222 return false;
223
224 m_playlists << playlistFile;
225 //just in case there wasn't one loaded before.
226 m_playlistsLoaded = true;
227
228 Q_EMIT playlistAdded( PlaylistPtr( playlistFile.data() ) );
229 return true;
230 }
231
232 void
233 PlaylistFileProvider::renamePlaylist(PlaylistPtr playlist, const QString &newName )
234 {
235 DEBUG_BLOCK
236 playlist->setName( newName );
237 }
238
239 bool
240 PlaylistFileProvider::deletePlaylists( const Playlists::PlaylistList &playlists )
241 {
242 Playlists::PlaylistFileList playlistFiles;
243 for( Playlists::PlaylistPtr playlist : playlists )
244 {
245 Playlists::PlaylistFilePtr playlistFile =
246 Playlists::PlaylistFilePtr::dynamicCast( playlist );
247 if( !playlistFile.isNull() )
248 playlistFiles << playlistFile;
249 }
250 return deletePlaylistFiles( playlistFiles );
251 }
252
253 bool
254 PlaylistFileProvider::deletePlaylistFiles( Playlists::PlaylistFileList playlistFiles )
255 {
256 for( Playlists::PlaylistFilePtr playlistFile : playlistFiles )
257 {
258 m_playlists.removeAll( playlistFile );
259 loadedPlaylistsConfig().deleteEntry( playlistFile->uidUrl().url() );
260 QFile::remove( playlistFile->uidUrl().path() );
261 Q_EMIT playlistRemoved( Playlists::PlaylistPtr::dynamicCast( playlistFile ) );
262 }
263 loadedPlaylistsConfig().sync();
264
265 return true;
266 }
267
268 void
269 PlaylistFileProvider::loadPlaylists()
270 {
271 if( m_urlsToLoad.isEmpty() )
272 return;
273
274 //arbitrary number of playlists to load during one mainloop run: 5
275 for( int i = 0; i < qMin( m_urlsToLoad.count(), 5 ); i++ )
276 {
277 QUrl url = m_urlsToLoad.takeFirst();
278 QString groups = loadedPlaylistsConfig().readEntry( url.url() );
279 Playlists::PlaylistFilePtr playlist = Playlists::loadPlaylistFile( url, this );
280 if( !playlist )
281 {
282 Amarok::Logger::longMessage(
283 i18n("The playlist file \"%1\" could not be loaded.", url.fileName() ),
284 Amarok::Logger::Error
285 );
286 continue;
287 }
288
289 if( !groups.isEmpty() && playlist->isWritable() )
290 playlist->setGroups( groups.split( QLatin1Char(','), Qt::SkipEmptyParts ) );
291
292 m_playlists << playlist;
293 Q_EMIT playlistAdded( PlaylistPtr( playlist.data() ) );
294 }
295
296 //give the mainloop time to run
297 if( !m_urlsToLoad.isEmpty() )
298 QTimer::singleShot( 0, this, &PlaylistFileProvider::loadPlaylists );
299 }
300
301 void
302 PlaylistFileProvider::saveLater( Playlists::PlaylistFilePtr playlist )
303 {
304 //WARNING: this assumes the playlistfile uses it's m_url for uidUrl
305 if( playlist->uidUrl().isEmpty() )
306 return;
307
308 if( !m_saveLaterPlaylists.contains( playlist ) )
309 m_saveLaterPlaylists << playlist;
310
311 if( !m_saveLaterTimer )
312 {
313 m_saveLaterTimer = new QTimer( this );
314 m_saveLaterTimer->setSingleShot( true );
315 m_saveLaterTimer->setInterval( 0 );
316 connect( m_saveLaterTimer, &QTimer::timeout, this, &PlaylistFileProvider::slotSaveLater );
317 }
318
319 m_saveLaterTimer->start();
320 }
321
322 void
323 PlaylistFileProvider::slotSaveLater() //SLOT
324 {
325 for( Playlists::PlaylistFilePtr playlist : m_saveLaterPlaylists )
326 {
327 playlist->save( true ); //TODO: read relative type when loading
328 }
329
330 m_saveLaterPlaylists.clear();
331 }
332
333 KConfigGroup
334 PlaylistFileProvider::loadedPlaylistsConfig() const
335 {
336 return Amarok::config( QStringLiteral("Loaded Playlist Files") );
337 }
338
339 } //namespace Playlists
340