unFtpDialog.h
#ifndef _UNFTPDIALOG_h_
#define _UNFTPDIALOG_h_
#include
#include
#include
#include
#include "ui_unFtpDialog.h"
//------------------------------------------------------->
class mainWindow : public QDialog,public Ui_ftpDialog
{
Q_OBJECT
public:
mainWindow(QWidget *parent = 0,const QString& type_work="",const QString& file_name="");
void setParams(const QString& ftp_host,const int& ftp_port,const QString& ftp_user,const QString& ftp_passw,const QString& ftp_path);
void setFinename(const QString& fname);
void setWorkType(const QString& worktype);
private:
QFtp *ftp;
QFile *file;
//------------------------->>>
QString adress;
int port;
QString login;
QString password;
QString pathFile;
//------------------------->>>
QString typeWork;
QString filename;
//------------------------->>>
signals:
void open_file(QString str);
public slots:
void ConDisconnect();
void cancelDownload();
void downloadFile();
void updateDataTransferProgress(qint64 readBytes,qint64 totalBytes);
void ftpCommandFinished(int commandId, bool error);
void ftpCommandStarted(int commandId);
void slot_done (bool error);
void slot_stateChanged(int state);
void slot_open(QString str);
};
bool WinExecAndWait32(QString str,int Visibility);
bool RunProg(QString str);
const QString GetCurrentUserName();
//------------------------------------------------------->
#endif// _UNFTPDIALOG_h_
unFtpDialog.cpp
#include
#include
#include "windows.h"
#include "unFtpDialog.h"
mainWindow::mainWindow(QWidget *parent,const QString& type_work,const QString& file_name):QDialog(parent)
{
setupUi(this);
connect(bCancel, SIGNAL(clicked()), this, SLOT(cancelDownload()));
progressBar->hide();
progressBar->setValue(0);
ftp = 0;
file = 0;
setWorkType(type_work);
if(typeWork=="SAVE"){
QString st=QFileDialog::getSaveFileName(this,"Сохранить файл",QDir::homePath ()+"/"+file_name);
if(st=="")exit(0);
setFinename(st);
}
if(typeWork=="OPEN"){
setFinename(QDir::tempPath ()+"/"+file_name);
}
if(typeWork!="OPEN" && typeWork!="SAVE"){
QMessageBox::information(this,"FTP","Укажите тип операции");
exit(0);
}
}
void mainWindow::setParams(const QString& ftp_host,const int& ftp_port,const QString& ftp_user,const QString& ftp_passw,const QString& ftp_path)
{
adress=ftp_host;
port=ftp_port;
login=ftp_user;
password=ftp_passw;
pathFile=ftp_path;
}
void mainWindow::setFinename(const QString& fname)
{
filename=fname;
}
void mainWindow::setWorkType(const QString& worktype)
{
typeWork=worktype;
}
void mainWindow::ConDisconnect()
{
label->setText("Соединение с сервером");
progressBar->hide();
if (ftp) {
setCursor(Qt::ArrowCursor);
ftp->abort();
ftp->deleteLater();
ftp = 0;
}
setCursor(Qt::WaitCursor);
ftp = new QFtp(this);
connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
this, SLOT(updateDataTransferProgress(qint64, qint64)));
connect(ftp, SIGNAL(commandFinished(int, bool)),
this, SLOT(ftpCommandFinished(int, bool)));
connect(ftp, SIGNAL(commandStarted(int)),
this, SLOT(ftpCommandStarted(int)));
connect(ftp, SIGNAL(done (bool)),this, SLOT(slot_done (bool)));
connect(ftp, SIGNAL(stateChanged ( int ) ),this, SLOT(slot_stateChanged ( int )));
connect(this, SIGNAL(open_file(QString) ),this, SLOT(slot_open(QString)));
QUrl url(adress);
ftp->connectToHost(adress, port);
if(ftp->error()){
QMessageBox::information(this,"FTP",QString("%1").arg(ftp->errorString ()));
cancelDownload();
}
//ftp->cd(pathFile);
}
void mainWindow::cancelDownload()
{
disconnect(ftp, 0, 0, 0);
if(ftp){
//QMessageBox::information(this,QString("0-0"),QString(""));
ftp->close ();
//QMessageBox::information(this,QString("0-1"),QString(""));
ftp->abort();
//QMessageBox::information(this,QString("0-2"),QString(""));
ftp->deleteLater();
//QMessageBox::information(this,QString("0-3"),QString(""));
ftp = 0;
//QMessageBox::information(this,QString("0-4"),QString(""));
}
if(file){
//QMessageBox::information(this,QString("0"),QString(""));
file->close();
//QMessageBox::information(this,QString("1"),QString(""));
file->deleteLater();
//QMessageBox::information(this,QString("2"),QString(""));
file = 0;
//QMessageBox::information(this,QString("3"),QString(""));
//QMessageBox::information(this,QString("4"),QString(""));
}
if (QFile::exists(filename)) {
QFile::remove(filename);
}
exit(0);
}
void mainWindow::downloadFile()
{
if (QFile::exists(filename)) {
QFile::remove(filename);
}
file = new QFile(filename);
if (!file->open(QIODevice::WriteOnly)) {
QMessageBox::information(this,QString(""),QString("Не могу сохранить файл %1:\n%2.").arg(filename).arg(file->errorString()));
delete file;file=0;
cancelDownload();
return;
}
ftp->get(pathFile, file);
progressBar->show();
label->setText(QString("Загрузка %1...").arg(filename));
}
void mainWindow::updateDataTransferProgress(qint64 readBytes,qint64 totalBytes)
{
progressBar->setMaximum(totalBytes);
progressBar->setValue(readBytes);
}
void mainWindow::ftpCommandFinished(int commandId, bool error)
{
//QMessageBox::information(this,"FTP",QString("+++").arg(ftp->errorString ()));
setCursor(Qt::ArrowCursor);
if (ftp->currentCommand() == QFtp::ConnectToHost) {
if (error) {
QMessageBox::information(this,"FTP",QString("Не могу подключиться к ftp-серверу %1").arg(adress));
cancelDownload();
return;
}else{
label->setText("Соединение OK");
ftp->login(login,password);
}
}
if (ftp->currentCommand() == QFtp::Login){
if (error) {
QMessageBox::information(this,"FTP",QString("%1").arg(ftp->errorString ()));
cancelDownload();
}else{
label->setText("Логин OK");
downloadFile();
}
}
if (ftp->currentCommand() == QFtp::Get) {
label->setText(QString("Загрузка %1 ").arg(file->fileName()));
if (error) {
label->setText( QString("Ошибка загрузки %1").arg(ftp->errorString ()) );
file->close();
file->remove();
//cancelDownload();
} else {
label->setText(QString("Загружен %1. ").arg(file->fileName()));
file->close();
bCancel->setText("Закрыть");
if(typeWork=="OPEN")emit open_file(filename);
if(typeWork=="SAVE"){
disconnect(bCancel, 0,0,0);
if(ftp){
ftp->close ();
ftp->abort();
ftp->deleteLater();
ftp = 0;
}
connect(bCancel, SIGNAL(clicked()), this, SLOT(accept()));
}
}
delete file;file=0;
progressBar->hide();
}
if (error) {
QMessageBox::information(this,"FTP",QString("%1").arg(ftp->errorString ()));
cancelDownload();
}
}
void mainWindow::slot_done (bool error)
{
if (error) {
QMessageBox::information(this,"FTP",QString("%1").arg(ftp->errorString ()));
cancelDownload();
}
}
void mainWindow::slot_stateChanged(int state)
{
//QMessageBox::information(this,"FTP",QString::number(state));
if(QFtp::Unconnected==state){label->setText("Сброшено подключение");QMessageBox::information(this,"FTP",QString("%1").arg(ftp->errorString ()));cancelDownload();}
if(QFtp::HostLookup==state)label->setText("Подключение...");
if(QFtp::Connecting==state)label->setText("Подключение..");
if(QFtp::Connected==state)label->setText("Соединились с сервером");
if(QFtp::LoggedIn==state)label->setText("Идентификация пользователя");
if(QFtp::Closing==state)label->setText("Теряется связь с сервером");
}
void mainWindow::ftpCommandStarted(int commandId)
{
if (ftp->currentCommand() == QFtp::Get) {
label->setText(QString("Загрузка %1 ").arg(file->fileName()));
}
}
void mainWindow::slot_open(QString str)
{
//WinExecAndWait32(str,0);
RunProg(str);
}
//------------------>
bool WinExecAndWait32(QString str,int Visibility)
{
STARTUPINFO sj;
PROCESS_INFORMATION pj;
ZeroMemory( &sj, sizeof(sj) );
sj.cb = sizeof(sj);
ZeroMemory( &pj, sizeof(pj) );
WCHAR array[1024];
memset(array,'\0',1024);
int col=str.toWCharArray(array);
QMessageBox::warning(0,"=str.toWCharArray(array)",QString::number(col),QMessageBox::Cancel,QMessageBox::Save);
QString str00=QString::fromWCharArray(array)+"|";
QMessageBox::warning(0,"QString::fromWCharArray(array,col)",str00,QMessageBox::Cancel,QMessageBox::Save);
if(!CreateProcess(NULL, array, NULL, NULL, FALSE, 0, NULL, NULL, &sj, &pj))
{
QMessageBox::warning(0,"---1",QString::number(GetLastError() ),QMessageBox::Cancel,QMessageBox::Save);
}
WaitForSingleObject( pj.hProcess, INFINITE );
CloseHandle( pj.hProcess );
CloseHandle( pj.hThread );
}
//--------------------->
bool RunProg(QString str)
{
//QMessageBox::warning(0,"===",str,QMessageBox::Cancel,QMessageBox::Save);
WCHAR array[1024];
memset(array,'\0',1024);
int col=str.toWCharArray(array);
HINSTANCE ooo=ShellExecute(0, L"open", array, NULL, NULL, SW_SHOW);
if (int(ooo)<=32)
QMessageBox::warning(0,"===","<=32",QMessageBox::Cancel,QMessageBox::Save);
}
const QString GetCurrentUserName()
{
QString s;
#ifdef Q_OS_UNIX
struct passwd *pwd=getpwuid(getuid());
s=QString(pwd->pw_name);
#endif
#ifdef Q_OS_WIN32
DWORD userbufflen=1024;
WCHAR userbuff[userbufflen];
if ( GetUserNameW(userbuff,&userbufflen) )
s=QString::fromWCharArray(userbuff,userbufflen-1);
#endif
return(s);
#ifdef DEBUGER
QMessageBox::warning(0,"рТЙЧЕФ ","рПМШЪПЧБФЕМШ "+::BDLogin+"", QMessageBox::Cancel);
#endif
}
main.cpp
#include
#include
#include
#include
#include "MoiClass.h"
#include "ui_untitled.h"
#include "unFtpDialog.h"
//---------------
enum perej{one=1,two=2,tri=4};
int main(int argc, char **argv)
{
#ifdef Q_OS_LINUX
QString codecname="KOI8-R";
#else
QString codecname="Windows-1251";
#endif
QFont fontcur(codecname,12);
QTextCodec::setCodecForTr(QTextCodec::codecForName(codecname.toAscii().data()));
QTextCodec::setCodecForLocale(QTextCodec::codecForName(codecname.toAscii().data()));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName(codecname.toAscii().data()));
QApplication Application(argc, argv);
QApplication::setStyle(QStyleFactory::create("Windows"));
//if(argc!=5){
//QMessageBox::critical(0,"в классе",">==="+QString::number(tri) +"Кол-во параметров не 5",QMessageBox::Cancel);
//exit(0);
// }
QString c_type(argv[1]);
QString c_host(argv[2]);
QString c_port(argv[3]);
QString c_file(argv[4]);
QString c_passw("123");
mainWindow * ftp_window= new mainWindow(0,c_type,QFileInfo(c_file).fileName());
ftp_window->setParams(c_host,c_port.toInt(),GetCurrentUserName(),c_passw,c_file);
ftp_window->show();
//ftp_window->setFinename(edite_savefile->text()/*"C:\\123.jpg"*/);
//ftp_window->setWorkType(c_type);
ftp_window->ConDisconnect();
//ftp_window->hide();
//-------------
return Application.exec();
}
<исходники>