tools/configure/environment.cpp
changeset 7 f7bc934e204c
parent 0 1918ee327afb
child 30 5dc02b23752f
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the tools applications of the Qt Toolkit.
     7 ** This file is part of the tools applications of the Qt Toolkit.
     8 **
     8 **
    58 
    58 
    59 #ifdef Q_OS_WIN32
    59 #ifdef Q_OS_WIN32
    60 #include <qt_windows.h>
    60 #include <qt_windows.h>
    61 #endif
    61 #endif
    62 
    62 
       
    63 #include <symbian/epocroot.h> // from tools/shared
       
    64 #include <windows/registry.h> // from tools/shared
    63 
    65 
    64 QT_BEGIN_NAMESPACE
    66 QT_BEGIN_NAMESPACE
    65 
    67 
    66 struct CompilerInfo{
    68 struct CompilerInfo{
    67     Compiler compiler;
    69     Compiler compiler;
    92 {
    94 {
    93     int i = 0;
    95     int i = 0;
    94     while(compiler_info[i].compiler != compiler && compiler_info[i].compiler != CC_UNKNOWN)
    96     while(compiler_info[i].compiler != compiler && compiler_info[i].compiler != CC_UNKNOWN)
    95         ++i;
    97         ++i;
    96     return &(compiler_info[i]);
    98     return &(compiler_info[i]);
    97 }
       
    98 
       
    99 /*!
       
   100     Returns the path part of a registry key.
       
   101     Ei.
       
   102         For a key
       
   103             "Software\\Microsoft\\VisualStudio\\8.0\\Setup\\VC\\ProductDir"
       
   104         it returns
       
   105             "Software\\Microsoft\\VisualStudio\\8.0\\Setup\\VC\\"
       
   106 */
       
   107 QString Environment::keyPath(const QString &rKey)
       
   108 {
       
   109     int idx = rKey.lastIndexOf(QLatin1Char('\\'));
       
   110     if (idx == -1)
       
   111         return QString();
       
   112     return rKey.left(idx + 1);
       
   113 }
       
   114 
       
   115 /*!
       
   116     Returns the name part of a registry key.
       
   117     Ei.
       
   118         For a key
       
   119             "Software\\Microsoft\\VisualStudio\\8.0\\Setup\\VC\\ProductDir"
       
   120         it returns
       
   121             "ProductDir"
       
   122 */
       
   123 QString Environment::keyName(const QString &rKey)
       
   124 {
       
   125     int idx = rKey.lastIndexOf(QLatin1Char('\\'));
       
   126     if (idx == -1)
       
   127         return rKey;
       
   128 
       
   129     QString res(rKey.mid(idx + 1));
       
   130     if (res == "Default" || res == ".")
       
   131         res = "";
       
   132     return res;
       
   133 }
       
   134 
       
   135 /*!
       
   136     Returns a registry keys value in string form.
       
   137     If the registry key does not exist, or cannot be accessed, a
       
   138     QString() is returned.
       
   139 */
       
   140 QString Environment::readRegistryKey(HKEY parentHandle, const QString &rSubkey)
       
   141 {
       
   142 #ifndef Q_OS_WIN32
       
   143     return QString();
       
   144 #else
       
   145     QString rSubkeyName = keyName(rSubkey);
       
   146     QString rSubkeyPath = keyPath(rSubkey);
       
   147 
       
   148     HKEY handle = 0;
       
   149     LONG res = RegOpenKeyEx(parentHandle, (wchar_t*)rSubkeyPath.utf16(), 0, KEY_READ, &handle);
       
   150     if (res != ERROR_SUCCESS)
       
   151         return QString();
       
   152 
       
   153     // get the size and type of the value
       
   154     DWORD dataType;
       
   155     DWORD dataSize;
       
   156     res = RegQueryValueEx(handle, (wchar_t*)rSubkeyName.utf16(), 0, &dataType, 0, &dataSize);
       
   157     if (res != ERROR_SUCCESS) {
       
   158         RegCloseKey(handle);
       
   159         return QString();
       
   160     }
       
   161 
       
   162     // get the value
       
   163     QByteArray data(dataSize, 0);
       
   164     res = RegQueryValueEx(handle, (wchar_t*)rSubkeyName.utf16(), 0, 0,
       
   165                            reinterpret_cast<unsigned char*>(data.data()), &dataSize);
       
   166     if (res != ERROR_SUCCESS) {
       
   167         RegCloseKey(handle);
       
   168         return QString();
       
   169     }
       
   170 
       
   171     QString result;
       
   172     switch (dataType) {
       
   173         case REG_EXPAND_SZ:
       
   174         case REG_SZ: {
       
   175             result = QString::fromWCharArray(((const wchar_t *)data.constData()));
       
   176             break;
       
   177         }
       
   178 
       
   179         case REG_MULTI_SZ: {
       
   180             QStringList l;
       
   181             int i = 0;
       
   182             for (;;) {
       
   183                 QString s = QString::fromWCharArray((const wchar_t *)data.constData() + i);
       
   184                 i += s.length() + 1;
       
   185 
       
   186                 if (s.isEmpty())
       
   187                     break;
       
   188                 l.append(s);
       
   189             }
       
   190             result = l.join(", ");
       
   191             break;
       
   192         }
       
   193 
       
   194         case REG_NONE:
       
   195         case REG_BINARY: {
       
   196             result = QString::fromWCharArray((const wchar_t *)data.constData(), data.size() / 2);
       
   197             break;
       
   198         }
       
   199 
       
   200         case REG_DWORD_BIG_ENDIAN:
       
   201         case REG_DWORD: {
       
   202             Q_ASSERT(data.size() == sizeof(int));
       
   203             int i;
       
   204             memcpy((char*)&i, data.constData(), sizeof(int));
       
   205             result = QString::number(i);
       
   206             break;
       
   207         }
       
   208 
       
   209         default:
       
   210             qWarning("QSettings: unknown data %d type in windows registry", dataType);
       
   211             break;
       
   212     }
       
   213 
       
   214     RegCloseKey(handle);
       
   215     return result;
       
   216 #endif
       
   217 }
    99 }
   218 
   100 
   219 /*!
   101 /*!
   220     Returns the qmakespec for the compiler detected on the system.
   102     Returns the qmakespec for the compiler detected on the system.
   221 */
   103 */
   577     }
   459     }
   578     result &= dir.rmdir(cleanName);
   460     result &= dir.rmdir(cleanName);
   579     return result;
   461     return result;
   580 }
   462 }
   581 
   463 
       
   464 QString Environment::symbianEpocRoot()
       
   465 {
       
   466     // Call function defined in tools/shared/symbian/epocroot.h
       
   467     return ::epocRoot();
       
   468 }
       
   469 
   582 QT_END_NAMESPACE
   470 QT_END_NAMESPACE