1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
/***************************************************************************
* Copyright (C) 2003-2005 Max Howell <max.howell@methylblue.com> *
* (C) 2003-2010 Mark Kretschmann <kretschmann@kde.org> *
* (C) 2005-2007 Alexandre Oliveira <aleprj@gmail.com> *
* (C) 2008 Dan Meltzer <parallelgrapefruit@gmail.com> *
* (C) 2008-2009 Jeff Mitchell <mitchell@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "Directory.h"
#include "collectionscanner/ScanningState.h"
#include "collectionscanner/Track.h"
#include <QDebug>
#include <QString>
#include <QStringList>
#include <QSettings>
#include <QDir>
#include <QFile>
#include <QDateTime>
#include <QFileInfo>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
CollectionScanner::Directory::Directory( const QString &path,
CollectionScanner::ScanningState *state,
bool skip )
: m_ignored( false )
{
m_path = path;
m_rpath = QDir::current().relativeFilePath( path );
m_mtime = QFileInfo( path ).lastModified().toSecsSinceEpoch();
m_skipped = skip;
if( m_skipped )
return;
QDir dir( path );
if( dir.exists( QStringLiteral("fmps_ignore") ) )
{
m_ignored = true;
return;
}
QStringList validImages;
validImages << QStringLiteral("jpg") << QStringLiteral("png") << QStringLiteral("gif") << QStringLiteral("jpeg") << QStringLiteral("bmp") << QStringLiteral("svg") << QStringLiteral("xpm");
QStringList validPlaylists;
validPlaylists << QStringLiteral("m3u") << QStringLiteral("pls") << QStringLiteral("xspf");
// --- check if we were restarted and failed at a file
QStringList badFiles;
if( state->lastDirectory() == path )
{
badFiles << state->badFiles();
QString lastFile = state->lastFile();
if( !lastFile.isEmpty() )
{
badFiles << state->lastFile();
state->setBadFiles( badFiles );
}
}
else
state->setLastDirectory( path );
state->setLastFile( QString() ); // reset so we don't add a leftover file
dir.setFilter( QDir::NoDotAndDotDot | QDir::Files );
QFileInfoList fileInfos = dir.entryInfoList();
for( const auto &fi : fileInfos )
{
if( !fi.exists() )
continue;
const QFileInfo &f = fi.isSymLink() ? QFileInfo( fi.symLinkTarget() ) : fi;
if( badFiles.contains( f.absoluteFilePath() ) )
continue;
const QString suffix = fi.suffix().toLower();
const QString filePath = f.absoluteFilePath();
// -- cover image ?
if( validImages.contains( suffix ) )
m_covers.append( filePath );
// -- playlist ?
else if( validPlaylists.contains( suffix ) )
m_playlists.append( CollectionScanner::Playlist( filePath ) );
// -- audio track ?
else
{
// remember the last file before it get's dangerous. Before starting taglib
state->setLastFile( f.absoluteFilePath() );
CollectionScanner::Track *newTrack = new CollectionScanner::Track( filePath, this );
if( newTrack->isValid() )
m_tracks.append( newTrack );
else
delete newTrack;
}
}
}
CollectionScanner::Directory::Directory( QXmlStreamReader *reader )
: m_mtime( 0 )
, m_skipped( false )
, m_ignored( false )
{
while (!reader->atEnd()) {
reader->readNext();
if( reader->isStartElement() )
{
QStringView name = reader->name();
if( name == QLatin1String("path") )
m_path = reader->readElementText(QXmlStreamReader::SkipChildElements);
else if( name == QLatin1String("rpath") )
m_rpath = reader->readElementText(QXmlStreamReader::SkipChildElements);
else if( name == QLatin1String("mtime") )
m_mtime = reader->readElementText(QXmlStreamReader::SkipChildElements).toULongLong();
else if( name == QLatin1String("cover") )
m_covers.append(reader->readElementText(QXmlStreamReader::SkipChildElements));
else if( name == QLatin1String("skipped") )
{
m_skipped = true;
reader->skipCurrentElement();
}
else if( name == QLatin1String("ignored") )
{
m_ignored = true;
reader->skipCurrentElement();
}
else if( name == QLatin1String("track") )
m_tracks.append( new CollectionScanner::Track( reader, this ) );
else if( name == QLatin1String("playlist") )
m_playlists.append( CollectionScanner::Playlist( reader ) );
else
{
qDebug() << "Unexpected xml start element"<<name<<"in input";
reader->skipCurrentElement();
}
}
else if( reader->isEndElement() )
{
break;
}
}
}
CollectionScanner::Directory::~Directory()
{
qDeleteAll( m_tracks );
}
QString
CollectionScanner::Directory::path() const
{
return m_path;
}
QString
CollectionScanner::Directory::rpath() const
{
return m_rpath;
}
quint64
CollectionScanner::Directory::mtime() const
{
return m_mtime;
}
bool
CollectionScanner::Directory::isSkipped() const
{
return m_skipped;
}
const QStringList&
CollectionScanner::Directory::covers() const
{
return m_covers;
}
const QList<CollectionScanner::Track *>&
CollectionScanner::Directory::tracks() const
{
return m_tracks;
}
const QList<CollectionScanner::Playlist>&
CollectionScanner::Directory::playlists() const
{
return m_playlists;
}
void
CollectionScanner::Directory::toXml( QXmlStreamWriter *writer ) const
{
writer->writeTextElement( QStringLiteral("path"), m_path );
writer->writeTextElement( QStringLiteral("rpath"), m_rpath );
writer->writeTextElement( QStringLiteral("mtime"), QString::number( m_mtime ) );
if( m_skipped )
writer->writeEmptyElement( QStringLiteral("skipped") );
if( m_ignored )
writer->writeEmptyElement( QStringLiteral("ignored") );
for( const auto &cover : m_covers )
{
writer->writeTextElement( QStringLiteral("cover"), cover );
}
for( const auto &track : m_tracks )
{
writer->writeStartElement( QStringLiteral("track") );
track->toXml( writer );
writer->writeEndElement();
}
for( const auto &playlist : m_playlists )
{
writer->writeStartElement( QStringLiteral("playlist") );
playlist.toXml( writer );
writer->writeEndElement();
}
}
|