svcore  1.9
FileSource.h
Go to the documentation of this file.
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 #ifndef _FILE_SOURCE_H_
00017 #define _FILE_SOURCE_H_
00018 
00019 #include <QUrl>
00020 #include <QMutex>
00021 #include <QString>
00022 #include <QTimer>
00023 #include <QNetworkReply>
00024 
00025 #include <map>
00026 
00027 #include "base/Debug.h"
00028 
00029 class QFile;
00030 class ProgressReporter;
00031 
00059 class FileSource : public QObject
00060 {
00061     Q_OBJECT
00062 
00063 public:
00073     FileSource(QString fileOrUrl,
00074                ProgressReporter *reporter = 0,
00075                QString preferredContentType = "");
00076 
00085     FileSource(QUrl url, ProgressReporter *reporter = 0);
00086 
00087     FileSource(const FileSource &);
00088 
00089     virtual ~FileSource();
00090 
00095     void waitForStatus();
00096 
00101     void waitForData();
00102 
00109     bool isOK() const;
00110 
00116     bool isAvailable();
00117 
00122     bool isDone() const;
00123 
00130     bool wasCancelled() const;
00131 
00135     bool isResource() const;
00136 
00140     bool isRemote() const;
00141 
00146     QString getLocation() const;
00147 
00156     QString getLocalFilename() const;
00157 
00162     QString getBasename() const;
00163 
00167     QString getContentType() const;
00168 
00172     QString getExtension() const;
00173 
00177     QString getErrorString() const;
00178 
00184     void setLeaveLocalFile(bool leave);
00185 
00190     static bool isRemote(QString fileOrUrl);
00191 
00196     static bool canHandleScheme(QUrl url);
00197 
00198 signals:
00203     void progress(int percent);
00204 
00210     void statusAvailable();
00211 
00216     void ready();
00217 
00218 protected slots:
00219     void metaDataChanged();
00220     void readyRead();
00221     void replyFailed(QNetworkReply::NetworkError);
00222     void replyFinished();
00223     void downloadProgress(qint64 done, qint64 total);
00224     void cancelled();
00225 
00226 protected:
00227     FileSource &operator=(const FileSource &); // not provided
00228 
00229     QString m_rawFileOrUrl;
00230     QUrl m_url;
00231     QFile *m_localFile;
00232     QNetworkReply *m_reply;
00233     QString m_localFilename;
00234     QString m_errorString;
00235     QString m_contentType;
00236     QString m_preferredContentType;
00237     bool m_ok;
00238     bool m_cancelled;
00239     int m_lastStatus;
00240     bool m_resource;
00241     bool m_remote;
00242     bool m_done;
00243     bool m_leaveLocalFile;
00244     ProgressReporter *m_reporter;
00245 
00246     typedef std::map<QUrl, int> RemoteRefCountMap;
00247     typedef std::map<QUrl, QString> RemoteLocalMap;
00248     static RemoteRefCountMap m_refCountMap;
00249     static RemoteLocalMap m_remoteLocalMap;
00250     static QMutex m_mapMutex;
00251     bool m_refCounted;
00252 
00253     void init();
00254     void initRemote();
00255 
00256     void cleanup();
00257 
00258     // Create a local file for m_url.  If it already existed, return true.
00259     // The local filename is stored in m_localFilename.
00260     bool createCacheFile();
00261     void deleteCacheFile();
00262 
00263     static QMutex m_fileCreationMutex;
00264     static int m_count;
00265 };
00266 
00267 #endif