New lua versions
[ryzomcore.git] / studio / src / plugins / translation_manager / ftp_selection.cpp
blobddaad8745a05dbc4d8aeaf9f040986f6700ebb23
1 // Translation Manager Plugin - OVQT Plugin <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2011 Emanuel COSTEA <cemycc@gmail.com>
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2011 Dzmitry KAMIAHIN (dnk-88) <dnk-88@tut.by>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "ftp_selection.h"
22 #include <QtGui/QMessageBox>
23 #include <QtNetwork/QFtp>
25 namespace TranslationManager
27 CFtpSelection::CFtpSelection(QWidget *parent): QDialog(parent)
29 _ui.setupUi(this);
30 connect(_ui.connectButton, SIGNAL(clicked()), this, SLOT(ConnectButtonClicked()));
31 connect(_ui.doneButton, SIGNAL(clicked()), this, SLOT(DoneButtonClicked()));
32 connect(_ui.cdToParrent, SIGNAL(clicked()), this, SLOT(cdToParent()));
33 connect(_ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
35 // file list
36 connect(_ui.fileList, SIGNAL(itemActivated(QTreeWidgetItem *,int)),this, SLOT(processItem(QTreeWidgetItem *,int)));
37 _ui.fileList->setEnabled(false);
38 _ui.fileList->setRootIsDecorated(false);
39 _ui.fileList->setHeaderLabels(QStringList() << tr("Name") << tr("Size") << tr("Owner") << tr("Group") << tr("Time"));
40 _ui.fileList->header()->setStretchLastSection(false);
42 // buttons
43 _ui.cdToParrent->setEnabled(false);
44 _ui.doneButton->setEnabled(false);
46 status = false;
49 // Connection with the FTP Server. We retrieve the file list.
50 void CFtpSelection::ConnectButtonClicked()
52 conn = new QFtp(this);
53 connect(conn, SIGNAL(commandFinished(int,bool)), this, SLOT(FtpCommandFinished(int,bool)));
54 connect(conn, SIGNAL(listInfo(QUrlInfo)), this, SLOT(AddToList(QUrlInfo)));
56 setCursor(Qt::WaitCursor);
58 QUrl url(_ui.url->text());
59 if (!url.isValid() || url.scheme().toLower() != QLatin1String("ftp"))
61 conn->connectToHost(_ui.url->text(), 21);
62 conn->login();
64 else
66 conn->connectToHost(url.host(), url.port(21));
68 if (!url.userName().isEmpty())
69 conn->login(QUrl::fromPercentEncoding(url.userName().toLatin1()), url.password());
70 else
71 conn->login();
72 if (!url.path().isEmpty())
73 conn->cd(url.path());
77 // Get the user action.
78 void CFtpSelection::FtpCommandFinished(int, bool error)
80 setCursor(Qt::ArrowCursor);
82 if (conn->currentCommand() == QFtp::ConnectToHost)
84 if (error)
86 QMessageBox::information(this, tr("FTP"),
87 tr("Unable to connect to the FTP server "
88 "at %1. Please check that the host "
89 "name is correct.")
90 .arg(_ui.url->text()));
91 return;
94 return;
97 if (conn->currentCommand() == QFtp::Login)
99 conn->list();
102 if (conn->currentCommand() == QFtp::Get)
104 if(error)
106 status = false;
107 file->close();
108 file->remove();
110 else
112 file->close();
113 status = true;
115 _ui.cancelButton->setEnabled(true);
118 if (conn->currentCommand() == QFtp::List)
120 if (isDirectory.isEmpty())
122 _ui.fileList->addTopLevelItem(new QTreeWidgetItem(QStringList() << tr("<empty>")));
123 _ui.fileList->setEnabled(false);
127 // Make the file list with directories and files
128 void CFtpSelection::AddToList(const QUrlInfo &urlInfo)
130 QTreeWidgetItem *item = new QTreeWidgetItem;
131 item->setText(0, urlInfo.name());
132 item->setText(1, QString::number(urlInfo.size()));
133 item->setText(2, urlInfo.owner());
134 item->setText(3, urlInfo.group());
135 item->setText(4, urlInfo.lastModified().toString("MMM dd yyyy"));
137 QPixmap pixmap(urlInfo.isDir() ? ":/translationManager/images/dir.png" : ":/translationManager/images/file.png");
138 item->setIcon(0, pixmap);
140 isDirectory[urlInfo.name()] = urlInfo.isDir();
141 _ui.fileList->addTopLevelItem(item);
142 if (!_ui.fileList->currentItem())
144 _ui.fileList->setCurrentItem(_ui.fileList->topLevelItem(0));
145 _ui.fileList->setEnabled(true);
149 void CFtpSelection::processItem(QTreeWidgetItem *item, int)
151 QString name = item->text(0);
152 if (isDirectory.value(name))
154 _ui.fileList->clear();
155 isDirectory.clear();
156 currentPath += '/';
157 currentPath += name;
158 conn->cd(name);
159 conn->list();
161 setCursor(Qt::WaitCursor);
162 return;
164 _ui.doneButton->setEnabled(true);
167 // Exit from a directory
168 void CFtpSelection::cdToParent()
170 setCursor(Qt::WaitCursor);
172 _ui.fileList->clear();
173 isDirectory.clear();
174 currentPath = currentPath.left(currentPath.lastIndexOf('/'));
175 if (currentPath.isEmpty())
177 _ui.cdToParrent->setEnabled(false);
178 conn->cd("/");
180 else
182 conn->cd(currentPath);
184 conn->list();
187 // Done action
188 void CFtpSelection::DoneButtonClicked()
190 QString fileName = _ui.fileList->currentItem()->text(0);
192 if (QFile::exists(fileName))
194 QMessageBox::information(this, tr("FTP"),
195 tr("There already exists a file called %1 in "
196 "the current directory.")
197 .arg(fileName));
198 return;
201 file = new QFile(fileName);
203 setCursor(Qt::WaitCursor);
205 if (!file->open(QIODevice::WriteOnly))
207 QMessageBox::information(this, tr("FTP"),
208 tr("Unable to save the file %1: %2.")
209 .arg(fileName).arg(file->errorString()));
210 delete file;
211 return;
213 _ui.cancelButton->setEnabled(false);
214 conn->get(_ui.fileList->currentItem()->text(0), file);
216 reject();