Datei Download (GET methode) mit oder ohne passwort

Code-Schnippsel, oder Tipps und Tricks, die einem beim Programmieren mit Qt helfen können.
Antworten
patrik08
Beiträge: 746
Registriert: 27. Februar 2006 10:48
Wohnort: DE Freiburg

Datei Download (GET methode) mit oder ohne passwort

Beitrag von patrik08 »

Ich habe ein programm gemacht welches uber webdav ein CMS updatet...
Bei Mac & Linux ist dass webdav niee im cache und diese 2 OS holen die dateien immer frisch vom server....

Ausnahme der Window wenn mal etwas im Cache ist bleibt es ewig dort...
aussert man click 20 check box um die funktion zu deaktivieren...

Also lasse ich window bestimmte dateien via web holen im password bereich...

Ist fast wie die demo im http ordner von qt4 aber die signale sind einfach anders ... und diese class geht mit oder ohne password file .... Jedoch nicht dort wo cookie oder session notwendig sind ... dazu macht man es am besten mit libcurl ... http://curl.haxx.se/ beispiel auch im source von meinem projekt ... http://sourceforge.net/projects/qtexcel-xslt/ ...

Die datei holen funktion....

Code: Alles auswählen

/* usage ... */
<=  funktion x start >
    get_file_db = new Gui_Wget(this);  /* im header der class declarieren ! */
    get_file_db->show();
    get_file_db->OrderGetUrl(urltoget,STATIC_TMP_SQL_DUMP,user,pass);
     connect( get_file_db, SIGNAL( ready_action() ), this, SLOT( Load_New_DB() ) );   /* Load_New_DB() Ihres signal  um weiter zu downloaden oder andere action ...*/
     /*qDebug() <<"### get+auth   (urlget,savetofileX,user,pass)  " << urltoget << " " << user << " " << pass; */
<=  funktion x ende >
    
<=  funktion x Ihre slot >
     /*  das fenster schliessen beim weitermachen signal  */
     get_file_db->accept();
<=  funktion x Ihre slot >


und die file gui_wget.h
und die file gui_wget.cpp

header

Code: Alles auswählen

#ifndef GUI_WGET_H
#define GUI_WGET_H

#include <QDialog>
#include <QProgressBar>
#include <QPointer>
#include <QDebug> 
#include <QtGui>
#include <QtNetwork>


class QFile;
class QHttp;
class QHttpResponseHeader;
class QLabel;
class QLineEdit;
class QPushButton;
class QProgressBar;


class Gui_Wget : public QDialog
{
	Q_OBJECT
	//
public:
	static Gui_Wget* self( QWidget* = 0 );
    void OrderGetUrl( QString geturl , QString savetofile , QString user , QString pass );
    Gui_Wget( QWidget* = 0 );
	//
protected:
	void closeEvent( QCloseEvent* );
	//
private:
	
	static QPointer<Gui_Wget> _self;
    QLabel *statusLabel;
    QLabel *urlLabel;
    QLineEdit *urlLineEdit;
    QProgressBar *progressDialog;
    QPushButton *quitButton;
    QPushButton *stopButton;
    QPushButton *downloadButton;

    QHttp *http;
    QString current_pass;
    QString current_user;
    QString url;
    QString currentfile;
    int httpGetId;
    bool httpRequestAborted;
    QFile *file;
    bool qt_unlink(QString fullFileName);
    bool is_file(QString fullFileName);


signals: 
    void ready_action();

	//
public slots:
    void updateDataReadProgress(int bytesRead, int totalBytes);
    void cancelDownload();
    void downloadFile();
    void httpRequestFinished(int requestId, bool error);
    void readResponseHeader(const QHttpResponseHeader &responseHeader);
    void lastwakeup(bool s);

};
//
#endif // GUI_WGET_H




source

Code: Alles auswählen

#include "gui_wget.h"
//
/*  Save file as gui_wget.cpp  */
/*  Class Gui_Wget Created on Fri May 12 8:22:12 CEST 2006  */
//

#include <QtGui>
#include <QtNetwork>

#include <QCloseEvent>
//
QPointer<Gui_Wget> Gui_Wget::_self = 0L;
//
Gui_Wget* Gui_Wget::self( QWidget* parent )
{
	if ( !_self )
		_self = new Gui_Wget( parent );
	return _self;
}
//
Gui_Wget::Gui_Wget( QWidget* parent )
	: QDialog( parent )
{
	httpRequestAborted = false;
    
    quitButton = new QPushButton(tr("Quit"));
    stopButton = new QPushButton(tr("Stop"));
    stopButton->setEnabled(false);
    connect(quitButton, SIGNAL(clicked()), this, SLOT(accept()));
    downloadButton = new QPushButton(tr("Download"));
    statusLabel = new QLabel(""); 
    progressDialog = new QProgressBar(this);
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);
    buttonLayout->addWidget(downloadButton);
    buttonLayout->addWidget(stopButton);
    buttonLayout->addWidget(quitButton);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(statusLabel);
    mainLayout->addWidget(progressDialog);
    mainLayout->addLayout(buttonLayout);
    setLayout(mainLayout);
    setWindowTitle(tr("Download Action"));
    /* OrderGetUrl("http://sourceforge.net/projects/qtexcel-xslt/","index.html","username","password");*/
    
}

