File: FTPDialog.cpp

package info (click to toggle)
imagevis3d 2.0.1-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,872 kB
  • sloc: cpp: 53,586; sh: 788; ansic: 292; xml: 258; python: 35; makefile: 16
file content (160 lines) | stat: -rw-r--r-- 4,532 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
   For more information, please see: http://software.sci.utah.edu

   The MIT License

   Copyright (c) 2008 Scientific Computing and Imaging Institute,
   University of Utah.


   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.
*/


//!    File   : FTPDialog.cpp
//!    Author : Jens Krueger
//!             SCI Institute
//!             University of Utah
//!    Date   : December 2008
//
//!    Copyright (C) 2008 SCI Institute

#include "FTPDialog.h"
#include <QtNetwork/QFtp>
#include <QtCore/QUrl>
#include <QtCore/QFile>

#include "../Tuvok/Basics/SysTools.h"
#include "../Tuvok/Controller/Controller.h"

using namespace std;

FTPDialog::FTPDialog(const string& strSource, const string& strTargetServer, const string& strTargetPath, QWidget* parent /* = 0 */, Qt::WindowFlags flags /* = 0 */) :
  QDialog(parent, flags),
  m_strSource(strSource),
  m_strTargetServer(strTargetServer),
  m_strTargetPath(strTargetPath),
  m_pFtp(NULL)
{
  setupUi(this);
}

FTPDialog::~FTPDialog(void)
{
  Disconnect();
}

void FTPDialog::Start() {
  show();

  m_pFtp = new QFtp(this);
  connect(m_pFtp, SIGNAL(commandFinished(int, bool)), this, SLOT(ftpCommandFinished(int, bool)));
  connect(m_pFtp, SIGNAL(dataTransferProgress(qint64, qint64)), this, SLOT(updateDataTransferProgress(qint64, qint64)));

  label_TransferDesc->setText(tr("Connecting to %1...").arg(m_strTargetServer.c_str()));

  QUrl url(m_strTargetServer.c_str());
  if (!url.isValid() || url.scheme().toLower() != QLatin1String("ftp")) {
     m_pFtp->connectToHost(m_strTargetServer.c_str(), 21);
     m_pFtp->login();
  } else {
     m_pFtp->connectToHost(url.host(), url.port(21));

     if (!url.userName().isEmpty())
         m_pFtp->login(QUrl::fromPercentEncoding(url.userName().toLatin1()), url.password());
     else
         m_pFtp->login();
     if (!url.path().isEmpty())
         m_pFtp->cd(url.path());
  }

  m_pFile = new QFile(m_strSource.c_str());
  if (!m_pFile->open(QIODevice::ReadOnly)) {
    T_ERROR("Could not create '%s' file.", m_strSource.c_str());
    delete m_pFile;
    m_pFile = NULL;
    emit TransferFailure();
    close();
    return;
  }

  m_pFtp->put(m_pFile, m_strTargetPath.c_str());
}


void FTPDialog::ftpCommandFinished(int cmdId, bool error) {
  if (cmdId == QFtp::ConnectToHost) {
    if (error) {
      T_ERROR("Error connecting to host: %s",
              m_pFtp->errorString().toStdString().c_str());
      m_pFile->close();
      delete m_pFile;
      m_pFile = NULL;
      emit TransferFailure();
      close();
      return;
    }
    label_TransferDesc->setText(tr("Uploading %1...").arg(SysTools::GetFilename(m_strSource).c_str()));
    return;
  }

  if (cmdId == QFtp::Put) {
    if (error) {
      T_ERROR("Error putting data on the remote host.");
      m_pFile->close();
      delete m_pFile;
      m_pFile = NULL;
      emit TransferFailure();
      close();
      return;
    } else {
      MESSAGE("File transfer complete.");
      m_pFile->close();
      delete m_pFile;
      m_pFile = NULL;
      emit TransferSuccess();
      close();
      return;
    }
  }
}

void FTPDialog::updateDataTransferProgress(qint64 readBytes, qint64 totalBytes) {
  progressBar->setMaximum(totalBytes);
  progressBar->setValue(readBytes);
}

void FTPDialog::Disconnect()
{
  if (m_pFtp) {
    m_pFtp->abort();
    m_pFtp->deleteLater();
    m_pFtp = NULL;
  }
}

void FTPDialog::AbortTransfer()
{
  m_pFile->close();
  delete m_pFile;
  m_pFile = NULL;
  Disconnect();
  emit TransferFailure();
  close();
}