File indexing completed on 2024-04-21 04:49:09

0001 /*
0002     SPDX-FileCopyrightText: 2003-2009 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-FileCopyrightText: 2010 Michal Malek <michalm@jabster.pl>
0004     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef K3B_EXTERNAL_BIN_MANAGER_H
0010 #define K3B_EXTERNAL_BIN_MANAGER_H
0011 
0012 #include "k3b_export.h"
0013 #include "k3bversion.h"
0014 
0015 #include <QMap>
0016 #include <QObject>
0017 #include <QString>
0018 #include <QStringList>
0019 
0020 class KConfigGroup;
0021 
0022 
0023 namespace K3b {
0024     class ExternalProgram;
0025 
0026     /**
0027      * A ExternalBin represents an installed version of a program.
0028      * All ExternalBin objects are managed by ExternalPrograms.
0029      *
0030      * A bin may have certain features that are represented by a string.
0031      */
0032     class LIBK3B_EXPORT ExternalBin
0033     {
0034     public:
0035         ExternalBin( ExternalProgram& program, const QString& path );
0036         virtual ~ExternalBin();
0037 
0038         void setNeedGroup( const QString& name );
0039         const QString& needGroup() const;
0040 
0041         void setVersion( const Version& version );
0042         const Version& version() const;
0043 
0044         void setCopyright( const QString& copyright );
0045         const QString& copyright() const;
0046 
0047         const QString& path() const;
0048         QString name() const;
0049         bool isEmpty() const;
0050         QStringList userParameters() const;
0051         QStringList features() const;
0052 
0053         bool hasFeature( const QString& ) const;
0054         void addFeature( const QString& );
0055 
0056         ExternalProgram& program() const;
0057 
0058     private:
0059         class Private;
0060         Private* const d;
0061     };
0062 
0063 
0064     /**
0065      * This is the main class that represents a program
0066      * It's scan method has to be reimplemented for every program
0067      * It manages a list of ExternalBin-objects that each represent
0068      * one installed version of the program.
0069      */
0070     class LIBK3B_EXPORT ExternalProgram
0071     {
0072     public:
0073         explicit ExternalProgram( const QString& name );
0074         virtual ~ExternalProgram();
0075 
0076         const ExternalBin* defaultBin() const;
0077         const ExternalBin* mostRecentBin() const;
0078 
0079         void addUserParameter( const QString& );
0080         void setUserParameters( const QStringList& list );
0081 
0082         QStringList userParameters() const;
0083         QString name() const;
0084 
0085         void addBin( ExternalBin* );
0086         void clear();
0087         void setDefault( const ExternalBin* );
0088         void setDefault( const QString& path );
0089 
0090         QList<const ExternalBin*> bins() const;
0091 
0092         /**
0093          * this scans for the program in the given path,
0094          * adds the found bin object to the list and returns true.
0095          * if nothing could be found false is returned.
0096          */
0097         virtual bool scan( const QString& ) = 0;
0098 
0099         /**
0100          * reimplement this if it does not make sense to have the user be able
0101          * to specify additional parameters
0102          */
0103         virtual bool supportsUserParameters() const;
0104 
0105         /**
0106          * Builds the path to the program from the \p dir and the \p programName.
0107          * On Windows the .exe extension is added automatically.
0108          */
0109         static QString buildProgramPath( const QString& dir, const QString& programName );
0110 
0111     private:
0112         class Private;
0113         Private* const d;
0114     };
0115 
0116 
0117     /**
0118      * Simple implementation of the ExternalProgram scan functionality based on calling the
0119      * program twice: once for the version and once for the features.
0120      */
0121     class LIBK3B_EXPORT SimpleExternalProgram : public ExternalProgram
0122     {
0123     public:
0124         explicit SimpleExternalProgram( const QString& name );
0125         ~SimpleExternalProgram() override;
0126 
0127         bool scan( const QString& path ) override;
0128 
0129         /**
0130          * Parses a version starting at \p pos by looking for the first digit
0131          * followed by the first space char.
0132          */
0133         static Version parseVersionAt( const QString& data, int pos );
0134 
0135     protected:
0136         /**
0137          * Build the program's path. The default implementation simply calls
0138          * buildProgramPath on the program's name and the given path.
0139          */
0140         virtual QString getProgramPath( const QString& dir ) const;
0141 
0142         /**
0143          * Scan the version. The default implementation calls the program with
0144          * parameter --version and then calls parseVersion and parseCopyright.
0145          */
0146         virtual bool scanVersion( ExternalBin& bin ) const;
0147 
0148         /**
0149          * Scan for features. The default implementation checks for suidroot and
0150          * calls the program with parameter --help and then calls parseFeatures.
0151          */
0152         virtual bool scanFeatures( ExternalBin& bin ) const;
0153 
0154         /**
0155          * Determine the version from the program's version output.
0156          * The default implementation searches for the program's name
0157          * and parses the version from there.
0158          */
0159         virtual Version parseVersion( const QString& output, const ExternalBin& bin ) const;
0160 
0161         /**
0162          * Determine the copyright statement from the program's version output.
0163          * The default implementation looks for "(C)" and uses the rest of the line
0164          * from there.
0165          */
0166         virtual QString parseCopyright( const QString& output, const ExternalBin& bin ) const;
0167 
0168         /**
0169          * Parse the features from the \p output of --help and add them to the \p bin.
0170          * The default implementation does nothing.
0171          */
0172         virtual void parseFeatures( const QString& output, ExternalBin& bin ) const;
0173 
0174         /**
0175          * @return the string preceding the actual version string, used by
0176          *         parseVersion() method. Default implementation returns name().
0177          */
0178         virtual QString versionIdentifier( const ExternalBin& bin ) const;
0179 
0180     private:
0181         class Private;
0182         Private* const d;
0183     };
0184 
0185 
0186     class LIBK3B_EXPORT ExternalBinManager : public QObject
0187     {
0188         Q_OBJECT
0189 
0190     public:
0191         explicit ExternalBinManager( QObject* parent = 0 );
0192         ~ExternalBinManager() override;
0193 
0194         void search();
0195 
0196         /**
0197          * read config and add changes to current map.
0198          * Takes care of setting the config group
0199          */
0200         bool readConfig( const KConfigGroup& );
0201 
0202         /**
0203          * Takes care of setting the config group
0204          */
0205         bool saveConfig( KConfigGroup );
0206 
0207         bool foundBin( const QString& name );
0208         QString binPath( const QString& name );
0209         const ExternalBin* binObject( const QString& name );
0210         QString binNeedGroup( const QString& name );
0211         const ExternalBin* mostRecentBinObject( const QString& name );
0212 
0213         ExternalProgram* program( const QString& ) const;
0214         QMap<QString, ExternalProgram*> programs() const;
0215 
0216         /** always extends the default searchpath */
0217         void setSearchPath( const QStringList& );
0218         void addSearchPath( const QString& );
0219         void loadDefaultSearchPath();
0220 
0221         QStringList searchPath() const;
0222 
0223         void addProgram( ExternalProgram* );
0224         void clear();
0225 
0226     private:
0227         class Private;
0228         Private* const d;
0229     };
0230 }
0231 
0232 #endif