pluginmodel.cpp 5.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/**
 *    Copyright (C) 2020 Savoir-faire Linux Inc.
 *   Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
18 19 20
#include "api/pluginmodel.h"

// Std
21
#include <algorithm> // std::sort
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <chrono>
#include <csignal>
#include <iomanip> // for std::put_time
#include <fstream>
#include <mutex>
#include <thread>
#include <string>
#include <sstream>

// Qt
#include <QtCore/QStandardPaths>
#include <QtCore/QDir>
#include <QUrl>

// Ring daemon

// LRC
#include "dbus/pluginmanager.h"

41
namespace lrc {
42 43 44 45

using namespace api;

PluginModel::PluginModel()
46
    : QObject()
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
{}

PluginModel::~PluginModel() {}

void
PluginModel::setPluginsEnabled(bool enable)
{
    PluginManager::instance().setPluginsEnabled(enable);
}

bool
PluginModel::getPluginsEnabled() const
{
    return PluginManager::instance().getPluginsEnabled();
}

VectorString
PluginModel::listAvailablePlugins() const
{
    return VectorString::fromList(PluginManager::instance().listAvailablePlugins());
}

VectorString
PluginModel::listLoadedPlugins() const
{
    return VectorString::fromList(PluginManager::instance().listLoadedPlugins());
}

plugin::PluginDetails
PluginModel::getPluginDetails(const QString& path)
{
    if (path.isEmpty()) {
        return plugin::PluginDetails();
    }
    MapStringString details = PluginManager::instance().getPluginDetails(path);
    plugin::PluginDetails result;
83
    if (!details.empty()) {
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        result.name = details["name"];
        result.path = path;
        result.iconPath = details["iconPath"];
    }

    VectorString loadedPlugins = listLoadedPlugins();
    if (std::find(loadedPlugins.begin(), loadedPlugins.end(), result.path) != loadedPlugins.end()) {
        result.loaded = true;
    }

    return result;
}

bool
PluginModel::installPlugin(const QString& jplPath, bool force)
{
100
    if (getPluginsEnabled()) {
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        return PluginManager::instance().installPlugin(jplPath, force);
    }
    return false;
}

bool
PluginModel::uninstallPlugin(const QString& rootPath)
{
    return PluginManager::instance().uninstallPlugin(rootPath);
}

bool
PluginModel::loadPlugin(const QString& path)
{
    bool status = PluginManager::instance().loadPlugin(path);
    return status;
}

bool
PluginModel::unloadPlugin(const QString& path)
{
    return PluginManager::instance().unloadPlugin(path);
}

VectorString
PluginModel::listCallMediaHandlers() const
{
    return VectorString::fromList(PluginManager::instance().listCallMediaHandlers());
}

void
PluginModel::toggleCallMediaHandler(const QString& id)
{
    MapStringString toggleInfo = PluginManager::instance().getCallMediaHandlerStatus();
135
    if (toggleInfo["name"] == id) {
136 137
        PluginManager::instance().toggleCallMediaHandler(id, false);
    } else {
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        PluginManager::instance().toggleCallMediaHandler(id, true);
    }
}

MapStringString
PluginModel::getCallMediaHandlerStatus()
{
    return PluginManager::instance().getCallMediaHandlerStatus();
}

plugin::MediaHandlerDetails
PluginModel::getCallMediaHandlerDetails(const QString& id)
{
    if (id.isEmpty()) {
        return plugin::MediaHandlerDetails();
    }
    MapStringString mediaHandlerDetails = PluginManager::instance().getCallMediaHandlerDetails(id);
    plugin::MediaHandlerDetails result;
156
    if (!mediaHandlerDetails.empty()) {
157 158 159
        result.id = id;
        result.iconPath = mediaHandlerDetails["iconPath"];
        result.name = mediaHandlerDetails["name"];
160
        result.pluginId = mediaHandlerDetails["pluginId"];
161 162 163 164 165
    }

    return result;
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
VectorMapStringString
PluginModel::getPluginPreferences(const QString& path)
{
    return PluginManager::instance().getPluginPreferences(path);
}

bool
PluginModel::setPluginPreference(const QString& path, const QString& key, const QString& value)
{
    return PluginManager::instance().setPluginPreference(path, key, value);
}

MapStringString
PluginModel::getPluginPreferencesValues(const QString& path)
{
    return PluginManager::instance().getPluginPreferencesValues(path);
}

bool
PluginModel::resetPluginPreferencesValues(const QString& path)
{
    return PluginManager::instance().resetPluginPreferencesValues(path);
}

190 191
bool
PluginModel::addValueToPreference(const QString& pluginId,
192 193
                                  const QString& preferenceKey,
                                  const QString& value)
194 195 196 197
{
    return PluginManager::instance().addValueToPreference(pluginId, preferenceKey, value);
}

198 199 200 201
} // namespace lrc

#include "api/moc_pluginmodel.cpp"
#include "pluginmodel.moc"