backend.cpp 5.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA  02110-1301, USA.

    ---
    Copyright (C) 2009 Alexander Rieder <alexanderrieder@gmail.com>
 */

#include "backend.h"
22
#include "extension.h"
23

24
#include <QDir>
Yuri Chornoivan's avatar
Yuri Chornoivan committed
25
#include <QRegularExpression>
26
#include <QUrl>
27

28 29 30 31
#include <KPluginMetaData>
#include <KLocalizedString>

using namespace Cantor;
32

Alexander Rieder's avatar
Alexander Rieder committed
33
class Cantor::BackendPrivate
34 35 36
{
  public:
    QString name;
37
    QString comment;
38
    QString icon;
39
    QString url;
40
    bool enabled{true};
41 42
};

Alexander Semke's avatar
Alexander Semke committed
43
Backend::Backend(QObject* parent, const QList<QVariant>& args) : QObject(parent),
44 45
                                                                d(new BackendPrivate)
{
Anne-Marie Mahfouf's avatar
Anne-Marie Mahfouf committed
46
    Q_UNUSED(args)
47 48 49 50
}

Backend::~Backend()
{
Laurent Montel's avatar
Laurent Montel committed
51
    delete d;
52 53
}

Alexander Rieder's avatar
Alexander Rieder committed
54
QString Backend::name() const
55 56 57 58
{
    return d->name;
}

Alexander Rieder's avatar
Alexander Rieder committed
59
QString Backend::comment() const
60 61 62 63
{
    return d->comment;
}

Alexander Rieder's avatar
Alexander Rieder committed
64
QString Backend::description() const
65
{
66
    return comment();
67 68
}

Alexander Rieder's avatar
Alexander Rieder committed
69
QString Backend::icon() const
70 71 72 73
{
    return d->icon;
}

Alexander Rieder's avatar
Alexander Rieder committed
74
QString Backend::url() const
75 76 77 78
{
    return d->url;
}

Alexander Rieder's avatar
Alexander Rieder committed
79
bool Backend::isEnabled() const
80
{
81
    return d->enabled && requirementsFullfilled();
82 83
}

84
void Backend::setEnabled(bool enabled)
85
{
86
    d->enabled = enabled;
87 88 89 90 91
}

QStringList Backend::listAvailableBackends()
{
    QStringList l;
92
    for (Backend* b : availableBackends())
93 94 95 96
    {
        if(b->isEnabled())
            l<<b->name();
    }
97

98
    return l;
99 100
}

101
QList<Backend*> Backend::availableBackends()
102
{
103
    static QList<Backend*> backendCache;
104 105 106 107 108 109 110
    //if we already have all backends Cached, just return the cache.
    //otherwise create the available backends
    if(!backendCache.isEmpty())
    {
        return backendCache;
    }

111
    QStringList pluginDirs;
112
    for (const QString& dir : QCoreApplication::libraryPaths()){
113 114
        pluginDirs << dir + QDir::separator() + QLatin1String("cantor/backends");
    }
115

116
    QPluginLoader loader;
117
    for (const QString &dir : pluginDirs){
118 119 120 121 122 123
        qDebug() << "dir: " << dir;
        QStringList plugins;
        QDir pluginDir = QDir(dir);

        plugins = pluginDir.entryList();

124
        for (const QString &plugin : plugins){
125 126 127
            if (plugin==QLatin1String(".") || plugin==QLatin1String(".."))
                continue;

128
            loader.setFileName(dir + QDir::separator() + plugin);
129

130 131 132 133 134 135
            if (!loader.load()){
                qDebug() << "Error while loading plugin: " << plugin;
                continue;
            }

            KPluginFactory* factory = KPluginLoader(loader.fileName()).factory();
136
            Backend* backend = factory->create<Backend>(QCoreApplication::instance());
137 138 139 140 141 142 143 144

            KPluginMetaData info(loader);
            backend->d->name=info.name();
            backend->d->comment=info.description();
            backend->d->icon=info.iconName();
            backend->d->url=info.website();
            backendCache<<backend;
        }
145
    }
146

147 148
    return backendCache;
}
149

150
Backend* Backend::getBackend(const QString& name)
151
{
152
    for (Backend* b : availableBackends())
153
    {
154
        if(b->name().toLower()==name.toLower() || b->id().toLower()==name.toLower())
155 156
            return b;
    }
157

158
    return nullptr;
159 160
}

Alexander Rieder's avatar
Alexander Rieder committed
161
QStringList Backend::extensions() const
162
{
163
    QList<Extension*> extensions = findChildren<Extension*>(QRegularExpression(QLatin1String(".*Extension")));
164
    QStringList names;
165 166
    for (Extension* e : extensions)
        names << e->objectName();
167 168 169
    return names;
}

Alexander Rieder's avatar
Alexander Rieder committed
170
Extension* Backend::extension(const QString& name) const
171 172 173 174
{
    return findChild<Extension*>(name);
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
bool Backend::checkExecutable(const QString& name, const QString& path, QString* reason)
{
    if (path.isEmpty())
    {
        if (reason)
            *reason = i18n("No path for the %1 executable specified. "
                        "Please provide the correct path in the application settings and try again.",
                        name);
        return false;
    }

    QFileInfo info(path);
    if (!info.exists())
    {
        if (reason)
            *reason = i18n("The specified file '%1' for the %2 executable doesn't exist. "
                        "Please provide the correct path in the application settings and try again.",
                        path, name);
        return false;
    }

    if (!info.isExecutable())
    {
        if (reason)
Yuri Chornoivan's avatar
Yuri Chornoivan committed
199
            *reason = i18n("The specified file '%1' doesn't point to an executable. "
200
                        "Please provide the correct path in the application settings and try again.",
201
                        path);
202 203 204 205 206
        return false;
    }

    return true;
}