File indexing completed on 2024-05-19 04:49:59

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Bart Cerneels <bart.cerneels@kde.org>                             *
0003  * Copyright (c) 2011 Lucas Lira Gomes <x8lucas8x@gmail.com>                            *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #include "KConfigSyncRelStore.h"
0019 
0020 #include "core/support/Amarok.h"
0021 #include "core/support/Debug.h"
0022 #include "playlistmanager/SyncedPlaylist.h"
0023 
0024 #include <KConfigGroup>
0025 
0026 #include <QString>
0027 
0028 using namespace Playlists;
0029 
0030 KConfigSyncRelStore::KConfigSyncRelStore()
0031 {
0032     DEBUG_BLOCK
0033 
0034     foreach( const QString &key, syncedPlaylistsConfig().keyList() )
0035     {
0036         QUrl masterUrl( key );
0037 
0038         m_syncMasterMap.insert( masterUrl, SyncedPlaylistPtr() );
0039 
0040         foreach( const QString &value, syncedPlaylistsConfig().readEntry( key ).split( QLatin1Char(',') ) )
0041         {
0042             m_syncSlaveMap.insert( QUrl( value ), masterUrl );
0043         }
0044     }
0045 }
0046 
0047 KConfigSyncRelStore::~KConfigSyncRelStore()
0048 {
0049 }
0050 
0051 bool
0052 KConfigSyncRelStore::hasToSync( Playlists::PlaylistPtr master, Playlists::PlaylistPtr slave ) const
0053 {
0054     return m_syncSlaveMap.values( slave->uidUrl() ).contains( master->uidUrl() );
0055 }
0056 
0057 SyncedPlaylistPtr
0058 KConfigSyncRelStore::asSyncedPlaylist( const PlaylistPtr playlist )
0059 {
0060     DEBUG_BLOCK
0061 
0062     debug() << QStringLiteral("UIDurl: %1").arg( playlist->uidUrl().url() );
0063 
0064     SyncedPlaylistPtr syncedPlaylist;
0065 
0066     if( m_syncMasterMap.keys().contains( playlist->uidUrl() ) )
0067     {
0068         syncedPlaylist = m_syncMasterMap.value( playlist->uidUrl() );
0069 
0070         if( syncedPlaylist )
0071             syncedPlaylist->addPlaylist( playlist );
0072         else
0073         {
0074             syncedPlaylist = createSyncedPlaylist( playlist );
0075             m_syncMasterMap.insert( playlist->uidUrl(), syncedPlaylist );
0076         }
0077     }
0078     else if( m_syncSlaveMap.keys().contains( playlist->uidUrl() ) )
0079     {
0080          syncedPlaylist = m_syncMasterMap.value( m_syncSlaveMap.value( playlist->uidUrl() ) );
0081 
0082          if( syncedPlaylist )
0083             syncedPlaylist->addPlaylist( playlist );
0084     }
0085 
0086     return syncedPlaylist;
0087 }
0088 
0089 inline KConfigGroup
0090 KConfigSyncRelStore::syncedPlaylistsConfig() const
0091 {
0092     return Amarok::config( QStringLiteral("Synchronized Playlists") );
0093 }
0094 
0095 void
0096 KConfigSyncRelStore::addSync( const PlaylistPtr master, const PlaylistPtr slave )
0097 {
0098     QUrl masterUrl( master->uidUrl() );
0099 
0100     if ( m_syncMasterMap.contains( masterUrl ) )
0101         m_syncSlaveMap.insert( slave->uidUrl(), masterUrl );
0102     else
0103     {
0104         m_syncMasterMap.insert( masterUrl, SyncedPlaylistPtr() );
0105         m_syncSlaveMap.insert( slave->uidUrl(), masterUrl );
0106     }
0107 
0108     QList<QString> slaveUrlStringList;
0109 
0110     foreach( const QUrl &slaveUrl, m_syncSlaveMap.keys() )
0111     {
0112         if( m_syncSlaveMap.value( slaveUrl ) == masterUrl )
0113             slaveUrlStringList.append( slaveUrl.url() );
0114     }
0115 
0116     syncedPlaylistsConfig().writeEntry( masterUrl.url(), slaveUrlStringList );
0117 }