File indexing completed on 2024-04-21 04:38:10

0001 /*
0002     SPDX-FileCopyrightText: 2018 Anton Anikin <anton@anikin.xyz>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "jobparameters.h"
0008 
0009 // plugin
0010 #include "debug.h"
0011 #include "globalsettings.h"
0012 #include "utils.h"
0013 // KF
0014 #include <KLocalizedString>
0015 // Qt
0016 #include <QDir>
0017 #include <QStandardPaths>
0018 
0019 using namespace KDevelop;
0020 
0021 namespace Clazy
0022 {
0023 
0024 JobGlobalParameters::JobGlobalParameters()
0025     : JobGlobalParameters(GlobalSettings::executablePath(), GlobalSettings::docsPath())
0026 {
0027 }
0028 
0029 JobGlobalParameters::JobGlobalParameters(const QUrl& executablePath, const QUrl& docsPath)
0030 {
0031     m_executablePath = executablePath.toLocalFile();
0032     m_docsPath = docsPath.toLocalFile();
0033 
0034     QFileInfo info;
0035 
0036     if (m_executablePath.isEmpty()) {
0037         if (defaultExecutablePath().toLocalFile().isEmpty()) {
0038             m_error = i18n(
0039                 "clazy-standalone path cannot be detected. "
0040                 "Set the path manually if Clazy is already installed.");
0041         } else {
0042             m_error = i18n("clazy-standalone path is empty.");
0043         }
0044         return;
0045     }
0046 
0047     info.setFile(m_executablePath);
0048 
0049     if (!info.exists()) {
0050         m_error = i18n("clazy-standalone path '%1' does not exists.", m_executablePath);
0051         return;
0052     }
0053 
0054     if (!info.isFile() || !info.isExecutable()) {
0055         m_error = i18n("clazy-standalone path '%1' is not an executable.", m_executablePath);
0056         return;
0057     }
0058 
0059     // =============================================================================================
0060 
0061     if (m_docsPath.isEmpty()) {
0062         if (defaultDocsPath().toLocalFile().isEmpty()) {
0063             m_error = i18n(
0064                 "Clazy documentation path cannot be detected. "
0065                 "Set the path manually if Clazy is already installed.");
0066         } else {
0067             m_error = i18n("Clazy documentation path is empty.");
0068         }
0069         return;
0070     }
0071 
0072     info.setFile(m_docsPath);
0073 
0074     if (!info.exists()) {
0075         m_error = i18n("Clazy documentation path '%1' does not exists.", m_docsPath);
0076         return;
0077     }
0078 
0079     if (!info.isDir()) {
0080         m_error = i18n("Clazy documentation path '%1' is not a directory.", m_docsPath);
0081         return;
0082     }
0083 
0084     m_error.clear();
0085 }
0086 
0087 QUrl JobGlobalParameters::defaultExecutablePath()
0088 {
0089     return QUrl::fromLocalFile(QStandardPaths::findExecutable(QStringLiteral("clazy-standalone")));
0090 }
0091 
0092 QUrl JobGlobalParameters::defaultDocsPath()
0093 {
0094     const QString subPathsCandidates[2]{
0095         // since clazy 1.4
0096         QStringLiteral("doc/clazy"),
0097         // before
0098         QStringLiteral("clazy/doc"),
0099     };
0100     for (auto subPath : subPathsCandidates) {
0101         const auto docsPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, subPath, QStandardPaths::LocateDirectory);
0102 
0103         if (!docsPath.isEmpty()) {
0104             return QUrl::fromLocalFile(QDir(docsPath).canonicalPath());
0105         }
0106     }
0107 
0108     return {};
0109 }
0110 
0111 bool JobGlobalParameters::isValid() const
0112 {
0113     return m_error.isEmpty();
0114 }
0115 
0116 QString JobGlobalParameters::error() const
0117 {
0118     return m_error;
0119 }
0120 
0121 }
0122 
0123 #include "moc_jobparameters.cpp"