clean up some debugging output.
[Rockbox.git] / rbutil / rbutilqt / detect.cpp
blob0f170b47e7971d337a4db6a78fdd9fa196165dac
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id: detect.cpp 17769 2008-06-23 20:31:44Z Domonoky $
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>
25 #include <cstdlib>
26 #include <stdio.h>
27 #include <QMessageBox>
30 // Windows Includes
31 #if defined(Q_OS_WIN32)
32 #if defined(UNICODE)
33 #define _UNICODE
34 #endif
35 #include <windows.h>
36 #include <tchar.h>
37 #include <lm.h>
38 #include <windows.h>
39 #include <setupapi.h>
40 #endif
42 // Linux and Mac includes
43 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
44 #include <usb.h>
45 #include <sys/utsname.h>
46 #include <unistd.h>
47 #include <pwd.h>
48 #endif
50 // Linux includes
51 #if defined(Q_OS_LINUX)
52 #include <mntent.h>
53 #endif
55 // Mac includes
56 #if defined(Q_OS_MACX)
57 #include <sys/param.h>
58 #include <sys/ucred.h>
59 #include <sys/mount.h>
60 #endif
63 /** @brief detect permission of user (only Windows at moment).
64 * @return enum userlevel.
66 #if defined(Q_OS_WIN32)
67 enum Detect::userlevel Detect::userPermissions(void)
69 LPUSER_INFO_1 buf;
70 NET_API_STATUS napistatus;
71 wchar_t userbuf[UNLEN];
72 DWORD usersize = UNLEN;
73 BOOL status;
74 enum userlevel result;
76 status = GetUserNameW(userbuf, &usersize);
77 if(!status)
78 return ERR;
80 napistatus = NetUserGetInfo(NULL, userbuf, (DWORD)1, (LPBYTE*)&buf);
82 switch(buf->usri1_priv) {
83 case USER_PRIV_GUEST:
84 result = GUEST;
85 break;
86 case USER_PRIV_USER:
87 result = USER;
88 break;
89 case USER_PRIV_ADMIN:
90 result = ADMIN;
91 break;
92 default:
93 result = ERR;
94 break;
96 NetApiBufferFree(buf);
98 return result;
101 /** @brief detects user permissions (only Windows at moment).
102 * @return a user readable string with the permission.
104 QString Detect::userPermissionsString(void)
106 QString result;
107 int perm = userPermissions();
108 switch(perm) {
109 case GUEST:
110 result = QObject::tr("Guest");
111 break;
112 case ADMIN:
113 result = QObject::tr("Admin");
114 break;
115 case USER:
116 result = QObject::tr("User");
117 break;
118 default:
119 result = QObject::tr("Error");
120 break;
122 return result;
124 #endif
127 /** @brief detects current Username.
128 * @return string with Username.
130 QString Detect::userName(void)
132 #if defined(Q_OS_WIN32)
133 wchar_t userbuf[UNLEN];
134 DWORD usersize = UNLEN;
135 BOOL status;
137 status = GetUserNameW(userbuf, &usersize);
139 return QString::fromWCharArray(userbuf);
140 #endif
141 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
142 struct passwd *user;
143 user = getpwuid(geteuid());
144 return QString(user->pw_name);
145 #endif
149 /** @brief detects the OS Version
150 * @return String with OS Version.
152 QString Detect::osVersionString(void)
154 QString result;
155 #if defined(Q_OS_WIN32)
156 OSVERSIONINFO osvi;
157 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
158 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
159 GetVersionEx(&osvi);
161 result = QString("Windows version %1.%2, ").arg(osvi.dwMajorVersion).arg(osvi.dwMinorVersion);
162 result += QString("build %1 (%2)").arg(osvi.dwBuildNumber).arg(QString::fromWCharArray(osvi.szCSDVersion));
163 #endif
164 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
165 struct utsname u;
166 int ret;
167 ret = uname(&u);
169 result = QString("CPU: %1<br/>System: %2<br/>Release: %3<br/>Version: %4")
170 .arg(u.machine).arg(u.sysname).arg(u.release).arg(u.version);
171 #endif
172 return result;
175 QList<uint32_t> Detect::listUsbIds(void)
177 return listUsbDevices().keys();
180 /** @brief detect devices based on usb pid / vid.
181 * @return list with usb VID / PID values.
183 QMap<uint32_t, QString> Detect::listUsbDevices(void)
185 QMap<uint32_t, QString> usbids;
186 // usb pid detection
187 #if defined(Q_OS_LINUX) | defined(Q_OS_MACX)
188 usb_init();
189 usb_find_busses();
190 usb_find_devices();
191 struct usb_bus *b;
192 b = usb_busses;
194 while(b) {
195 qDebug() << "bus:" << b->dirname << b->devices;
196 if(b->devices) {
197 qDebug() << "devices present.";
198 struct usb_device *u;
199 u = b->devices;
200 while(u) {
201 uint32_t id;
202 id = u->descriptor.idVendor << 16 | u->descriptor.idProduct;
203 // FIXME: until description is empty for now.
204 if(id) usbids.insert(id, QString(""));
205 u = u->next;
208 b = b->next;
210 #endif
212 #if defined(Q_OS_WIN32)
213 HDEVINFO deviceInfo;
214 SP_DEVINFO_DATA infoData;
215 DWORD i;
217 // Iterate over all devices
218 // by doing it this way it's unneccessary to use GUIDs which might be not
219 // present in current MinGW. It also seemed to be more reliably than using
220 // a GUID.
221 // See KB259695 for an example.
222 deviceInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
224 infoData.cbSize = sizeof(SP_DEVINFO_DATA);
226 for(i = 0; SetupDiEnumDeviceInfo(deviceInfo, i, &infoData); i++) {
227 DWORD data;
228 LPTSTR buffer = NULL;
229 DWORD buffersize = 0;
230 QString description;
232 // get device desriptor first
233 // for some reason not doing so results in bad things (tm)
234 while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
235 SPDRP_DEVICEDESC,&data, (PBYTE)buffer, buffersize, &buffersize)) {
236 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
237 if(buffer) free(buffer);
238 // double buffer size to avoid problems as per KB888609
239 buffer = (LPTSTR)malloc(buffersize * 2);
241 else {
242 break;
246 // now get the hardware id, which contains PID and VID.
247 while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
248 SPDRP_LOCATION_INFORMATION,&data, (PBYTE)buffer, buffersize, &buffersize)) {
249 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
250 if(buffer) free(buffer);
251 // double buffer size to avoid problems as per KB888609
252 buffer = (LPTSTR)malloc(buffersize * 2);
254 else {
255 break;
258 description = QString::fromWCharArray(buffer);
260 while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
261 SPDRP_HARDWAREID,&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 unsigned int vid, pid, rev;
273 if(_stscanf(buffer, _TEXT("USB\\Vid_%x&Pid_%x&Rev_%x"), &vid, &pid, &rev) == 3) {
274 uint32_t id;
275 id = vid << 16 | pid;
276 usbids.insert(id, description);
277 qDebug("VID: %04x, PID: %04x", vid, pid);
279 if(buffer) free(buffer);
281 SetupDiDestroyDeviceInfoList(deviceInfo);
283 #endif
284 return usbids;
288 /** @brief detects current system proxy
289 * @return QUrl with proxy or empty
291 QUrl Detect::systemProxy(void)
293 #if defined(Q_OS_LINUX)
294 return QUrl(getenv("http_proxy"));
295 #elif defined(Q_OS_WIN32)
296 HKEY hk;
297 wchar_t proxyval[80];
298 DWORD buflen = 80;
299 long ret;
300 DWORD enable;
301 DWORD enalen = sizeof(DWORD);
303 ret = RegOpenKeyEx(HKEY_CURRENT_USER,
304 _TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"),
305 0, KEY_QUERY_VALUE, &hk);
306 if(ret != ERROR_SUCCESS) return QUrl("");
308 ret = RegQueryValueEx(hk, _TEXT("ProxyServer"), NULL, NULL, (LPBYTE)proxyval, &buflen);
309 if(ret != ERROR_SUCCESS) return QUrl("");
311 ret = RegQueryValueEx(hk, _TEXT("ProxyEnable"), NULL, NULL, (LPBYTE)&enable, &enalen);
312 if(ret != ERROR_SUCCESS) return QUrl("");
314 RegCloseKey(hk);
316 //qDebug() << QString::fromWCharArray(proxyval) << QString("%1").arg(enable);
317 if(enable != 0)
318 return QUrl("http://" + QString::fromWCharArray(proxyval));
319 else
320 return QUrl("");
321 #else
322 return QUrl("");
323 #endif
327 /** @brief detects the installed Rockbox version
328 * @return QString with version. Empty if not aviable
330 QString Detect::installedVersion(QString mountpoint)
332 // read rockbox-info.txt
333 QFile info(mountpoint +"/.rockbox/rockbox-info.txt");
334 if(!info.open(QIODevice::ReadOnly))
336 return "";
339 while (!info.atEnd()) {
340 QString line = info.readLine();
342 if(line.contains("Version:"))
344 return line.remove("Version:").trimmed();
347 info.close();
348 return "";
352 /** @brief detects installed rockbox target id
353 * @return TargetId of installed rockbox, or -1 if not available
355 int Detect::installedTargetId(QString mountpoint)
357 // read rockbox-info.txt
358 QFile info(mountpoint +"/.rockbox/rockbox-info.txt");
359 if(!info.open(QIODevice::ReadOnly))
361 return -1;
364 while (!info.atEnd())
366 QString line = info.readLine();
367 if(line.contains("Target id:"))
369 qDebug() << line;
370 return line.remove("Target id:").trimmed().toInt();
373 info.close();
374 return -1;
378 /** @brief checks different Enviroment things. Ask if user wants to continue.
379 * @param settings A pointer to rbutils settings class
380 * @param permission if it should check for permission
381 * @param targetId the targetID to check for. if it is -1 no check is done.
382 * @return true if everything is ok, or user wants to continue
384 bool Detect::check(RbSettings* settings,bool permission,int targetId)
386 QString text = "";
388 // check permission
389 if(permission)
391 #if defined(Q_OS_WIN32)
392 if(Detect::userPermissions() != Detect::ADMIN)
394 text += QObject::tr("Permissions are not sufficient! \n Run with admin rights. \n\n");
396 #endif
399 // Check TargetId
400 if(targetId > 0)
402 int installedID = Detect::installedTargetId(settings->mountpoint());
403 if( installedID != -1 && installedID != targetId)
405 text += QObject::tr("Target mismatch detected. \n\n"
406 "Installed target: %1.\n"
407 "New Target: %2.\n\n").arg(settings->nameOfTargetId(installedID),settings->curName());
412 // show message Box
413 if(text != "")
415 text += QObject::tr("\n Do you want to continue ?");
416 if(QMessageBox::warning(NULL, QObject::tr("Problems detected"),text,
417 QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
419 return false;
423 return true;