fileformatmanager.cpp 5.08 KB
Newer Older
1
/*
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 *  Copyright 2012-2014  Andreas Cord-Landwehr <cordlandwehr@kde.org>
 *
 *  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) version 3, or any
 *  later version accepted by the membership of KDE e.V. (or its
 *  successor approved by the membership of KDE e.V.), which shall
 *  act as a proxy defined in Section 6 of version 3 of the license.
 *
 *  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 Lesser General Public
18
 *  License along with this library.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20

21
22
#include "fileformatmanager.h"
#include "fileformatinterface.h"
23
#include "logging_p.h"
24

25
26
#include <KPluginLoader>
#include <KPluginMetaData>
27
#include <KServiceTypeTrader>
Luan Simões's avatar
Luan Simões committed
28
#include <KLocalizedString>
29
#include <QString>
30
31
32
33
#include <QDir>
#include <QDirIterator>
#include <QJsonArray>
#include <QJsonObject>
34
#include <QMessageBox>
35

36
37
38
using namespace GraphTheory;

class GraphTheory::FileFormatManagerPrivate
39
40
{
public:
41
    FileFormatManagerPrivate()
Andreas Cord-Landwehr's avatar
Andreas Cord-Landwehr committed
42
        : defaultGraphFilePlugin(nullptr)
43
    {
44

45
46
    }

47
    ~FileFormatManagerPrivate()
48
49
    { }

50
51
    QList<FileFormatInterface*> backends;
    FileFormatInterface *defaultGraphFilePlugin;
52
53
};

54
55
FileFormatManager::FileFormatManager()
    : d(new FileFormatManagerPrivate)
56
57
58
59
{
    loadBackends();
}

60
FileFormatManager::~FileFormatManager()
61
62
{

63
}
64

65
QList<FileFormatInterface*> FileFormatManager::backends() const
66
67
68
69
{
    return d->backends;
}

70
QList<FileFormatInterface*> FileFormatManager::backends(PluginType type) const
71
{
72
73
    QList<FileFormatInterface*> backends;
    foreach(FileFormatInterface *backend, d->backends) {
74
75
        switch(type) {
            case Import:
76
77
                if (backend->pluginCapability() == FileFormatInterface::ImportOnly
                    || backend->pluginCapability() == FileFormatInterface::ImportAndExport)
78
79
80
81
82
                {
                    backends.append(backend);
                }
                break;
            case Export:
83
84
                if (backend->pluginCapability() == FileFormatInterface::ExportOnly
                    || backend->pluginCapability() == FileFormatInterface::ImportAndExport)
85
86
87
88
89
90
91
92
93
94
95
                {
                    backends.append(backend);
                }
                break;
            default:
                break;
        }
    }
    return backends;
}

96
void FileFormatManager::loadBackends()
97
98
{
    // remove all present backends
99
    foreach(FileFormatInterface *f, d->backends) {
100
101
102
103
        delete f;
    }
    d->backends.clear();

104
105
106
107
108
109
    QVector<KPluginMetaData> metadataList = KPluginLoader::findPlugins(
        "rocs/fileformats",
        [](const KPluginMetaData &data){
            return data.serviceTypes().contains("rocs/graphtheory/fileformat");
        }
    );
110

111
    QPluginLoader loader;
112
113
114
115
116
117
118

    foreach (const auto &metadata, metadataList) {
        loader.setFileName(metadata.fileName());

        qCDebug(GRAPHTHEORY_FILEFORMAT) << "Load Plugin: " << metadata.name();
        if (!loader.load()) {
            qCCritical(GRAPHTHEORY_FILEFORMAT) << "Error while loading plugin: " << metadata.name();
119
        }
120
121
122
123

        KPluginFactory *factory = KPluginLoader(loader.fileName()).factory();
        FileFormatInterface *plugin = factory->create<FileFormatInterface>(this);
        d->backends.append(plugin);
124
125
    }

126
127
128
    // display a QMessageBox if no plugins are found
    if (d->backends.empty()) {
        QMessageBox pluginErrorMessageBox;
Luan Simões's avatar
Luan Simões committed
129
        pluginErrorMessageBox.setWindowTitle(i18n("Plugin Error"));
130
        pluginErrorMessageBox.setTextFormat(Qt::RichText);
Luan Simões's avatar
Luan Simões committed
131
        pluginErrorMessageBox.setText(i18n("Plugins could not be found in specified directories:<br>")+
132
                                         QCoreApplication::libraryPaths().join("/rocs/fileformats<br>")+
133
                                         i18n("<br><br> Check <a href='https://doc.qt.io/qt-5/deployment-plugins.html'>"
Luan Simões's avatar
Luan Simões committed
134
                                         "this link</a> for further information."));
135
136
137
138
139
        pluginErrorMessageBox.setDefaultButton(QMessageBox::Close);
        pluginErrorMessageBox.exec();
        exit(1);
    }

140
    // load static plugins
141
    d->defaultGraphFilePlugin = backendByExtension("graph2");
142
143
}

144
FileFormatInterface * FileFormatManager::backendByExtension(const QString &ext)
145
{
146
    QString suffix = ext.section('.', -1); // get suffix
147
    if (suffix.isEmpty()) {
148
        qCWarning(GRAPHTHEORY_FILEFORMAT) << "File does not contain extension, falling back to default file format";
149
150
        return defaultBackend();
    }
151
    foreach(FileFormatInterface * p,  d->backends) {
152
        if (p->extensions().join(";").contains(suffix, Qt::CaseInsensitive)) {
153
154
155
            return p;
        }
    }
Andreas Cord-Landwehr's avatar
Andreas Cord-Landwehr committed
156
    return nullptr;
157
158
}

159
FileFormatInterface * FileFormatManager::defaultBackend()
160
161
162
{
    return d->defaultGraphFilePlugin;
}