| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2010 Bart Cerneels <[email protected]> * |
| 3 |
* Copyright (c) 2011 Lucas Lira Gomes <[email protected]> * |
| 4 |
* * |
| 5 |
* This program is free software; you can redistribute it and/or modify it under * |
| 6 |
* the terms of the GNU General Public License as published by the Free Software * |
| 7 |
* Foundation; either version 2 of the License, or (at your option) any later * |
| 8 |
* version. * |
| 9 |
* * |
| 10 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 11 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 12 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 13 |
* * |
| 14 |
* You should have received a copy of the GNU General Public License along with * |
| 15 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 16 |
****************************************************************************************/ |
| 17 |
|
| 18 |
#include "KConfigSyncRelStore.h" |
| 19 |
|
| 20 |
#include "core/support/Amarok.h" |
| 21 |
#include "core/support/Debug.h" |
| 22 |
#include "playlistmanager/SyncedPlaylist.h" |
| 23 |
|
| 24 |
#include <KConfigGroup> |
| 25 |
|
| 26 |
#include <QString> |
| 27 |
|
| 28 |
using namespace Playlists; |
| 29 |
|
| 30 |
KConfigSyncRelStore::KConfigSyncRelStore() |
| 31 |
{ |
| 32 |
DEBUG_BLOCK |
| 33 |
|
| 34 |
for( const QString &key : syncedPlaylistsConfig().keyList() ) |
| 35 |
{ |
| 36 |
QUrl masterUrl( key ); |
| 37 |
|
| 38 |
m_syncMasterMap.insert( masterUrl, SyncedPlaylistPtr() ); |
| 39 |
|
| 40 |
for( const QString &value : syncedPlaylistsConfig().readEntry( key ).split( QLatin1Char(',') ) ) |
| 41 |
{ |
| 42 |
m_syncSlaveMap.insert( QUrl( value ), masterUrl ); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
KConfigSyncRelStore::~KConfigSyncRelStore() |
| 48 |
{ |
| 49 |
} |
| 50 |
|
| 51 |
bool |
| 52 |
KConfigSyncRelStore::hasToSync( Playlists::PlaylistPtr master, Playlists::PlaylistPtr slave ) const |
| 53 |
{ |
| 54 |
return m_syncSlaveMap.values( slave->uidUrl() ).contains( master->uidUrl() ); |
| 55 |
} |
| 56 |
|
| 57 |
SyncedPlaylistPtr |
| 58 |
KConfigSyncRelStore::asSyncedPlaylist( const PlaylistPtr playlist ) |
| 59 |
{ |
| 60 |
DEBUG_BLOCK |
| 61 |
|
| 62 |
debug() << QStringLiteral("UIDurl: %1").arg( playlist->uidUrl().url() ); |
| 63 |
|
| 64 |
SyncedPlaylistPtr syncedPlaylist; |
| 65 |
|
| 66 |
if( m_syncMasterMap.keys().contains( playlist->uidUrl() ) ) |
| 67 |
{ |
| 68 |
syncedPlaylist = m_syncMasterMap.value( playlist->uidUrl() ); |
| 69 |
|
| 70 |
if( syncedPlaylist ) |
| 71 |
syncedPlaylist->addPlaylist( playlist ); |
| 72 |
else |
| 73 |
{ |
| 74 |
syncedPlaylist = createSyncedPlaylist( playlist ); |
| 75 |
m_syncMasterMap.insert( playlist->uidUrl(), syncedPlaylist ); |
| 76 |
} |
| 77 |
} |
| 78 |
else if( m_syncSlaveMap.keys().contains( playlist->uidUrl() ) ) |
| 79 |
{ |
| 80 |
syncedPlaylist = m_syncMasterMap.value( m_syncSlaveMap.value( playlist->uidUrl() ) ); |
| 81 |
|
| 82 |
if( syncedPlaylist ) |
| 83 |
syncedPlaylist->addPlaylist( playlist ); |
| 84 |
} |
| 85 |
|
| 86 |
return syncedPlaylist; |
| 87 |
} |
| 88 |
|
| 89 |
inline KConfigGroup |
| 90 |
KConfigSyncRelStore::syncedPlaylistsConfig() const |
| 91 |
{ |
| 92 |
return Amarok::config( QStringLiteral("Synchronized Playlists") ); |
| 93 |
} |
| 94 |
|
| 95 |
void |
| 96 |
KConfigSyncRelStore::addSync( const PlaylistPtr master, const PlaylistPtr slave ) |
| 97 |
{ |
| 98 |
QUrl masterUrl( master->uidUrl() ); |
| 99 |
|
| 100 |
if ( m_syncMasterMap.contains( masterUrl ) ) |
| 101 |
m_syncSlaveMap.insert( slave->uidUrl(), masterUrl ); |
| 102 |
else |
| 103 |
{ |
| 104 |
m_syncMasterMap.insert( masterUrl, SyncedPlaylistPtr() ); |
| 105 |
m_syncSlaveMap.insert( slave->uidUrl(), masterUrl ); |
| 106 |
} |
| 107 |
|
| 108 |
QList<QString> slaveUrlStringList; |
| 109 |
|
| 110 |
for( const QUrl &slaveUrl : m_syncSlaveMap.keys() ) |
| 111 |
{ |
| 112 |
if( m_syncSlaveMap.value( slaveUrl ) == masterUrl ) |
| 113 |
slaveUrlStringList.append( slaveUrl.url() ); |
| 114 |
} |
| 115 |
|
| 116 |
syncedPlaylistsConfig().writeEntry( masterUrl.url(), slaveUrlStringList ); |
| 117 |
} |