~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/database/DatabaseCommand_CreatePlaylist.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.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
 
 
19
#include "DatabaseCommand_CreatePlaylist.h"
 
20
 
 
21
#include <QSqlQuery>
 
22
 
 
23
#include "Source.h"
 
24
#include "DatabaseImpl.h"
 
25
#include "TomahawkSqlQuery.h"
 
26
#include "network/Servent.h"
 
27
#include "utils/Logger.h"
 
28
 
 
29
#ifndef ENABLE_HEADLESS
 
30
    #include "ViewManager.h"
 
31
#endif
 
32
 
 
33
using namespace Tomahawk;
 
34
 
 
35
 
 
36
DatabaseCommand_CreatePlaylist::DatabaseCommand_CreatePlaylist( QObject* parent )
 
37
    : DatabaseCommandLoggable( parent )
 
38
    , m_report( true )
 
39
{
 
40
}
 
41
 
 
42
 
 
43
DatabaseCommand_CreatePlaylist::DatabaseCommand_CreatePlaylist( const source_ptr& author,
 
44
                                                                const playlist_ptr& playlist )
 
45
    : DatabaseCommandLoggable( author )
 
46
    , m_playlist( playlist )
 
47
    , m_report( false ) //this ctor used when creating locally, reporting done elsewhere
 
48
{
 
49
}
 
50
 
 
51
DatabaseCommand_CreatePlaylist::~DatabaseCommand_CreatePlaylist()
 
52
{}
 
53
 
 
54
void
 
55
DatabaseCommand_CreatePlaylist::exec( DatabaseImpl* lib )
 
56
{
 
57
    createPlaylist( lib, false );
 
58
}
 
59
 
 
60
QVariant
 
61
DatabaseCommand_CreatePlaylist::playlistV() const
 
62
{
 
63
    if( m_v.isNull() )
 
64
        return QJson::QObjectHelper::qobject2qvariant( (QObject*)m_playlist.data() );
 
65
    else
 
66
        return m_v;
 
67
 }
 
68
 
 
69
 
 
70
void
 
71
DatabaseCommand_CreatePlaylist::postCommitHook()
 
72
{
 
73
    qDebug() << Q_FUNC_INFO;
 
74
 
 
75
    if ( source()->isLocal() )
 
76
        Servent::instance()->triggerDBSync();
 
77
 
 
78
    if ( m_report == false )
 
79
        return;
 
80
 
 
81
    tDebug() << Q_FUNC_INFO << "reporting...";
 
82
    if ( m_playlist.isNull() )
 
83
    {
 
84
        source_ptr src = source();
 
85
#ifndef ENABLE_HEADLESS
 
86
        QMetaObject::invokeMethod( ViewManager::instance(),
 
87
                                   "createPlaylist",
 
88
                                   Qt::BlockingQueuedConnection,
 
89
                                   QGenericArgument( "Tomahawk::source_ptr", (const void*)&src ),
 
90
                                   Q_ARG( QVariant, m_v ) );
 
91
#endif
 
92
    }
 
93
    else
 
94
    {
 
95
        m_playlist->reportCreated( m_playlist );
 
96
    }
 
97
}
 
98
 
 
99
 
 
100
void
 
101
DatabaseCommand_CreatePlaylist::createPlaylist( DatabaseImpl* lib, bool dynamic)
 
102
{
 
103
    Q_ASSERT( !( m_playlist.isNull() && m_v.isNull() ) );
 
104
    Q_ASSERT( !source().isNull() );
 
105
 
 
106
    uint now = 0;
 
107
    if ( m_playlist.isNull() )
 
108
    {
 
109
        now = m_v.toMap()[ "createdon" ].toUInt();
 
110
    }
 
111
    else
 
112
    {
 
113
        now = QDateTime::currentDateTime().toTime_t();
 
114
        m_playlist->setCreatedOn( now );
 
115
    }
 
116
 
 
117
    TomahawkSqlQuery cre = lib->newquery();
 
118
    cre.prepare( "INSERT INTO playlist( guid, source, shared, title, info, creator, lastmodified, dynplaylist, createdOn ) "
 
119
                 "VALUES( :guid, :source, :shared, :title, :info, :creator, :lastmodified, :dynplaylist, :createdOn )" );
 
120
 
 
121
    cre.bindValue( ":source", source()->isLocal() ? QVariant(QVariant::Int) : source()->id() );
 
122
    cre.bindValue( ":dynplaylist", dynamic );
 
123
    cre.bindValue( ":createdOn", now );
 
124
    if ( !m_playlist.isNull() )
 
125
    {
 
126
        cre.bindValue( ":guid", m_playlist->guid() );
 
127
        cre.bindValue( ":shared", m_playlist->shared() );
 
128
        cre.bindValue( ":title", m_playlist->title() );
 
129
        cre.bindValue( ":info", m_playlist->info() );
 
130
        cre.bindValue( ":creator", m_playlist->creator() );
 
131
        cre.bindValue( ":lastmodified", m_playlist->lastmodified() );
 
132
    }
 
133
    else
 
134
    {
 
135
        QVariantMap m = m_v.toMap();
 
136
        cre.bindValue( ":guid", m.value( "guid" ) );
 
137
        cre.bindValue( ":shared", m.value( "shared" ) );
 
138
        cre.bindValue( ":title", m.value( "title" ) );
 
139
        cre.bindValue( ":info", m.value( "info" ) );
 
140
        cre.bindValue( ":creator", m.value( "creator" ) );
 
141
        cre.bindValue( ":lastmodified", m.value( "lastmodified", 0 ) );
 
142
    }
 
143
 
 
144
    cre.exec();
 
145
}