File indexing completed on 2024-04-28 16:52:16

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2011 Craig Drummond <craig.p.drummond@gmail.com>
0003 // SPDX-FileCopyrightText: 2018 Alexis Lopes Zubeta <contact@azubieta.net>
0004 // SPDX-FileCopyrightText: 2020 Tomaz Canabrava <tcanabrava@kde.org>
0005 /*
0006  * UFW KControl Module
0007  */
0008 #include <KLocalizedString>
0009 #include <QProcess>
0010 
0011 #include <QStandardPaths>
0012 
0013 #include "ifirewallclientbackend.h"
0014 
0015 IFirewallClientBackend::IFirewallClientBackend(QObject *parent, const QVariantList &)
0016     : QObject(parent){};
0017 
0018 void IFirewallClientBackend::setProfiles(const QVector<Entry> &profiles)
0019 {
0020     m_profiles = profiles;
0021     std::sort(std::begin(m_profiles), std::end(m_profiles));
0022 }
0023 
0024 Entry IFirewallClientBackend::profileByName(const QString &name)
0025 {
0026     auto it = std::find_if(std::begin(m_profiles), std::end(m_profiles), [&name](const Entry &entry) {
0027         return entry.name == name;
0028     });
0029 
0030     if (it != std::end(m_profiles)) {
0031         return *it;
0032     }
0033 
0034     return Entry({});
0035 }
0036 
0037 FirewallClient::Capabilities IFirewallClientBackend::capabilities() const
0038 {
0039     return FirewallClient::None;
0040 }
0041 
0042 // just implement it when needed.
0043 KJob *IFirewallClientBackend::save()
0044 {
0045     return nullptr;
0046 }
0047 
0048 void IFirewallClientBackend::queryExecutable(const QString &executableName)
0049 {
0050     // sometimes ufw is not installed on a standard path - like on opensuse, that's installed on /usr/sbin
0051     // so, look at there too.
0052     static QStringList searchPaths = {
0053         QStringLiteral("/bin"),
0054         QStringLiteral("/usr/bin"),
0055         QStringLiteral("/usr/sbin"),
0056     };
0057 
0058     m_executablePath = QStandardPaths::findExecutable(executableName);
0059     if (!m_executablePath.isEmpty()) {
0060         return;
0061     }
0062     m_executablePath = QStandardPaths::findExecutable(executableName, searchPaths);
0063 }
0064 
0065 bool IFirewallClientBackend::hasExecutable() const
0066 {
0067     return !m_executablePath.isEmpty();
0068 }
0069 
0070 QString IFirewallClientBackend::executablePath() const
0071 {
0072     return m_executablePath;
0073 }