1 /****************************************************************************************
2  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
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 "SqlPlaylistGroup.h"
18 
19 #include "core-impl/storage/StorageManager.h"
20 #include "core/support/Debug.h"
21 #include <core/storage/SqlStorage.h>
22 
23 #include <typeinfo>
24 
25 namespace Playlists {
26 
SqlPlaylistGroup(const QStringList & dbResultRow,const SqlPlaylistGroupPtr & parent,PlaylistProvider * provider)27 SqlPlaylistGroup::SqlPlaylistGroup( const QStringList & dbResultRow,
28                                           const SqlPlaylistGroupPtr &parent,
29                                           PlaylistProvider *provider )
30     : m_hasFetchedChildGroups( false )
31     , m_hasFetchedChildPlaylists( false )
32     , m_parent( parent )
33     , m_provider( provider )
34 {
35     m_dbId = dbResultRow[0].toInt();
36     m_name = dbResultRow[2];
37     m_description = dbResultRow[3];
38 }
39 
SqlPlaylistGroup(const QString & name,const SqlPlaylistGroupPtr & parent,PlaylistProvider * provider)40 SqlPlaylistGroup::SqlPlaylistGroup( const QString & name,
41                                           const SqlPlaylistGroupPtr &parent,
42                                           PlaylistProvider *provider )
43     : m_dbId( -1 )
44     , m_hasFetchedChildGroups( false )
45     , m_hasFetchedChildPlaylists( false )
46     , m_name( name )
47     , m_description( QString() )
48     , m_parent( parent )
49     , m_provider( provider )
50 {}
51 
~SqlPlaylistGroup()52 SqlPlaylistGroup::~SqlPlaylistGroup()
53 {
54     //DEBUG_BLOCK
55     //debug() << "deleting " << m_name;
56 }
57 
58 void
save()59 SqlPlaylistGroup::save()
60 {
61     int parentId = 0;
62     if ( m_parent )
63         parentId = m_parent->id();
64 
65     auto sqlStorage = StorageManager::instance()->sqlStorage();
66     if( !sqlStorage )
67         return;
68 
69     if ( m_dbId != -1 )
70     {
71         //update existing
72         QString query = QStringLiteral("UPDATE playlist_groups SET parent_id=%1, name='%2', \
73                 description='%3' WHERE id=%4;");
74         query = query.arg( QString::number( parentId ), m_name,
75                            m_description, QString::number( m_dbId ) );
76         sqlStorage->query( query );
77     }
78     else
79     {
80         //insert new
81         QString query = QStringLiteral("INSERT INTO playlist_groups ( parent_id, name, \
82                 description) VALUES ( %1, '%2', '%3' );");
83         query = query.arg( QString::number( parentId ), m_name, m_description );
84         m_dbId = sqlStorage->insert( query, nullptr );
85     }
86 }
87 
88 void
setName(const QString & name)89 SqlPlaylistGroup::setName( const QString & name )
90 {
91     m_name = name;
92     save();
93 }
94 
95 void
setDescription(const QString & description)96 SqlPlaylistGroup::setDescription( const QString &description )
97 {
98     m_description = description;
99     save();
100 }
101 
102 void
removeFromDb()103 SqlPlaylistGroup::removeFromDb()
104 {
105     auto sqlStorage = StorageManager::instance()->sqlStorage();
106     if( !sqlStorage )
107         return;
108 
109 
110     QString query = QStringLiteral("DELETE FROM playlist_groups where id=%1;");
111     query = query.arg( QString::number( m_dbId ) );
112     QStringList result = sqlStorage->query( query );
113 }
114 
115 void
clear()116 SqlPlaylistGroup::clear()
117 {
118     /* m_childPlaylists, m_childGroups are AmarokSharedPointers, so we should be able to
119        just clear the list and the playlistptrs will delete themselves
120     */
121     m_childGroups.clear();
122     m_childPlaylists.clear();
123 
124     m_hasFetchedChildGroups = false;
125     m_hasFetchedChildPlaylists = false;
126 }
127 
128 void
setParent(const SqlPlaylistGroupPtr & parent)129 SqlPlaylistGroup::setParent( const SqlPlaylistGroupPtr &parent )
130 {
131     if( parent )
132         m_parent = SqlPlaylistGroupPtr::staticCast( parent );
133     else
134         debug() << "You have to create the parent first before " << name() <<
135             " can be added to it";
136     save();
137 }
138 
139 SqlPlaylistGroupList
childSqlGroups() const140 SqlPlaylistGroup::childSqlGroups() const
141 {
142     auto sqlStorage = StorageManager::instance()->sqlStorage();
143     if( !sqlStorage )
144         return SqlPlaylistGroupList();
145 
146     if ( !m_hasFetchedChildGroups )
147     {
148         QString query = QStringLiteral("SELECT id, parent_id, name, description FROM \
149                 playlist_groups where parent_id=%1 ORDER BY name;");
150         query = query.arg( QString::number( m_dbId ) );
151         QStringList result = sqlStorage->query( query );
152 
153         int resultRows = result.count() / 4;
154 
155         for( int i = 0; i < resultRows; i++ )
156         {
157             QStringList row = result.mid( i*4, 4 );
158             SqlPlaylistGroup* mutableThis =
159                     const_cast<SqlPlaylistGroup*>( this );
160             m_childGroups << SqlPlaylistGroupPtr(
161                 new SqlPlaylistGroup( row, SqlPlaylistGroupPtr( mutableThis ), m_provider )
162             );
163         }
164 
165         m_hasFetchedChildGroups = true;
166     }
167 
168     return m_childGroups;
169 }
170 
171 SqlPlaylistList
childSqlPlaylists() const172 SqlPlaylistGroup::childSqlPlaylists() const
173 {
174     auto sqlStorage = StorageManager::instance()->sqlStorage();
175     if( !sqlStorage )
176         return SqlPlaylistList();
177 
178     if ( !m_hasFetchedChildPlaylists )
179     {
180         QString query = QStringLiteral("SELECT id, parent_id, name, urlid FROM \
181                 playlists where parent_id=%1 ORDER BY name;");
182         query = query.arg( QString::number( m_dbId ) );
183         QStringList result = sqlStorage->query( query );
184 
185         int resultRows = result.count() / 4;
186 
187         for( int i = 0; i < resultRows; i++ )
188         {
189             QStringList row = result.mid( i*4, 4 );
190             SqlPlaylistGroup* mutableThis =
191                     const_cast<SqlPlaylistGroup*>( this );
192             m_childPlaylists << SqlPlaylistPtr(
193                     new SqlPlaylist(
194                                 row,
195                                 SqlPlaylistGroupPtr( mutableThis ),
196                                 m_provider
197                     )
198             );
199         }
200         m_hasFetchedChildPlaylists = true;
201     }
202     return m_childPlaylists;
203 }
204 
205 SqlPlaylistGroupList
allChildGroups() const206 SqlPlaylistGroup::allChildGroups() const
207 {
208     SqlPlaylistGroupList groups;
209     groups << childSqlGroups();
210     foreach( SqlPlaylistGroupPtr childGroup, groups )
211     {
212         groups << childGroup->allChildGroups();
213     }
214     return groups;
215 }
216 
217 SqlPlaylistList
allChildPlaylists() const218 SqlPlaylistGroup::allChildPlaylists() const
219 {
220     SqlPlaylistList playlists;
221     playlists << childSqlPlaylists();
222     foreach( SqlPlaylistGroupPtr childGroup, childSqlGroups() )
223     {
224         playlists << childGroup->allChildPlaylists();
225     }
226     return playlists;
227 }
228 
229 } //namespace Playlists
230 
231