File indexing completed on 2024-05-05 04:38:44

0001 /*
0002     SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_ENVIRONMENTPROFILELIST_H
0008 #define KDEVPLATFORM_ENVIRONMENTPROFILELIST_H
0009 
0010 #include "utilexport.h"
0011 
0012 #include <KSharedConfig>
0013 
0014 class QProcessEnvironment;
0015 class KConfig;
0016 template<typename T1, typename T2> class QMap;
0017 class QString;
0018 class QStringList;
0019 
0020 namespace KDevelop {
0021 class EnvironmentProfileListPrivate;
0022 
0023 /**
0024  * This class manages a list of environment profiles, each profile containing a number
0025  * of environment variables and their values.
0026  *
0027  * The class is constructed from a KConfig object for easy usage in the plugins.
0028  *
0029  * The methods to change the environments is protected to disallow access to those methods
0030  * from plugins, only the environment widget is allowed to change them.
0031  *
0032  * Example Usage
0033  * \code
0034  *   KSharedConfigPtr config = KSharedConfig::openConfig();
0035  *   EnvironmentProfileList env(config);
0036  *   KConfigGroup cfg(config, "QMake Builder");
0037  *   QMap<QString,QString> myenvVars = env.variables( cfg.readEntry("QMake Environment") );
0038  * \endcode
0039  *
0040  * Two entries are used by this class:
0041  * "Default Environment Group" and "Environment Variables".
0042  *
0043  * "Default Environment Variables" stores the default profile that should be used if the
0044  * user didn't select a profile via a plugins configuration dialog.
0045  *
0046  * "Environment Variables" entry stores the actual list of
0047  * <profilename%varname=varvalue>. The profilename can't contain '%' or '='.
0048  * For example, suppose that two configuration, say "release" and "debug" exist.
0049  * Then the actual contents of .kdev4 project file will be
0050  *
0051  * \code
0052  * [Environment Settings]
0053  * Default Environment Group=debug
0054  * Environment Variables=debug_PATH=/home/kde-devel/usr/bin,release_PATH=/usr/bin
0055  * \endcode
0056  *
0057  */
0058 class KDEVPLATFORMUTIL_EXPORT EnvironmentProfileList
0059 {
0060 public:
0061     EnvironmentProfileList(const EnvironmentProfileList& rhs);
0062     EnvironmentProfileList& operator=(const EnvironmentProfileList& rhs);
0063 
0064     /**
0065      * Creates an a list of environment profiles from a KConfig object
0066      * @param config the KConfig object to read the environment profiles from
0067      */
0068     explicit EnvironmentProfileList(const KSharedConfigPtr& config);
0069     explicit EnvironmentProfileList(KConfig* config);
0070     ~EnvironmentProfileList();
0071 
0072     /**
0073      * Creates a merged environment between the defaults specified by
0074      * \a defaultEnvironment and those saved in \a profileName
0075      */
0076     QStringList createEnvironment(const QString& profileName, const QStringList& defaultEnvironment) const;
0077 
0078     /**
0079      * returns the variables that are set for a given profile.
0080      * This function provides read-only access to the environment
0081      * @param profileName  the name of the profile for which the environment should be returned
0082      * @return a map containing the environment variables for this profile, or an empty map if the profile doesn't exist in this list
0083      */
0084     QMap<QString, QString> variables(const QString& profileName) const;
0085 
0086     /**
0087      * returns the name of the default profile
0088      * The default profile should be used by plugins unless the user chooses a different profile
0089      * @return the name of the default profile, defaults to "default"
0090      */
0091     QString defaultProfileName() const;
0092 
0093     /**
0094      * Fetch the list of names of known profiles from the list
0095      * @return the list of profile names
0096      */
0097     QStringList profileNames() const;
0098 
0099 protected:
0100     EnvironmentProfileList();
0101     /**
0102      * returns the variables that are set for a given profile.
0103      * This function provides write access to the environment, so new variables can be inserted, existing ones changed or deleted
0104      *
0105      * If a non-existing profile is specified this returns a new empty map and that way this function can be used to add a new profile
0106      * to the list of environment profiles
0107      * @param profileName  the name of the profile for which the environment should be returned
0108      * @return a map containing the environment variables for this profile, or an empty map if the profile doesn't exist in this list
0109      */
0110     QMap<QString, QString>& variables(const QString& profileName);
0111 
0112     /**
0113      * Changes the default profile.
0114      * @param profileName the name of the new default profile, if a profile of this name doesn't exist the default profile is not changed
0115      */
0116     void setDefaultProfile(const QString& profileName);
0117 
0118     /**
0119      * Stores the environment profiles in this list to the given KConfig object
0120      * @param config a KConfig object to which the environment settings should be stored
0121      */
0122     void saveSettings(KConfig* config) const;
0123 
0124     void loadSettings(KConfig* config);
0125     void removeProfile(const QString& profileName);
0126 
0127 private:
0128     const QScopedPointer<class EnvironmentProfileListPrivate> d_ptr;
0129     Q_DECLARE_PRIVATE(EnvironmentProfileList)
0130 };
0131 
0132 KDEVPLATFORMUTIL_EXPORT void expandVariables(QMap<QString, QString>& variables, const QProcessEnvironment& environment);
0133 
0134 }
0135 
0136 #endif