| 1 |
/* |
| 2 |
Copyright (C) 2004, 2005 Benjamin Meyer <ben at meyerhome dot net> |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 17 |
USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "audiocdencoder.h" |
| 21 |
#include "audiocd_kio_debug.h" |
| 22 |
|
| 23 |
#include <QCoreApplication> |
| 24 |
#include <QDir> |
| 25 |
#include <QLibrary> |
| 26 |
#include <QRegularExpression> |
| 27 |
|
| 28 |
Q_LOGGING_CATEGORY(AUDIOCD_KIO_LOG, "kf.kio.workers.audiocd") |
| 29 |
|
| 30 |
/** |
| 31 |
* Attempt to load a plugin and see if it has the symbol |
| 32 |
* create_audiocd_encoders. |
| 33 |
* @param libFileName file to try to load. |
| 34 |
* @returns pointer to the symbol or NULL |
| 35 |
*/ |
| 36 |
static QFunctionPointer loadPlugin(const QString &libFileName) |
| 37 |
{ |
| 38 |
qCDebug(AUDIOCD_KIO_LOG) << "Trying to load" << libFileName; |
| 39 |
QFunctionPointer cplugin = QLibrary(libFileName).resolve("create_audiocd_encoders"); |
| 40 |
if (!cplugin) |
| 41 |
return nullptr; |
| 42 |
qCDebug(AUDIOCD_KIO_LOG) << "Loaded plugin"; |
| 43 |
return cplugin; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* There might be a "better" way of doing this, but I don't know it |
| 48 |
* and I do know that this does work. :) Feel free to improve the loading |
| 49 |
* system, there isn't much code anyway. |
| 50 |
*/ |
| 51 |
void AudioCDEncoder::findAllPlugins(KIO::WorkerBase *worker, QList<AudioCDEncoder *> &encoders) |
| 52 |
{ |
| 53 |
QString foundEncoders; |
| 54 |
|
| 55 |
const auto libraryPaths{QCoreApplication::libraryPaths()}; |
| 56 |
for (const QString &path : libraryPaths) { |
| 57 |
QDir dir(path); |
| 58 |
if (!dir.exists()) { |
| 59 |
// qCDebug(AUDIOCD_KIO_LOG) << "Library path" << path << "does not exist"; |
| 60 |
continue; |
| 61 |
} |
| 62 |
|
| 63 |
dir.setFilter(QDir::Files); |
| 64 |
const QFileInfoList files = dir.entryInfoList(); |
| 65 |
|
| 66 |
for (const QFileInfo &fi : std::as_const(files)) { |
| 67 |
if (fi.fileName().contains(QRegularExpression(QStringLiteral("^libaudiocd_encoder_.*.so$")))) { |
| 68 |
QString fileName = fi.baseName(); |
| 69 |
|
| 70 |
if (foundEncoders.contains(fileName)) { |
| 71 |
qCWarning(AUDIOCD_KIO_LOG) << "Encoder" << fileName << "found in multiple locations"; |
| 72 |
continue; |
| 73 |
} |
| 74 |
foundEncoders.append(fileName); |
| 75 |
|
| 76 |
QFunctionPointer function = loadPlugin(fi.absoluteFilePath()); |
| 77 |
if (function) { |
| 78 |
void (*functionPointer)(KIO::WorkerBase *, QList<AudioCDEncoder *> &) = |
| 79 |
(void (*)(KIO::WorkerBase *worker, QList<AudioCDEncoder *> &encoders))function; |
| 80 |
functionPointer(worker, encoders); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |