ftpmodel.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include "ftpmodel.h"
  2. #include <QDebug>
  3. #include <QStringList>
  4. #include <QTemporaryFile>
  5. #include <QFileInfo>
  6. #include <QDesktopServices>
  7. #include <QDir>
  8. #include "common.h"
  9. FtpModel::FtpModel(QObject *parent) :
  10. QAbstractListModel(parent)
  11. {
  12. mFtp = new QFtp();
  13. mLoading = false;
  14. mCurrentPath = "HOME";
  15. mConnectionName = "Not connected";
  16. mProgress = 0;
  17. mMessage = "no current jobs";
  18. mDownloadId = -1;
  19. mUploadId = -1;
  20. QHash<int, QByteArray> roles;
  21. roles[nameRole] = "name";
  22. roles[isDirRole] = "isDir";
  23. roles[isFileRole] = "isFile";
  24. roles[sizeRole] = "size";
  25. roles[lastModifiedRole] = "lastModified";
  26. setRoleNames(roles);
  27. connect(mFtp,SIGNAL(stateChanged(int)),this,SLOT(showState(int)));
  28. connect(mFtp,SIGNAL(done(bool)),this,SLOT(done(bool)));
  29. connect(mFtp,SIGNAL(listInfo(QUrlInfo)),this,SLOT(addFile(QUrlInfo)));
  30. connect(mFtp,SIGNAL(rawCommandReply(int,QString)),this,SLOT(rawCommandReply(int,QString)));
  31. connect(mFtp,SIGNAL(dataTransferProgress(qint64,qint64)),this,SLOT(dataTransferProgress(qint64,qint64)));
  32. connect(mFtp,SIGNAL(commandFinished(int,bool)),this,SLOT(commandFinished(int,bool)));
  33. //verifie si le dossier de reception existe! Sinon on le crée
  34. mDestDir = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
  35. QDir dir;
  36. #if defined(Q_OS_SYMBIAN)
  37. if (dir.mkdir("E:/toadftp"))
  38. mDestDir = dir.absolutePath();
  39. #endif
  40. qDebug()<<"destDir"<<mDestDir;
  41. }
  42. FtpModel::~FtpModel()
  43. {
  44. delete mFtp;
  45. mDownloadFiles.clear();
  46. mUploadFiles.clear();
  47. mFilesList.clear();
  48. }
  49. //------------------------------------------------------
  50. int FtpModel::rowCount(const QModelIndex &parent) const
  51. {
  52. return mFilesList.count();
  53. }
  54. //------------------------------------------------------
  55. QVariant FtpModel::data(const QModelIndex &index, int role) const
  56. {
  57. if ( index.row() >= mFilesList.count())
  58. return QVariant();
  59. if ( role == nameRole)
  60. return mFilesList[index.row()].name();
  61. if ( role == isDirRole)
  62. return mFilesList[index.row()].isDir();
  63. if ( role == isFileRole)
  64. return mFilesList[index.row()].isFile();
  65. if ( role == sizeRole){
  66. return Common::sizeToString(mFilesList[index.row()].size());
  67. }
  68. if ( role == lastModifiedRole)
  69. return mFilesList[index.row()].lastModified();
  70. if ( role == Qt::DisplayRole)
  71. return mFilesList[index.row()].name();
  72. if ( role == Qt::TextColorRole)
  73. {
  74. if ( mFilesList[index.row()].isDir())
  75. return Qt::red;
  76. return Qt::white;
  77. }
  78. return QVariant();
  79. }
  80. //------------------------------------------------------
  81. void FtpModel::showState(int state)
  82. {
  83. switch (state)
  84. {
  85. case QFtp::Unconnected: mMessage = "unconnected";break;
  86. case QFtp::HostLookup : mMessage = "Host Lookup...";break;
  87. case QFtp::Connecting : mMessage = "connecting...";break;
  88. case QFtp::Connected : mMessage = "connected";break;
  89. case QFtp::LoggedIn : mMessage = "loggedIn";break;
  90. case QFtp::Closing : mMessage = "closing"; ;break;
  91. }
  92. qDebug()<<mMessage;
  93. emit messageChanged();
  94. emit stateChanged();
  95. if ( state == QFtp::LoggedIn)
  96. refresh();
  97. }
  98. //------------------------------------------------------
  99. void FtpModel::done(bool error)
  100. {
  101. // mLoading = false;
  102. qDebug()<<"done";
  103. // emit loadingChanged();
  104. // if ( error)
  105. // {
  106. // qDebug()<<mMessage;
  107. // mMessage = mFtp->errorString();
  108. // emit messageChanged();
  109. // }
  110. }
  111. //------------------------------------------------------
  112. void FtpModel::connectToHost(const QString &host, quint16 port)
  113. {
  114. qDebug()<<"Connect to "<<host<<":"<<port;
  115. mConnectionName = host+":"+QString::number(port);
  116. mFtp->connectToHost(host,port);
  117. // emit connectionNameChanged();
  118. }
  119. //------------------------------------------------------
  120. void FtpModel::login(const QString &user, const QString &password)
  121. {
  122. qDebug()<<"Login with "<<user<<":"<<password;
  123. mFtp->login(user,password);
  124. }
  125. //------------------------------------------------------
  126. void FtpModel::refresh(const QString &dir)
  127. {
  128. mFilesList.clear();
  129. mFtp->list(dir);
  130. mFtp->rawCommand("PWD");
  131. //emit reset(); // Pas sur qu'on doit le mettre la
  132. }
  133. //------------------------------------------------------
  134. void FtpModel::addFile(const QUrlInfo &i)
  135. {
  136. beginInsertRows(QModelIndex(),mFilesList.count(), mFilesList.count());
  137. mFilesList.append(i);
  138. endInsertRows();
  139. }
  140. //------------------------------------------------------
  141. void FtpModel::openFolder(int index)
  142. {
  143. if (mFilesList[index].isDir())
  144. mFtp->cd(mFilesList[index].name());
  145. }
  146. //------------------------------------------------------
  147. void FtpModel::back()
  148. {
  149. mFtp->cd("..");
  150. }
  151. //------------------------------------------------------
  152. void FtpModel::rawCommandReply(int replyCode, const QString &detail)
  153. {
  154. Q_UNUSED(replyCode)
  155. qDebug()<<"raw reply";
  156. QStringList list = detail.split(" ");
  157. setCurrentPath(list.first().replace("\"",""));
  158. emit currentPathChanged();
  159. }
  160. //------------------------------------------------------
  161. void FtpModel::removeFile(int index)
  162. {
  163. if (mFilesList[index].isDir())
  164. mFtp->rmdir(mFilesList[index].name());
  165. if (mFilesList[index].isFile())
  166. mFtp->remove(mFilesList[index].name());
  167. }
  168. //------------------------------------------------------
  169. void FtpModel::mkdir(const QString& dir)
  170. {
  171. mFtp->mkdir(dir);
  172. }
  173. //------------------------------------------------------
  174. void FtpModel::dataTransferProgress(qint64 byte, qint64 total)
  175. {
  176. if (total <= 0)
  177. return;
  178. float percent = float(byte)/float(total);
  179. percent = float(qRound(percent * 100)) / 100;
  180. mProgress = percent;
  181. if (mFtp->currentCommand() == QFtp::Get){
  182. QString name = QFileInfo(*mDownloadFiles.first()).baseName();
  183. name.truncate(10);
  184. mMessage = "DL "+name+"... "+QString::number(int(percent*100)) + "%";
  185. }
  186. if (mFtp->currentCommand() == QFtp::Put){
  187. QString name = QFileInfo(*mUploadFiles.first()).baseName();
  188. name.truncate(10);
  189. mMessage = "UP "+name+"... "+QString::number(int(percent*100)) + "%";
  190. }
  191. emit progressChanged();
  192. emit messageChanged();
  193. }
  194. //------------------------------------------------------
  195. void FtpModel::downloadFile(int index, const QString &dest)
  196. {
  197. if (mFilesList[index].isFile())
  198. {
  199. QString fileName = mFilesList[index].name();
  200. QString newFile ;
  201. if (dest.isEmpty() )
  202. newFile = mDestDir+QDir::separator()+fileName;
  203. else
  204. newFile = dest+QDir::separator()+fileName;
  205. QFile* file = new QFile(newFile);
  206. if (!file->open(QIODevice::WriteOnly)) {
  207. qDebug()<<"cannot create file";
  208. delete file;
  209. return;
  210. }
  211. mDownloadFiles.append(file);
  212. emit taskCountChanged();
  213. mDownloadId = mFtp->get(mFilesList[index].name(), file);
  214. }
  215. }
  216. //------------------------------------------------------
  217. void FtpModel::uploadFile(const QString &path)
  218. {
  219. QFileInfo * i = new QFileInfo(path);
  220. QFile * file = new QFile(path);
  221. if (!file->open(QIODevice::ReadOnly)) {
  222. qDebug()<<"cannot read file";
  223. delete file;
  224. return;
  225. }
  226. mUploadFiles.append(file);
  227. emit taskCountChanged();
  228. mUploadId = mFtp->put(file,i->fileName());
  229. }
  230. //------------------------------------------------------
  231. void FtpModel::commandFinished(int id, bool error)
  232. {
  233. qDebug()<<"commande finished.."<<mFtp->currentCommand();
  234. if ( error)
  235. {
  236. mMessage=mFtp->errorString();
  237. emit messageChanged();
  238. }
  239. if ( mFtp->currentCommand() == QFtp::Cd)
  240. {
  241. qDebug()<<"list";
  242. refresh();
  243. }
  244. if (mFtp->currentCommand() == QFtp::Get)
  245. {
  246. mMessage="no current jobs";
  247. qDebug()<<mMessage;
  248. mDownloadFiles.first()->close();
  249. mDownloadFiles.removeFirst();
  250. emit messageChanged();
  251. emit taskCountChanged();
  252. }
  253. if ( mFtp->currentCommand() == QFtp::Put)
  254. {
  255. mMessage="no current jobs";
  256. qDebug()<<mMessage;
  257. mUploadFiles.first()->close();
  258. mUploadFiles.removeFirst();
  259. emit messageChanged();
  260. emit taskCountChanged();
  261. if ( mUploadFiles.count() <= 0)
  262. refresh();
  263. }
  264. if ( mFtp->currentCommand() == QFtp::Mkdir)
  265. {
  266. refresh();
  267. }
  268. if ( (mFtp->currentCommand() == QFtp::Rmdir) || (mFtp->currentCommand() == QFtp::Remove))
  269. {
  270. refresh();
  271. }
  272. if ( mFtp->currentCommand() == QFtp::Rename)
  273. {
  274. refresh();
  275. }
  276. // if ( mFtp->currentCommand() == QFtp::List)
  277. // {
  278. // emit reset();
  279. // }
  280. if ( mFtp->currentCommand() == QFtp::Close)
  281. {
  282. qDebug()<<"close";
  283. mUploadFiles.clear();
  284. mDownloadFiles.clear();
  285. mFilesList.clear();
  286. mProgress = 0;
  287. emit progressChanged();
  288. emit taskCountChanged();
  289. }
  290. }
  291. //------------------------------------------------------
  292. void FtpModel::close()
  293. {
  294. mFtp->abort();
  295. mFtp->close();
  296. mConnectionName = "No Connection";
  297. emit connectionNameChanged();
  298. emit taskCountChanged();
  299. }
  300. //------------------------------------------------------
  301. void FtpModel::clear()
  302. {
  303. beginRemoveRows(QModelIndex(),0,mFilesList.count());
  304. mFilesList.clear();
  305. endRemoveRows();
  306. }
  307. //========================================================
  308. int FtpModel::taskCount()
  309. {
  310. return mUploadFiles.count() + mDownloadFiles.count();
  311. }
  312. //=========================================================
  313. void FtpModel::renameFile(int index, const QString &name)
  314. {
  315. QString oldName = mFilesList[index].name();
  316. mFtp->rename(oldName, name);
  317. }
  318. //=========================================================
  319. QString FtpModel::stateName(int state)
  320. {
  321. QString name = "unconnected";
  322. switch (state)
  323. {
  324. case QFtp::Unconnected: name = "unconnected";break;
  325. case QFtp::HostLookup : name = "Host Lookup...";break;
  326. case QFtp::Connecting : name = "connecting...";break;
  327. case QFtp::Connected : name = "connected";break;
  328. case QFtp::LoggedIn : name = "loggedIn";break;
  329. case QFtp::Closing : name = "closing"; ;break;
  330. }
  331. return name;
  332. }
  333. //------------------------------------------------------