msimurin
13th July 2010, 04:07
Hi, this is the problem i v been struggling with now for a while and i kinda feel desparate. I am trying to upload directory with all of its contest in my small ftp client app but just when it looks as everything works some small bug slips in. Here is my code so please take a look
void FtpWindow::upload_slot(){
QString name = homefileList->currentItem()->text(0); // Here i get dir name from the treewidgetlist selected item
QString filepath = path.absoluteFilePath(name); // Since path is changed each time i click on a directory in treewidget and enter it, here is how i get the path of directory i want to upload
if (!isHomeDir.value(name)) {uploadFile(filepath, name);} // this is just checking if its dir or file
else { uploadDirectory(filepath, name); }
ftp->list();
}
void FtpWindow::uploadFile(const QString& filePath, const QString &name)
{
QFile *file = new QFile(filePath);
if ( !file->open(QIODevice::ReadOnly) ) {
QMessageBox::critical( this, tr("Upload error"),
tr("Can't open file '%1' for reading.").arg(filePath) );
delete file;
return;
}
ftp->put(file, name);
}
void FtpWindow::uploadDirectory(const QString& dirPath, const QString &name)
{
QDir targetDir(dirPath);
uploadPath = currentPath;
qDebug() << uploadPath;
ftp->mkdir(name);
ftp->cd(name);
QDirIterator it(targetDir, QDirIterator::Subdirectories);
while (it.hasNext()) {
QFileInfo info = it.next();
if(it.fileName() == "." | it.fileName() == ".." | it.fileName() == "/") continue;
if(info.isDir()) {
info.dir().absolutePath();
QString dirpath = info.absoluteFilePath().remove(0, info.absoluteFilePath().indexOf('/'+ name));
QString parentdir = uploadPath + dirpath.left(dirpath.lastIndexOf('/'));
qDebug() << dirpath << "is dir and it's parent dir is " << parentdir ;
ftp->cd(parentdir);
ftp->mkdir(info.baseName());
}
else {
QString fajlovi = info.absoluteFilePath().remove(0, info.absoluteFilePath().indexOf('/'+ name));
QString parentdir = uploadPath + fajlovi.left(fajlovi.lastIndexOf('/'));
// ftp->cd(parentdir);
qDebug() << fajlovi << "is file and its parent dir is " << parentdir ;
ftp->cd(parentdir);
uploadFile(info.absoluteFilePath(), info.baseName());
}
}
}
Basically what uploadirectory function does is creates the dir of the main dir(i find the name of it in the top of code as described) inside ftp server, then i use qiterator to go trough every dir or file and if its dir i get its parent dir, ftp->cd inside of it then upload the file or just create dir if its dir.
The problem is this sometimes doesnt work because getting parent dir with this QString functions is as i experienced unreliable, what i really need is either fix that would make this work or another reliable way to handle uploading of entire dirs, please help me finally solve this, if anyone knows a source that explains how this is done maybe?
I am sick of this problem totally :(
void FtpWindow::upload_slot(){
QString name = homefileList->currentItem()->text(0); // Here i get dir name from the treewidgetlist selected item
QString filepath = path.absoluteFilePath(name); // Since path is changed each time i click on a directory in treewidget and enter it, here is how i get the path of directory i want to upload
if (!isHomeDir.value(name)) {uploadFile(filepath, name);} // this is just checking if its dir or file
else { uploadDirectory(filepath, name); }
ftp->list();
}
void FtpWindow::uploadFile(const QString& filePath, const QString &name)
{
QFile *file = new QFile(filePath);
if ( !file->open(QIODevice::ReadOnly) ) {
QMessageBox::critical( this, tr("Upload error"),
tr("Can't open file '%1' for reading.").arg(filePath) );
delete file;
return;
}
ftp->put(file, name);
}
void FtpWindow::uploadDirectory(const QString& dirPath, const QString &name)
{
QDir targetDir(dirPath);
uploadPath = currentPath;
qDebug() << uploadPath;
ftp->mkdir(name);
ftp->cd(name);
QDirIterator it(targetDir, QDirIterator::Subdirectories);
while (it.hasNext()) {
QFileInfo info = it.next();
if(it.fileName() == "." | it.fileName() == ".." | it.fileName() == "/") continue;
if(info.isDir()) {
info.dir().absolutePath();
QString dirpath = info.absoluteFilePath().remove(0, info.absoluteFilePath().indexOf('/'+ name));
QString parentdir = uploadPath + dirpath.left(dirpath.lastIndexOf('/'));
qDebug() << dirpath << "is dir and it's parent dir is " << parentdir ;
ftp->cd(parentdir);
ftp->mkdir(info.baseName());
}
else {
QString fajlovi = info.absoluteFilePath().remove(0, info.absoluteFilePath().indexOf('/'+ name));
QString parentdir = uploadPath + fajlovi.left(fajlovi.lastIndexOf('/'));
// ftp->cd(parentdir);
qDebug() << fajlovi << "is file and its parent dir is " << parentdir ;
ftp->cd(parentdir);
uploadFile(info.absoluteFilePath(), info.baseName());
}
}
}
Basically what uploadirectory function does is creates the dir of the main dir(i find the name of it in the top of code as described) inside ftp server, then i use qiterator to go trough every dir or file and if its dir i get its parent dir, ftp->cd inside of it then upload the file or just create dir if its dir.
The problem is this sometimes doesnt work because getting parent dir with this QString functions is as i experienced unreliable, what i really need is either fix that would make this work or another reliable way to handle uploading of entire dirs, please help me finally solve this, if anyone knows a source that explains how this is done maybe?
I am sick of this problem totally :(