Bonjour tout le monde
Je suis nouveau sur ce forum de développement et j'espère que vous saurez me donner une piste afin que je trouve la solution à mon problème

J'ai commencé l'écriture d'un petit programme "formulaire" (écrit en Qt) qui, après avoir rempli les champ "titre", "fichier audio", et "fichier image", génère une petite page HTML puis envoie cette dernière et les deux fichiers sur un serveur web via FTP.
Je n'ai pas eu de souci pour génerer le fichier HTML ou pour envoyer un fichier via FTP. Là où ça coince, c'est pour envoyer plusieurs fichiers de facon consécutive en conservant la même session FTP.

Je sais que certain vont se voir pousser des boutons à la vue de mon code mais je débute avec Qt (et surtout avec les signaux/slots) alors, soyez indulgents svp.
Voilà ce que j'ai réussi à coder jusqu'ici.

mainWindows.ui
Nom : program.png
Affichages : 280
Taille : 11,9 Ko

main.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
#include <QApplication>
#include "mainwindow.h"
 
int main(int argc, char *argv[]){
    QApplication app(argc, argv);
    mainWindow fenetre;
    fenetre.show();
    return app.exec();
}
mainWindow.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QWidget>
#include <QMessageBox>
#include <QFileDialog>
#include <QInputDialog>
#include <QPalette>
#include <QFile>
#include <QUrl>
#include <QTextStream>
#include "ftp.h"
 
namespace Ui {
class mainWindow;
}
 
class mainWindow : public QWidget
{
    Q_OBJECT
 
public:
    explicit mainWindow(QWidget *parent = 0);
    ~mainWindow();
    int create_file();
    QString regex(QString);
private:
    Ui::mainWindow* ui;
    QFile* file;
public slots:
    void requestFinished(QNetworkReply* r);
    void updateProgressBar(qint64 done, qint64 total);
    void uploadDone(QNetworkReply::NetworkError r);
    void browse_audio();
    void browse_image();
    void send();
    void copy_link();
};
 
#endif // MAINWINDOW_H
mainWindow.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "mainwindow.h"
#include "ui_mainwindow.h"
 
mainWindow::mainWindow(QWidget *parent) : QWidget(parent), ui(new Ui::mainWindow){
    ui->setupUi(this);
    ui->progressBar->setRange(0,100);
}
 
mainWindow::~mainWindow(){
    delete ui;
}
 
void mainWindow::browse_audio(){
    QString chemin_audio = QFileDialog::getOpenFileName(this,"Ouvrir un fichier","C:\\Users","MP3 (*.mp3)");
    ui->path_audio->setText(chemin_audio);
}
 
void mainWindow::browse_image(){
    QString chemin_image = QFileDialog::getOpenFileName(this,"Ouvrir un fichier","C:\\Users","IMAGE (*.jpg *.png *.jpeg)");
    ui->path_image->setText(chemin_image);
}
 
QString mainWindow::regex(QString texte){
    texte = texte.toLower();
    texte.replace("\"", "_");
    texte.replace("'", "_");
    texte.replace(" ","_");
    texte.replace("ç", "c");
    texte.replace("à","a");
    texte.replace("â","a");
    texte.replace("ä","a");
    texte.replace("á","a");
    texte.replace("é","e");
    texte.replace("è","e");
    texte.replace("ê","e");
    texte.replace("ë","e");
    texte.replace("ï","i");
    texte.replace("î","i");
    texte.replace("ì","i");
    texte.replace("í","i");
    texte.replace("ò","o");
    texte.replace("ô","o");
    texte.replace("ö","o");
    texte.replace("ó","o");
    texte.replace("ù","u");
    texte.replace("û","u");
    texte.replace("ü","u");
    texte.replace("ú","u");
    texte.replace("ÿ","y");
    return texte;
}
 
void mainWindow::updateProgressBar(qint64 done, qint64 total){
    double percent;
    percent = (done*100)/total;
    ui->progressBar->setValue(percent);
}
 
void mainWindow::uploadDone(QNetworkReply::NetworkError r){
    qDebug() << r;
}
 
void mainWindow::requestFinished(QNetworkReply* r){
    qDebug() << "Finished: " << r->error();
}
 
