Update copyright year in license headers.
[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 <qmap.h>
47 #include <qsettings.h>
48 #include <qstringlist.h>
49 #include <stdio.h>
50
51 QT_BEGIN_NAMESPACE
52
53 QStringList qmake_mkspec_paths(); //project.cpp
54
55 QMakeProperty::QMakeProperty() : settings(0)
56 {
57 }
58
59 QMakeProperty::~QMakeProperty()
60 {
61     delete settings;
62     settings = 0;
63 }
64
65 void QMakeProperty::initSettings()
66 {
67     if(!settings) {
68         settings = new QSettings(QSettings::UserScope, "Trolltech", "QMake");
69         settings->setFallbacksEnabled(false);
70     }
71 }
72
73 QString
74 QMakeProperty::keyBase(bool version) const
75 {
76     if (version)
77         return QString(qmake_version()) + "/";
78     return QString();
79 }
80
81 QString
82 QMakeProperty::value(QString v, bool just_check)
83 {
84     if(v == "QT_INSTALL_PREFIX")
85         return QLibraryInfo::location(QLibraryInfo::PrefixPath);
86     else if(v == "QT_INSTALL_DATA")
87         return QLibraryInfo::location(QLibraryInfo::DataPath);
88     else if(v == "QT_INSTALL_DOCS")
89         return QLibraryInfo::location(QLibraryInfo::DocumentationPath);
90     else if(v == "QT_INSTALL_HEADERS")
91         return QLibraryInfo::location(QLibraryInfo::HeadersPath);
92     else if(v == "QT_INSTALL_LIBS")
93         return QLibraryInfo::location(QLibraryInfo::LibrariesPath);
94     else if(v == "QT_INSTALL_BINS")
95         return QLibraryInfo::location(QLibraryInfo::BinariesPath);
96     else if(v == "QT_INSTALL_TESTS")
97         return QLibraryInfo::location(QLibraryInfo::TestsPath);
98     else if(v == "QT_INSTALL_PLUGINS")
99         return QLibraryInfo::location(QLibraryInfo::PluginsPath);
100     else if(v == "QT_INSTALL_IMPORTS")
101         return QLibraryInfo::location(QLibraryInfo::ImportsPath);
102     else if(v == "QT_INSTALL_TRANSLATIONS")
103         return QLibraryInfo::location(QLibraryInfo::TranslationsPath);
104     else if(v == "QT_INSTALL_CONFIGURATION")
105         return QLibraryInfo::location(QLibraryInfo::SettingsPath);
106     else if(v == "QT_INSTALL_EXAMPLES")
107         return QLibraryInfo::location(QLibraryInfo::ExamplesPath);
108     else if(v == "QT_INSTALL_DEMOS")
109         return QLibraryInfo::location(QLibraryInfo::ExamplesPath);
110     else if(v == "QMAKE_MKSPECS")
111         return qmake_mkspec_paths().join(Option::dirlist_sep);
112     else if(v == "QMAKE_VERSION")
113         return qmake_version();
114 #ifdef QT_VERSION_STR
115     else if(v == "QT_VERSION")
116         return QT_VERSION_STR;
117 #endif
118
119     initSettings();
120     int slash = v.lastIndexOf('/');
121     QVariant var = settings->value(keyBase(slash == -1) + v);
122     bool ok = var.isValid();
123     QString ret = var.toString();
124     if(!ok) {
125         QString version = qmake_version();
126         if(slash != -1) {
127             version = v.left(slash-1);
128             v = v.mid(slash+1);
129         }
130         settings->beginGroup(keyBase(false));
131         QStringList subs = settings->childGroups();
132         settings->endGroup();
133         subs.sort();
134         for (int x = subs.count() - 1; x >= 0; x--) {
135             QString s = subs[x];
136             if(s.isEmpty() || s > version)
137                 continue;
138             var = settings->value(keyBase(false) + s + "/" + v);
139             ok = var.isValid();
140             ret = var.toString();
141             if (ok) {
142                 if(!just_check)
143                     debug_msg(1, "Fell back from %s -> %s for '%s'.", version.toLatin1().constData(),
144                               s.toLatin1().constData(), v.toLatin1().constData());
145                 return ret;
146             }
147         }
148     }
149     return ok ? ret : QString();
150 }
151
152 bool
153 QMakeProperty::hasValue(QString v)
154 {
155     return !value(v, true).isNull();
156 }
157
158 void
159 QMakeProperty::setValue(QString var, const QString &val)
160 {
161     initSettings();
162     settings->setValue(keyBase() + var, val);
163 }
164
165 void
166 QMakeProperty::remove(const QString &var)
167 {
168     initSettings();
169     settings->remove(keyBase() + var);
170 }
171
172 bool
173 QMakeProperty::exec()
174 {
175     bool ret = true;
176     if(Option::qmake_mode == Option::QMAKE_QUERY_PROPERTY) {
177         if(Option::prop::properties.isEmpty()) {
178             initSettings();
179             settings->beginGroup(keyBase(false));
180             QStringList subs = settings->childGroups();
181             settings->endGroup();
182             subs.sort();
183             for(int x = subs.count() - 1; x >= 0; x--) {
184                 QString s = subs[x];
185                 if(s.isEmpty())
186                     continue;
187                 settings->beginGroup(keyBase(false) + s);
188                 QStringList keys = settings->childKeys();
189                 settings->endGroup();
190                 for(QStringList::ConstIterator it2 = keys.begin(); it2 != keys.end(); it2++) {
191                     QString ret = settings->value(keyBase(false) + s + "/" + (*it2)).toString();
192                     if(s != qmake_version())
193                         fprintf(stdout, "%s/", s.toLatin1().constData());
194                     fprintf(stdout, "%s:%s\n", (*it2).toLatin1().constData(), ret.toLatin1().constData());
195                 }
196             }
197             QStringList specialProps;
198             specialProps.append("QT_INSTALL_PREFIX");
199             specialProps.append("QT_INSTALL_DATA");
200             specialProps.append("QT_INSTALL_DOCS");
201             specialProps.append("QT_INSTALL_HEADERS");
202             specialProps.append("QT_INSTALL_LIBS");
203             specialProps.append("QT_INSTALL_BINS");
204             specialProps.append("QT_INSTALL_TESTS");
205             specialProps.append("QT_INSTALL_PLUGINS");
206             specialProps.append("QT_INSTALL_IMPORTS");
207             specialProps.append("QT_INSTALL_TRANSLATIONS");
208             specialProps.append("QT_INSTALL_CONFIGURATION");
209             specialProps.append("QT_INSTALL_EXAMPLES");
210             specialProps.append("QT_INSTALL_DEMOS");
211             specialProps.append("QMAKE_MKSPECS");
212             specialProps.append("QMAKE_VERSION");
213 #ifdef QT_VERSION_STR
214             specialProps.append("QT_VERSION");
215 #endif
216             foreach (QString prop, specialProps)
217                 fprintf(stdout, "%s:%s\n", prop.toLatin1().constData(), value(prop).toLatin1().constData());
218             return true;
219         }
220         for(QStringList::ConstIterator it = Option::prop::properties.begin();
221             it != Option::prop::properties.end(); it++) {
222             if(Option::prop::properties.count() > 1)
223                 fprintf(stdout, "%s:", (*it).toLatin1().constData());
224             if(!hasValue((*it))) {
225                 ret = false;
226                 fprintf(stdout, "**Unknown**\n");
227             } else {
228                 fprintf(stdout, "%s\n", value((*it)).toLatin1().constData());
229             }
230         }
231     } else if(Option::qmake_mode == Option::QMAKE_SET_PROPERTY) {
232         for(QStringList::ConstIterator it = Option::prop::properties.begin();
233             it != Option::prop::properties.end(); it++) {
234             QString var = (*it);
235             it++;
236             if(it == Option::prop::properties.end()) {
237                 ret = false;
238                 break;
239             }
240             if(!var.startsWith("."))
241                 setValue(var, (*it));
242         }
243     } else if(Option::qmake_mode == Option::QMAKE_UNSET_PROPERTY) {
244         for(QStringList::ConstIterator it = Option::prop::properties.begin();
245             it != Option::prop::properties.end(); it++) {
246             QString var = (*it);
247             if(!var.startsWith("."))
248                 remove(var);
249         }
250     }
251     return ret;
252 }
253
254 QT_END_NAMESPACE