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 _REMOTE_FILE_H_
00017 #define _REMOTE_FILE_H_
00018 
00019 #include <QUrl>
00020 #include <QMutex>
00021 #include <QString>
00022 #include <QTimer>
00023 
00024 #include <map>
00025 
00026 class QFtp;
00027 class QHttp;
00028 class QFile;
00029 class QProgressDialog;
00030 class QHttpResponseHeader;
00031 class ProgressPrinter;
00032 
00060 class FileSource : public QObject
00061 {
00062     Q_OBJECT
00063 
00064 public:
00065 
00066     enum ShowProgressType {
00067         ProgressNone,
00068         ProgressDialog,
00069         ProgressToConsole
00070     };
00071 
00082     FileSource(QString fileOrUrl,
00083                ShowProgressType progressType = ProgressNone);
00084 
00094     FileSource(QUrl url,
00095                ShowProgressType progressType = ProgressNone);
00096 
00097     FileSource(const FileSource &);
00098 
00099     virtual ~FileSource();
00100 
00105     void waitForStatus();
00106 
00111     void waitForData();
00112 
00119     bool isOK() const;
00120 
00126     bool isAvailable();
00127 
00132     bool isDone() const;
00133 
00137     bool isRemote() const;
00138 
00143     QString getLocation() const;
00144 
00153     QString getLocalFilename() const;
00154 
00158     QString getContentType() const;
00159 
00163     QString getExtension() const;
00164 
00168     QString getErrorString() const;
00169 
00175     void setLeaveLocalFile(bool leave);
00176 
00181     static bool isRemote(QString fileOrUrl);
00182 
00187     static bool canHandleScheme(QUrl url);
00188 
00189 signals:
00194     void progress(int percent);
00195 
00201     void statusAvailable();
00202 
00207     void ready();
00208 
00209 protected slots:
00210     void dataReadProgress(int done, int total);
00211     void httpResponseHeaderReceived(const QHttpResponseHeader &resp);
00212     void ftpCommandFinished(int, bool);
00213     void dataTransferProgress(qint64 done, qint64 total);
00214     void done(bool error);
00215     void showProgressDialog();
00216     void cancelled();
00217 
00218 protected:
00219     FileSource &operator=(const FileSource &); // not provided
00220 
00221     QUrl m_url;
00222     QFtp *m_ftp;
00223     QHttp *m_http;
00224     QFile *m_localFile;
00225     QString m_localFilename;
00226     QString m_errorString;
00227     QString m_contentType;
00228     bool m_ok;
00229     int m_lastStatus;
00230     bool m_remote;
00231     bool m_done;
00232     bool m_leaveLocalFile;
00233     ShowProgressType m_progressType;
00234     ProgressPrinter *m_progressPrinter;
00235     QProgressDialog *m_progressDialog;
00236     QTimer m_progressShowTimer;
00237 
00238     typedef std::map<QUrl, int> RemoteRefCountMap;
00239     typedef std::map<QUrl, QString> RemoteLocalMap;
00240     static RemoteRefCountMap m_refCountMap;
00241     static RemoteLocalMap m_remoteLocalMap;
00242     static QMutex m_mapMutex;
00243     bool m_refCounted;
00244 
00245     void init();
00246     void initHttp();
00247     void initFtp();
00248 
00249     void cleanup();
00250 
00251     // Create a local file for m_url.  If it already existed, return true.
00252     // The local filename is stored in m_localFilename.
00253     bool createCacheFile();
00254     void deleteCacheFile();
00255 
00256     static QMutex m_fileCreationMutex;
00257     static int m_count;
00258 };
00259 
00260 #endif

Generated on Wed Feb 20 15:45:25 2008 for SonicVisualiser by  doxygen 1.5.1