File indexing completed on 2025-01-19 04:24:25

0001 /*
0002     Copyright (C) 2009 Dan Meltzer <parallelgrapefruit@gmail.com>
0003 
0004     This program is free software: you can redistribute it and/or modify
0005     it under the terms of the GNU General Public License as published by
0006     the Free Software Foundation, either version 2 of the License, or
0007     (at your option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012     GNU General Public License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <http://www.gnu.org/licenses/>.
0016 
0017 */
0018 
0019 #include "SqlReadLabelCapability.h"
0020 
0021 #include "core-impl/collections/support/CollectionManager.h"
0022 #include "core/support/Debug.h"
0023 #include <core/storage/SqlStorage.h>
0024 
0025 namespace Capabilities
0026 {
0027 
0028 SqlReadLabelCapability::SqlReadLabelCapability( Meta::SqlTrack *track, const QSharedPointer<SqlStorage>& storage )
0029     : ReadLabelCapability()
0030     , m_track( track )
0031     , m_storage( storage )
0032 {
0033     //TODO: Update cached labels when new labels are added.
0034     fetchLabels();
0035 }
0036 
0037 void
0038 SqlReadLabelCapability::fetch( const QString &uniqueURL )
0039 {
0040     QStringList labels;
0041 
0042     if( !m_storage )
0043     {
0044         debug() << "Could not get SqlStorage, aborting" << Qt::endl;
0045         return;
0046     }
0047 
0048     QString query = "SELECT a.label FROM labels a";
0049     QStringList result;
0050 
0051     if ( !uniqueURL.isEmpty() )
0052     {
0053         query = query + QString( ", urls_labels b, urls c WHERE a.id=b.label AND b.url=c.id AND c.uniqueid=\"%1\"" );
0054         result = m_storage->query( query.arg( m_storage->escape( uniqueURL ) ) );
0055     }
0056     else
0057         result = m_storage->query( query );
0058 
0059     if( !result.isEmpty() )
0060     {
0061         for ( int x = 0; x < result.count(); x++)
0062         {
0063             if ( !labels.contains( result.value(x) ) )
0064                 labels.append( result.value(x) );
0065         }
0066     }
0067 
0068     m_labels = labels;
0069     Q_EMIT labelsFetched( labels );
0070 }
0071 
0072 
0073 void
0074 SqlReadLabelCapability::fetchLabels()
0075 {
0076     fetch( m_track->uidUrl() );
0077 }
0078 
0079 //TODO: This shouldn't be in a track capability
0080 void
0081 SqlReadLabelCapability::fetchGlobalLabels()
0082 {
0083     fetch( QString() );
0084 }
0085 
0086 QStringList
0087 SqlReadLabelCapability::labels()
0088 {
0089     return m_labels;
0090 }
0091 
0092 }
0093