File indexing completed on 2024-05-05 04:47:21
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 "BookmarkGroup.h" 0018 0019 #include "AmarokUrl.h" 0020 #include "core-impl/storage/StorageManager.h" 0021 #include "core/support/Debug.h" 0022 #include <core/storage/SqlStorage.h> 0023 0024 #include <typeinfo> 0025 0026 BookmarkGroup::BookmarkGroup( const QStringList & dbResultRow, const BookmarkGroupPtr &parent ) 0027 : BookmarkViewItem() 0028 , m_parent( parent ) 0029 , m_customType() 0030 , m_hasFetchedChildGroups( false ) 0031 , m_hasFetchedChildPlaylists( false ) 0032 { 0033 m_dbId = dbResultRow[0].toInt(); 0034 m_name = dbResultRow[2]; 0035 m_description = dbResultRow[3]; 0036 } 0037 0038 BookmarkGroup::BookmarkGroup( const QString & name, const BookmarkGroupPtr &parent ) 0039 : BookmarkViewItem() 0040 , m_dbId( -1 ) 0041 , m_parent( parent ) 0042 , m_name( name ) 0043 , m_description() 0044 , m_customType() 0045 , m_hasFetchedChildGroups( false ) 0046 , m_hasFetchedChildPlaylists( false ) 0047 { 0048 } 0049 0050 BookmarkGroup::BookmarkGroup( const QString &name, const QString &customType ) 0051 : BookmarkViewItem() 0052 { 0053 DEBUG_BLOCK; 0054 0055 m_parent = BookmarkGroupPtr(); 0056 m_hasFetchedChildGroups = false; 0057 m_hasFetchedChildPlaylists = false; 0058 m_customType = customType; 0059 0060 debug() << "custom type: " << customType << " named '" << name << "'"; 0061 //check if this custom group already exists and if so, just load that data. 0062 QString query = QStringLiteral("SELECT id, parent_id, name, description FROM bookmark_groups where custom='%1';"); 0063 query = query.arg( customType ); 0064 QStringList result = StorageManager::instance()->sqlStorage()->query( query ); 0065 0066 if ( result.count() == 4 ) 0067 { 0068 debug() << "already exists, loading..." << result; 0069 m_dbId = result[0].toInt(); 0070 m_name = result[2]; 0071 m_description = result[3]; 0072 debug() << "id: " << m_dbId; 0073 } 0074 else 0075 { 0076 debug() << "creating new"; 0077 //create new and store 0078 m_name = name; 0079 m_dbId = -1; 0080 save(); 0081 } 0082 } 0083 0084 0085 BookmarkGroup::~BookmarkGroup() 0086 { 0087 //DEBUG_BLOCK 0088 //debug() << "deleting " << m_name; 0089 clear(); 0090 } 0091 0092 void BookmarkGroup::save() 0093 { 0094 DEBUG_BLOCK 0095 0096 int parentId = -1; 0097 if ( m_parent ) 0098 parentId = m_parent->id(); 0099 0100 if ( m_dbId != -1 ) { 0101 //update existing 0102 QString query = QStringLiteral("UPDATE bookmark_groups SET parent_id=%1, name='%2', description='%3', custom='%4%' WHERE id=%5;"); 0103 query = query.arg( QString::number( parentId ), m_name, m_description, m_customType, QString::number( m_dbId ) ); 0104 StorageManager::instance()->sqlStorage()->query( query ); 0105 } 0106 else 0107 { 0108 //insert new 0109 QString query = QStringLiteral("INSERT INTO bookmark_groups ( parent_id, name, description, custom) VALUES ( %1, '%2', '%3', '%4' );"); 0110 query = query.arg( QString::number( parentId ), m_name, m_description, m_customType ); 0111 m_dbId = StorageManager::instance()->sqlStorage()->insert( query, nullptr ); 0112 0113 } 0114 } 0115 0116 BookmarkGroupList BookmarkGroup::childGroups() const 0117 { 0118 //DEBUG_BLOCK 0119 if ( !m_hasFetchedChildGroups ) 0120 { 0121 0122 QString query = QStringLiteral("SELECT id, parent_id, name, description FROM bookmark_groups where parent_id=%1 ORDER BY name;"); 0123 query = query.arg( QString::number( m_dbId ) ); 0124 QStringList result = StorageManager::instance()->sqlStorage()->query( query ); 0125 0126 0127 int resultRows = result.count() / 4; 0128 0129 for( int i = 0; i < resultRows; i++ ) 0130 { 0131 QStringList row = result.mid( i*4, 4 ); 0132 BookmarkGroup* mutableThis = const_cast<BookmarkGroup*>( this ); 0133 m_childGroups << BookmarkGroupPtr( new BookmarkGroup( row, BookmarkGroupPtr( mutableThis ) ) ); 0134 } 0135 0136 m_hasFetchedChildGroups = true; 0137 0138 } 0139 0140 return m_childGroups; 0141 } 0142 0143 BookmarkList BookmarkGroup::childBookmarks() const 0144 { 0145 //DEBUG_BLOCK 0146 //debug() << "my name: " << m_name << " my pointer: " << this; 0147 if ( !m_hasFetchedChildPlaylists ) { 0148 QString query = QStringLiteral("SELECT id, parent_id, name, url, description, custom FROM bookmarks where parent_id=%1 ORDER BY name;"); 0149 query = query.arg( QString::number( m_dbId ) ); 0150 QStringList result = StorageManager::instance()->sqlStorage()->query( query ); 0151 0152 //debug() << "Result: " << result; 0153 int resultRows = result.count() / 6; 0154 0155 for( int i = 0; i < resultRows; i++ ) 0156 { 0157 QStringList row = result.mid( i*6, 6 ); 0158 BookmarkGroup* mutableThis = const_cast<BookmarkGroup*>( this ); 0159 m_childBookmarks << AmarokUrlPtr( new AmarokUrl( row, BookmarkGroupPtr( mutableThis ) ) ); 0160 } 0161 m_hasFetchedChildPlaylists = true; 0162 } 0163 0164 return m_childBookmarks; 0165 } 0166 0167 int BookmarkGroup::id() const 0168 { 0169 return m_dbId; 0170 } 0171 0172 QString BookmarkGroup::name() const 0173 { 0174 return m_name; 0175 } 0176 0177 QString BookmarkGroup::description() const 0178 { 0179 return m_description; 0180 } 0181 0182 int BookmarkGroup::childCount() const 0183 { 0184 //DEBUG_BLOCK 0185 return childGroups().count() + childBookmarks().count(); 0186 } 0187 0188 void BookmarkGroup::clear() 0189 { 0190 //DEBUG_BLOCK 0191 //m_childBookmarks, m_childGroups are AmarokSharedPointers, so we should be able to just clear the list 0192 //and the playlistptrs will delete themselves 0193 m_childGroups.clear(); 0194 m_childBookmarks.clear(); 0195 0196 m_hasFetchedChildGroups = false; 0197 m_hasFetchedChildPlaylists = false; 0198 } 0199 0200 void BookmarkGroup::rename(const QString & name) 0201 { 0202 m_name = name; 0203 save(); 0204 } 0205 0206 void BookmarkGroup::setDescription( const QString &description ) 0207 { 0208 m_description = description; 0209 save(); 0210 } 0211 0212 void BookmarkGroup::deleteChild( const BookmarkViewItemPtr &item ) 0213 { 0214 if ( auto group = BookmarkGroupPtr::dynamicCast( item ) ) 0215 { 0216 m_childGroups.removeAll( group ); 0217 } 0218 else if ( auto bookmark = AmarokUrlPtr::dynamicCast( item ) ) 0219 { 0220 m_childBookmarks.removeAll( bookmark ); 0221 } 0222 } 0223 0224 void BookmarkGroup::removeFromDb() 0225 { 0226 DEBUG_BLOCK 0227 0228 foreach( BookmarkGroupPtr group, m_childGroups ) 0229 group->removeFromDb(); 0230 foreach( AmarokUrlPtr bookmark, m_childBookmarks ) 0231 bookmark->removeFromDb(); 0232 0233 QString query = QStringLiteral( "DELETE FROM bookmark_groups where id=%1;").arg( QString::number( m_dbId ) ); 0234 debug() << "query: " << query; 0235 QStringList result = StorageManager::instance()->sqlStorage()->query( query ); 0236 } 0237 0238 void BookmarkGroup::reparent( const BookmarkGroupPtr &parent ) 0239 { 0240 m_parent = parent; 0241 save(); 0242 } 0243 0244