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

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
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) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "SqlPlaylistGroup.h"
0018 
0019 #include "core-impl/storage/StorageManager.h"
0020 #include "core/support/Debug.h"
0021 #include <core/storage/SqlStorage.h>
0022 
0023 #include <typeinfo>
0024 
0025 namespace Playlists {
0026 
0027 SqlPlaylistGroup::SqlPlaylistGroup( const QStringList & dbResultRow,
0028                                           const SqlPlaylistGroupPtr &parent,
0029                                           PlaylistProvider *provider )
0030     : m_hasFetchedChildGroups( false )
0031     , m_hasFetchedChildPlaylists( false )
0032     , m_parent( parent )
0033     , m_provider( provider )
0034 {
0035     m_dbId = dbResultRow[0].toInt();
0036     m_name = dbResultRow[2];
0037     m_description = dbResultRow[3];
0038 }
0039 
0040 SqlPlaylistGroup::SqlPlaylistGroup( const QString & name,
0041                                           const SqlPlaylistGroupPtr &parent,
0042                                           PlaylistProvider *provider )
0043     : m_dbId( -1 )
0044     , m_hasFetchedChildGroups( false )
0045     , m_hasFetchedChildPlaylists( false )
0046     , m_name( name )
0047     , m_description( QString() )
0048     , m_parent( parent )
0049     , m_provider( provider )
0050 {}
0051 
0052 SqlPlaylistGroup::~SqlPlaylistGroup()
0053 {
0054     //DEBUG_BLOCK
0055     //debug() << "deleting " << m_name;
0056 }
0057 
0058 void
0059 SqlPlaylistGroup::save()
0060 {
0061     int parentId = 0;
0062     if ( m_parent )
0063         parentId = m_parent->id();
0064 
0065     auto sqlStorage = StorageManager::instance()->sqlStorage();
0066     if( !sqlStorage )
0067         return;
0068 
0069     if ( m_dbId != -1 )
0070     {
0071         //update existing
0072         QString query = QStringLiteral("UPDATE playlist_groups SET parent_id=%1, name='%2', \
0073                 description='%3' WHERE id=%4;");
0074         query = query.arg( QString::number( parentId ), m_name,
0075                            m_description, QString::number( m_dbId ) );
0076         sqlStorage->query( query );
0077     }
0078     else
0079     {
0080         //insert new
0081         QString query = QStringLiteral("INSERT INTO playlist_groups ( parent_id, name, \
0082                 description) VALUES ( %1, '%2', '%3' );");
0083         query = query.arg( QString::number( parentId ), m_name, m_description );
0084         m_dbId = sqlStorage->insert( query, nullptr );
0085     }
0086 }
0087 
0088 void
0089 SqlPlaylistGroup::setName( const QString & name )
0090 {
0091     m_name = name;
0092     save();
0093 }
0094 
0095 void
0096 SqlPlaylistGroup::setDescription( const QString &description )
0097 {
0098     m_description = description;
0099     save();
0100 }
0101 
0102 void
0103 SqlPlaylistGroup::removeFromDb()
0104 {
0105     auto sqlStorage = StorageManager::instance()->sqlStorage();
0106     if( !sqlStorage )
0107         return;
0108 
0109 
0110     QString query = QStringLiteral("DELETE FROM playlist_groups where id=%1;");
0111     query = query.arg( QString::number( m_dbId ) );
0112     QStringList result = sqlStorage->query( query );
0113 }
0114 
0115 void
0116 SqlPlaylistGroup::clear()
0117 {
0118     /* m_childPlaylists, m_childGroups are AmarokSharedPointers, so we should be able to
0119        just clear the list and the playlistptrs will delete themselves
0120     */
0121     m_childGroups.clear();
0122     m_childPlaylists.clear();
0123 
0124     m_hasFetchedChildGroups = false;
0125     m_hasFetchedChildPlaylists = false;
0126 }
0127 
0128 void
0129 SqlPlaylistGroup::setParent( const SqlPlaylistGroupPtr &parent )
0130 {
0131     if( parent )
0132         m_parent = SqlPlaylistGroupPtr::staticCast( parent );
0133     else
0134         debug() << "You have to create the parent first before " << name() <<
0135             " can be added to it";
0136     save();
0137 }
0138 
0139 SqlPlaylistGroupList
0140 SqlPlaylistGroup::childSqlGroups() const
0141 {
0142     auto sqlStorage = StorageManager::instance()->sqlStorage();
0143     if( !sqlStorage )
0144         return SqlPlaylistGroupList();
0145 
0146     if ( !m_hasFetchedChildGroups )
0147     {
0148         QString query = QStringLiteral("SELECT id, parent_id, name, description FROM \
0149                 playlist_groups where parent_id=%1 ORDER BY name;");
0150         query = query.arg( QString::number( m_dbId ) );
0151         QStringList result = sqlStorage->query( query );
0152 
0153         int resultRows = result.count() / 4;
0154 
0155         for( int i = 0; i < resultRows; i++ )
0156         {
0157             QStringList row = result.mid( i*4, 4 );
0158             SqlPlaylistGroup* mutableThis =
0159                     const_cast<SqlPlaylistGroup*>( this );
0160             m_childGroups << SqlPlaylistGroupPtr(
0161                 new SqlPlaylistGroup( row, SqlPlaylistGroupPtr( mutableThis ), m_provider )
0162             );
0163         }
0164 
0165         m_hasFetchedChildGroups = true;
0166     }
0167 
0168     return m_childGroups;
0169 }
0170 
0171 SqlPlaylistList
0172 SqlPlaylistGroup::childSqlPlaylists() const
0173 {
0174     auto sqlStorage = StorageManager::instance()->sqlStorage();
0175     if( !sqlStorage )
0176         return SqlPlaylistList();
0177 
0178     if ( !m_hasFetchedChildPlaylists )
0179     {
0180         QString query = QStringLiteral("SELECT id, parent_id, name, urlid FROM \
0181                 playlists where parent_id=%1 ORDER BY name;");
0182         query = query.arg( QString::number( m_dbId ) );
0183         QStringList result = sqlStorage->query( query );
0184 
0185         int resultRows = result.count() / 4;
0186 
0187         for( int i = 0; i < resultRows; i++ )
0188         {
0189             QStringList row = result.mid( i*4, 4 );
0190             SqlPlaylistGroup* mutableThis =
0191                     const_cast<SqlPlaylistGroup*>( this );
0192             m_childPlaylists << SqlPlaylistPtr(
0193                     new SqlPlaylist(
0194                                 row,
0195                                 SqlPlaylistGroupPtr( mutableThis ),
0196                                 m_provider
0197                     )
0198             );
0199         }
0200         m_hasFetchedChildPlaylists = true;
0201     }
0202     return m_childPlaylists;
0203 }
0204 
0205 SqlPlaylistGroupList
0206 SqlPlaylistGroup::allChildGroups() const
0207 {
0208     SqlPlaylistGroupList groups;
0209     groups << childSqlGroups();
0210     foreach( SqlPlaylistGroupPtr childGroup, groups )
0211     {
0212         groups << childGroup->allChildGroups();
0213     }
0214     return groups;
0215 }
0216 
0217 SqlPlaylistList
0218 SqlPlaylistGroup::allChildPlaylists() const
0219 {
0220     SqlPlaylistList playlists;
0221     playlists << childSqlPlaylists();
0222     foreach( SqlPlaylistGroupPtr childGroup, childSqlGroups() )
0223     {
0224         playlists << childGroup->allChildPlaylists();
0225     }
0226     return playlists;
0227 }
0228 
0229 } //namespace Playlists
0230