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