• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mcallegari / qlcplus / 42371974533

07 Nov 2026 12:19PM UTC coverage: 36.826% (-0.1%) from 36.925%
42371974533

push

github

mcallegari
Back to 5.4.0 debug

19248 of 52268 relevant lines covered (36.83%)

41917.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

10.2
/engine/src/serviceloader.cpp
1
/*
2
  Q Light Controller Plus
3
  serviceloader.cpp
4

5
  Copyright (c) Massimo Callegari
6

7
  Licensed under the Apache License, Version 2.0 (the "License");
8
  you may not use this file except in compliance with the License.
9
  You may obtain a copy of the License at
10

11
      http://www.apache.org/licenses/LICENSE-2.0.txt
12

13
  Unless required by applicable law or agreed to in writing, software
14
  distributed under the License is distributed on an "AS IS" BASIS,
15
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
  See the License for the specific language governing permissions and
17
  limitations under the License.
18
*/
19

20
#include <QStandardPaths>
21
#include <QPluginLoader>
22
#include <QSettings>
23
#include <QDebug>
24

25
#include "serviceloader.h"
26
#include "qlcserviceplugin.h"
27
#include "qlcconfig.h"
28
#include "qlcfile.h"
29

30
/** Engine side implementation of the interface handed to the services */
31
class ServiceHost final : public QLCServiceHost
32
{
33
public:
34
    explicit ServiceHost(const ServiceLoader *loader)
220 ✔
35
        : m_loader(loader)
220 ✔
36
    {
37
    }
220 ✔
38

39
    int hostApiVersion() const override
×
40
    {
41
        return QLC_SERVICE_HOST_API_VERSION;
×
42
    }
43

44
    QString hostVersion() const override
×
45
    {
46
        return QString(APPVERSION);
×
47
    }
48

49
    QObject *service(const QString &id) override
×
50
    {
51
        return m_loader->service(id);
×
52
    }
53

54
    QString settingsValue(const QString &key) override
×
55
    {
56
        QSettings settings;
×
57
        return settings.value(QString("services/%1").arg(key)).toString();
×
58
    }
×
59

60
    QString userDataDir(const QString &serviceId) override
×
61
    {
62
        QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
×
63
        QString path = QString("services/%1").arg(serviceId);
×
64
        dir.mkpath(path);
×
65
        return dir.absoluteFilePath(path);
×
66
    }
×
67

68
    void log(int level, const QString &message) override
×
69
    {
70
        if (level <= 0)
×
71
            qDebug().noquote() << "[Service]" << message;
×
72
        else if (level == 1)
×
73
            qInfo().noquote() << "[Service]" << message;
×
74
        else
75
            qWarning().noquote() << "[Service]" << message;
×
76
    }
×
77

78
private:
79
    const ServiceLoader *m_loader;
80
};
81

82
ServiceLoader::ServiceLoader(QObject *parent)
220 ✔
83
    : QObject(parent)
84
    , m_host(new ServiceHost(this))
220 ✔
85
{
86
}
220 ✔
87

88
ServiceLoader::~ServiceLoader()
440 ✔
89
{
90
    while (m_entries.isEmpty() == false)
220 ✔
91
    {
92
        Entry entry = m_entries.takeLast();
×
93
        entry.plugin->shutdown();
×
94
        entry.loader->unload();
×
95
        delete entry.loader;
×
96
    }
97
    delete m_host;
220 ✔
98
}
440 ✔
99

100
void ServiceLoader::load(const QDir &dir)
×
101
{
102
    qDebug() << Q_FUNC_INFO << dir.path();
×
103

104
    /* Check that we can access the directory */
105
    if (dir.exists() == false || dir.isReadable() == false)
×
106
        return;
×
107

108
    /* Pass 1: instantiate the plugins and register their ids */
109
    QList<Entry> loaded;
×
110
    const QStringList files = dir.entryList();
×
111
    for (const QString &fileName : files)
×
112
    {
113
        QString path = dir.absoluteFilePath(fileName);
×
114
        QPluginLoader *loader = new QPluginLoader(path, this);
×
115
        QLCServicePlugin *ptr = qobject_cast<QLCServicePlugin*>(loader->instance());
×
116

117
        if (ptr == nullptr)
×
118
        {
119
            qWarning() << Q_FUNC_INFO << fileName << "doesn't contain a service plugin:"
×
120
                       << loader->errorString();
×
121
            loader->unload();
×
122
            delete loader;
×
123
            continue;
×
124
        }
125

126
        if (ptr->minHostApiVersion() > QLC_SERVICE_HOST_API_VERSION)
×
127
        {
128
            qWarning() << Q_FUNC_INFO << "Service" << ptr->serviceId() << "in" << fileName
×
129
                       << "requires host API" << ptr->minHostApiVersion()
×
130
                       << "but this is" << QLC_SERVICE_HOST_API_VERSION;
×
131
            loader->unload();
×
132
            delete loader;
×
133
            continue;
×
134
        }
135

136
        if (hasService(ptr->serviceId()))
×
137
        {
138
            qWarning() << Q_FUNC_INFO << "Discarded duplicate service"
×
139
                       << ptr->serviceId() << "in" << path;
×
140
            loader->unload();
×
141
            delete loader;
×
142
            continue;
×
143
        }
144

145
        Entry entry = { loader, ptr };
×
146
        m_entries.append(entry);
×
147
        loaded.append(entry);
×
148
    }
×
149

150
    /* Pass 2: initialize them. Now every service can see the others */
151
    for (const Entry &entry : loaded)
×
152
    {
153
        QString id = entry.plugin->serviceId();
×
154

155
        if (entry.plugin->init(m_host) == false)
×
156
        {
157
            qWarning() << Q_FUNC_INFO << "Service" << id << "refused to initialize";
×
158
            m_entries.removeOne(entry);
×
159
            entry.loader->unload();
×
160
            delete entry.loader;
×
161
            continue;
×
162
        }
163

164
        qInfo() << "Loaded service plugin" << entry.plugin->name()
×
165
                << entry.plugin->version() << "(" << id << ")";
×
166
        emit serviceAvailable(id);
×
167
    }
×
168
}
×
169

170
QList<QLCServicePlugin*> ServiceLoader::plugins() const
×
171
{
172
    QList<QLCServicePlugin*> list;
×
173
    for (const Entry &entry : m_entries)
×
174
        list.append(entry.plugin);
×
175
    return list;
×
176
}
×
177

178
bool ServiceLoader::hasService(const QString &id) const
×
179
{
180
    return service(id) != nullptr;
×
181
}
182

183
QObject *ServiceLoader::service(const QString &id) const
×
184
{
185
    for (const Entry &entry : m_entries)
×
186
    {
187
        if (entry.plugin->serviceId() == id)
×
188
            return entry.plugin->service();
×
189
    }
190
    return nullptr;
×
191
}
192

193
QDir ServiceLoader::systemServiceDirectory()
×
194
{
195
    return QLCFile::systemDirectory(QString("%1/services").arg(PLUGINDIR), KExtPlugin);
×
196
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc