Fix some quotation marks. Thanks to Alexander Levin for pointing it out.
[Rockbox.git] / rbutil / rbutilqt / utils.cpp
blobd77a1ac125b0f1834a9925e17b988a220f4fc81a
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 = "/";
65 elems = path.split("/", QString::SkipEmptyParts);
67 for(int i = 0; i < elems.size(); i++) {
68 QStringList direlems = QDir(realpath).entryList(QDir::AllEntries);
69 if(direlems.contains(elems.at(i), Qt::CaseInsensitive)) {
70 // need to filter using QRegExp as QStringList::filter(QString)
71 // matches any substring
72 QString expr = QString("^" + elems.at(i) + "$");
73 QRegExp rx = QRegExp(expr, Qt::CaseInsensitive);
74 QStringList a = direlems.filter(rx);
76 if(a.size() != 1)
77 return QString("");
78 if(!realpath.endsWith("/"))
79 realpath += "/";
80 realpath += a.at(0);
82 else
83 return QString("");
85 qDebug() << __func__ << path << "->" << realpath;
86 return realpath;
90 //! @brief get system proxy value.
91 QUrl systemProxy(void)
93 #if defined(Q_OS_LINUX)
94 return QUrl(getenv("http_proxy"));
95 #elif defined(Q_OS_WIN32)
96 HKEY hk;
97 wchar_t proxyval[80];
98 DWORD buflen = 80;
99 long ret;
100 DWORD enable;
101 DWORD enalen = sizeof(DWORD);
103 ret = RegOpenKeyEx(HKEY_CURRENT_USER,
104 _TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"),
105 0, KEY_QUERY_VALUE, &hk);
106 if(ret != ERROR_SUCCESS) return QUrl("");
108 ret = RegQueryValueEx(hk, _TEXT("ProxyServer"), NULL, NULL, (LPBYTE)proxyval, &buflen);
109 if(ret != ERROR_SUCCESS) return QUrl("");
111 ret = RegQueryValueEx(hk, _TEXT("ProxyEnable"), NULL, NULL, (LPBYTE)&enable, &enalen);
112 if(ret != ERROR_SUCCESS) return QUrl("");
114 RegCloseKey(hk);
116 //qDebug() << QString::fromWCharArray(proxyval) << QString("%1").arg(enable);
117 if(enable != 0)
118 return QUrl("http://" + QString::fromWCharArray(proxyval));
119 else
120 return QUrl("");
121 #else
122 return QUrl("");
123 #endif