File indexing completed on 2025-01-19 03:57:46

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2011-03-22
0007  * Description : a MediaWiki C++ interface
0008  *
0009  * SPDX-FileCopyrightText: 2011-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2011      by Alexandre Mendes <alex dot mendes1988 at gmail dot com>
0011  * SPDX-FileCopyrightText: 2011      by Hormiere Guillaume <hormiere dot guillaume at gmail dot com>
0012  * SPDX-FileCopyrightText: 2011      by Manuel Campomanes <campomanes dot manuel at gmail dot com>
0013  *
0014  * SPDX-License-Identifier: GPL-2.0-or-later
0015  *
0016  * ============================================================ */
0017 
0018 #include "mainwindow.h"
0019 #include "ui_mainwindow.h"
0020 
0021 MainWindow::MainWindow(QWidget* const parent)
0022     : QMainWindow(parent),
0023       ui         (new Ui::MainWindow),
0024       MediaWiki  (nullptr)
0025 {
0026     ui->setupUi(this);
0027 }
0028 
0029 MainWindow::~MainWindow()
0030 {
0031     delete ui;
0032 }
0033 
0034 //Load page
0035 void MainWindow::slot_pushButton2_clicked()
0036 {
0037     MediaWiki = new Iface(QUrl(this->ui->mWikiEdit->text()));
0038     QueryRevision* const queryrevision(new QueryRevision(*MediaWiki));
0039     queryrevision->setPageName(this->ui->mPageEdit->text());
0040     queryrevision->setProperties(QueryRevision::Content);
0041     queryrevision->setExpandTemplates(true);
0042     queryrevision->setLimit(1);
0043 
0044     connect(queryrevision, SIGNAL(revision(QList<Revision>)),
0045             this, SLOT(revisionHandle(QList<Revision>)));
0046 
0047     connect(queryrevision, SIGNAL(result(KJob*)),
0048             this, SLOT(revisionError(KJob*)));
0049 
0050     queryrevision->start();
0051 }
0052 
0053 void MainWindow::revisionHandle(const QList<Revision>& revisions)
0054 {
0055     if (revisions.isEmpty())
0056     {
0057         QMessageBox popup;
0058         popup.setText(QLatin1String("This page doesn't exist."));
0059         popup.exec();
0060         return;
0061     }
0062 
0063     this->ui->plainTextEdit->setPlainText(revisions[0].content());
0064 }
0065 
0066 // Send page
0067 
0068 void MainWindow::slot_pushButton1_clicked()
0069 {
0070     Login* const login = new Login(*MediaWiki, this->ui->mLoginEdit->text(), this->ui->mMdpEdit->text());
0071 
0072     connect(login, SIGNAL(result(KJob*)),
0073             this, SLOT(loginHandle(KJob*)));
0074 
0075     login->start();
0076 }
0077 
0078 void MainWindow::loginHandle(KJob* login)
0079 {
0080     if (login->error()!= 0)
0081     {
0082         QMessageBox popup;
0083         popup.setText(QLatin1String("Wrong authentication."));
0084         popup.exec();
0085     }
0086     else
0087     {
0088         Edit* const job = new Edit(*MediaWiki, nullptr);
0089         job->setPageName(this->ui->mPageEdit->text());
0090         job->setText(this->ui->plainTextEdit->toPlainText());
0091 
0092         connect(job, SIGNAL(result(KJob*)),
0093                 this, SLOT(editError(KJob*)));
0094 
0095         job->start();
0096     }
0097 }
0098 
0099 void MainWindow::editError(KJob* job)
0100 {
0101     QString errorMessage;
0102 
0103     if (job->error() == 0)
0104     {
0105         errorMessage = QLatin1String("The Wiki page modified successfully.");
0106     }
0107     else
0108     {
0109         errorMessage = QLatin1String("The Wiki page can't be modified.");
0110     }
0111 
0112     QMessageBox popup;
0113     popup.setText(errorMessage);
0114     popup.exec();
0115 }
0116 
0117 void MainWindow::revisionError(KJob* job)
0118 {
0119     if (job->error() != 0)
0120     {
0121         QMessageBox popup;
0122         popup.setText(QLatin1String("The Wiki page can't be loaded."));
0123         popup.exec();
0124     }
0125 }
0126 
0127 void MainWindow::slot_mPageEdit_textChanged(const QString& text)
0128 {
0129     this->ui->pushButton2->setEnabled(!text.isEmpty() && !text.isNull() && !this->ui->mWikiEdit->text().isEmpty());
0130 }
0131 
0132 void MainWindow::slot_mWikiEdit_textChanged(const QString& text)
0133 {
0134     this->ui->pushButton2->setEnabled(!text.isEmpty() && !text.isNull() && !this->ui->mPageEdit->text().isEmpty());
0135 }
0136 
0137 void MainWindow::slot_plainTextEdit_textChanged()
0138 {
0139     QString text = this->ui->plainTextEdit->toPlainText();
0140     this->ui->pushButton1->setEnabled(!text.isEmpty() && !text.isNull());
0141 }
0142 
0143 #include "moc_mainwindow.cpp"