File indexing completed on 2024-05-05 04:50:02

0001 /*
0002   Copyright (C) 2004, 2005 Benjamin Meyer <ben at meyerhome dot net>
0003 
0004   This program is free software; you can redistribute it and/or modify
0005   it under the terms of the GNU General Public License as published by
0006   the Free Software Foundation; either version 2 of the License, or
0007   (at your option) any later version.
0008 
0009   This program is distributed in the hope that it will be useful,
0010   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012   GNU General Public License for more details.
0013 
0014   You should have received a copy of the GNU General Public License
0015   along with this program; if not, write to the Free Software
0016   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
0017   USA.
0018 */
0019 
0020 #include "audiocdencoder.h"
0021 #include "audiocd_kio_debug.h"
0022 
0023 #include <QCoreApplication>
0024 #include <QDir>
0025 #include <QLibrary>
0026 #include <QRegularExpression>
0027 
0028 Q_LOGGING_CATEGORY(AUDIOCD_KIO_LOG, "kf.kio.workers.audiocd")
0029 
0030 /**
0031  * Attempt to load a plugin and see if it has the symbol
0032  * create_audiocd_encoders.
0033  * @param libFileName file to try to load.
0034  * @returns pointer to the symbol or NULL
0035  */
0036 static QFunctionPointer loadPlugin(const QString &libFileName)
0037 {
0038     qCDebug(AUDIOCD_KIO_LOG) << "Trying to load" << libFileName;
0039     QFunctionPointer cplugin = QLibrary(libFileName).resolve("create_audiocd_encoders");
0040     if (!cplugin)
0041         return nullptr;
0042     qCDebug(AUDIOCD_KIO_LOG) << "Loaded plugin";
0043     return cplugin;
0044 }
0045 
0046 /**
0047  * There might be a "better" way of doing this, but I don't know it
0048  * and I do know that this does work. :)  Feel free to improve the loading
0049  * system, there isn't much code anyway.
0050  */
0051 void AudioCDEncoder::findAllPlugins(KIO::WorkerBase *worker, QList<AudioCDEncoder *> &encoders)
0052 {
0053     QString foundEncoders;
0054 
0055     const auto libraryPaths{QCoreApplication::libraryPaths()};
0056     for (const QString &path : libraryPaths) {
0057         QDir dir(path);
0058         if (!dir.exists()) {
0059             // qCDebug(AUDIOCD_KIO_LOG) << "Library path" << path << "does not exist";
0060             continue;
0061         }
0062 
0063         dir.setFilter(QDir::Files);
0064         const QFileInfoList files = dir.entryInfoList();
0065 
0066         for (const QFileInfo &fi : std::as_const(files)) {
0067             if (fi.fileName().contains(QRegularExpression(QStringLiteral("^libaudiocd_encoder_.*.so$")))) {
0068                 QString fileName = fi.baseName();
0069 
0070                 if (foundEncoders.contains(fileName)) {
0071                     qCWarning(AUDIOCD_KIO_LOG) << "Encoder" << fileName << "found in multiple locations";
0072                     continue;
0073                 }
0074                 foundEncoders.append(fileName);
0075 
0076                 QFunctionPointer function = loadPlugin(fi.absoluteFilePath());
0077                 if (function) {
0078                     void (*functionPointer)(KIO::WorkerBase *, QList<AudioCDEncoder *> &) =
0079                         (void (*)(KIO::WorkerBase * worker, QList<AudioCDEncoder *> & encoders)) function;
0080                     functionPointer(worker, encoders);
0081                 }
0082             }
0083         }
0084     }
0085 }