| 1 |
/* ============================================================ |
| 2 |
* |
| 3 |
* This file is a part of digiKam project |
| 4 |
* https://www.digikam.org |
| 5 |
* |
| 6 |
* Date : 2011-03-22 |
| 7 |
* Description : a MediaWiki C++ interface |
| 8 |
* |
| 9 |
* SPDX-FileCopyrightText: 2011-2026 by Gilles Caulier <caulier dot gilles at gmail dot com> |
| 10 |
* SPDX-FileCopyrightText: 2011 by Alexandre Mendes <alex dot mendes1988 at gmail dot com> |
| 11 |
* SPDX-FileCopyrightText: 2011 by Hormiere Guillaume <hormiere dot guillaume at gmail dot com> |
| 12 |
* SPDX-FileCopyrightText: 2011 by Manuel Campomanes <campomanes dot manuel at gmail dot com> |
| 13 |
* |
| 14 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 15 |
* |
| 16 |
* ============================================================ */ |
| 17 |
|
| 18 |
#include "mainwindow.h" |
| 19 |
#include "ui_mainwindow.h" |
| 20 |
|
| 21 |
// Qt includes |
| 22 |
|
| 23 |
#include <QFile> |
| 24 |
#include <QFileDialog> |
| 25 |
|
| 26 |
MainWindow::MainWindow(QWidget *parent) |
| 27 |
: QMainWindow(parent), |
| 28 |
ui(new Ui::MainWindow), |
| 29 |
MediaWiki(QUrl(QLatin1String("https://test.wikipedia.org/w/api.php"))) |
| 30 |
{ |
| 31 |
ui->setupUi(this); |
| 32 |
init(); |
| 33 |
} |
| 34 |
|
| 35 |
MainWindow::~MainWindow() |
| 36 |
{ |
| 37 |
delete ui; |
| 38 |
} |
| 39 |
|
| 40 |
void MainWindow::init() |
| 41 |
{ |
| 42 |
this->ui->comboBox->addItem(QLatin1String("Own work, multi-license with CC-BY-SA-3.0 and GFDL"), QLatin1String("{{self|cc-by-sa-3.0|GFDL|migration=redundant}}")); |
| 43 |
this->ui->comboBox->addItem(QLatin1String("Own work, multi-license with CC-BY-SA-3.0 and older"), QLatin1String("{{self|cc-by-sa-3.0,2.5,2.0,1.0}}")); |
| 44 |
this->ui->comboBox->addItem(QLatin1String("Creative Commons Attribution-Share Alike 3.0"), QLatin1String("{{self|cc-by-sa-3.0}}")); |
| 45 |
this->ui->comboBox->addItem(QLatin1String("Own work, Creative Commons Attribution 3.0"), QLatin1String("{{self|cc-by-3.0}}")); |
| 46 |
this->ui->comboBox->addItem(QLatin1String("Own work, release into public domain under the CC-Zero license"), QLatin1String("{{self|cc-zero}}")); |
| 47 |
this->ui->comboBox->addItem(QLatin1String("Author died more than 100 years ago"), QLatin1String("{{PD-old}}")); |
| 48 |
this->ui->comboBox->addItem(QLatin1String("Photo of a two-dimensional work whose author died more than 100 years ago"), QLatin1String("{{PD-art}}")); |
| 49 |
this->ui->comboBox->addItem(QLatin1String("First published in the United States before 1923"), QLatin1String("{{PD-US}}")); |
| 50 |
this->ui->comboBox->addItem(QLatin1String("Work of a U.S. government agency"), QLatin1String("{{PD-USGov}}")); |
| 51 |
this->ui->comboBox->addItem(QLatin1String("Simple typefaces, individual words or geometric shapes"), QLatin1String("{{PD-text}}")); |
| 52 |
this->ui->comboBox->addItem(QLatin1String("Logos with only simple typefaces, individual words or geometric shapes"), QLatin1String("{{PD-textlogo}}")); |
| 53 |
} |
| 54 |
|
| 55 |
void MainWindow::slot_pushButtslot_clicked() |
| 56 |
{ |
| 57 |
this->ui->progressBar->setValue(0); |
| 58 |
Login* const login = new Login(MediaWiki, this->ui->mLoginEdit->text(), this->ui->mMdpEdit->text()); |
| 59 |
|
| 60 |
connect(login, SIGNAL(result(KJob*)), |
| 61 |
this, SLOT(loginHandle(KJob*))); |
| 62 |
|
| 63 |
login->start(); |
| 64 |
} |
| 65 |
|
| 66 |
void MainWindow::loginHandle(KJob* login) |
| 67 |
{ |
| 68 |
if (login->error() != 0) |
| 69 |
{ |
| 70 |
QMessageBox popup; |
| 71 |
popup.setText(QLatin1String("Wrong authentication.")); |
| 72 |
popup.exec(); |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
QFile* const file = new QFile(this->ui->lineEdit->text()); |
| 77 |
|
| 78 |
if (!file->open(QIODevice::ReadOnly)) |
| 79 |
{ |
| 80 |
QMessageBox popup; |
| 81 |
popup.setText(QString::fromLatin1("Cannot open file %1.").arg(this->ui->lineEdit->text())); |
| 82 |
popup.exec(); |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
Upload* const e1 = new Upload(MediaWiki); |
| 87 |
e1->setFile(file); |
| 88 |
e1->setFilename(this->ui->lineEdit_2->text()); |
| 89 |
|
| 90 |
QString text = QLatin1String("== {{int:filedesc}} == \n{{Information |Description="); |
| 91 |
text.append(this->ui->descriptionEdit->text()); |
| 92 |
text.append(QLatin1String("\n|Source=")).append(this->ui->sourceEdit->text()); |
| 93 |
text.append(QLatin1String("\n|Date=")).append(this->ui->dateEdit->text()); |
| 94 |
text.append(QLatin1String("\n|Author=")).append(this->ui->authorEdit->text()); |
| 95 |
text.append(QLatin1String("\n|Permission=")).append(this->ui->permissionEdit->text()); |
| 96 |
text.append(QLatin1String("\n|other_versions=")).append(this->ui->versionsEdit->text()); |
| 97 |
text.append(QLatin1String("\n}}\n== {{int:license}} ==\n")); |
| 98 |
text.append(this->ui->comboBox->itemData(this->ui->comboBox->currentIndex()).toString()); |
| 99 |
|
| 100 |
e1->setText(text); |
| 101 |
|
| 102 |
connect(e1, SIGNAL(result(KJob*)), |
| 103 |
this, SLOT(uploadHandle(KJob*))); |
| 104 |
|
| 105 |
connect(e1,SIGNAL(processedSize(KJob*,qulonglong)), |
| 106 |
this, SLOT(processedUploadSize(KJob*,qulonglong))); |
| 107 |
|
| 108 |
connect(e1,SIGNAL(totalSize(KJob*,qulonglong)), |
| 109 |
this,SLOT(TotalUploadSize(KJob*,qulonglong))); |
| 110 |
|
| 111 |
e1->start(); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void MainWindow::uploadHandle(KJob* job) |
| 116 |
{ |
| 117 |
disconnect(this, SIGNAL(result(KJob*)), |
| 118 |
this, SLOT(uploadHandle(KJob*))); |
| 119 |
|
| 120 |
disconnect(this, SIGNAL(processedSize(KJob*,qulonglong)), |
| 121 |
this, SLOT(processedUploadSize(KJob*,qulonglong))); |
| 122 |
|
| 123 |
disconnect(this, SIGNAL(totalSize(KJob*,qulonglong)), |
| 124 |
this, SLOT(TotalUploadSize(KJob*,qulonglong))); |
| 125 |
|
| 126 |
QString errorMessage; |
| 127 |
|
| 128 |
if (job->error() == 0) |
| 129 |
{ |
| 130 |
errorMessage = QLatin1String("Image uploaded successfully."); |
| 131 |
} |
| 132 |
else |
| 133 |
{ |
| 134 |
errorMessage = QLatin1String("Image upload failed."); |
| 135 |
} |
| 136 |
|
| 137 |
QMessageBox popup; |
| 138 |
popup.setText(errorMessage); |
| 139 |
popup.exec(); |
| 140 |
} |
| 141 |
|
| 142 |
void MainWindow::processedUploadSize(KJob* job, qulonglong size) |
| 143 |
{ |
| 144 |
Q_UNUSED(job) |
| 145 |
this->ui->progressBar->setValue(size); |
| 146 |
} |
| 147 |
|
| 148 |
void MainWindow::TotalUploadSize(KJob* job, qulonglong size) |
| 149 |
{ |
| 150 |
Q_UNUSED(job) |
| 151 |
this->ui->progressBar->setMaximum(size); |
| 152 |
} |
| 153 |
|
| 154 |
void MainWindow::slot_parcourir_clicked() |
| 155 |
{ |
| 156 |
QString fileName = QFileDialog::getOpenFileName(this, |
| 157 |
QLatin1String("Open Image"), |
| 158 |
QLatin1String("~"), |
| 159 |
QLatin1String("Image Files (*.png *.jpg *.bmp *.jpeg *.gif)")); |
| 160 |
|
| 161 |
if (!fileName.isEmpty()) |
| 162 |
{ |
| 163 |
QPixmap preview(fileName); |
| 164 |
QSize size(preview.size()); |
| 165 |
size.scale(400, 200, Qt::KeepAspectRatio); |
| 166 |
preview = preview.scaled(size, Qt::KeepAspectRatio, Qt::FastTransformation); |
| 167 |
|
| 168 |
this->ui->previewLabel->setPixmap(preview); |
| 169 |
this->ui->lineEdit->setText(fileName); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
void MainWindow::slot_lineEdit_textChanged(const QString& text) |
| 174 |
{ |
| 175 |
this->ui->pushButton->setEnabled(!text.isEmpty() && !text.isNull()); |
| 176 |
} |
| 177 |
|
| 178 |
#include "moc_mainwindow.cpp" |