1 // Translation Manager Plugin - OVQT Plugin <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2011 Emanuel COSTEA <cemycc@gmail.com>
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2011 Dzmitry KAMIAHIN (dnk-88) <dnk-88@tut.by>
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
)
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()));
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);
43 _ui
.cdToParrent
->setEnabled(false);
44 _ui
.doneButton
->setEnabled(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);
66 conn
->connectToHost(url
.host(), url
.port(21));
68 if (!url
.userName().isEmpty())
69 conn
->login(QUrl::fromPercentEncoding(url
.userName().toLatin1()), url
.password());
72 if (!url
.path().isEmpty())
77 // Get the user action.
78 void CFtpSelection::FtpCommandFinished(int, bool error
)
80 setCursor(Qt::ArrowCursor
);
82 if (conn
->currentCommand() == QFtp::ConnectToHost
)
86 QMessageBox::information(this, tr("FTP"),
87 tr("Unable to connect to the FTP server "
88 "at %1. Please check that the host "
90 .arg(_ui
.url
->text()));
97 if (conn
->currentCommand() == QFtp::Login
)
102 if (conn
->currentCommand() == QFtp::Get
)
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();
161 setCursor(Qt::WaitCursor
);
164 _ui
.doneButton
->setEnabled(true);
167 // Exit from a directory
168 void CFtpSelection::cdToParent()
170 setCursor(Qt::WaitCursor
);
172 _ui
.fileList
->clear();
174 currentPath
= currentPath
.left(currentPath
.lastIndexOf('/'));
175 if (currentPath
.isEmpty())
177 _ui
.cdToParrent
->setEnabled(false);
182 conn
->cd(currentPath
);
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.")
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()));
213 _ui
.cancelButton
->setEnabled(false);
214 conn
->get(_ui
.fileList
->currentItem()->text(0), file
);