svcore
1.9
|
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 00002 00003 /* 00004 Sonic Visualiser 00005 An audio file viewer and annotation editor. 00006 Centre for Digital Music, Queen Mary, University of London. 00007 This file copyright 2007 QMUL. 00008 00009 This program is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU General Public License as 00011 published by the Free Software Foundation; either version 2 of the 00012 License, or (at your option) any later version. See the file 00013 COPYING included with this distribution for more information. 00014 */ 00015 00016 #include "PlaylistFileReader.h" 00017 00018 #include <QFile> 00019 #include <QTextStream> 00020 #include <QStringList> 00021 #include <QFileInfo> 00022 #include <QDir> 00023 00024 #include <iostream> 00025 00026 PlaylistFileReader::PlaylistFileReader(QString path) : 00027 m_source(path), 00028 m_file(0) 00029 { 00030 if (!m_source.isAvailable()) { 00031 m_error = QFile::tr("File or URL \"%1\" could not be retrieved") 00032 .arg(path); 00033 return; 00034 } 00035 init(); 00036 } 00037 00038 PlaylistFileReader::PlaylistFileReader(FileSource source) : 00039 m_source(source), 00040 m_file(0) 00041 { 00042 if (!m_source.isAvailable()) { 00043 m_error = QFile::tr("File or URL \"%1\" could not be retrieved") 00044 .arg(source.getLocation()); 00045 return; 00046 } 00047 init(); 00048 } 00049 00050 PlaylistFileReader::~PlaylistFileReader() 00051 { 00052 if (m_file) m_file->close(); 00053 delete m_file; 00054 } 00055 00056 void 00057 PlaylistFileReader::init() 00058 { 00059 if (!m_source.isAvailable()) return; 00060 00061 m_source.waitForData(); 00062 00063 QString filename = m_source.getLocalFilename(); 00064 00065 m_file = new QFile(filename); 00066 bool good = false; 00067 00068 if (!m_file->exists()) { 00069 m_error = QFile::tr("File \"%1\" does not exist") 00070 .arg(m_source.getLocation()); 00071 } else if (!m_file->open(QIODevice::ReadOnly | QIODevice::Text)) { 00072 m_error = QFile::tr("Failed to open file \"%1\"") 00073 .arg(m_source.getLocation()); 00074 } else { 00075 good = true; 00076 } 00077 00078 if (good) { 00079 if (!m_source.isRemote()) { 00080 m_basedir = QFileInfo(filename).dir().canonicalPath(); 00081 } 00082 } 00083 00084 if (!good) { 00085 delete m_file; 00086 m_file = 0; 00087 } 00088 } 00089 00090 bool 00091 PlaylistFileReader::isOK() const 00092 { 00093 return (m_file != 0); 00094 } 00095 00096 QString 00097 PlaylistFileReader::getError() const 00098 { 00099 return m_error; 00100 } 00101 00102 PlaylistFileReader::Playlist 00103 PlaylistFileReader::load() const 00104 { 00105 if (!m_file) return Playlist(); 00106 00107 QTextStream in(m_file); 00108 in.seek(0); 00109 00110 Playlist playlist; 00111 00112 while (!in.atEnd()) { 00113 00114 // cope with old-style Mac line endings (c.f. CSVFileReader) 00115 // as well as DOS/Unix style 00116 00117 QString chunk = in.readLine(); 00118 QStringList lines = chunk.split('\r', QString::SkipEmptyParts); 00119 00120 for (int li = 0; li < lines.size(); ++li) { 00121 00122 QString line = lines[li]; 00123 00124 if (line.startsWith("#")) continue; 00125 00126 // line is expected to be a URL or a file path. If it 00127 // appears to be a local relative file path, then we 00128 // should check whether it can be resolved relative to the 00129 // location of the playlist file and, if so, do so. 00130 00131 if (!FileSource::isRemote(line)) { 00132 if (QFileInfo(line).isRelative() && m_basedir != "") { 00133 QString testpath = QDir(m_basedir).filePath(line); 00134 if (QFileInfo(testpath).exists() && 00135 QFileInfo(testpath).isFile()) { 00136 cerr << "Path \"" << line 00137 << "\" is relative, resolving to \"" 00138 << testpath << "\"" 00139 << endl; 00140 line = testpath; 00141 } 00142 } 00143 } 00144 00145 playlist.push_back(line); 00146 } 00147 } 00148 00149 return playlist; 00150 } 00151 00152 void 00153 PlaylistFileReader::getSupportedExtensions(std::set<QString> &extensions) 00154 { 00155 extensions.insert("m3u"); 00156 }