PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_PlaybackHistory.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 95 lines | 60 code | 18 blank | 17 comment | 6 complexity | 0436a81f1381911b2fb56f4ff0b43bf1 MD5 | raw file
 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_PlaybackHistory.h"
20
21#include <QSqlQuery>
22
23#include "DatabaseImpl.h"
24#include "SourceList.h"
25
26#include "utils/Logger.h"
27
28
29namespace Tomahawk
30{
31
32void
33DatabaseCommand_PlaybackHistory::exec( DatabaseImpl* dbi )
34{
35    TomahawkSqlQuery query = dbi->newquery();
36    QString whereToken( "WHERE 1" );
37
38    if ( !source().isNull() )
39    {
40        whereToken += QString( " AND source %1" ).arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) );
41    }
42    if ( m_dateFrom.year() > 1900 && m_dateTo.year() > 1900 )
43    {
44        whereToken += QString( " AND playtime >= %1 AND playtime <= %2" )
45                         .arg( QDateTime( m_dateFrom ).toUTC().toTime_t() )
46                         .arg( QDateTime( m_dateTo.addDays( 1 ) ).toUTC().toTime_t() );
47    }
48
49    QString sql = QString(
50            "SELECT track, playtime, secs_played, source "
51            "FROM playback_log "
52            "%1 "
53            "ORDER BY playtime DESC "
54            "%2" ).arg( whereToken )
55                  .arg( m_amount > 0 ? QString( "LIMIT 0, %1" ).arg( m_amount ) : QString() );
56
57    query.prepare( sql );
58    query.exec();
59
60    QList<Tomahawk::track_ptr> tl;
61    QList<Tomahawk::PlaybackLog> logs;
62    while ( query.next() )
63    {
64        TomahawkSqlQuery query_track = dbi->newquery();
65
66        QString sql = QString(
67                "SELECT track.name, artist.name "
68                "FROM track, artist "
69                "WHERE artist.id = track.artist "
70                "AND track.id = %1"
71                ).arg( query.value( 0 ).toUInt() );
72
73        query_track.prepare( sql );
74        query_track.exec();
75
76        if ( query_track.next() )
77        {
78            Tomahawk::track_ptr track = Tomahawk::Track::get( query_track.value( 1 ).toString(), query_track.value( 0 ).toString(), QString() );
79            if ( !track )
80                continue;
81
82            Tomahawk::PlaybackLog log;
83            log.timestamp = query.value( 1 ).toUInt();
84            log.secsPlayed = query.value( 2 ).toUInt();
85            log.source = SourceList::instance()->get( query.value( 3 ).toUInt() );
86
87            logs << log;
88            tl << track;
89        }
90    }
91
92    emit tracks( tl, logs );
93}
94
95}