Let qmake generate an install Makefile target to install the binary. Doesn't handle...
[Rockbox.git] / rbutil / rbutilqt / utils.cpp
bloba552b5cd5eb30d4b02e0360c895f0deba95d885c
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 ****************************************************************************/
20 #include "utils.h"
22 #include <cstdlib>
24 #include <QDir>
26 #if defined(Q_OS_WIN32)
27 #if defined(UNICODE)
28 #define _UNICODE
29 #endif
30 #include <windows.h>
31 #include <tchar.h>
32 #endif
33 #include <QDebug>
35 // recursive function to delete a dir with files
36 bool recRmdir( const QString &dirName )
38 QString dirN = dirName;
39 QDir dir(dirN);
40 // make list of entries in directory
41 QStringList list = dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot);
42 QFileInfo fileInfo;
43 QString curItem, lstAt;
44 for(int i = 0; i < list.size(); i++){ // loop through all items of list
45 QString name = list.at(i);
46 curItem = dirN + "/" + name;
47 fileInfo.setFile(curItem);
48 if(fileInfo.isDir()) // is directory
49 recRmdir(curItem); // call recRmdir() recursively for deleting subdirectory
50 else // is file
51 QFile::remove(curItem); // ok, delete file
53 dir.cdUp();
54 return dir.rmdir(dirN); // delete empty dir and return if (now empty) dir-removing was successfull
58 //! @brief resolves the given path, ignoring case.
59 //! @param path absolute path to resolve.
60 //! @return returns exact casing of path, empty string if path not found.
61 QString resolvePathCase(QString path)
63 QStringList elems;
64 QString realpath;
66 elems = path.split("/", QString::SkipEmptyParts);
67 int start;
68 #if defined(Q_OS_WIN32)
69 // on windows we must make sure to start with the first entry (i.e. the
70 // drive letter) instead of a single / to make resolving work.
71 start = 1;
72 realpath = elems.at(0) + "/";
73 #else
74 start = 0;
75 realpath = "/";
76 #endif
78 for(int i = start; i < elems.size(); i++) {
79 QStringList direlems
80 = QDir(realpath).entryList(QDir::AllEntries|QDir::Hidden|QDir::System);
81 if(direlems.contains(elems.at(i), Qt::CaseInsensitive)) {
82 // need to filter using QRegExp as QStringList::filter(QString)
83 // matches any substring
84 QString expr = QString("^" + elems.at(i) + "$");
85 QRegExp rx = QRegExp(expr, Qt::CaseInsensitive);
86 QStringList a = direlems.filter(rx);
88 if(a.size() != 1)
89 return QString("");
90 if(!realpath.endsWith("/"))
91 realpath += "/";
92 realpath += a.at(0);
94 else
95 return QString("");
97 qDebug() << __func__ << path << "->" << realpath;
98 return realpath;
102 //! @brief get system proxy value.
103 QUrl systemProxy(void)
105 #if defined(Q_OS_LINUX)
106 return QUrl(getenv("http_proxy"));
107 #elif defined(Q_OS_WIN32)
108 HKEY hk;
109 wchar_t proxyval[80];
110 DWORD buflen = 80;
111 long ret;
112 DWORD enable;
113 DWORD enalen = sizeof(DWORD);
115 ret = RegOpenKeyEx(HKEY_CURRENT_USER,
116 _TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"),
117 0, KEY_QUERY_VALUE, &hk);
118 if(ret != ERROR_SUCCESS) return QUrl("");
120 ret = RegQueryValueEx(hk, _TEXT("ProxyServer"), NULL, NULL, (LPBYTE)proxyval, &buflen);
121 if(ret != ERROR_SUCCESS) return QUrl("");
123 ret = RegQueryValueEx(hk, _TEXT("ProxyEnable"), NULL, NULL, (LPBYTE)&enable, &enalen);
124 if(ret != ERROR_SUCCESS) return QUrl("");
126 RegCloseKey(hk);
128 //qDebug() << QString::fromWCharArray(proxyval) << QString("%1").arg(enable);
129 if(enable != 0)
130 return QUrl("http://" + QString::fromWCharArray(proxyval));
131 else
132 return QUrl("");
133 #else
134 return QUrl("");
135 #endif
138 QString installedVersion(QString mountpoint)
140 // read rockbox-info.txt
141 QFile info(mountpoint +"/.rockbox/rockbox-info.txt");
142 if(!info.open(QIODevice::ReadOnly))
144 return "";
147 QString target, features,version;
148 while (!info.atEnd()) {
149 QString line = info.readLine();
151 if(line.contains("Version:"))
153 return line.remove("Version:").trimmed();
156 info.close();
157 return "";