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