Chris@208: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@208: 
Chris@208: /*
Chris@208:     Sonic Visualiser
Chris@208:     An audio file viewer and annotation editor.
Chris@208:     Centre for Digital Music, Queen Mary, University of London.
Chris@208:     This file copyright 2007 QMUL.
Chris@208:     
Chris@208:     This program is free software; you can redistribute it and/or
Chris@208:     modify it under the terms of the GNU General Public License as
Chris@208:     published by the Free Software Foundation; either version 2 of the
Chris@208:     License, or (at your option) any later version.  See the file
Chris@208:     COPYING included with this distribution for more information.
Chris@208: */
Chris@208: 
Chris@208: #ifndef _REMOTE_FILE_H_
Chris@208: #define _REMOTE_FILE_H_
Chris@208: 
Chris@208: #include <QUrl>
Chris@208: #include <QMutex>
Chris@208: #include <QString>
Chris@210: #include <QTimer>
Chris@208: 
Chris@304: #include <map>
Chris@304: 
Chris@208: class QFtp;
Chris@208: class QHttp;
Chris@208: class QFile;
Chris@208: class QProgressDialog;
Chris@210: class QHttpResponseHeader;
Chris@208: 
Chris@317: class FileSource : public QObject
Chris@208: {
Chris@208:     Q_OBJECT
Chris@208: 
Chris@208: public:
Chris@317:     FileSource(QString fileOrUrl, bool showProgress = true);
Chris@317:     FileSource(QUrl url, bool showProgress = true);
Chris@317:     FileSource(const FileSource &);
Chris@316: 
Chris@317:     virtual ~FileSource();
Chris@208: 
Chris@210:     bool isAvailable();
Chris@210: 
Chris@316:     void waitForStatus();
Chris@316:     void waitForData();
Chris@316: 
Chris@316:     void setLeaveLocalFile(bool leave);
Chris@208: 
Chris@208:     bool isOK() const;
Chris@208:     bool isDone() const;
Chris@316:     bool isRemote() const;
Chris@210: 
Chris@316:     QString getLocation() const;
Chris@316:     QString getLocalFilename() const;
Chris@315:     QString getContentType() const;
Chris@316:     QString getExtension() const;
Chris@315: 
Chris@208:     QString getErrorString() const;
Chris@208: 
Chris@304:     static bool isRemote(QString fileOrUrl);
Chris@208:     static bool canHandleScheme(QUrl url);
Chris@208: 
Chris@208: signals:
Chris@208:     void progress(int percent);
Chris@208:     void ready();
Chris@208: 
Chris@208: protected slots:
Chris@208:     void dataReadProgress(int done, int total);
Chris@214:     void httpResponseHeaderReceived(const QHttpResponseHeader &resp);
Chris@214:     void ftpCommandFinished(int, bool);
Chris@208:     void dataTransferProgress(qint64 done, qint64 total);
Chris@208:     void done(bool error);
Chris@210:     void showProgressDialog();
Chris@210:     void cancelled();
Chris@208: 
Chris@208: protected:
Chris@317:     FileSource &operator=(const FileSource &); // not provided
Chris@316: 
Chris@304:     QUrl m_url;
Chris@208:     QFtp *m_ftp;
Chris@208:     QHttp *m_http;
Chris@208:     QFile *m_localFile;
Chris@208:     QString m_localFilename;
Chris@208:     QString m_errorString;
Chris@315:     QString m_contentType;
Chris@208:     bool m_ok;
Chris@210:     int m_lastStatus;
Chris@316:     bool m_remote;
Chris@208:     bool m_done;
Chris@316:     bool m_leaveLocalFile;
Chris@208:     QProgressDialog *m_progressDialog;
Chris@210:     QTimer m_progressShowTimer;
Chris@208: 
Chris@304:     typedef std::map<QUrl, int> RemoteRefCountMap;
Chris@304:     typedef std::map<QUrl, QString> RemoteLocalMap;
Chris@304:     static RemoteRefCountMap m_refCountMap;
Chris@304:     static RemoteLocalMap m_remoteLocalMap;
Chris@304:     static QMutex m_mapMutex;
Chris@316:     bool m_refCounted;
Chris@316: 
Chris@316:     void init(bool showProgress);
Chris@316:     void initHttp();
Chris@316:     void initFtp();
Chris@304: 
Chris@211:     void cleanup();
Chris@211: 
Chris@316:     // Create a local file for m_url.  If it already existed, return true.
Chris@316:     // The local filename is stored in m_localFilename.
Chris@316:     bool createCacheFile();
Chris@316:     void deleteCacheFile();
Chris@208: 
Chris@208:     static QMutex m_fileCreationMutex;
Chris@208:     static int m_count;
Chris@208: };
Chris@208: 
Chris@208: #endif