PageRenderTime 101ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 33ms app.codeStats 1ms

/src/libtomahawk/database/DatabaseCommand_DirMtimes.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 99 lines | 66 code | 15 blank | 18 comment | 6 complexity | 122ae6721032cfe43713cc6c26aac3b7 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_DirMtimes.h"
21
22#include <QSqlQuery>
23
24#include "DatabaseImpl.h"
25#include "utils/Logger.h"
26#include "Source.h"
27
28namespace Tomahawk
29{
30
31void
32DatabaseCommand_DirMtimes::exec( DatabaseImpl* dbi )
33{
34    if( m_update )
35        execUpdate( dbi );
36    else
37        execSelect( dbi );
38}
39
40
41void
42DatabaseCommand_DirMtimes::execSelect( DatabaseImpl* dbi )
43{
44    QMap<QString,unsigned int> mtimes;
45    TomahawkSqlQuery query = dbi->newquery();
46    if( m_prefix.isEmpty() && m_prefixes.isEmpty() )
47    {
48        query.exec( "SELECT name, mtime FROM dirs_scanned" );
49        while( query.next() )
50            mtimes.insert( query.value( 0 ).toString(), query.value( 1 ).toUInt() );
51    }
52    else if( m_prefixes.isEmpty() )
53        execSelectPath( dbi, m_prefix, mtimes );
54    else
55    {
56        if( !m_prefix.isEmpty() )
57            execSelectPath( dbi, m_prefix, mtimes );
58        foreach( QString path, m_prefixes )
59            execSelectPath( dbi, path, mtimes );
60    }
61    emit done( mtimes );
62}
63
64void
65DatabaseCommand_DirMtimes::execSelectPath( DatabaseImpl *dbi, const QDir& path, QMap<QString, unsigned int> &mtimes )
66{
67    TomahawkSqlQuery query = dbi->newquery();
68    query.prepare( QString( "SELECT name, mtime "
69                            "FROM dirs_scanned "
70                            "WHERE name LIKE :prefix" ) );
71
72    query.bindValue( ":prefix", path.canonicalPath() + "%" );
73    query.exec();
74
75    while( query.next() )
76        mtimes.insert( query.value( 0 ).toString(), query.value( 1 ).toUInt() );
77}
78
79
80void
81DatabaseCommand_DirMtimes::execUpdate( DatabaseImpl* dbi )
82{
83    qDebug() << "Saving mtimes...";
84    TomahawkSqlQuery query = dbi->newquery();
85    query.exec( "DELETE FROM dirs_scanned" );
86    query.prepare( "INSERT INTO dirs_scanned(name, mtime) VALUES(?, ?)" );
87
88    foreach( const QString& k, m_tosave.keys() )
89    {
90        query.bindValue( 0, k );
91        query.bindValue( 1, m_tosave.value( k ) );
92        query.exec();
93    }
94
95    qDebug() << "Saved mtimes for" << m_tosave.size() << "dirs.";
96    emit done( QMap< QString, unsigned int >() );
97}
98
99}