OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / shared / registryaccess / registryaccess.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "registryaccess.h"
35
36 #include <QtGui/QApplication>
37
38 #include <QtCore/QDir>
39 #include <QtCore/QTextStream>
40
41 namespace RegistryAccess {
42
43 static QString winErrorMessage(unsigned long error)
44 {
45     QString rc = QString::fromLatin1("#%1: ").arg(error);
46     ushort *lpMsgBuf;
47
48     const int len = FormatMessage(
49             FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
50             NULL, error, 0, (LPTSTR)&lpMsgBuf, 0, NULL);
51     if (len) {
52        rc = QString::fromUtf16(lpMsgBuf, len);
53         LocalFree(lpMsgBuf);
54     } else {
55         rc += QString::fromLatin1("<unknown error>");
56     }
57     return rc;
58 }
59
60 QString msgFunctionFailed(const char *f, unsigned long error)
61 {
62     return QString::fromLatin1("'%1' failed: %2").arg(QLatin1String(f), winErrorMessage(error));
63 }
64
65 static bool registryReadBinaryKey(HKEY handle, // HKEY_LOCAL_MACHINE, etc.
66                                   const WCHAR *valueName,
67                                   QByteArray *data,
68                                   QString *errorMessage)
69 {
70     data->clear();
71     DWORD type;
72     DWORD size;
73     // get size and retrieve
74     LONG rc = RegQueryValueEx(handle, valueName, 0, &type, 0, &size);
75     if (rc != ERROR_SUCCESS) {
76         *errorMessage = msgRegistryOperationFailed("read", valueName, msgFunctionFailed("RegQueryValueEx1", rc));
77         return false;
78     }
79     BYTE *dataC = new BYTE[size + 1];
80     // Will be Utf16 in case of a string
81     rc = RegQueryValueEx(handle, valueName, 0, &type, dataC, &size);
82     if (rc != ERROR_SUCCESS) {
83         *errorMessage = msgRegistryOperationFailed("read", valueName, msgFunctionFailed("RegQueryValueEx2", rc));
84         return false;
85     }
86     *data = QByteArray(reinterpret_cast<const char*>(dataC), size);
87     delete [] dataC;
88     return true;
89 }
90
91 bool registryReadStringKey(HKEY handle, // HKEY_LOCAL_MACHINE, etc.
92                            const WCHAR *valueName,
93                            QString *s,
94                            QString *errorMessage)
95 {
96     QByteArray data;
97     if (!registryReadBinaryKey(handle, valueName, &data, errorMessage))
98         return false;
99     data += '\0';
100     data += '\0';
101     *s = QString::fromUtf16(reinterpret_cast<const unsigned short*>(data.data()));
102     return true;
103 }
104
105 bool openRegistryKey(HKEY category, // HKEY_LOCAL_MACHINE, etc.
106                      const WCHAR *key,
107                      bool readWrite,
108                      HKEY *keyHandle,
109                      QString *errorMessage)
110 {
111     Q_UNUSED(debuggerRegistryKeyC);  // avoid warning from MinGW
112
113     REGSAM accessRights = KEY_READ;
114     if (readWrite)
115          accessRights |= KEY_SET_VALUE;
116     const LONG rc = RegOpenKeyEx(category, key, 0, accessRights, keyHandle);
117     if (rc != ERROR_SUCCESS) {
118         *errorMessage = msgFunctionFailed("RegOpenKeyEx", rc);
119         if (readWrite)
120             *errorMessage += "You need administrator privileges to edit the registry.";
121         return false;
122     }
123     return true;
124 }
125
126 // Installation helpers: Format the debugger call with placeholders for PID and event
127 // '"[path]\qtcdebugger" [-wow] %ld %ld'.
128
129 QString debuggerCall(const QString &additionalOption)
130 {
131     QString rc;
132     QTextStream str(&rc);
133     str << '"' << QDir::toNativeSeparators(QApplication::applicationDirPath() + QLatin1Char('/')
134                                            + debuggerApplicationFileC + QLatin1String(".exe")) << '"';
135     if (!additionalOption.isEmpty())
136         str << ' ' << additionalOption;
137     str << " %ld %ld";
138     return rc;
139 }
140
141 bool isRegistered(HKEY handle, const QString &call, QString *errorMessage, QString *oldDebugger)
142 {
143     QString registeredDebugger;
144     registryReadStringKey(handle, debuggerRegistryValueNameC, &registeredDebugger, errorMessage);
145     if (oldDebugger)
146         *oldDebugger = registeredDebugger;
147     return !registeredDebugger.compare(call, Qt::CaseInsensitive);
148 }
149
150 } // namespace RegistryAccess