/src/libtomahawk/database/DatabaseCommand_DirMtimes.cpp

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