Don't close libusb device handle if opening failed.
[kugel-rb.git] / rbutil / rbutilqt / base / system.cpp
blob61a5b95cde9e960b2c5d968e936688aeabf51e2a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id$
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 #include "system.h"
23 #include <QtCore>
24 #include <QDebug>
26 #include <cstdlib>
27 #include <stdio.h>
29 // Windows Includes
30 #if defined(Q_OS_WIN32)
31 #if defined(UNICODE)
32 #define _UNICODE
33 #endif
34 #include <windows.h>
35 #include <tchar.h>
36 #include <lm.h>
37 #include <windows.h>
38 #include <setupapi.h>
39 #endif
41 // Linux and Mac includes
42 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
43 #if defined(LIBUSB1)
44 #include <libusb-1.0/libusb.h>
45 #else
46 #include <usb.h>
47 #endif
48 #include <sys/utsname.h>
49 #include <unistd.h>
50 #include <pwd.h>
51 #endif
53 // Linux includes
54 #if defined(Q_OS_LINUX)
55 #include <mntent.h>
56 #endif
58 // Mac includes
59 #if defined(Q_OS_MACX)
60 #include <sys/param.h>
61 #include <sys/ucred.h>
62 #include <sys/mount.h>
64 #include <CoreFoundation/CoreFoundation.h>
65 #include <SystemConfiguration/SystemConfiguration.h>
66 #include <CoreServices/CoreServices.h>
67 #endif
69 #include "utils.h"
70 #include "rbsettings.h"
72 /** @brief detect permission of user (only Windows at moment).
73 * @return enum userlevel.
75 #if defined(Q_OS_WIN32)
76 enum System::userlevel System::userPermissions(void)
78 LPUSER_INFO_1 buf;
79 NET_API_STATUS napistatus;
80 wchar_t userbuf[UNLEN];
81 DWORD usersize = UNLEN;
82 BOOL status;
83 enum userlevel result;
85 status = GetUserNameW(userbuf, &usersize);
86 if(!status)
87 return ERR;
89 napistatus = NetUserGetInfo(NULL, userbuf, (DWORD)1, (LPBYTE*)&buf);
91 switch(buf->usri1_priv) {
92 case USER_PRIV_GUEST:
93 result = GUEST;
94 break;
95 case USER_PRIV_USER:
96 result = USER;
97 break;
98 case USER_PRIV_ADMIN:
99 result = ADMIN;
100 break;
101 default:
102 result = ERR;
103 break;
105 NetApiBufferFree(buf);
107 return result;
110 /** @brief detects user permissions (only Windows at moment).
111 * @return a user readable string with the permission.
113 QString System::userPermissionsString(void)
115 QString result;
116 int perm = userPermissions();
117 switch(perm) {
118 case GUEST:
119 result = tr("Guest");
120 break;
121 case ADMIN:
122 result = tr("Admin");
123 break;
124 case USER:
125 result = tr("User");
126 break;
127 default:
128 result = tr("Error");
129 break;
131 return result;
133 #endif
136 /** @brief detects current Username.
137 * @return string with Username.
139 QString System::userName(void)
141 #if defined(Q_OS_WIN32)
142 wchar_t userbuf[UNLEN];
143 DWORD usersize = UNLEN;
144 BOOL status;
146 status = GetUserNameW(userbuf, &usersize);
148 return QString::fromWCharArray(userbuf);
149 #endif
150 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
151 struct passwd *user;
152 user = getpwuid(geteuid());
153 return QString(user->pw_name);
154 #endif
158 /** @brief detects the OS Version
159 * @return String with OS Version.
161 QString System::osVersionString(void)
163 QString result;
164 #if defined(Q_OS_WIN32)
165 OSVERSIONINFO osvi;
166 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
167 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
168 GetVersionEx(&osvi);
170 result = QString("Windows version %1.%2, ").arg(osvi.dwMajorVersion).arg(osvi.dwMinorVersion);
171 if(osvi.szCSDVersion)
172 result += QString("build %1 (%2)").arg(osvi.dwBuildNumber)
173 .arg(QString::fromWCharArray(osvi.szCSDVersion));
174 else
175 result += QString("build %1").arg(osvi.dwBuildNumber);
176 #endif
177 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
178 struct utsname u;
179 int ret;
180 ret = uname(&u);
182 result = QString("CPU: %1<br/>System: %2<br/>Release: %3<br/>Version: %4")
183 .arg(u.machine).arg(u.sysname).arg(u.release).arg(u.version);
184 #if defined(Q_OS_MACX)
185 SInt32 major;
186 SInt32 minor;
187 SInt32 bugfix;
188 OSErr error;
189 error = Gestalt(gestaltSystemVersionMajor, &major);
190 error = Gestalt(gestaltSystemVersionMinor, &minor);
191 error = Gestalt(gestaltSystemVersionBugFix, &bugfix);
193 result += QString("<br/>OS X %1.%2.%3 ").arg(major).arg(minor).arg(bugfix);
194 // 1: 86k, 2: ppc, 10: i386
195 SInt32 arch;
196 error = Gestalt(gestaltSysArchitecture, &arch);
197 switch(arch) {
198 case 1:
199 result.append("(86k)");
200 break;
201 case 2:
202 result.append("(ppc)");
203 break;
204 case 10:
205 result.append("(x86)");
206 break;
207 default:
208 result.append("(unknown)");
209 break;
211 #endif
212 #endif
213 result += QString("<br/>Qt version %1").arg(qVersion());
214 return result;
217 QList<uint32_t> System::listUsbIds(void)
219 return listUsbDevices().keys();
222 /** @brief detect devices based on usb pid / vid.
223 * @return list with usb VID / PID values.
225 QMap<uint32_t, QString> System::listUsbDevices(void)
227 QMap<uint32_t, QString> usbids;
228 // usb pid detection
229 qDebug() << "[System] Searching for USB devices";
230 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
231 #if defined(LIBUSB1)
232 libusb_device **devs;
233 int res;
234 ssize_t count;
235 res = libusb_init(NULL);
237 count = libusb_get_device_list(NULL, &devs);
238 libusb_device *dev;
239 int i = 0;
240 while((dev = devs[i++]) != NULL) {
241 QString name;
242 unsigned char buf[256];
243 uint32_t id;
244 struct libusb_device_descriptor descriptor;
245 if(libusb_get_device_descriptor(dev, &descriptor) == 0) {
246 id = descriptor.idVendor << 16 | descriptor.idProduct;
248 libusb_device_handle *dh;
249 if(libusb_open(dev, &dh) == 0) {
250 libusb_get_string_descriptor_ascii(dh, descriptor.iManufacturer, buf, 256);
251 name += QString::fromAscii((char*)buf) + " ";
252 libusb_get_string_descriptor_ascii(dh, descriptor.iProduct, buf, 256);
253 name += QString::fromAscii((char*)buf);
254 libusb_close(dh);
256 if(name.isEmpty())
257 name = tr("(no description available)");
258 if(id) {
259 usbids.insert(id, name);
260 qDebug("[System] USB: 0x%08x, %s", id, name.toLocal8Bit().data());
265 libusb_free_device_list(devs, 1);
266 libusb_exit(NULL);
267 #else
268 usb_init();
269 usb_find_busses();
270 usb_find_devices();
271 struct usb_bus *b;
272 b = usb_busses;
274 while(b) {
275 if(b->devices) {
276 struct usb_device *u;
277 u = b->devices;
278 while(u) {
279 uint32_t id;
280 id = u->descriptor.idVendor << 16 | u->descriptor.idProduct;
281 // get identification strings
282 usb_dev_handle *dev;
283 QString name;
284 char string[256];
285 int res;
286 dev = usb_open(u);
287 if(dev) {
288 if(u->descriptor.iManufacturer) {
289 res = usb_get_string_simple(dev, u->descriptor.iManufacturer,
290 string, sizeof(string));
291 if(res > 0)
292 name += QString::fromAscii(string) + " ";
294 if(u->descriptor.iProduct) {
295 res = usb_get_string_simple(dev, u->descriptor.iProduct,
296 string, sizeof(string));
297 if(res > 0)
298 name += QString::fromAscii(string);
300 usb_close(dev);
302 if(name.isEmpty()) name = tr("(no description available)");
304 if(id) {
305 usbids.insert(id, name);
306 qDebug() << "[System] USB:" << QString("0x%1").arg(id, 8, 16) << name;
308 u = u->next;
311 b = b->next;
313 #endif
314 #endif
316 #if defined(Q_OS_WIN32)
317 HDEVINFO deviceInfo;
318 SP_DEVINFO_DATA infoData;
319 DWORD i;
321 // Iterate over all devices
322 // by doing it this way it's unneccessary to use GUIDs which might be not
323 // present in current MinGW. It also seemed to be more reliably than using
324 // a GUID.
325 // See KB259695 for an example.
326 deviceInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
328 infoData.cbSize = sizeof(SP_DEVINFO_DATA);
330 for(i = 0; SetupDiEnumDeviceInfo(deviceInfo, i, &infoData); i++) {
331 DWORD data;
332 LPTSTR buffer = NULL;
333 DWORD buffersize = 0;
334 QString description;
336 // get device desriptor first
337 // for some reason not doing so results in bad things (tm)
338 while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
339 SPDRP_DEVICEDESC,&data, (PBYTE)buffer, buffersize, &buffersize)) {
340 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
341 if(buffer) free(buffer);
342 // double buffer size to avoid problems as per KB888609
343 buffer = (LPTSTR)malloc(buffersize * 2);
345 else {
346 break;
350 // now get the hardware id, which contains PID and VID.
351 while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
352 SPDRP_LOCATION_INFORMATION,&data, (PBYTE)buffer, buffersize, &buffersize)) {
353 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
354 if(buffer) free(buffer);
355 // double buffer size to avoid problems as per KB888609
356 buffer = (LPTSTR)malloc(buffersize * 2);
358 else {
359 break;
362 description = QString::fromWCharArray(buffer);
364 while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
365 SPDRP_HARDWAREID,&data, (PBYTE)buffer, buffersize, &buffersize)) {
366 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
367 if(buffer) free(buffer);
368 // double buffer size to avoid problems as per KB888609
369 buffer = (LPTSTR)malloc(buffersize * 2);
371 else {
372 break;
376 unsigned int vid, pid;
377 if(_stscanf(buffer, _TEXT("USB\\Vid_%x&Pid_%x"), &vid, &pid) == 2) {
378 uint32_t id;
379 id = vid << 16 | pid;
380 usbids.insert(id, description);
381 qDebug("[System] USB VID: %04x, PID: %04x", vid, pid);
383 if(buffer) free(buffer);
385 SetupDiDestroyDeviceInfoList(deviceInfo);
387 #endif
388 return usbids;
392 /** @brief detects current system proxy
393 * @return QUrl with proxy or empty
395 QUrl System::systemProxy(void)
397 #if defined(Q_OS_LINUX)
398 return QUrl(getenv("http_proxy"));
399 #elif defined(Q_OS_WIN32)
400 HKEY hk;
401 wchar_t proxyval[80];
402 DWORD buflen = 80;
403 long ret;
404 DWORD enable;
405 DWORD enalen = sizeof(DWORD);
407 ret = RegOpenKeyEx(HKEY_CURRENT_USER,
408 _TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"),
409 0, KEY_QUERY_VALUE, &hk);
410 if(ret != ERROR_SUCCESS) return QUrl("");
412 ret = RegQueryValueEx(hk, _TEXT("ProxyServer"), NULL, NULL, (LPBYTE)proxyval, &buflen);
413 if(ret != ERROR_SUCCESS) return QUrl("");
415 ret = RegQueryValueEx(hk, _TEXT("ProxyEnable"), NULL, NULL, (LPBYTE)&enable, &enalen);
416 if(ret != ERROR_SUCCESS) return QUrl("");
418 RegCloseKey(hk);
420 //qDebug() << QString::fromWCharArray(proxyval) << QString("%1").arg(enable);
421 if(enable != 0)
422 return QUrl("http://" + QString::fromWCharArray(proxyval));
423 else
424 return QUrl("");
425 #elif defined(Q_OS_MACX)
427 CFDictionaryRef dictref;
428 CFStringRef stringref;
429 CFNumberRef numberref;
430 int enable;
431 int port;
432 unsigned int bufsize = 0;
433 char *buf;
434 QUrl proxy;
436 dictref = SCDynamicStoreCopyProxies(NULL);
437 stringref = (CFStringRef)CFDictionaryGetValue(dictref, kSCPropNetProxiesHTTPProxy);
438 numberref = (CFNumberRef)CFDictionaryGetValue(dictref, kSCPropNetProxiesHTTPEnable);
439 CFNumberGetValue(numberref, kCFNumberIntType, &enable);
440 if(enable == 1) {
441 // get number of characters. CFStringGetLength uses UTF-16 code pairs
442 bufsize = CFStringGetLength(stringref) * 2 + 1;
443 buf = (char*)malloc(sizeof(char) * bufsize);
444 if(buf == NULL) {
445 qDebug() << "[System] can't allocate memory for proxy string!";
446 CFRelease(dictref);
447 return QUrl("");
449 CFStringGetCString(stringref, buf, bufsize, kCFStringEncodingUTF16);
450 numberref = (CFNumberRef)CFDictionaryGetValue(dictref, kSCPropNetProxiesHTTPPort);
451 CFNumberGetValue(numberref, kCFNumberIntType, &port);
452 proxy.setScheme("http");
453 proxy.setHost(QString::fromUtf16((unsigned short*)buf));
454 proxy.setPort(port);
456 free(buf);
458 CFRelease(dictref);
460 return proxy;
461 #else
462 return QUrl("");
463 #endif