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