svcore  1.9
FileSource.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2007 QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef _FILE_SOURCE_H_
17 #define _FILE_SOURCE_H_
18 
19 #include <QUrl>
20 #include <QMutex>
21 #include <QString>
22 #include <QTimer>
23 #include <QNetworkReply>
24 
25 #include <map>
26 
27 #include "base/Debug.h"
28 
29 class QFile;
30 class ProgressReporter;
31 
59 class FileSource : public QObject
60 {
61  Q_OBJECT
62 
63 public:
73  FileSource(QString fileOrUrl,
74  ProgressReporter *reporter = 0,
75  QString preferredContentType = "");
76 
85  FileSource(QUrl url, ProgressReporter *reporter = 0);
86 
87  FileSource(const FileSource &);
88 
89  virtual ~FileSource();
90 
95  void waitForStatus();
96 
101  void waitForData();
102 
109  bool isOK() const;
110 
116  bool isAvailable();
117 
122  bool isDone() const;
123 
130  bool wasCancelled() const;
131 
135  bool isResource() const;
136 
140  bool isRemote() const;
141 
146  QString getLocation() const;
147 
156  QString getLocalFilename() const;
157 
162  QString getBasename() const;
163 
167  QString getContentType() const;
168 
172  QString getExtension() const;
173 
177  QString getErrorString() const;
178 
184  void setLeaveLocalFile(bool leave);
185 
190  static bool isRemote(QString fileOrUrl);
191 
196  static bool canHandleScheme(QUrl url);
197 
198 signals:
203  void progress(int percent);
204 
210  void statusAvailable();
211 
216  void ready();
217 
218 protected slots:
219  void metaDataChanged();
220  void readyRead();
221  void replyFailed(QNetworkReply::NetworkError);
222  void replyFinished();
223  void downloadProgress(qint64 done, qint64 total);
224  void cancelled();
225 
226 protected:
227  FileSource &operator=(const FileSource &); // not provided
228 
229  QString m_rawFileOrUrl;
230  QUrl m_url;
231  QFile *m_localFile;
232  QNetworkReply *m_reply;
234  QString m_errorString;
235  QString m_contentType;
237  bool m_ok;
241  bool m_remote;
242  bool m_done;
245 
246  typedef std::map<QUrl, int> RemoteRefCountMap;
247  typedef std::map<QUrl, QString> RemoteLocalMap;
250  static QMutex m_mapMutex;
252 
253  void init();
254  void initRemote();
255 
256  void cleanup();
257 
258  // Create a local file for m_url. If it already existed, return true.
259  // The local filename is stored in m_localFilename.
260  bool createCacheFile();
261  void deleteCacheFile();
262 
263  static QMutex m_fileCreationMutex;
264  static int m_count;
265 };
266 
267 #endif
int m_lastStatus
Definition: FileSource.h:239
QString getExtension() const
Return the file extension for this file, if any.
Definition: FileSource.cpp:619
QString m_contentType
Definition: FileSource.h:235
void setLeaveLocalFile(bool leave)
Specify whether any local, cached file should remain on disc after this FileSource has been destroyed...
Definition: FileSource.cpp:559
bool isResource() const
Return true if this FileSource is referring to a QRC resource.
Definition: FileSource.cpp:583
static int m_count
Definition: FileSource.h:264
void metaDataChanged()
Definition: FileSource.cpp:641
QString getLocation() const
Return the location filename or URL as passed to the constructor.
Definition: FileSource.cpp:595
void downloadProgress(qint64 done, qint64 total)
Definition: FileSource.cpp:709
FileSource(QString fileOrUrl, ProgressReporter *reporter=0, QString preferredContentType="")
Construct a FileSource using the given local file path or URL.
Definition: FileSource.cpp:73
QFile * m_localFile
Definition: FileSource.h:231
bool isDone() const
Return true if the entire file has been retrieved and is available.
Definition: FileSource.cpp:571
void statusAvailable()
Emitted when the file's existence has been tested and/or response header received.
bool m_done
Definition: FileSource.h:242
static QMutex m_fileCreationMutex
Definition: FileSource.h:263
QString m_preferredContentType
Definition: FileSource.h:236
void cleanup()
Definition: FileSource.cpp:481
void cancelled()
Definition: FileSource.cpp:716
static RemoteLocalMap m_remoteLocalMap
Definition: FileSource.h:249
void waitForData()
Block on a sub-event-loop until the whole of the data has been retrieved (if it is remote).
Definition: FileSource.cpp:549
QNetworkReply * m_reply
Definition: FileSource.h:232
void progress(int percent)
Emitted during URL retrieval, when the retrieval progress notches up to a new percentage.
void init()
Definition: FileSource.cpp:269
void waitForStatus()
Block on a sub-event-loop until the availability of the file or remote URL is known.
Definition: FileSource.cpp:540
void replyFinished()
Definition: FileSource.cpp:727
bool isOK() const
Return true if the FileSource object is valid and neither error nor cancellation occurred while retri...
Definition: FileSource.cpp:565
virtual ~FileSource()
Definition: FileSource.cpp:256
bool m_refCounted
Definition: FileSource.h:251
bool m_leaveLocalFile
Definition: FileSource.h:243
void initRemote()
Definition: FileSource.cpp:449
std::map< QUrl, QString > RemoteLocalMap
Definition: FileSource.h:247
bool m_remote
Definition: FileSource.h:241
void replyFailed(QNetworkReply::NetworkError)
Definition: FileSource.cpp:773
ProgressReporter * m_reporter
Definition: FileSource.h:244
FileSource & operator=(const FileSource &)
FileSource is a class used to refer to the contents of a file that may be either local or at a remote...
Definition: FileSource.h:59
QString m_errorString
Definition: FileSource.h:234
std::map< QUrl, int > RemoteRefCountMap
Definition: FileSource.h:246
bool isRemote() const
Return true if this FileSource is referring to a remote URL.
Definition: FileSource.cpp:589
QString getErrorString() const
Return an error message, if isOK() is false.
Definition: FileSource.cpp:629
static QMutex m_mapMutex
Definition: FileSource.h:250
QString getLocalFilename() const
Return the name of the local file this FileSource refers to.
Definition: FileSource.cpp:601
QString getBasename() const
Return the base name, i.e.
Definition: FileSource.cpp:607
static RemoteRefCountMap m_refCountMap
Definition: FileSource.h:248
bool wasCancelled() const
Return true if the operation was cancelled by the user through the ProgressReporter interface.
Definition: FileSource.cpp:577
QUrl m_url
Definition: FileSource.h:230
void readyRead()
Definition: FileSource.cpp:635
bool m_resource
Definition: FileSource.h:240
void ready()
Emitted when the entire file data has been retrieved and the local file is complete (if no error has ...
QString m_localFilename
Definition: FileSource.h:233
static bool canHandleScheme(QUrl url)
Return true if FileSource can handle the retrieval scheme for the given URL (or if the URL is for a l...
Definition: FileSource.cpp:513
bool m_cancelled
Definition: FileSource.h:238
QString getContentType() const
Return the MIME content type of this file, if known.
Definition: FileSource.cpp:613
QString m_rawFileOrUrl
Definition: FileSource.h:229
bool createCacheFile()
Definition: FileSource.cpp:843
void deleteCacheFile()
Definition: FileSource.cpp:788
bool isAvailable()
Return true if the file or remote URL exists.
Definition: FileSource.cpp:523