rbutil: completely rework how tts and encoders are configured. (FS#10070)
[kugel-rb.git] / rbutil / rbutilqt / base / utils.cpp
blob966ce335eabe3fd32ba3e27d7e271361bcde8f43
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 = (qulonglong)fs.f_bsize * (qulonglong)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 //! \brief searches for a Executable in the Environement Path
134 QString findExecutable(QString name)
136 QString exepath;
137 //try autodetect tts
138 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX) || defined(Q_OS_OPENBSD)
139 QStringList path = QString(getenv("PATH")).split(":", QString::SkipEmptyParts);
140 #elif defined(Q_OS_WIN)
141 QStringList path = QString(getenv("PATH")).split(";", QString::SkipEmptyParts);
142 #endif
143 qDebug() << path;
144 for(int i = 0; i < path.size(); i++)
146 QString executable = QDir::fromNativeSeparators(path.at(i)) + "/" + name;
147 #if defined(Q_OS_WIN)
148 executable += ".exe";
149 QStringList ex = executable.split("\"", QString::SkipEmptyParts);
150 executable = ex.join("");
151 #endif
152 qDebug() << executable;
153 if(QFileInfo(executable).isExecutable())
155 return QDir::toNativeSeparators(executable);
158 return "";
162 RockboxInfo::RockboxInfo(QString mountpoint)
164 m_path = mountpoint +"/.rockbox/rockbox-info.txt";
167 bool RockboxInfo::open()
169 QFile file(m_path);
170 if(!file.exists())
171 return false;
173 if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
174 return false;
176 // read file contents
177 while (!file.atEnd())
179 QString line = file.readLine();
181 if(line.contains("Version:"))
183 m_version = line.remove("Version:").trimmed();
185 else if(line.contains("Target: "))
187 m_target = line.remove("Target: ").trimmed();
189 else if(line.contains("Features:"))
191 m_features = line.remove("Features:").trimmed();
193 else if(line.contains("Target id:"))
195 m_targetid = line.remove("Target id:").trimmed();
199 file.close();
200 return true;