Guest User

artworksuploader.cpp

a guest
Aug 8th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. /*
  2. * This file is a part of Xpiks - cross platform application for
  3. * keywording and uploading images for microstocks
  4. * Copyright (C) 2014-2016 Taras Kushnir <kushnirTV@gmail.com>
  5. *
  6. * Xpiks is distributed under the GNU General Public License, version 3.0
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21.  
  22. #include "artworkuploader.h"
  23. #include <QtConcurrent>
  24. #include <QFileInfo>
  25. #include "uploadinforepository.h"
  26. #include "uploadinfo.h"
  27. #include "../Common/defines.h"
  28. #include "../Helpers/ziphelper.h"
  29. #include "../Models/artworkmetadata.h"
  30. #include "../Commands/commandmanager.h"
  31. #include "../Models/settingsmodel.h"
  32. #include "../Helpers/filenameshelpers.h"
  33. #include "../Conectivity/iftpcoordinator.h"
  34. #include "../Conectivity/testconnection.h"
  35. #include "../Conectivity/uploadcontext.h"
  36. #include "../Models/imageartwork.h"
  37. #include "../Conectivity/ftphelpers.h"
  38.  
  39. #ifndef CORE_TESTS
  40. #include "../Conectivity/ftpcoordinator.h"
  41. #endif
  42.  
  43. namespace Models {
  44. ArtworkUploader::ArtworkUploader(Conectivity::IFtpCoordinator *ftpCoordinator, QObject *parent):
  45. ArtworksProcessor(parent),
  46. m_FtpCoordinator(ftpCoordinator),
  47. m_Percent(0) {
  48. #ifndef CORE_TESTS
  49. Conectivity::FtpCoordinator *coordinator = dynamic_cast<Conectivity::FtpCoordinator *>(ftpCoordinator);
  50. QObject::connect(coordinator, SIGNAL(uploadStarted()), this, SLOT(onUploadStarted()));
  51. QObject::connect(coordinator, SIGNAL(uploadFinished(bool)), this, SLOT(allFinished(bool)));
  52. QObject::connect(coordinator, SIGNAL(overallProgressChanged(double)), this, SLOT(uploaderPercentChanged(double)));
  53.  
  54. m_TestingCredentialWatcher = new QFutureWatcher<Conectivity::ContextValidationResult>(this);
  55. QObject::connect(m_TestingCredentialWatcher, SIGNAL(finished()), SLOT(credentialsTestingFinished()));
  56. QObject::connect(coordinator, SIGNAL(transferFailed(QString, QString)),
  57. &m_UploadWatcher, SLOT(reportUploadErrorHandler(QString, QString)));
  58. #endif
  59.  
  60. QObject::connect(&m_StocksFtpList, SIGNAL(stocksListUpdated()), this, SLOT(stocksListUpdated()));
  61. }
  62.  
  63. ArtworkUploader::~ArtworkUploader() {
  64. #ifndef CORE_TESTS
  65. delete m_TestingCredentialWatcher;
  66. #endif
  67.  
  68. if (m_FtpCoordinator != NULL) {
  69. delete m_FtpCoordinator;
  70. }
  71. }
  72.  
  73. void ArtworkUploader::setCommandManager(Commands::CommandManager *commandManager) {
  74. Common::BaseEntity::setCommandManager(commandManager);
  75.  
  76. #ifndef CORE_TESTS
  77. Conectivity::FtpCoordinator *coordinator = dynamic_cast<Conectivity::FtpCoordinator *>(m_FtpCoordinator);
  78. Q_ASSERT(coordinator != NULL);
  79. coordinator->setCommandManager(commandManager);
  80. #endif
  81. }
  82.  
  83. void ArtworkUploader::onUploadStarted() {
  84. LOG_DEBUG << "#";
  85. beginProcessing();
  86. m_Percent = 0;
  87. updateProgress();
  88. }
  89.  
  90. void ArtworkUploader::allFinished(bool anyError) {
  91. LOG_INFO << "anyError =" << anyError;
  92. setIsError(anyError);
  93. endProcessing();
  94. m_Percent = 100;
  95. updateProgress();
  96. }
  97.  
  98. void ArtworkUploader::credentialsTestingFinished() {
  99. #ifndef CORE_TESTS
  100. Conectivity::ContextValidationResult result = m_TestingCredentialWatcher->result();
  101. emit credentialsChecked(result.m_Result, result.m_Host);
  102. #endif
  103. }
  104.  
  105. void ArtworkUploader::uploaderPercentChanged(double percent) {
  106. m_Percent = (int)(percent);
  107. LOG_DEBUG << "Overall progress =" << percent;
  108. updateProgress();
  109. #ifndef CORE_TESTS
  110. UploadInfoRepository *uploadInfoRepository = m_CommandManager->getUploadInfoRepository();
  111. uploadInfoRepository->updatePercentages();
  112. #endif
  113. }
  114.  
  115. void ArtworkUploader::stocksListUpdated() {
  116. LOG_DEBUG << "#";
  117.  
  118. QStringList stocks = m_StocksFtpList.getStockNamesList();
  119. m_StocksCompletionSource.setStrings(stocks);
  120. }
  121.  
  122. #ifndef CORE_TESTS
  123. void ArtworkUploader::uploadArtworks() { doUploadArtworks(getArtworkList()); }
  124.  
  125. void ArtworkUploader::checkCredentials(const QString &host, const QString &username,
  126. const QString &password, bool disablePassiveMode, bool disableEPSV) const {
  127. Conectivity::UploadContext *context = new Conectivity::UploadContext();
  128.  
  129. context->m_Host = host;
  130. context->m_Username = username;
  131. context->m_Password = password;
  132. context->m_TimeoutSeconds = 10;
  133. context->m_UsePassiveMode = !disablePassiveMode;
  134. context->m_UseEPSV = !disableEPSV;
  135.  
  136. Models::SettingsModel *settingsModel = m_CommandManager->getSettingsModel();
  137. context->m_UseProxy = settingsModel->getUseProxy();
  138. context->m_ProxySettings = settingsModel->getProxySettings();
  139.  
  140. m_TestingCredentialWatcher->setFuture(QtConcurrent::run(Conectivity::isContextValid, context));
  141. }
  142.  
  143. #endif
  144.  
  145. bool ArtworkUploader::needCreateArchives() const {
  146. bool anyZipNeeded = false;
  147. const UploadInfoRepository *uploadInfoRepository = m_CommandManager->getUploadInfoRepository();
  148. auto &infos = uploadInfoRepository->getUploadInfos();
  149.  
  150. for (auto &info: infos) {
  151. if (info->getIsSelected() && info->getZipBeforeUpload()) {
  152. anyZipNeeded = true;
  153. LOG_DEBUG << "at least for" << info->getHost();
  154. break;
  155. }
  156. }
  157.  
  158. bool needCreate = false;
  159.  
  160. if (anyZipNeeded) {
  161. const QVector<ArtworkMetadata *> &artworkList = this->getArtworkList();
  162. foreach(ArtworkMetadata *metadata, artworkList) {
  163. ImageArtwork *image = dynamic_cast<ImageArtwork *>(metadata);
  164.  
  165. if (image == NULL || !image->hasVectorAttached()) {
  166. continue;
  167. }
  168.  
  169. const QString &filepath = metadata->getFilepath();
  170. QString archivePath = Helpers::getArchivePath(filepath);
  171. QFileInfo fi(archivePath);
  172.  
  173. if (!fi.exists()) {
  174. needCreate = true;
  175. LOG_DEBUG << "Zip needed at least for" << archivePath;
  176. break;
  177. }
  178. }
  179. }
  180.  
  181. return needCreate;
  182. }
  183.  
  184. void ArtworkUploader::initializeStocksList() {
  185. m_StocksFtpList.initializeConfigs();
  186. }
  187.  
  188. #ifndef CORE_TESTS
  189. void ArtworkUploader::doUploadArtworks(const QVector<ArtworkMetadata *> &artworkList) {
  190. int artworksCount = artworkList.length();
  191.  
  192. if (artworksCount == 0) {
  193. return;
  194. }
  195.  
  196. UploadInfoRepository *uploadInfoRepository = m_CommandManager->getUploadInfoRepository();
  197. std::vector<std::shared_ptr<Models::UploadInfo> > selectedInfos = std::move(uploadInfoRepository->retrieveSelectedUploadInfos());
  198.  
  199. uploadInfoRepository->resetPercents();
  200. uploadInfoRepository->updatePercentages();
  201.  
  202. m_FtpCoordinator->uploadArtworks(artworkList, selectedInfos);
  203. m_CommandManager->reportUserAction(Conectivity::UserActionUpload);
  204. }
  205.  
  206. #endif
  207.  
  208. void ArtworkUploader::cancelProcessing() {
  209. m_FtpCoordinator->cancelUpload();
  210. }
  211.  
  212. QString ArtworkUploader::getFtpName(const QString &stockAddress) const {
  213. #if !(defined(CORE_TESTS) || defined(INTEGRATION_TEST))
  214. const UploadInfoRepository *uploadInfoRepository = m_CommandManager->getUploadInfoRepository();
  215. auto &infos = uploadInfoRepository->getUploadInfos();
  216. for (auto &info: infos) {
  217. if (Conectivity::sanitizeHost(info->getHost()) == stockAddress) {
  218. return info->getTitle();
  219. }
  220. }
  221.  
  222. #endif
  223. return QString();
  224. }
  225. }
Add Comment
Please, Sign In to add comment