ftpmodel.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef FTPMODEL_H
  2. #define FTPMODEL_H
  3. #include <QAbstractListModel>
  4. #include <QFtp>
  5. #include <QList>
  6. #include <QUrlInfo>
  7. #include <QFile>
  8. class FtpModel : public QAbstractListModel
  9. {
  10. Q_OBJECT
  11. Q_PROPERTY(bool loading READ loading WRITE setLoading NOTIFY loadingChanged)
  12. Q_PROPERTY(QString currentPath READ currentPath WRITE setCurrentPath NOTIFY currentPathChanged)
  13. Q_PROPERTY(float progress READ progress WRITE setProgress NOTIFY progressChanged)
  14. Q_PROPERTY(QString message READ message WRITE setMessage NOTIFY messageChanged)
  15. Q_PROPERTY(int state READ state NOTIFY stateChanged)
  16. Q_PROPERTY(QString connectionName READ connectionName NOTIFY connectionNameChanged)
  17. Q_PROPERTY(int taskCount READ taskCount NOTIFY taskCountChanged)
  18. public:
  19. enum FileRoles {
  20. nameRole = Qt::UserRole + 1,
  21. isDirRole,
  22. isFileRole,
  23. sizeRole,
  24. lastModifiedRole
  25. };
  26. explicit FtpModel(QObject *parent = 0);
  27. ~FtpModel();
  28. int rowCount(const QModelIndex &parent) const;
  29. QVariant data(const QModelIndex &index, int role) const;
  30. void setLoading(bool l){mLoading = l;}
  31. bool loading(){return mLoading;}
  32. void setCurrentPath(const QString& path){mCurrentPath = path;}
  33. const QString& currentPath(){return mCurrentPath;}
  34. void setProgress(float p){mProgress = p;}
  35. float progress(){return mProgress;}
  36. int state(){return mFtp->state();}
  37. const QString& message(){return mMessage;}
  38. void setMessage(const QString& m ){mMessage = m;}
  39. const QString& connectionName(){return mConnectionName;}
  40. int taskCount();
  41. Q_INVOKABLE QString stateName(int state);
  42. //command
  43. signals:
  44. void loadingChanged();
  45. void currentPathChanged();
  46. void progressChanged();
  47. void stateChanged();
  48. void messageChanged();
  49. void connectionNameChanged();
  50. void taskCountChanged();
  51. public slots:
  52. void connectToHost(const QString& host, quint16 port = 21);
  53. void login(const QString& user = QString(),const QString& password = QString());
  54. void refresh(const QString& dir = QString());
  55. void showState(int state);
  56. void done(bool error);
  57. void addFile(const QUrlInfo &i);
  58. void close();
  59. void openFolder(int index);
  60. void back();
  61. void removeFile(int index);
  62. void mkdir(const QString& dir);
  63. void downloadFile(int index, const QString& dest = QString());
  64. void renameFile(int index, const QString& name);
  65. void uploadFile(const QString& path);
  66. QString destDir(){return mDestDir;}
  67. protected slots:
  68. void dataTransferProgress(qint64 byte, qint64 total);
  69. void commandFinished(int id, bool error);
  70. void rawCommandReply(int replyCode, const QString& detail);
  71. void clear();
  72. private:
  73. QFtp * mFtp;
  74. QList <QUrlInfo> mFilesList;
  75. bool mLoading;
  76. QString mCurrentPath;
  77. QString mMessage;
  78. QString mDestDir;
  79. QString mConnectionName;
  80. float mProgress;
  81. QList <QFile*> mDownloadFiles;
  82. QList <QFile*> mUploadFiles;
  83. int mDownloadId;
  84. int mUploadId;
  85. };
  86. #endif // FTPMODEL_H