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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
| #include <QtGui>
#include <QtNetwork>
#include <JlCompress.h>
#include "httpwindow.h"
#include "ui_authenticationdialog.h"
HttpWindow::HttpWindow(QWidget *parent)
: QDialog(parent)
{
urlLineEdit = new QLineEdit("http://mc.deedyou.com/DATA/movies.txt");
urlLabel = new QLabel(tr("&URL:"));
urlLabel->setBuddy(urlLineEdit);
statusLabel = new QLabel(tr("Entrer l'URL du fichier que vous voulez télécharger."));
statusLabel->setWordWrap(true);
downloadButton = new QPushButton(tr("Télécharger"));
downloadButton->setDefault(true);
quitButton = new QPushButton(tr("Quitter"));
quitButton->setAutoDefault(false);
unzipButton = new QPushButton(tr("UnZip"));
buttonBox = new QDialogButtonBox;
buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(unzipButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
listeFilmOkMoviecovers = new QListWidget;
QSettings settings("GestXavMedias", "GestXavMedias.ini");
utilisateur = settings.value("NomUtilisateurDef").toString();
connect(urlLineEdit, SIGNAL(textChanged(QString)), this, SLOT(enableDownloadButton()));
connect(&qnam, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
this, SLOT(slotAuthenticationRequired(QNetworkReply*,QAuthenticator*)));
#ifndef QT_NO_OPENSSL
connect(&qnam, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
this, SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
#endif
connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(listeFilmOkMoviecovers,SIGNAL(itemDoubleClicked(QListWidgetItem*)), this,SLOT(Telecharger_fiche()));
urlLineEdit->adjustSize();
listeFilmOkMoviecovers->setFixedSize(450,450);
statusLabel->setMinimumHeight(50);
QHBoxLayout *topLayout = new QHBoxLayout;
topLayout->addWidget(urlLabel);
topLayout->addWidget(urlLineEdit);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(topLayout);
mainLayout->addWidget(listeFilmOkMoviecovers);
mainLayout->addWidget(statusLabel);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
setWindowTitle(tr("Téléchargement Fiches Films Moviecovers"));
urlLineEdit->setFocus();
downloadFile();
}
void HttpWindow::downloadFile()
{
url = urlLineEdit->text();
MyAppDirPath = QCoreApplication::applicationDirPath();
QDir::setCurrent(MyAppDirPath + "/" + utilisateur + "/Zip");
QFileInfo fileInfo(url.path());
QString fileName = fileInfo.fileName();
if (fileName.isEmpty())
fileName = "index.html";
if (QFile::exists(fileName)) {
if (QMessageBox::question(this, tr("HTTP"),
tr("Il existe déjà un fichier appeler : %1 dans "
"le repertoire courant. Remplacer ?").arg(fileName),
QMessageBox::Yes|QMessageBox::No, QMessageBox::No)
== QMessageBox::No){
QFile fichier2(fileName);
fichier2.open(QIODevice::ReadWrite | QIODevice::Text);
QTextStream file2(&fichier2);
while (! file2.atEnd())
{
QString line = file2.readLine();
listeFilmOkMoviecovers->addItem(line);
}
fichier2.close();
return;
}
QFile::remove(fileName);
}
file = new QFile(fileName);
if (!file->open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("HTTP"),
tr("Impossible de sauvegarder le fichier : %1: %2.")
.arg(fileName).arg(file->errorString()));
delete file;
file = 0;
return;
}
downloadButton->setEnabled(false);
// schedule the request
httpRequestAborted = false;
startRequest(url);
}
void HttpWindow::startRequest(QUrl url)
{
reply = qnam.get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDataReadProgress(qint64,qint64)));
}
void HttpWindow::cancelDownload()
{
statusLabel->setText(tr("Téléchargement annulé."));
httpRequestAborted = true;
reply->abort();
downloadButton->setEnabled(true);
}
void HttpWindow::httpFinished()
{
if (httpRequestAborted) {
if (file) {
file->close();
file->remove();
delete file;
file = 0;
}
reply->deleteLater();
return;
}
file->flush();
file->close();
QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error()) {
file->remove();
QMessageBox::information(this, tr("HTTP"),
tr("Problème de téléchargement: %1.")
.arg(reply->errorString()));
downloadButton->setEnabled(true);
} else if (!redirectionTarget.isNull()) {
QUrl newUrl = url.resolved(redirectionTarget.toUrl());
url = newUrl;
reply->deleteLater();
file->open(QIODevice::WriteOnly);
file->resize(0);
startRequest(url);
return;
} else {
QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName();
statusLabel->setText(tr("Téléchargement de %1 vers %2.").arg(fileName).arg(QDir::currentPath()+ " terminé"));
QDir::setCurrent(MyAppDirPath);
MyAppDirPath = QCoreApplication::applicationDirPath();
ficheZip = MyAppDirPath + "/" + utilisateur + "/Zip/"+ fiche + ".zip";
ficheExtract = MyAppDirPath + "/" + utilisateur;
bool valid = QFile::exists(ficheZip);
if (valid){
UnZip(ficheZip, ficheExtract);
}
downloadButton->setEnabled(true);
}
reply->deleteLater();
reply = 0;
delete file;
file = 0;
}
void HttpWindow::httpReadyRead()
{
if (file)
file->write(reply->readAll());
Test();
}
void HttpWindow::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
{
if (httpRequestAborted)
return;
Q_UNUSED(bytesRead);
Q_UNUSED(totalBytes);
}
void HttpWindow::enableDownloadButton()
{
downloadButton->setEnabled(!urlLineEdit->text().isEmpty());
}
void HttpWindow::slotAuthenticationRequired(QNetworkReply*,QAuthenticator *authenticator)
{
QDialog dlg;
Ui::Dialog ui;
ui.setupUi(&dlg);
dlg.adjustSize();
ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(url.host()));
ui.userEdit->setText(url.userName());
ui.passwordEdit->setText(url.password());
if (dlg.exec() == QDialog::Accepted) {
authenticator->setUser(ui.userEdit->text());
authenticator->setPassword(ui.passwordEdit->text());
}
}
#ifndef QT_NO_OPENSSL
void HttpWindow::sslErrors(QNetworkReply*,const QList<QSslError> &errors)
{
QString errorString;
foreach (const QSslError &error, errors) {
if (!errorString.isEmpty())
errorString += ", ";
errorString += error.errorString();
}
if (QMessageBox::warning(this, tr("HTTP"),
tr("Une ou plusieurs erreurs SSL ont été trouvé: %1").arg(errorString),
QMessageBox::Ignore | QMessageBox::Abort) == QMessageBox::Ignore) {
reply->ignoreSslErrors();
}
}
#endif
void HttpWindow::Test()
{
url = urlLineEdit->text();
QFileInfo fileInfo(url.path());
QString fileName = fileInfo.fileName();
QFile file(fileName); // gestion de fichier
file.open(QIODevice::ReadOnly); // on ouvre le fichier
QTextStream files(&file);
while (! files.atEnd())
{
QString line = files.readLine();
listeFilmOkMoviecovers->addItem(line);
}
file.close();
}
void HttpWindow::Telecharger_fiche()
{
fiche = (listeFilmOkMoviecovers->currentItem()->text());
if (fiche.contains("(L')", Qt::CaseInsensitive))
{
fiche = fiche.replace(" (L')", "");
fiche = ("L'" + fiche);
}
if (fiche.contains("(LE)", Qt::CaseInsensitive))
{
fiche = fiche.replace(" (LE)", "");
fiche = ("LE " + fiche);
}
if (fiche.contains("(LA)", Qt::CaseInsensitive))
{
fiche = fiche.replace(" (LA)", "");
fiche = ("LA " + fiche);
}
if (fiche.contains("(LES)", Qt::CaseInsensitive))
{
fiche = fiche.replace(" (LES)", "");
fiche = ("LES " + fiche);
}
if (fiche.contains("(UN)", Qt::CaseInsensitive))
{
fiche = fiche.replace(" (UN)", "");
fiche = ("UN " + fiche);
}
if (fiche.contains("(UNE)", Qt::CaseInsensitive))
{
fiche = fiche.replace(" (UNE)", "");
fiche = ("UNE " + fiche);
}
if (fiche.contains("(THE)", Qt::CaseInsensitive))
{
fiche = fiche.replace(" (THE)", "");
fiche = ("THE " + fiche);
}
if (fiche.contains("(AN)", Qt::CaseInsensitive))
{
fiche = fiche.replace(" (AN)", "");
fiche = ("AN " + fiche);
}
if (fiche.contains("(A)", Qt::CaseInsensitive))
{
fiche = fiche.replace(" (A)", "");
fiche = ("A " + fiche);
}
urlLineEdit = new QLineEdit("http://mc.deedyou.com/getzip.html/" + fiche + ".zip");
downloadFile();
//connect(unzipButton, SIGNAL(clicked()), this, SLOT(versunzip()));
}
/*void HttpWindow::versunzip ()
{
UnZip(ficheZip, ficheExtract);
}*/
void HttpWindow::UnZip (QString ficheZip , QString ficheExtract)
{
QDir::setCurrent(MyAppDirPath);
MyAppDirPath = QCoreApplication::applicationDirPath();
JlCompress::extractDir(ficheZip,ficheExtract);
bool valid = QFile::exists(MyAppDirPath + "/" + utilisateur + "/"+ fiche + ".film");
if (valid){
statusLabel->setText(tr("Fichiers extraits"));
listFilter << "*.zip";
QDirIterator dirIterator((MyAppDirPath + "/" + utilisateur + "/Zip"), listFilter ,QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories);
while(dirIterator.hasNext())
{
dirIterator.next();
QFile::remove(MyAppDirPath + "/" + utilisateur + "/Zip/"+ dirIterator.fileName());
}
listFilter << "*.txt";
QDirIterator dirIterator2((MyAppDirPath + "/" + utilisateur), listFilter ,QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories);
while(dirIterator2.hasNext())
{
dirIterator2.next();
QFile::remove(MyAppDirPath + "/" + utilisateur + "/"+ dirIterator2.fileName());
}
}
} |
Partager