File indexing completed on 2024-05-05 16:49:31

0001 /** ===========================================================
0002  * @file
0003  *
0004  * This file is a part of KDE project
0005  * <a href="https://commits.kde.org/libmediawiki">libmediawiki</a>
0006  *
0007  * @date   2011-03-22
0008  * @brief  a MediaWiki C++ interface for KDE
0009  *
0010  * @author Copyright (C) 2011-2013 by Gilles Caulier
0011  *         <a href="mailto:caulier dot gilles at gmail dot com">caulier dot gilles at gmail dot com</a>
0012  * @author Copyright (C) 2010 by Alexandre Mendes
0013  *         <a href="mailto:alex dot mendes1988 at gmail dot com">alex dot mendes1988 at gmail dot com</a>
0014  * @author Copyright (C) 2011 by Guillaume Hormiere
0015  *         <a href="mailto:hormiere dot guillaume at gmail dot com">hormiere dot guillaume at gmail dot com</a>
0016  *
0017  * This program is free software; you can redistribute it
0018  * and/or modify it under the terms of the GNU General
0019  * Public License as published by the Free Software Foundation;
0020  * either version 2, or (at your option)
0021  * any later version.
0022  *
0023  * This program is distributed in the hope that it will be useful,
0024  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0025  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0026  * GNU General Public License for more details.
0027  *
0028  * ============================================================ */
0029 
0030 #include "mainwindow.h"
0031 #include "ui_mainwindow.h"
0032 
0033 MainWindow::MainWindow(QWidget* const parent)
0034     : QMainWindow(parent),
0035       ui(new Ui::MainWindow),
0036       mediawiki(nullptr)
0037 {
0038     ui->setupUi(this);
0039 }
0040 
0041 MainWindow::~MainWindow()
0042 {
0043     delete ui;
0044 }
0045 
0046 //Load page
0047 void MainWindow::on_pushButton2_clicked()
0048 {
0049     mediawiki = new MediaWiki(QUrl(this->ui->mWikiEdit->text()));
0050     QueryRevision* const queryrevision(new QueryRevision(*mediawiki));
0051     queryrevision->setPageName(this->ui->mPageEdit->text());
0052     queryrevision->setProperties(QueryRevision::Content);
0053     queryrevision->setExpandTemplates(true);
0054     queryrevision->setLimit(1);
0055 
0056     connect(queryrevision, SIGNAL(revision(QList<Revision>)),
0057             this, SLOT(revisionHandle(QList<Revision>)));
0058 
0059     connect(queryrevision, SIGNAL(result(KJob*)),
0060             this, SLOT(revisionError(KJob*)));
0061 
0062     queryrevision->start();
0063 }
0064 
0065 void MainWindow::revisionHandle(const QList<Revision>& revisions)
0066 {
0067     if(revisions.isEmpty())
0068     {
0069         QMessageBox popup;
0070         popup.setText("This page doesn't exist.");
0071         popup.exec();
0072         return;
0073     }
0074 
0075     this->ui->plainTextEdit->setPlainText(revisions[0].content().toUtf8());
0076 }
0077 
0078 //Send page
0079 void MainWindow::on_pushButton1_clicked()
0080 {
0081     Login* const login = new Login(*mediawiki, this->ui->mLoginEdit->text(), this->ui->mMdpEdit->text());
0082 
0083     connect(login, SIGNAL(result(KJob*)),
0084             this, SLOT(loginHandle(KJob*)));
0085 
0086     login->start();
0087 }
0088 
0089 void MainWindow::loginHandle(KJob* login)
0090 {
0091     if(login->error()!= 0)
0092     {
0093         QMessageBox popup;
0094         popup.setText("Wrong authentication.");
0095         popup.exec();
0096     }
0097     else
0098     {
0099         Edit* const job = new Edit(*mediawiki, nullptr);
0100         job->setPageName(this->ui->mPageEdit->text());
0101         job->setText(this->ui->plainTextEdit->toPlainText());
0102 
0103         connect(job, SIGNAL(result(KJob*)),
0104                 this, SLOT(editError(KJob*)));
0105 
0106         job->start();
0107     }
0108 }
0109 
0110 void MainWindow::editError(KJob* job)
0111 {
0112     QString errorMessage;
0113 
0114     if(job->error() == 0)
0115         errorMessage = "The Wiki page modified successfully.";
0116     else
0117         errorMessage = "The Wiki page can't be modified.";
0118 
0119     QMessageBox popup;
0120     popup.setText(errorMessage);
0121     popup.exec();
0122 }
0123 
0124 void MainWindow::revisionError(KJob* job)
0125 {
0126     if(job->error() != 0)
0127     {
0128         QMessageBox popup;
0129         popup.setText("The Wiki page can't be loaded.");
0130         popup.exec();
0131     }
0132 }
0133 
0134 void MainWindow::on_mPageEdit_textChanged(QString text)
0135 {
0136     this->ui->pushButton2->setEnabled(!text.isEmpty() && !text.isNull() && !this->ui->mWikiEdit->text().isEmpty());
0137 }
0138 
0139 void MainWindow::on_mWikiEdit_textChanged(QString text)
0140 {
0141     this->ui->pushButton2->setEnabled(!text.isEmpty() && !text.isNull() && !this->ui->mPageEdit->text().isEmpty());
0142 }
0143 
0144 void MainWindow::on_plainTextEdit_textChanged()
0145 {
0146     QString text = this->ui->plainTextEdit->toPlainText();
0147     this->ui->pushButton1->setEnabled(!text.isEmpty() && !text.isNull());
0148 }