File indexing completed on 2024-05-12 16:18:06

0001 /***************************************************************************
0002  *   Copyright (C) 2010 Ralf Engels <ralf-engels@gmx.de>                  *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0018  ***************************************************************************/
0019 
0020 #include "BatchFile.h"
0021 
0022 #include "Version.h"  // for AMAROK_VERSION
0023 
0024 #include <QFile>
0025 #include <QDateTime>
0026 #include <QXmlStreamReader>
0027 #include <QXmlStreamWriter>
0028 
0029 #include <QDebug>
0030 CollectionScanner::BatchFile::BatchFile()
0031 {
0032 }
0033 
0034 CollectionScanner::BatchFile::BatchFile( const QString &batchPath )
0035 {
0036     QFile batchFile( batchPath );
0037 
0038     if( !batchFile.exists() ||
0039         !batchFile.open( QIODevice::ReadOnly ) )
0040         return;
0041 
0042     QString path;
0043     uint mtime = 0;
0044     bool haveMtime = false;
0045     QXmlStreamReader reader( &batchFile );
0046 
0047     // very simple parser
0048     while (!reader.atEnd()) {
0049         reader.readNext();
0050 
0051         if( reader.isStartElement() )
0052         {
0053             QStringRef name = reader.name();
0054 
0055             if( name == QLatin1String("scanner") )
0056             {
0057                 ; // just recurse into the element
0058             }
0059             else if( name == QLatin1String("directory") )
0060             {
0061                 path.clear();
0062                 mtime = 0;
0063                 haveMtime = false;
0064             }
0065             else if( name == QLatin1String("path") )
0066                 path = reader.readElementText(QXmlStreamReader::SkipChildElements);
0067             else if( name == QLatin1String("mtime") )
0068             {
0069                 mtime = reader.readElementText(QXmlStreamReader::SkipChildElements).toUInt();
0070                 haveMtime = true;
0071             }
0072             else
0073             {
0074                 reader.skipCurrentElement();
0075             }
0076         }
0077         else if( reader.isEndElement() )
0078         {
0079             QStringRef name = reader.name();
0080             if( name == QLatin1String("directory") )
0081             {
0082                 if( !path.isEmpty() )
0083                 {
0084                     if( haveMtime )
0085                         m_timeDefinitions.append( TimeDefinition( path, mtime ) );
0086                     else
0087                         m_directories.append( path );
0088                 }
0089             }
0090         }
0091     }
0092 
0093 }
0094 const QStringList&
0095 CollectionScanner::BatchFile::directories() const
0096 {
0097     return m_directories;
0098 }
0099 
0100 void
0101 CollectionScanner::BatchFile::setDirectories( const QStringList &value )
0102 {
0103     m_directories = value;
0104 }
0105 
0106 const QList<CollectionScanner::BatchFile::TimeDefinition>&
0107 CollectionScanner::BatchFile::timeDefinitions() const
0108 {
0109     return m_timeDefinitions;
0110 }
0111 
0112 void
0113 CollectionScanner::BatchFile::setTimeDefinitions( const QList<TimeDefinition> &value )
0114 {
0115     m_timeDefinitions = value;
0116 }
0117 
0118 bool
0119 CollectionScanner::BatchFile::write( const QString &batchPath )
0120 {
0121     QFile batchFile( batchPath );
0122     if( !batchFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
0123         return false;
0124 
0125     QXmlStreamWriter writer( &batchFile );
0126     writer.setAutoFormatting( true );
0127 
0128     writer.writeStartDocument();
0129     writer.writeStartElement( QStringLiteral("scanner") );
0130     writer.writeComment(QStringLiteral("Batch file for amarokcollectionscanner ") + QLatin1String(AMAROK_VERSION) + QStringLiteral(" created on ") + QDateTime::currentDateTime().toString());
0131 
0132     foreach( const QString &dir, m_directories )
0133     {
0134         writer.writeStartElement( QStringLiteral("directory") );
0135         writer.writeTextElement( QStringLiteral("path"), dir );
0136         writer.writeEndElement();
0137     }
0138 
0139     foreach( const TimeDefinition &pair, m_timeDefinitions )
0140     {
0141         QString path( pair.first );
0142         uint mtime = pair.second;
0143 
0144         writer.writeStartElement( QStringLiteral("directory") );
0145         writer.writeTextElement( QStringLiteral("path"), path );
0146         // note: some file systems return an mtime of 0
0147         writer.writeTextElement( QStringLiteral("mtime"), QString::number( mtime ) );
0148         writer.writeEndElement();
0149     }
0150 
0151     writer.writeEndElement();
0152     writer.writeEndDocument();
0153 
0154     return true;
0155 }