PageRenderTime 120ms CodeModel.GetById 98ms app.highlight 9ms RepoModel.GetById 11ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_FileMTimes.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 89 lines | 58 code | 12 blank | 19 comment | 5 complexity | cbcca59dc72e21827a60e6cb07d5f1da 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 *   Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
 5 *
 6 *   Tomahawk is free software: you can redistribute it and/or modify
 7 *   it under the terms of the GNU General Public License as published by
 8 *   the Free Software Foundation, either version 3 of the License, or
 9 *   (at your option) any later version.
10 *
11 *   Tomahawk is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *   GNU General Public License for more details.
15 *
16 *   You should have received a copy of the GNU General Public License
17 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "DatabaseCommand_FileMTimes.h"
21
22#include <QSqlQuery>
23
24#include "DatabaseImpl.h"
25#include "utils/Logger.h"
26#include "Source.h"
27
28
29namespace Tomahawk
30{
31
32void
33DatabaseCommand_FileMtimes::exec( DatabaseImpl* dbi )
34{
35    execSelect( dbi );
36}
37
38
39void
40DatabaseCommand_FileMtimes::execSelect( DatabaseImpl* dbi )
41{
42    qDebug() << Q_FUNC_INFO;
43    //FIXME: If ever needed for a non-local source this will have to be fixed/updated
44    QMap< QString, QMap< unsigned int, unsigned int > > mtimes;
45    TomahawkSqlQuery query = dbi->newquery();
46    if( m_prefix.isEmpty() && m_prefixes.isEmpty() )
47    {
48        QString limit( m_checkonly ? QString( "LIMIT 1" ) : QString() );
49        query.exec( QString( "SELECT url, id, mtime FROM file WHERE source IS NULL %1" ).arg( limit ) );
50        while( query.next() )
51        {
52            QMap< unsigned int, unsigned int > map;
53            map.insert( query.value( 1 ).toUInt(), query.value( 2 ).toUInt() );
54            mtimes.insert( query.value( 0 ).toString(), map );
55        }
56    }
57    else if( m_prefixes.isEmpty() )
58        execSelectPath( dbi, m_prefix, mtimes );
59    else
60    {
61        if( !m_prefix.isEmpty() )
62            execSelectPath( dbi, m_prefix, mtimes );
63        foreach( QString path, m_prefixes )
64            execSelectPath( dbi, path, mtimes );
65    }
66    emit done( mtimes );
67}
68
69void
70DatabaseCommand_FileMtimes::execSelectPath( DatabaseImpl *dbi, const QDir& path, QMap<QString, QMap< unsigned int, unsigned int > > &mtimes )
71{
72    TomahawkSqlQuery query = dbi->newquery();
73    query.prepare( QString( "SELECT url, id, mtime "
74                            "FROM file "
75                            "WHERE source IS NULL "
76                            "AND url LIKE :prefix" ) );
77
78    query.bindValue( ":prefix", "file://" + path.canonicalPath() + "%" );
79    query.exec();
80
81    while( query.next() )
82    {
83        QMap< unsigned int, unsigned int > map;
84        map.insert( query.value( 1 ).toUInt(), query.value( 2 ).toUInt() );
85        mtimes.insert( query.value( 0 ).toString(), map );
86    }
87}
88
89}