PageRenderTime 121ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 52ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_SetCollectionAttributes.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 88 lines | 51 code | 15 blank | 22 comment | 10 complexity | e81a4d1d3cd0369e3d52483bb1d58b5d MD5 | raw file
 1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 2 *
 3 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
 4 *
 5 *   Tomahawk is free software: you can redistribute it and/or modify
 6 *   it under the terms of the GNU General Public License as published by
 7 *   the Free Software Foundation, either version 3 of the License, or
 8 *   (at your option) any later version.
 9 *
10 *   Tomahawk is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
17 */
18#include "DatabaseCommand_SetCollectionAttributes.h"
19
20#include "DatabaseImpl.h"
21#include "Source.h"
22#include "network/Servent.h"
23#include "SourceList.h"
24
25namespace Tomahawk
26{
27
28DatabaseCommand_SetCollectionAttributes::DatabaseCommand_SetCollectionAttributes( AttributeType type, const QByteArray& id )
29    : DatabaseCommandLoggable( )
30    , m_delete( false )
31    , m_type( type )
32    , m_id( id )
33{
34}
35
36DatabaseCommand_SetCollectionAttributes::DatabaseCommand_SetCollectionAttributes( DatabaseCommand_SetCollectionAttributes::AttributeType type, bool toDelete )
37    : DatabaseCommandLoggable()
38    , m_delete( toDelete )
39    , m_type( type )
40{
41}
42
43
44void
45DatabaseCommand_SetCollectionAttributes::exec( DatabaseImpl *lib )
46{
47    TomahawkSqlQuery query = lib->newquery();
48
49    QString sourceStr;
50    if ( source().isNull() )
51        setSource( SourceList::instance()->getLocal() );
52
53    if ( source().isNull() || source()->isLocal() )
54        sourceStr = "NULL";
55    else
56        sourceStr = QString( "%1" ).arg( source()->id() );
57
58    QString typeStr;
59    if ( m_type == EchonestSongCatalog )
60        typeStr = "echonest_song";
61    else if ( m_type == EchonestArtistCatalog )
62        typeStr = "echonest_artist";
63
64    TomahawkSqlQuery delQuery = lib->newquery();
65    delQuery.exec( QString( "DELETE FROM collection_attributes WHERE id %1" ).arg( source()->isLocal() ? QString("IS NULL") : QString( "= %1" ).arg( source()->id() )));
66
67    if ( m_delete )
68        return;
69
70    QString queryStr = QString( "INSERT INTO collection_attributes ( id, k, v ) VALUES( %1, \"%2\", \"%3\" )" ).arg( sourceStr ).arg( typeStr ).arg( QString::fromUtf8( m_id ) );
71    qDebug() << "Doing query:" << queryStr;
72    query.exec( queryStr );
73}
74
75void
76DatabaseCommand_SetCollectionAttributes::postCommitHook()
77{
78    /*
79    if ( m_type == EchonestSongCatalog ||
80         m_type == EchonestArtistCatalog )
81        Tomahawk::EchonestCatalogSynchronizer::instance()->knownCatalogsChanged();
82    */
83
84    if ( source()->isLocal() )
85        Servent::instance()->triggerDBSync();
86}
87
88}