File indexing completed on 2024-04-21 04:34:33

0001 /*
0002  * This file is part of KDevelop Krazy2 Plugin.
0003  *
0004  * Copyright 2012 Daniel Calviño Sánchez <danxuliu@gmail.com>
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License
0008  * as published by the Free Software Foundation; either version 2
0009  * of the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program. If not, see <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "checkerlistjob.h"
0021 
0022 #include <KConfigGroup>
0023 #include <KLocalizedString>
0024 #include <KSharedConfig>
0025 
0026 #include "checkerlistparser.h"
0027 #include "./common.h"
0028 
0029 #include <QUrl>
0030 
0031 //public:
0032 
0033 CheckerListJob::CheckerListJob(QObject* parent /*= 0*/): KJob(parent),
0034     m_checkerList(nullptr),
0035     m_process(new QProcess(this)) {
0036 
0037     setObjectName(xi18nc("@action:inmenu", "Find available checkers for <command>krazy2</command>"));
0038 
0039     connect(m_process, SIGNAL(finished(int)),
0040             this, SLOT(handleProcessFinished(int)));
0041     connect(m_process, SIGNAL(error(QProcess::ProcessError)),
0042             this, SLOT(handleProcessError(QProcess::ProcessError)));
0043 
0044     setCapabilities(Killable);
0045 }
0046 
0047 void CheckerListJob::start() {
0048     Q_ASSERT(m_checkerList);
0049 
0050     KConfigGroup krazy2Configuration = KSharedConfig::openConfig()->group("Krazy2");
0051     QUrl krazy2Url = krazy2Configuration.readEntry("krazy2 Path");
0052     QString krazy2Path = urlToString(krazy2Url);
0053     if(krazy2Path.isEmpty())
0054         krazy2Path = defaultPath();
0055 
0056     QStringList arguments;
0057     arguments << "--list";
0058     arguments << "--explain";
0059     arguments << "--export" << "xml";
0060 
0061     m_process->setProgram(krazy2Path);
0062     m_process->setArguments(arguments);
0063     m_process->setReadChannelMode(QProcess::SeparateChannels);
0064 
0065     m_process->start();
0066 }
0067 
0068 void CheckerListJob::setCheckerList(QList<const Checker*>* checkerList) {
0069     m_checkerList = checkerList;
0070 }
0071 
0072 //protected:
0073 
0074 bool CheckerListJob::doKill() {
0075     if (m_process) {
0076         m_process->kill();
0077     }
0078 
0079     return true;
0080 }
0081 
0082 //private slots:
0083 
0084 void CheckerListJob::handleProcessFinished(int exitCode) {
0085     Q_UNUSED(exitCode);
0086 
0087     CheckerListParser checkerListParser;
0088     checkerListParser.setCheckerList(m_checkerList);
0089     checkerListParser.parse(m_process->readAllStandardOutput());
0090 
0091     emitResult();
0092 }
0093 
0094 void CheckerListJob::handleProcessError(QProcess::ProcessError processError) {
0095     setError(UserDefinedError);
0096 
0097     if (processError == QProcess::FailedToStart && m_process->program().isEmpty()) {
0098         setErrorText(xi18nc("@info", "<para>There is no path set in the Krazy2 configuration "
0099                                      "for the <command>krazy2</command> executable.</para>"));
0100     } else if (processError == QProcess::FailedToStart) {
0101         setErrorText(xi18nc("@info", "<para><command>krazy2</command> failed to start "
0102                                      "using the path "
0103                                      "(<filename>%1</filename>).</para>", m_process->program()));
0104     } else if (processError == QProcess::Crashed) {
0105         setErrorText(xi18nc("@info", "<para><command>krazy2</command> crashed.</para>"));
0106     } else {
0107         setErrorText(xi18nc("@info", "<para>An error occurred while executing "
0108                                      "<command>krazy2</command>.</para>"));
0109     }
0110 
0111     emitResult();
0112 }