|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (developer.feedback@nokia.com) |
|
6 ** |
|
7 ** This file is part of the HbCore module of the UI Extensions for Mobile. |
|
8 ** |
|
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 file. |
|
13 ** Please review the following information to ensure the GNU Lesser General |
|
14 ** 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 ** If you have questions regarding the use of this file, please contact |
|
22 ** Nokia at developer.feedback@nokia.com. |
|
23 ** |
|
24 ****************************************************************************/ |
|
25 |
|
26 #include <QtGlobal> |
|
27 |
|
28 #ifndef Q_CC_MINGW |
|
29 #include <objbase.h> |
|
30 #include <QDebug> |
|
31 |
|
32 #include "hbwmihelper_win_p.h" |
|
33 |
|
34 WMIHelper::WMIHelper(QObject * parent) |
|
35 : QObject(parent) |
|
36 { |
|
37 m_conditional = QString(); |
|
38 } |
|
39 |
|
40 WMIHelper::~WMIHelper() |
|
41 { |
|
42 //wbemServices->Release(); |
|
43 //wbemLocator->Release(); |
|
44 CoUninitialize(); |
|
45 } |
|
46 |
|
47 QVariant WMIHelper::getWMIData() |
|
48 { |
|
49 if (!m_wmiNamespace.isEmpty() && !m_className.isEmpty() && !m_classProperties.isEmpty()) { |
|
50 return getWMIData(m_wmiNamespace, m_className, m_classProperties); |
|
51 } |
|
52 return QVariant(); |
|
53 } |
|
54 |
|
55 void WMIHelper::initializeWMI(const QString &wmiNamespace) |
|
56 { |
|
57 HRESULT hres; |
|
58 wbemLocator = 0; |
|
59 |
|
60 hres = CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER, |
|
61 IID_IWbemLocator, (LPVOID *) &wbemLocator); |
|
62 |
|
63 if (hres == CO_E_NOTINITIALIZED) { // COM was not initialized |
|
64 CoInitializeEx(0, COINIT_MULTITHREADED); |
|
65 hres = CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER, |
|
66 IID_IWbemLocator, (LPVOID *) &wbemLocator); |
|
67 } |
|
68 |
|
69 if (hres != S_OK) { |
|
70 qWarning() << "Failed to create IWbemLocator object." << hres; |
|
71 return ; |
|
72 } |
|
73 wbemServices = 0; |
|
74 hres = wbemLocator->ConnectServer(::SysAllocString((const OLECHAR *)wmiNamespace.utf16()),0,0,0,0,0,0,&wbemServices); |
|
75 |
|
76 if (hres != WBEM_S_NO_ERROR){ |
|
77 qWarning() << "Could not connect"; |
|
78 return ; |
|
79 } |
|
80 |
|
81 hres = CoSetProxyBlanket( wbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, 0, |
|
82 RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, 0, EOAC_NONE ); |
|
83 |
|
84 if (hres != S_OK) { |
|
85 qWarning() << "Could not set proxy blanket" << hres; |
|
86 return ; |
|
87 } |
|
88 |
|
89 if (!initializedNamespaces.contains(wmiNamespace)) { |
|
90 initializedNamespaces.insert(wmiNamespace, true); |
|
91 } else { |
|
92 initializedNamespaces[wmiNamespace] = true; |
|
93 } |
|
94 } |
|
95 |
|
96 QVariant WMIHelper::getWMIData(const QString &wmiNamespace, const QString &className, const QStringList &classProperty) |
|
97 { |
|
98 if(!initializedNamespaces.contains(wmiNamespace)) { |
|
99 initializeWMI(wmiNamespace); |
|
100 } |
|
101 |
|
102 HRESULT hres; |
|
103 QVariant returnVariant; |
|
104 |
|
105 IEnumWbemClassObject* wbemEnumerator = 0; |
|
106 if (!m_conditional.isEmpty()) { |
|
107 if (m_conditional.left(1) != " ") { |
|
108 m_conditional.prepend(" "); |
|
109 } |
|
110 } |
|
111 |
|
112 QString aString = "SELECT * FROM " + className + m_conditional; |
|
113 BSTR bstrQuery; |
|
114 bstrQuery = ::SysAllocString((const OLECHAR *)aString.utf16()); |
|
115 |
|
116 hres = wbemServices->ExecQuery(L"WQL", bstrQuery, |
|
117 WBEM_FLAG_BIDIRECTIONAL | WBEM_FLAG_RETURN_IMMEDIATELY,0,&wbemEnumerator); |
|
118 |
|
119 if (hres != WBEM_S_NO_ERROR){ |
|
120 qWarning() << "WMI Query failed."; |
|
121 wbemLocator->Release(); |
|
122 wbemEnumerator->Release(); |
|
123 return returnVariant; |
|
124 } |
|
125 |
|
126 ::SysFreeString(bstrQuery); |
|
127 |
|
128 wbemCLassObject = 0; |
|
129 ULONG result = 0; |
|
130 |
|
131 wmiVariantList.clear(); |
|
132 while (wbemEnumerator) { |
|
133 HRESULT hr = wbemEnumerator->Next(WBEM_INFINITE, 1,&wbemCLassObject, &result); |
|
134 if(0 == result){ |
|
135 break; |
|
136 } |
|
137 |
|
138 foreach (QString property, classProperty) { |
|
139 VARIANT msVariant; |
|
140 CIMTYPE variantType; |
|
141 hr = wbemCLassObject->Get((LPCWSTR)property.utf16(), 0, &msVariant, &variantType, 0); |
|
142 returnVariant = msVariantToQVariant(msVariant, variantType); |
|
143 wmiVariantList << returnVariant; |
|
144 |
|
145 VariantClear(&msVariant); |
|
146 } |
|
147 wbemCLassObject->Release(); |
|
148 } |
|
149 |
|
150 wbemEnumerator->Release(); |
|
151 return returnVariant; |
|
152 } |
|
153 |
|
154 QVariant WMIHelper::msVariantToQVariant(VARIANT msVariant, CIMTYPE variantType) |
|
155 { |
|
156 QVariant returnVariant; |
|
157 switch(variantType) { |
|
158 case CIM_STRING: |
|
159 case CIM_CHAR16: |
|
160 { |
|
161 QString str((QChar*)msVariant.bstrVal, wcslen(msVariant.bstrVal)); |
|
162 QVariant vs(str); |
|
163 returnVariant = vs; |
|
164 } |
|
165 break; |
|
166 case CIM_BOOLEAN: |
|
167 { |
|
168 QVariant vb(msVariant.boolVal); |
|
169 returnVariant = vb; |
|
170 } |
|
171 break; |
|
172 case CIM_UINT8: |
|
173 { |
|
174 QVariant vb(msVariant.uintVal); |
|
175 returnVariant = vb; |
|
176 } |
|
177 break; |
|
178 case CIM_UINT16: |
|
179 { |
|
180 QVariant vb(msVariant.uintVal); |
|
181 returnVariant = vb; |
|
182 } |
|
183 case CIM_UINT32: |
|
184 { |
|
185 QVariant vb(msVariant.uintVal); |
|
186 returnVariant = vb; |
|
187 } |
|
188 break; |
|
189 }; |
|
190 VariantClear(&msVariant); |
|
191 return returnVariant; |
|
192 } |
|
193 |
|
194 void WMIHelper::setWmiNamespace(const QString &wmiNamespace) |
|
195 { |
|
196 m_wmiNamespace = wmiNamespace; |
|
197 } |
|
198 |
|
199 void WMIHelper::setClassName(const QString &className) |
|
200 { |
|
201 m_className = className; |
|
202 } |
|
203 |
|
204 void WMIHelper::setClassProperty(const QStringList &classProperties) |
|
205 { |
|
206 m_classProperties = classProperties; |
|
207 } |
|
208 |
|
209 void WMIHelper::setConditional(const QString &conditional) |
|
210 { |
|
211 m_conditional = conditional; |
|
212 } |
|
213 |
|
214 #endif |