comparison data/fileio/FileSource.h @ 317:c324d410b096

* RemoteFile -> FileSource now it's used all over the place for local files as well.
author Chris Cannam
date Thu, 18 Oct 2007 16:20:26 +0000
parents data/fileio/RemoteFile.h@3a6725f285d6
children 9cdc7a4efde2
comparison
equal deleted inserted replaced
316:3a6725f285d6 317:c324d410b096
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 _REMOTE_FILE_H_
17 #define _REMOTE_FILE_H_
18
19 #include <QUrl>
20 #include <QMutex>
21 #include <QString>
22 #include <QTimer>
23
24 #include <map>
25
26 class QFtp;
27 class QHttp;
28 class QFile;
29 class QProgressDialog;
30 class QHttpResponseHeader;
31
32 class FileSource : public QObject
33 {
34 Q_OBJECT
35
36 public:
37 FileSource(QString fileOrUrl, bool showProgress = true);
38 FileSource(QUrl url, bool showProgress = true);
39 FileSource(const FileSource &);
40
41 virtual ~FileSource();
42
43 bool isAvailable();
44
45 void waitForStatus();
46 void waitForData();
47
48 void setLeaveLocalFile(bool leave);
49
50 bool isOK() const;
51 bool isDone() const;
52 bool isRemote() const;
53
54 QString getLocation() const;
55 QString getLocalFilename() const;
56 QString getContentType() const;
57 QString getExtension() const;
58
59 QString getErrorString() const;
60
61 static bool isRemote(QString fileOrUrl);
62 static bool canHandleScheme(QUrl url);
63
64 signals:
65 void progress(int percent);
66 void ready();
67
68 protected slots:
69 void dataReadProgress(int done, int total);
70 void httpResponseHeaderReceived(const QHttpResponseHeader &resp);
71 void ftpCommandFinished(int, bool);
72 void dataTransferProgress(qint64 done, qint64 total);
73 void done(bool error);
74 void showProgressDialog();
75 void cancelled();
76
77 protected:
78 FileSource &operator=(const FileSource &); // not provided
79
80 QUrl m_url;
81 QFtp *m_ftp;
82 QHttp *m_http;
83 QFile *m_localFile;
84 QString m_localFilename;
85 QString m_errorString;
86 QString m_contentType;
87 bool m_ok;
88 int m_lastStatus;
89 bool m_remote;
90 bool m_done;
91 bool m_leaveLocalFile;
92 QProgressDialog *m_progressDialog;
93 QTimer m_progressShowTimer;
94
95 typedef std::map<QUrl, int> RemoteRefCountMap;
96 typedef std::map<QUrl, QString> RemoteLocalMap;
97 static RemoteRefCountMap m_refCountMap;
98 static RemoteLocalMap m_remoteLocalMap;
99 static QMutex m_mapMutex;
100 bool m_refCounted;
101
102 void init(bool showProgress);
103 void initHttp();
104 void initFtp();
105
106 void cleanup();
107
108 // Create a local file for m_url. If it already existed, return true.
109 // The local filename is stored in m_localFilename.
110 bool createCacheFile();
111 void deleteCacheFile();
112
113 static QMutex m_fileCreationMutex;
114 static int m_count;
115 };
116
117 #endif