annotate data/fileio/PlaylistFileReader.cpp @ 423:6a96bff0bd59

* Update translations, and add change counter to name of bundle command
author Chris Cannam
date Thu, 12 Jun 2008 10:47:11 +0000
parents d030801113b6
children 5746c559af15
rev   line source
Chris@297 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@297 2
Chris@297 3 /*
Chris@297 4 Sonic Visualiser
Chris@297 5 An audio file viewer and annotation editor.
Chris@297 6 Centre for Digital Music, Queen Mary, University of London.
Chris@297 7 This file copyright 2007 QMUL.
Chris@297 8
Chris@297 9 This program is free software; you can redistribute it and/or
Chris@297 10 modify it under the terms of the GNU General Public License as
Chris@297 11 published by the Free Software Foundation; either version 2 of the
Chris@297 12 License, or (at your option) any later version. See the file
Chris@297 13 COPYING included with this distribution for more information.
Chris@297 14 */
Chris@297 15
Chris@297 16 #include "PlaylistFileReader.h"
Chris@297 17
Chris@297 18 #include <QFile>
Chris@297 19 #include <QTextStream>
Chris@297 20 #include <QStringList>
Chris@297 21
Chris@401 22 PlaylistFileReader::PlaylistFileReader(QString path) :
Chris@401 23 m_source(path),
Chris@401 24 m_file(0)
Chris@297 25 {
Chris@401 26 if (!m_source.isAvailable()) {
Chris@401 27 m_error = QFile::tr("File or URL \"%1\" could not be retrieved")
Chris@401 28 .arg(path);
Chris@401 29 return;
Chris@401 30 }
Chris@401 31 init();
Chris@401 32 }
Chris@401 33
Chris@401 34 PlaylistFileReader::PlaylistFileReader(FileSource source) :
Chris@401 35 m_source(source),
Chris@401 36 m_file(0)
Chris@401 37 {
Chris@401 38 if (!m_source.isAvailable()) {
Chris@401 39 m_error = QFile::tr("File or URL \"%1\" could not be retrieved")
Chris@401 40 .arg(source.getLocation());
Chris@401 41 return;
Chris@401 42 }
Chris@401 43 init();
Chris@401 44 }
Chris@401 45
Chris@401 46 PlaylistFileReader::~PlaylistFileReader()
Chris@401 47 {
Chris@401 48 if (m_file) m_file->close();
Chris@401 49 delete m_file;
Chris@401 50 }
Chris@401 51
Chris@401 52 void
Chris@401 53 PlaylistFileReader::init()
Chris@401 54 {
Chris@401 55 if (!m_source.isAvailable()) return;
Chris@401 56
Chris@401 57 m_source.waitForData();
Chris@401 58
Chris@401 59 m_file = new QFile(m_source.getLocalFilename());
Chris@297 60 bool good = false;
Chris@297 61
Chris@297 62 if (!m_file->exists()) {
Chris@401 63 m_error = QFile::tr("File \"%1\" does not exist")
Chris@401 64 .arg(m_source.getLocation());
Chris@297 65 } else if (!m_file->open(QIODevice::ReadOnly | QIODevice::Text)) {
Chris@401 66 m_error = QFile::tr("Failed to open file \"%1\"")
Chris@401 67 .arg(m_source.getLocation());
Chris@297 68 } else {
Chris@297 69 good = true;
Chris@297 70 }
Chris@297 71
Chris@297 72 if (!good) {
Chris@297 73 delete m_file;
Chris@297 74 m_file = 0;
Chris@297 75 }
Chris@297 76 }
Chris@297 77
Chris@297 78 bool
Chris@297 79 PlaylistFileReader::isOK() const
Chris@297 80 {
Chris@297 81 return (m_file != 0);
Chris@297 82 }
Chris@297 83
Chris@297 84 QString
Chris@297 85 PlaylistFileReader::getError() const
Chris@297 86 {
Chris@297 87 return m_error;
Chris@297 88 }
Chris@297 89
Chris@297 90 PlaylistFileReader::Playlist
Chris@297 91 PlaylistFileReader::load() const
Chris@297 92 {
Chris@297 93 if (!m_file) return Playlist();
Chris@297 94
Chris@297 95 QTextStream in(m_file);
Chris@297 96 in.seek(0);
Chris@297 97
Chris@297 98 Playlist playlist;
Chris@297 99
Chris@297 100 while (!in.atEnd()) {
Chris@297 101
Chris@297 102 // cope with old-style Mac line endings (c.f. CSVFileReader)
Chris@297 103 // as well as DOS/Unix style
Chris@297 104
Chris@297 105 QString chunk = in.readLine();
Chris@297 106 QStringList lines = chunk.split('\r', QString::SkipEmptyParts);
Chris@297 107
Chris@297 108 for (size_t li = 0; li < lines.size(); ++li) {
Chris@297 109
Chris@297 110 QString line = lines[li];
Chris@297 111
Chris@297 112 if (line.startsWith("#")) continue;
Chris@297 113
Chris@297 114 playlist.push_back(line);
Chris@297 115 }
Chris@297 116 }
Chris@297 117
Chris@297 118 return playlist;
Chris@297 119 }
Chris@297 120
Chris@297 121 void
Chris@297 122 PlaylistFileReader::getSupportedExtensions(std::set<QString> &extensions)
Chris@297 123 {
Chris@297 124 extensions.insert("m3u");
Chris@297 125 }