int mainWindow::create_file(){
    QString data;
    file = new QFile(QCoreApplication::applicationDirPath() + "\\template.html", this);
    if(file->open(QIODevice::ReadOnly | QIODevice::Text)){
        QTextStream in(file);
        in.setCodec("UTF-8");
        while(!in.atEnd()){
            data += in.readLine();
        }
        file->close();
 
        data.replace("$title$", ui->title->text());
        QString audio(QFileInfo(ui->path_audio->text()).fileName());
        data.replace("$audio$", "rcf_" + audio);
        QString image(QFileInfo(ui->path_image->text()).fileName());
        data.replace("$image$", "rcf_" + image);
        QFile html(QCoreApplication::applicationDirPath() + "\\rcf_" + mainWindow::regex(ui->title->text()) + ".html");
        if(html.open(QIODevice::WriteOnly | QIODevice::Text)){
            QTextStream out(&html);
            out.setCodec("UTF-8");
            out << data;
            html.close();
        }else{
            QMessageBox::warning(this, "Erreur d'écriture !", "Impossible de créer le fichier" + mainWindow::regex(ui->title->text()) + ".html");
            return 1;
        }
    }else{
        QMessageBox::warning(this, "Erreur de lecture !", "Impossible de trouver template.html");
        return 1;
    }
    return 0;
}
 
void mainWindow::send(){
    unsigned char  err = 0;
    if(ui->title->text() == ""){
        err += 0b1;
        ui->titre->setForegroundRole(QPalette::Light);
    }else{
        ui->titre->setForegroundRole(QPalette::WindowText);
    }
    if(ui->path_audio->text() == ""){
        err += 0b10;
        ui->audio->setForegroundRole(QPalette::Light);
    }else{
        ui->audio->setForegroundRole(QPalette::WindowText);
    }
    if(ui->path_image->text() == ""){
        err += 0b100;
        ui->image->setForegroundRole(QPalette::Light);
    }else{
        ui->image->setForegroundRole(QPalette::WindowText);
    }
    if(err == 0){
        if(!create_file()){
            ui->progressBar->setValue(0);
            bool ok;
            QString password(QInputDialog::getText(this, "Mot de passe FTP", "Entrez le mot de passe de connexion au serveur.",QLineEdit::Password,"",&ok));
            if(ok && !password.isEmpty()){
                Ftp upload1(password,this,this);
                upload1.ftpUpload(QCoreApplication::applicationDirPath() + "\\rcf_" + mainWindow::regex(ui->title->text()) + ".html",
                              "public_html/rcf_" + mainWindow::regex(ui->title->text()) + ".html");
 
                ui->link->setText(QString("http://adresse-de-mon-site.fr/rcf_" + mainWindow::regex(ui->title->text()) + ".html"));
                QPalette *palette = new QPalette();
                palette->setColor(QPalette::Text,Qt::green);
                ui->link->setPalette(*palette);
            }else{
                QMessageBox::warning(this,"Attention","Veuillez saisir un mot de pass !");
            }
        }else{
            QMessageBox::critical(this, "Erreur !", "Impossible de générer le fichier HTML !");
        }
    }else{
        QMessageBox::warning(this, "Attention !", "Tout les champs ne sont pas complétés");
    }
}
 
void mainWindow::copy_link(){
    ui->link->setFocus();
    ui->link->selectAll();
    ui->link->copy();
}
ftp.h

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef FTP_H
#define FTP_H
 
#include <QWidget>
#include <QFile>
#include <QFileInfo>
#include <QUrl>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QMessageBox>
#include <iostream>
#include "mainwindow.h"
 
using namespace std;
class mainWindow;
class Ftp : public QWidget
{
    Q_OBJECT
public:
    explicit Ftp(QString pass, mainWindow* main, QWidget* parent);
    bool ftpUpload(QString origin, QString destination);
private:
    mainWindow* mainW;
    QString password;
    QNetworkAccessManager* nam;
public slots:
    void finished(QNetworkReply* reply);
};
 
#endif // FTP_H
ftp.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "ftp.h"
 
Ftp::Ftp(QString pass, mainWindow* main, QWidget* parent) : QWidget(parent) {
    mainW = main;
    password = pass;
    nam = new QNetworkAccessManager();
    QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
}
 
 
bool Ftp::ftpUpload(QString origin, QString destination) {
    QUrl url("ftp://adress-de-mon-site.fr/" + destination);
    url.setUserName("user");
    url.setPassword(password);
 
    QFile file(origin);
    if (file.open(QIODevice::ReadOnly)) {
        QByteArray data = file.readAll();
        nam->put(QNetworkRequest(url), data);
        return true;
 
    }else   {
      return false;
    }
}
 
void Ftp::finished(QNetworkReply *reply) {
    reply->deleteLater();  //important!!!!
}