Fix more missing mutex_init calls.
[kugel-rb.git] / rbutil / rbutilqt / base / detect.cpp
blob70f2de6f41d3e96f9896bb3e3fdfda04d84050e8
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 "detect.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 #include <usb.h>
44 #include <sys/utsname.h>
45 #include <unistd.h>
46 #include <pwd.h>
47 #endif
49 // Linux includes
50 #if defined(Q_OS_LINUX)
51 #include <mntent.h>
52 #endif
54 // Mac includes
55 #if defined(Q_OS_MACX)
56 #include <sys/param.h>
57 #include <sys/ucred.h>
58 #include <sys/mount.h>
59 #endif
61 #include "utils.h"
62 #include "rbsettings.h"
64 /** @brief detect permission of user (only Windows at moment).
65 * @return enum userlevel.
67 #if defined(Q_OS_WIN32)
68 enum Detect::userlevel Detect::userPermissions(void)
70 LPUSER_INFO_1 buf;
71 NET_API_STATUS napistatus;
72 wchar_t userbuf[UNLEN];
73 DWORD usersize = UNLEN;
74 BOOL status;
75 enum userlevel result;
77 status = GetUserNameW(userbuf, &usersize);
78 if(!status)
79 return ERR;
81 napistatus = NetUserGetInfo(NULL, userbuf, (DWORD)1, (LPBYTE*)&buf);
83 switch(buf->usri1_priv) {
84 case USER_PRIV_GUEST:
85 result = GUEST;
86 break;
87 case USER_PRIV_USER:
88 result = USER;
89 break;
90 case USER_PRIV_ADMIN:
91 result = ADMIN;
92 break;
93 default:
94 result = ERR;
95 break;
97 NetApiBufferFree(buf);
99 return result;
102 /** @brief detects user permissions (only Windows at moment).
103 * @return a user readable string with the permission.
105 QString Detect::userPermissionsString(void)
107 QString result;
108 int perm = userPermissions();
109 switch(perm) {
110 case GUEST:
111 result = QObject::tr("Guest");
112 break;
113 case ADMIN:
114 result = QObject::tr("Admin");
115 break;
116 case USER:
117 result = QObject::tr("User");
118 break;
119 default:
120 result = QObject::tr("Error");
121 break;
123 return result;
125 #endif
128 /** @brief detects current Username.
129 * @return string with Username.
131 QString Detect::userName(void)
133 #if defined(Q_OS_WIN32)
134 wchar_t userbuf[UNLEN];
135 DWORD usersize = UNLEN;
136 BOOL status;
138 status = GetUserNameW(userbuf, &usersize);
140 return QString::fromWCharArray(userbuf);
141 #endif
142 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
143 struct passwd *user;
144 user = getpwuid(geteuid());
145 return QString(user->pw_name);
146 #endif
150 /** @brief detects the OS Version
151 * @return String with OS Version.
153 QString Detect::osVersionString(void)
155 QString result;
156 #if defined(Q_OS_WIN32)
157 OSVERSIONINFO osvi;
158 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
159 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
160 GetVersionEx(&osvi);
162 result = QString("Windows version %1.%2, ").arg(osvi.dwMajorVersion).arg(osvi.dwMinorVersion);
163 if(osvi.szCSDVersion)
164 result += QString("build %1 (%2)").arg(osvi.dwBuildNumber)
165 .arg(QString::fromWCharArray(osvi.szCSDVersion));
166 else
167 result += QString("build %1").arg(osvi.dwBuildNumber);
168 #endif
169 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
170 struct utsname u;
171 int ret;
172 ret = uname(&u);
174 result = QString("CPU: %1<br/>System: %2<br/>Release: %3<br/>Version: %4")
175 .arg(u.machine).arg(u.sysname).arg(u.release).arg(u.version);
176 #endif
177 result += QString("<br/>Qt version %1").arg(qVersion());
178 return result;
181 QList<uint32_t> Detect::listUsbIds(void)
183 return listUsbDevices().keys();
186 /** @brief detect devices based on usb pid / vid.
187 * @return list with usb VID / PID values.
189 QMap<uint32_t, QString> Detect::listUsbDevices(void)
191 QMap<uint32_t, QString> usbids;
192 // usb pid detection
193 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
194 usb_init();
195 usb_find_busses();
196 usb_find_devices();
197 struct usb_bus *b;
198 b = usb_busses;
200 while(b) {
201 qDebug() << "bus:" << b->dirname << b->devices;
202 if(b->devices) {
203 qDebug() << "devices present.";
204 struct usb_device *u;
205 u = b->devices;
206 while(u) {
207 uint32_t id;
208 id = u->descriptor.idVendor << 16 | u->descriptor.idProduct;
209 // get identification strings
210 usb_dev_handle *dev;
211 QString name;
212 char string[256];
213 int res;
214 dev = usb_open(u);
215 if(dev) {
216 if(u->descriptor.iManufacturer) {
217 res = usb_get_string_simple(dev, u->descriptor.iManufacturer, string, sizeof(string));
218 if(res > 0)
219 name += QString::fromAscii(string) + " ";
221 if(u->descriptor.iProduct) {
222 res = usb_get_string_simple(dev, u->descriptor.iProduct, string, sizeof(string));
223 if(res > 0)
224 name += QString::fromAscii(string);
227 usb_close(dev);
228 if(name.isEmpty()) name = QObject::tr("(no description available)");
230 if(id) usbids.insert(id, name);
231 u = u->next;
234 b = b->next;
236 #endif
238 #if defined(Q_OS_WIN32)
239 HDEVINFO deviceInfo;
240 SP_DEVINFO_DATA infoData;
241 DWORD i;
243 // Iterate over all devices
244 // by doing it this way it's unneccessary to use GUIDs which might be not
245 // present in current MinGW. It also seemed to be more reliably than using
246 // a GUID.
247 // See KB259695 for an example.
248 deviceInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
250 infoData.cbSize = sizeof(SP_DEVINFO_DATA);
252 for(i = 0; SetupDiEnumDeviceInfo(deviceInfo, i, &infoData); i++) {
253 DWORD data;
254 LPTSTR buffer = NULL;
255 DWORD buffersize = 0;
256 QString description;
258 // get device desriptor first
259 // for some reason not doing so results in bad things (tm)
260 while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
261 SPDRP_DEVICEDESC,&data, (PBYTE)buffer, buffersize, &buffersize)) {
262 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
263 if(buffer) free(buffer);
264 // double buffer size to avoid problems as per KB888609
265 buffer = (LPTSTR)malloc(buffersize * 2);
267 else {
268 break;
272 // now get the hardware id, which contains PID and VID.
273 while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
274 SPDRP_LOCATION_INFORMATION,&data, (PBYTE)buffer, buffersize, &buffersize)) {
275 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
276 if(buffer) free(buffer);
277 // double buffer size to avoid problems as per KB888609
278 buffer = (LPTSTR)malloc(buffersize * 2);
280 else {
281 break;
284 description = QString::fromWCharArray(buffer);
286 while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
287 SPDRP_HARDWAREID,&data, (PBYTE)buffer, buffersize, &buffersize)) {
288 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
289 if(buffer) free(buffer);
290 // double buffer size to avoid problems as per KB888609
291 buffer = (LPTSTR)malloc(buffersize * 2);
293 else {
294 break;
298 unsigned int vid, pid, rev;
299 if(_stscanf(buffer, _TEXT("USB\\Vid_%x&Pid_%x&Rev_%x"), &vid, &pid, &rev) == 3) {
300 uint32_t id;
301 id = vid << 16 | pid;
302 usbids.insert(id, description);
303 qDebug("VID: %04x, PID: %04x", vid, pid);
305 if(buffer) free(buffer);
307 SetupDiDestroyDeviceInfoList(deviceInfo);
309 #endif
310 return usbids;
314 /** @brief detects current system proxy
315 * @return QUrl with proxy or empty
317 QUrl Detect::systemProxy(void)
319 #if defined(Q_OS_LINUX)
320 return QUrl(getenv("http_proxy"));
321 #elif defined(Q_OS_WIN32)
322 HKEY hk;
323 wchar_t proxyval[80];
324 DWORD buflen = 80;
325 long ret;
326 DWORD enable;
327 DWORD enalen = sizeof(DWORD);
329 ret = RegOpenKeyEx(HKEY_CURRENT_USER,
330 _TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"),
331 0, KEY_QUERY_VALUE, &hk);
332 if(ret != ERROR_SUCCESS) return QUrl("");
334 ret = RegQueryValueEx(hk, _TEXT("ProxyServer"), NULL, NULL, (LPBYTE)proxyval, &buflen);
335 if(ret != ERROR_SUCCESS) return QUrl("");
337 ret = RegQueryValueEx(hk, _TEXT("ProxyEnable"), NULL, NULL, (LPBYTE)&enable, &enalen);
338 if(ret != ERROR_SUCCESS) return QUrl("");
340 RegCloseKey(hk);
342 //qDebug() << QString::fromWCharArray(proxyval) << QString("%1").arg(enable);
343 if(enable != 0)
344 return QUrl("http://" + QString::fromWCharArray(proxyval));
345 else
346 return QUrl("");
347 #else
348 return QUrl("");
349 #endif
353 /** @brief detects the installed Rockbox version
354 * @return QString with version. Empty if not aviable
356 QString Detect::installedVersion(QString mountpoint)
358 RockboxInfo info(mountpoint);
359 if(!info.open())
361 return "";
364 return info.version();
368 /** @brief detects installed rockbox target string
369 * @return target name (platform) of installed Rockbox, empty string on error.
371 QString Detect::installedTarget(QString mountpoint)
373 RockboxInfo info(mountpoint);
374 if(!info.open())
376 return "";
379 return info.target();
383 /** @brief checks different Enviroment things. Ask if user wants to continue.
384 * @param settings A pointer to rbutils settings class
385 * @param permission if it should check for permission
386 * @param targetId the targetID to check for. if it is -1 no check is done.
387 * @return string with error messages if problems occurred, empty strings if none.
389 QString Detect::check(bool permission)
391 QString text = "";
393 // check permission
394 if(permission)
396 #if defined(Q_OS_WIN32)
397 if(Detect::userPermissions() != Detect::ADMIN)
399 text += QObject::tr("<li>Permissions insufficient for bootloader "
400 "installation.\nAdministrator priviledges are necessary.</li>");
402 #endif
405 // Check TargetId
406 QString installed = installedTarget(RbSettings::value(RbSettings::Mountpoint).toString());
407 if(!installed.isEmpty() && installed != RbSettings::value(RbSettings::CurConfigureModel).toString())
409 text += QObject::tr("<li>Target mismatch detected.\n"
410 "Installed target: %1, selected target: %2.</li>")
411 .arg(installed, RbSettings::value(RbSettings::CurPlatformName).toString());
412 // FIXME: replace installed by human-friendly name
415 if(!text.isEmpty())
416 return QObject::tr("Problem detected:") + "<ul>" + text + "</ul>";
417 else
418 return text;