| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 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 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <QtCore/qdir.h> | 
|---|
| 43 | #include <QtCore/qxmlstream.h> | 
|---|
| 44 |  | 
|---|
| 45 | #include "epocroot_p.h" | 
|---|
| 46 | #include "../windows/registry_p.h" | 
|---|
| 47 |  | 
|---|
| 48 | QT_BEGIN_NAMESPACE | 
|---|
| 49 |  | 
|---|
| 50 | // Registry key under which the location of the Symbian devices.xml file is | 
|---|
| 51 | // stored. | 
|---|
| 52 | // Note that, on 64-bit machines, this key is located under the 32-bit | 
|---|
| 53 | // compatibility key: | 
|---|
| 54 | //     HKEY_LOCAL_MACHINE\Software\Wow6432Node | 
|---|
| 55 | #define SYMBIAN_SDKS_REG_SUBKEY "Software\\Symbian\\EPOC SDKs\\CommonPath" | 
|---|
| 56 |  | 
|---|
| 57 | #ifdef Q_OS_WIN32 | 
|---|
| 58 | #   define SYMBIAN_SDKS_REG_HANDLE HKEY_LOCAL_MACHINE | 
|---|
| 59 | #else | 
|---|
| 60 | #   define SYMBIAN_SDKS_REG_HANDLE 0 | 
|---|
| 61 | #endif | 
|---|
| 62 |  | 
|---|
| 63 | // Value which is populated and returned by the epocRoot() function. | 
|---|
| 64 | // Stored as a static value in order to avoid unnecessary re-evaluation. | 
|---|
| 65 | static QString epocRootValue; | 
|---|
| 66 |  | 
|---|
| 67 | static QString getDevicesXmlPath() | 
|---|
| 68 | { | 
|---|
| 69 | // Note that the following call will return a null string on platforms other | 
|---|
| 70 | // than Windows.  If support is required on other platforms for devices.xml, | 
|---|
| 71 | // an alternative mechanism for retrieving the location of this file will | 
|---|
| 72 | // be required. | 
|---|
| 73 | return qt_readRegistryKey(SYMBIAN_SDKS_REG_HANDLE, QLatin1String(SYMBIAN_SDKS_REG_SUBKEY)); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | /** | 
|---|
| 77 | * Checks whether epocRootValue points to an existent directory. | 
|---|
| 78 | * If not, epocRootValue is set to an empty string and an error message is printed. | 
|---|
| 79 | */ | 
|---|
| 80 | static void checkEpocRootExists(const QString &source) | 
|---|
| 81 | { | 
|---|
| 82 | if (!epocRootValue.isEmpty()) { | 
|---|
| 83 | QDir dir(epocRootValue); | 
|---|
| 84 | if (!dir.exists()) { | 
|---|
| 85 | qWarning("Warning: %s is set to an invalid path: '%s'", qPrintable(source), | 
|---|
| 86 | qPrintable(epocRootValue)); | 
|---|
| 87 | epocRootValue = QString(); | 
|---|
| 88 | } | 
|---|
| 89 | } | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | /** | 
|---|
| 93 | * Translate path from Windows to Qt format. | 
|---|
| 94 | */ | 
|---|
| 95 | static void fixEpocRoot(QString &path) | 
|---|
| 96 | { | 
|---|
| 97 | path.replace(QLatin1Char('\\'), QLatin1Char('/')); | 
|---|
| 98 |  | 
|---|
| 99 | if (!path.size() || path[path.size()-1] != QLatin1Char('/')) { | 
|---|
| 100 | path += QLatin1Char('/'); | 
|---|
| 101 | } | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | /** | 
|---|
| 105 | * Determine the epoc root for the currently active SDK. | 
|---|
| 106 | */ | 
|---|
| 107 | QString qt_epocRoot() | 
|---|
| 108 | { | 
|---|
| 109 | if (epocRootValue.isEmpty()) { | 
|---|
| 110 | // 1. If environment variable EPOCROOT is set and points to an existent | 
|---|
| 111 | //    directory, this is returned. | 
|---|
| 112 | epocRootValue = QString::fromLocal8Bit(qgetenv("EPOCROOT").constData()); | 
|---|
| 113 | checkEpocRootExists(QLatin1String("EPOCROOT environment variable")); | 
|---|
| 114 |  | 
|---|
| 115 | if (epocRootValue.isEmpty()) { | 
|---|
| 116 | // 2. The location of devices.xml is specified by a registry key.  If this | 
|---|
| 117 | //    file exists, it is parsed. | 
|---|
| 118 | QString devicesXmlPath = getDevicesXmlPath(); | 
|---|
| 119 | if (!devicesXmlPath.isEmpty()) { | 
|---|
| 120 | devicesXmlPath += QLatin1String("/devices.xml"); | 
|---|
| 121 | QFile devicesFile(devicesXmlPath); | 
|---|
| 122 | if (devicesFile.open(QIODevice::ReadOnly)) { | 
|---|
| 123 |  | 
|---|
| 124 | // 3. If the EPOCDEVICE environment variable is set and a corresponding | 
|---|
| 125 | //    entry is found in devices.xml, and its epocroot value points to an | 
|---|
| 126 | //    existent directory, it is returned. | 
|---|
| 127 | // 4. If a device element marked as default is found in devices.xml and its | 
|---|
| 128 | //    epocroot value points to an existent directory, this is returned. | 
|---|
| 129 |  | 
|---|
| 130 | const QString epocDeviceValue = QString::fromLocal8Bit(qgetenv("EPOCDEVICE").constData()); | 
|---|
| 131 | bool epocDeviceFound = false; | 
|---|
| 132 |  | 
|---|
| 133 | QXmlStreamReader xml(&devicesFile); | 
|---|
| 134 | while (!xml.atEnd()) { | 
|---|
| 135 | xml.readNext(); | 
|---|
| 136 | if (xml.isStartElement() && xml.name() == QLatin1String("devices")) { | 
|---|
| 137 | if (xml.attributes().value(QLatin1String("version")) == QLatin1String("1.0")) { | 
|---|
| 138 | while (!(xml.isEndElement() && xml.name() == QLatin1String("devices")) && !xml.atEnd()) { | 
|---|
| 139 | xml.readNext(); | 
|---|
| 140 | if (xml.isStartElement() && xml.name() == QLatin1String("device")) { | 
|---|
| 141 | const bool isDefault = xml.attributes().value(QLatin1String("default")) == QLatin1String("yes"); | 
|---|
| 142 | const QString id = xml.attributes().value(QLatin1String("id")).toString(); | 
|---|
| 143 | const QString name = xml.attributes().value(QLatin1String("name")).toString(); | 
|---|
| 144 | const QString alias = xml.attributes().value(QLatin1String("alias")).toString(); | 
|---|
| 145 | bool epocDeviceMatch = QString(id + QLatin1String(":") + name) == epocDeviceValue; | 
|---|
| 146 | if (!alias.isEmpty()) | 
|---|
| 147 | epocDeviceMatch |= alias == epocDeviceValue; | 
|---|
| 148 | epocDeviceFound |= epocDeviceMatch; | 
|---|
| 149 |  | 
|---|
| 150 | if((epocDeviceValue.isEmpty() && isDefault) || epocDeviceMatch) { | 
|---|
| 151 | // Found a matching device | 
|---|
| 152 | while (!(xml.isEndElement() && xml.name() == QLatin1String("device")) && !xml.atEnd()) { | 
|---|
| 153 | xml.readNext(); | 
|---|
| 154 | if (xml.isStartElement() && xml.name() == QLatin1String("epocroot")) { | 
|---|
| 155 | epocRootValue = xml.readElementText(); | 
|---|
| 156 | const QString deviceSource = epocDeviceValue.isEmpty() | 
|---|
| 157 | ? QLatin1String("default device") | 
|---|
| 158 | : QString(QLatin1String("EPOCDEVICE (") + epocDeviceValue + QLatin1String(")")); | 
|---|
| 159 | checkEpocRootExists(deviceSource); | 
|---|
| 160 | } | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | if (epocRootValue.isEmpty()) | 
|---|
| 164 | xml.raiseError(QLatin1String("No epocroot element found")); | 
|---|
| 165 | } | 
|---|
| 166 | } | 
|---|
| 167 | } | 
|---|
| 168 | } else { | 
|---|
| 169 | xml.raiseError(QLatin1String("Invalid 'devices' element version")); | 
|---|
| 170 | } | 
|---|
| 171 | } | 
|---|
| 172 | } | 
|---|
| 173 | if (xml.hasError()) { | 
|---|
| 174 | qWarning("Warning: Error \"%s\" when parsing devices.xml", | 
|---|
| 175 | qPrintable(xml.errorString())); | 
|---|
| 176 | } else { | 
|---|
| 177 | if (epocRootValue.isEmpty()) { | 
|---|
| 178 | if (!epocDeviceValue.isEmpty()) { | 
|---|
| 179 | if (epocDeviceFound) { | 
|---|
| 180 | qWarning("Warning: Missing or invalid epocroot attribute in device '%s' in devices.xml.", | 
|---|
| 181 | qPrintable(epocDeviceValue)); | 
|---|
| 182 | } else { | 
|---|
| 183 | qWarning("Warning: No device matching EPOCDEVICE (%s) in devices.xml.", | 
|---|
| 184 | qPrintable(epocDeviceValue)); | 
|---|
| 185 | } | 
|---|
| 186 | } else { | 
|---|
| 187 | if (epocDeviceFound) { | 
|---|
| 188 | qWarning("Warning: Missing or invalid epocroot attribute in default device in devices.xml."); | 
|---|
| 189 | } else { | 
|---|
| 190 | qWarning("Warning: No default device set in devices.xml."); | 
|---|
| 191 | } | 
|---|
| 192 | } | 
|---|
| 193 | } | 
|---|
| 194 | } | 
|---|
| 195 | } else { | 
|---|
| 196 | qWarning("Warning: Could not open file: '%s'.", qPrintable(devicesXmlPath)); | 
|---|
| 197 | } | 
|---|
| 198 | } | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | if (epocRootValue.isEmpty()) { | 
|---|
| 202 | // 5. An empty string is returned. | 
|---|
| 203 | qWarning("Warning: failed to resolve epocroot." | 
|---|
| 204 | #ifdef Q_OS_WIN32 | 
|---|
| 205 | "\nEither\n" | 
|---|
| 206 | "    1. Set EPOCROOT environment variable to a valid value.\n" | 
|---|
| 207 | " or 2. Ensure that the HKEY_LOCAL_MACHINE\\" SYMBIAN_SDKS_REG_SUBKEY | 
|---|
| 208 | " registry key is set, and then\n" | 
|---|
| 209 | "       a. Set EPOCDEVICE environment variable to a valid device\n" | 
|---|
| 210 | "    or b. Specify a default device in the devices.xml file."); | 
|---|
| 211 | #else | 
|---|
| 212 | " Set EPOCROOT environment variable to a valid value."); | 
|---|
| 213 | #endif | 
|---|
| 214 | } else { | 
|---|
| 215 | fixEpocRoot(epocRootValue); | 
|---|
| 216 | } | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | return epocRootValue; | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | QT_END_NAMESPACE | 
|---|