environment.h 4.43 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
hjk's avatar
hjk committed
30

31 32
#ifndef UTILS_ENVIRONMENT_H
#define UTILS_ENVIRONMENT_H
con's avatar
con committed
33

34
#include "fileutils.h"
35
#include "hostosinfo.h"
36
#include "utils_global.h"
con's avatar
con committed
37

38 39
#include <QMap>
#include <QStringList>
con's avatar
con committed
40

Friedemann Kleint's avatar
Friedemann Kleint committed
41 42
QT_FORWARD_DECLARE_CLASS(QProcessEnvironment)

43
namespace Utils {
con's avatar
con committed
44

45
class QTCREATOR_UTILS_EXPORT EnvironmentItem
con's avatar
con committed
46
{
47
public:
hjk's avatar
hjk committed
48
    EnvironmentItem(const QString &n, const QString &v)
con's avatar
con committed
49 50 51 52 53 54 55
            : name(n), value(v), unset(false)
    {}

    QString name;
    QString value;
    bool unset;

Tobias Hunger's avatar
Tobias Hunger committed
56
    bool operator==(const EnvironmentItem &other) const
57
    {
hjk's avatar
hjk committed
58
        return unset == other.unset && name == other.name && value == other.value;
59 60
    }

61
    static void sort(QList<EnvironmentItem> *list);
hjk's avatar
hjk committed
62 63
    static QList<EnvironmentItem> fromStringList(const QStringList &list);
    static QStringList toStringList(const QList<EnvironmentItem> &list);
con's avatar
con committed
64 65
};

hjk's avatar
hjk committed
66 67
class QTCREATOR_UTILS_EXPORT Environment
{
con's avatar
con committed
68 69 70
public:
    typedef QMap<QString, QString>::const_iterator const_iterator;

71 72
    explicit Environment(OsType osType = HostOsInfo::hostOs()) : m_osType(osType) {}
    explicit Environment(const QStringList &env, OsType osType = HostOsInfo::hostOs());
con's avatar
con committed
73 74
    static Environment systemEnvironment();

75
    QStringList toStringList() const;
76
    QProcessEnvironment toProcessEnvironment() const;
con's avatar
con committed
77 78 79
    QString value(const QString &key) const;
    void set(const QString &key, const QString &value);
    void unset(const QString &key);
hjk's avatar
hjk committed
80
    void modify(const QList<EnvironmentItem> &list);
81 82
    /// Return the Environment changes necessary to modify this into the other environment.
    QList<EnvironmentItem> diff(const Environment &other) const;
83
    bool hasKey(const QString &key) const;
con's avatar
con committed
84

85 86
    QString userName() const;

87 88
    void appendOrSet(const QString &key, const QString &value, const QString &sep = QString());
    void prependOrSet(const QString &key, const QString &value, const QString &sep = QString());
con's avatar
con committed
89 90 91 92

    void appendOrSetPath(const QString &value);
    void prependOrSetPath(const QString &value);

93 94
    void prependOrSetLibrarySearchPath(const QString &value);

con's avatar
con committed
95 96 97 98 99 100 101 102
    void clear();
    int size() const;

    QString key(Environment::const_iterator it) const;
    QString value(Environment::const_iterator it) const;

    Environment::const_iterator constBegin() const;
    Environment::const_iterator constEnd() const;
103
    Environment::const_iterator constFind(const QString &name) const;
con's avatar
con committed
104

105 106
    FileName searchInPath(const QString &executable,
                          const QStringList &additionalDirs = QStringList()) const;
con's avatar
con committed
107
    QStringList path() const;
108
    QStringList appendExeExtensions(const QString &executable) const;
con's avatar
con committed
109

hjk's avatar
hjk committed
110 111
    QString expandVariables(const QString &input) const;
    QStringList expandVariables(const QStringList &input) const;
112

113 114
    bool operator!=(const Environment &other) const;
    bool operator==(const Environment &other) const;
hjk's avatar
hjk committed
115

con's avatar
con committed
116
private:
117
    FileName searchInDirectory(const QStringList &execs, QString directory) const;
con's avatar
con committed
118
    QMap<QString, QString> m_values;
119
    OsType m_osType;
con's avatar
con committed
120 121
};

122
} // namespace Utils
con's avatar
con committed
123

124
#endif // UTILS_ENVIRONMENT_H