Merge remote-tracking branch 'gerrit/master' into containers
[profile/ivi/qtbase.git] / qmake / property.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the qmake application of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "property.h"
43 #include "option.h"
44
45 #include <qdir.h>
46 #include <qsettings.h>
47 #include <qstringlist.h>
48 #include <stdio.h>
49
50 QT_BEGIN_NAMESPACE
51
52 QStringList qmake_mkspec_paths(); //project.cpp
53
54 QMakeProperty::QMakeProperty() : settings(0)
55 {
56 }
57
58 QMakeProperty::~QMakeProperty()
59 {
60     delete settings;
61     settings = 0;
62 }
63
64 void QMakeProperty::initSettings()
65 {
66     if(!settings) {
67         settings = new QSettings(QSettings::UserScope, "Trolltech", "QMake");
68         settings->setFallbacksEnabled(false);
69     }
70 }
71
72 QString
73 QMakeProperty::keyBase(bool version) const
74 {
75     if (version)
76         return QString(qmake_version()) + "/";
77     return QString();
78 }
79
80 QString
81 QMakeProperty::value(QString v, bool just_check)
82 {
83     if(v == "QT_INSTALL_PREFIX")
84         return QLibraryInfo::location(QLibraryInfo::PrefixPath);
85     else if(v == "QT_INSTALL_DATA")
86         return QLibraryInfo::location(QLibraryInfo::DataPath);
87     else if(v == "QT_INSTALL_DOCS")
88         return QLibraryInfo::location(QLibraryInfo::DocumentationPath);
89     else if(v == "QT_INSTALL_HEADERS")
90         return QLibraryInfo::location(QLibraryInfo::HeadersPath);
91     else if(v == "QT_INSTALL_LIBS")
92         return QLibraryInfo::location(QLibraryInfo::LibrariesPath);
93     else if(v == "QT_INSTALL_BINS")
94         return QLibraryInfo::location(QLibraryInfo::BinariesPath);
95     else if(v == "QT_INSTALL_TESTS")
96         return QLibraryInfo::location(QLibraryInfo::TestsPath);
97     else if(v == "QT_INSTALL_PLUGINS")
98         return QLibraryInfo::location(QLibraryInfo::PluginsPath);
99     else if(v == "QT_INSTALL_IMPORTS")
100         return QLibraryInfo::location(QLibraryInfo::ImportsPath);
101     else if(v == "QT_INSTALL_TRANSLATIONS")
102         return QLibraryInfo::location(QLibraryInfo::TranslationsPath);
103     else if(v == "QT_INSTALL_CONFIGURATION")
104         return QLibraryInfo::location(QLibraryInfo::SettingsPath);
105     else if(v == "QT_INSTALL_EXAMPLES")
106         return QLibraryInfo::location(QLibraryInfo::ExamplesPath);
107     else if(v == "QT_INSTALL_DEMOS")
108         return QLibraryInfo::location(QLibraryInfo::ExamplesPath);
109     else if(v == "QMAKE_MKSPECS")
110         return qmake_mkspec_paths().join(Option::dirlist_sep);
111     else if(v == "QMAKE_VERSION")
112         return qmake_version();
113 #ifdef QT_VERSION_STR
114     else if(v == "QT_VERSION")
115         return QT_VERSION_STR;
116 #endif
117
118     initSettings();
119     int slash = v.lastIndexOf('/');
120     QVariant var = settings->value(keyBase(slash == -1) + v);
121     bool ok = var.isValid();
122     QString ret = var.toString();
123     if(!ok) {
124         QString version = qmake_version();
125         if(slash != -1) {
126             version = v.left(slash-1);
127             v = v.mid(slash+1);
128         }
129         settings->beginGroup(keyBase(false));
130         QStringList subs = settings->childGroups();
131         settings->endGroup();
132         subs.sort();
133         for (int x = subs.count() - 1; x >= 0; x--) {
134             QString s = subs[x];
135             if(s.isEmpty() || s > version)
136                 continue;
137             var = settings->value(keyBase(false) + s + "/" + v);
138             ok = var.isValid();
139             ret = var.toString();
140             if (ok) {
141                 if(!just_check)
142                     debug_msg(1, "Fell back from %s -> %s for '%s'.", version.toLatin1().constData(),
143                               s.toLatin1().constData(), v.toLatin1().constData());
144                 return ret;
145             }
146         }
147     }
148     return ok ? ret : QString();
149 }
150
151 bool
152 QMakeProperty::hasValue(QString v)
153 {
154     return !value(v, true).isNull();
155 }
156
157 void
158 QMakeProperty::setValue(QString var, const QString &val)
159 {
160     initSettings();
161     settings->setValue(keyBase() + var, val);
162 }
163
164 void
165 QMakeProperty::remove(const QString &var)
166 {
167     initSettings();
168     settings->remove(keyBase() + var);
169 }
170
171 bool
172 QMakeProperty::exec()
173 {
174     bool ret = true;
175     if(Option::qmake_mode == Option::QMAKE_QUERY_PROPERTY) {
176         if(Option::prop::properties.isEmpty()) {
177             initSettings();
178             settings->beginGroup(keyBase(false));
179             QStringList subs = settings->childGroups();
180             settings->endGroup();
181             subs.sort();
182             for(int x = subs.count() - 1; x >= 0; x--) {
183                 QString s = subs[x];
184                 if(s.isEmpty())
185                     continue;
186                 settings->beginGroup(keyBase(false) + s);
187                 QStringList keys = settings->childKeys();
188                 settings->endGroup();
189                 for(QStringList::ConstIterator it2 = keys.begin(); it2 != keys.end(); it2++) {
190                     QString ret = settings->value(keyBase(false) + s + "/" + (*it2)).toString();
191                     if(s != qmake_version())
192                         fprintf(stdout, "%s/", s.toLatin1().constData());
193                     fprintf(stdout, "%s:%s\n", (*it2).toLatin1().constData(), ret.toLatin1().constData());
194                 }
195             }
196             QStringList specialProps;
197             specialProps.append("QT_INSTALL_PREFIX");
198             specialProps.append("QT_INSTALL_DATA");
199             specialProps.append("QT_INSTALL_DOCS");
200             specialProps.append("QT_INSTALL_HEADERS");
201             specialProps.append("QT_INSTALL_LIBS");
202             specialProps.append("QT_INSTALL_BINS");
203             specialProps.append("QT_INSTALL_TESTS");
204             specialProps.append("QT_INSTALL_PLUGINS");
205             specialProps.append("QT_INSTALL_IMPORTS");
206             specialProps.append("QT_INSTALL_TRANSLATIONS");
207             specialProps.append("QT_INSTALL_CONFIGURATION");
208             specialProps.append("QT_INSTALL_EXAMPLES");
209             specialProps.append("QT_INSTALL_DEMOS");
210             specialProps.append("QMAKE_MKSPECS");
211             specialProps.append("QMAKE_VERSION");
212 #ifdef QT_VERSION_STR
213             specialProps.append("QT_VERSION");
214 #endif
215             foreach (QString prop, specialProps)
216                 fprintf(stdout, "%s:%s\n", prop.toLatin1().constData(), value(prop).toLatin1().constData());
217             return true;
218         }
219         for(QStringList::ConstIterator it = Option::prop::properties.begin();
220             it != Option::prop::properties.end(); it++) {
221             if(Option::prop::properties.count() > 1)
222                 fprintf(stdout, "%s:", (*it).toLatin1().constData());
223             if(!hasValue((*it))) {
224                 ret = false;
225                 fprintf(stdout, "**Unknown**\n");
226             } else {
227                 fprintf(stdout, "%s\n", value((*it)).toLatin1().constData());
228             }
229         }
230     } else if(Option::qmake_mode == Option::QMAKE_SET_PROPERTY) {
231         for(QStringList::ConstIterator it = Option::prop::properties.begin();
232             it != Option::prop::properties.end(); it++) {
233             QString var = (*it);
234             it++;
235             if(it == Option::prop::properties.end()) {
236                 ret = false;
237                 break;
238             }
239             if(!var.startsWith("."))
240                 setValue(var, (*it));
241         }
242     } else if(Option::qmake_mode == Option::QMAKE_UNSET_PROPERTY) {
243         for(QStringList::ConstIterator it = Option::prop::properties.begin();
244             it != Option::prop::properties.end(); it++) {
245             QString var = (*it);
246             if(!var.startsWith("."))
247                 remove(var);
248         }
249     }
250     return ret;
251 }
252
253 QT_END_NAMESPACE