/src/libtomahawk/database/DatabaseCommand_FileMTimes.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 89 lines · 58 code · 12 blank · 19 comment · 7 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. #include "DatabaseCommand_FileMTimes.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_FileMtimes::exec( DatabaseImpl* dbi )
  28. {
  29. execSelect( dbi );
  30. }
  31. void
  32. DatabaseCommand_FileMtimes::execSelect( DatabaseImpl* dbi )
  33. {
  34. qDebug() << Q_FUNC_INFO;
  35. //FIXME: If ever needed for a non-local source this will have to be fixed/updated
  36. QMap< QString, QMap< unsigned int, unsigned int > > mtimes;
  37. TomahawkSqlQuery query = dbi->newquery();
  38. if( m_prefix.isEmpty() && m_prefixes.isEmpty() )
  39. {
  40. QString limit( m_checkonly ? QString( "LIMIT 1" ) : QString() );
  41. query.exec( QString( "SELECT url, id, mtime FROM file WHERE source IS NULL %1" ).arg( limit ) );
  42. while( query.next() )
  43. {
  44. QMap< unsigned int, unsigned int > map;
  45. map.insert( query.value( 1 ).toUInt(), query.value( 2 ).toUInt() );
  46. mtimes.insert( query.value( 0 ).toString(), map );
  47. }
  48. }
  49. else if( m_prefixes.isEmpty() )
  50. execSelectPath( dbi, m_prefix, mtimes );
  51. else
  52. {
  53. if( !m_prefix.isEmpty() )
  54. execSelectPath( dbi, m_prefix, mtimes );
  55. foreach( QString path, m_prefixes )
  56. execSelectPath( dbi, path, mtimes );
  57. }
  58. emit done( mtimes );
  59. }
  60. void
  61. DatabaseCommand_FileMtimes::execSelectPath( DatabaseImpl *dbi, const QDir& path, QMap<QString, QMap< unsigned int, unsigned int > > &mtimes )
  62. {
  63. TomahawkSqlQuery query = dbi->newquery();
  64. query.prepare( QString( "SELECT url, id, mtime "
  65. "FROM file "
  66. "WHERE source IS NULL "
  67. "AND url LIKE :prefix" ) );
  68. query.bindValue( ":prefix", "file://" + path.canonicalPath() + "%" );
  69. query.exec();
  70. while( query.next() )
  71. {
  72. QMap< unsigned int, unsigned int > map;
  73. map.insert( query.value( 1 ).toUInt(), query.value( 2 ).toUInt() );
  74. mtimes.insert( query.value( 0 ).toString(), map );
  75. }
  76. }
  77. }