rbutil: create a RockboxInfo class so all rockbox-info.txt handling is in one place.
[kugel-rb.git] / rbutil / rbutilqt / base / utils.cpp
blob2e6b04ecff904fd61faad863ea4f075700ba8fc8
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"
21 #ifdef UNICODE
22 #define _UNICODE
23 #endif
25 #include <QtCore>
26 #include <QDebug>
27 #include <cstdlib>
28 #include <stdio.h>
30 #if defined(Q_OS_WIN32)
31 #include <windows.h>
32 #include <tchar.h>
33 #include <winioctl.h>
34 #endif
35 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
36 #include <sys/statvfs.h>
37 #endif
39 // recursive function to delete a dir with files
40 bool recRmdir( const QString &dirName )
42 QString dirN = dirName;
43 QDir dir(dirN);
44 // make list of entries in directory
45 QStringList list = dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot);
46 QFileInfo fileInfo;
47 QString curItem, lstAt;
48 for(int i = 0; i < list.size(); i++){ // loop through all items of list
49 QString name = list.at(i);
50 curItem = dirN + "/" + name;
51 fileInfo.setFile(curItem);
52 if(fileInfo.isDir()) // is directory
53 recRmdir(curItem); // call recRmdir() recursively for deleting subdirectory
54 else // is file
55 QFile::remove(curItem); // ok, delete file
57 dir.cdUp();
58 return dir.rmdir(dirN); // delete empty dir and return if (now empty) dir-removing was successfull
62 //! @brief resolves the given path, ignoring case.
63 //! @param path absolute path to resolve.
64 //! @return returns exact casing of path, empty string if path not found.
65 QString resolvePathCase(QString path)
67 QStringList elems;
68 QString realpath;
70 elems = path.split("/", QString::SkipEmptyParts);
71 int start;
72 #if defined(Q_OS_WIN32)
73 // on windows we must make sure to start with the first entry (i.e. the
74 // drive letter) instead of a single / to make resolving work.
75 start = 1;
76 realpath = elems.at(0) + "/";
77 #else
78 start = 0;
79 realpath = "/";
80 #endif
82 for(int i = start; i < elems.size(); i++) {
83 QStringList direlems
84 = QDir(realpath).entryList(QDir::AllEntries|QDir::Hidden|QDir::System);
85 if(direlems.contains(elems.at(i), Qt::CaseInsensitive)) {
86 // need to filter using QRegExp as QStringList::filter(QString)
87 // matches any substring
88 QString expr = QString("^" + elems.at(i) + "$");
89 QRegExp rx = QRegExp(expr, Qt::CaseInsensitive);
90 QStringList a = direlems.filter(rx);
92 if(a.size() != 1)
93 return QString("");
94 if(!realpath.endsWith("/"))
95 realpath += "/";
96 realpath += a.at(0);
98 else
99 return QString("");
101 qDebug() << __func__ << path << "->" << realpath;
102 return realpath;
106 //! @brief figure the free disk space on a filesystem
107 //! @param path path on the filesystem to check
108 //! @return size in bytes
109 qulonglong filesystemFree(QString path)
111 qlonglong size = 0;
112 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
113 // the usage of statfs() is deprecated by the LSB so use statvfs().
114 struct statvfs fs;
115 int ret;
117 ret = statvfs(qPrintable(path), &fs);
119 if(ret == 0)
120 size = fs.f_bsize * fs.f_bavail;
121 #endif
122 #if defined(Q_OS_WIN32)
123 BOOL ret;
124 ULARGE_INTEGER freeAvailBytes;
126 ret = GetDiskFreeSpaceExW((LPCTSTR)path.utf16(), &freeAvailBytes, NULL, NULL);
127 if(ret)
128 size = freeAvailBytes.QuadPart;
129 #endif
130 return size;
133 RockboxInfo::RockboxInfo(QString mountpoint)
135 m_path = mountpoint +"/.rockbox/rockbox-info.txt";
138 bool RockboxInfo::open()
140 QFile file(m_path);
141 if(!file.exists())
142 return false;
144 if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
145 return false;
147 // read file contents
148 while (!file.atEnd())
150 QString line = file.readLine();
152 if(line.contains("Version:"))
154 m_version = line.remove("Version:").trimmed();
156 else if(line.contains("Target: "))
158 m_target = line.remove("Target: ").trimmed();
160 else if(line.contains("Features:"))
162 m_features = line.remove("Features:").trimmed();
164 else if(line.contains("Target id:"))
166 m_targetid = line.remove("Target id:").trimmed();
170 file.close();
171 return true;