void Gui_Wget::OrderGetUrl( QString geturl , QString savetofile , QString user , QString pass )
{
	current_pass = pass;
    current_user = user;
    url = geturl;
    currentfile = savetofile;
    
     if (url.size() > 1 && currentfile.size() > 1 ) 
    {
        statusLabel->setText(tr("Download Url:")+" "+url);
        connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));  
        downloadFile();        
    }
    
    
}

void Gui_Wget::downloadFile()
{
    if (url.size() > 1 && currentfile.size() > 1 ) 
    {
    QUrl urls(url);
        if (is_file(currentfile)) {
         qt_unlink(currentfile);   
        }
            file = new QFile(currentfile);
                    if (!file->open(QIODevice::WriteOnly)) {
                        QMessageBox::information(this, tr("File Error!"),
                                                 tr("Unable to save the file %1: %2.")
                                                 .arg(currentfile).arg(file->errorString()));
                        delete file;
                        file = 0;
                        return;
                    }
                    
            http = new QHttp(this);
            stopButton->setEnabled(true);
            http->setHost(urls.host(),80);
            http->setUser( current_user , current_pass );
           
            statusLabel->setText(tr("Download Url:")+" "+url);
            connect(http, SIGNAL(dataReadProgress(int, int)),this, SLOT(updateDataReadProgress(int, int)));
            connect(http, SIGNAL(requestFinished(int, bool)),this, SLOT(httpRequestFinished(int, bool)));
            connect(http, SIGNAL(dataReadProgress(int, int)),this, SLOT(updateDataReadProgress(int, int)));
            connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
            connect(http, SIGNAL(done(bool)), this, SLOT(lastwakeup(bool)));
            connect(stopButton, SIGNAL(clicked()), this, SLOT(cancelDownload()));
           
            httpRequestAborted = false;
            httpGetId = http->get(urls.path(), file);
            downloadButton->setEnabled(false);
        
    }
    
    
}


void Gui_Wget::readResponseHeader(const QHttpResponseHeader &responseHeader)
{
        /*qDebug() << "### responseHeader.statusCode() " << responseHeader.statusCode();
        qDebug() << "### responseHeader.reasonPhrase() " << responseHeader.reasonPhrase();*/
        /*emit ready_action();*/
    
    if (responseHeader.reasonPhrase() !="OK") {
        QMessageBox::information(this, tr("HTTP"),tr("Header responder  message: %1. from %2 ").arg(responseHeader.reasonPhrase() , url ));
    }
    
    
    
 
}

void Gui_Wget::lastwakeup(bool s)
{
     if (s) {
     qDebug() << "### lastwakeup by done yes " << s;
     } else {
     qDebug() << "### lastwakeup by done no" << s; 
     }
     emit ready_action();
}



void Gui_Wget::updateDataReadProgress(int bytesRead, int totalBytes)
{
    if (httpRequestAborted)
        return;

    progressDialog->setMaximum(totalBytes);
    progressDialog->setValue(bytesRead);
}


void Gui_Wget::cancelDownload()
{
    statusLabel->setText(tr("Download canceled."));
    httpRequestAborted = true;
    http->abort();
    downloadButton->setEnabled(true);
    stopButton->setEnabled(false);
}





void Gui_Wget::httpRequestFinished(int requestId, bool error)
{
    if (httpRequestAborted) {
        if (file) {
            file->close();
            file->remove();
            delete file;
            file = 0;
        }

        progressDialog->setValue(1);
        return;
    }

    if (requestId != httpGetId)
        return;
    
    file->close();

    if (error) {
        file->remove();
        QMessageBox::information(this, tr("HTTP"),tr("Download failed: %1.").arg(http->errorString()));
    } else {
        progressDialog->setMaximum(100);
        progressDialog->setMinimum(0);
        progressDialog->setValue(100);
        statusLabel->setText(tr("Downloaded %1 success.").arg(currentfile));
        emit ready_action();
        
    }
    
    downloadButton->setEnabled(true);
    stopButton->setEnabled(false);
    delete file;
    file = 0;
}


bool Gui_Wget::is_file(QString fullFileName)
{
    QFile f( fullFileName );
	if ( f.exists() ) {
    return true;
	} else {
	return false;
    }
}


bool Gui_Wget::qt_unlink(QString fullFileName)
{
    QFile f( fullFileName );
	if ( is_file( fullFileName ) ) {
       if (f.remove()) {
        return true;
       }
	}
return false;
}




void Gui_Wget::closeEvent( QCloseEvent* e )
{
    emit ready_action();
	e->accept();
}

Antworten