- check for specific files / folders case-insensitive
[Rockbox.git] / rbutil / rbutilqt / autodetection.cpp
blobf29df535ab35dd1bd011354832442c2497f37c6f
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 "autodetection.h"
22 #if defined(Q_OS_LINUX)
23 #include <stdio.h>
24 #include <mntent.h>
25 #endif
27 Autodetection::Autodetection(QObject* parent): QObject(parent)
32 bool Autodetection::detect()
34 m_device = "";
35 m_mountpoint = "";
37 // Try detection via rockbox.info / rbutil.log
38 QStringList mountpoints = getMountpoints();
40 for(int i=0; i< mountpoints.size();i++)
42 // do the file checking
43 QDir dir(mountpoints.at(i));
44 if(dir.exists())
46 // check logfile first.
47 if(QFile(mountpoints.at(i) + "/.rockbox/rbutil.log").exists()) {
48 QSettings log(mountpoints.at(i) + "/.rockbox/rbutil.log",
49 QSettings::IniFormat, this);
50 if(!log.value("platform").toString().isEmpty()) {
51 m_device = log.value("platform").toString();
52 m_mountpoint = mountpoints.at(i);
53 qDebug() << "rbutil.log detected:" << m_device << m_mountpoint;
54 return true;
58 // check rockbox-info.txt afterwards.
59 QFile file(mountpoints.at(i) + "/.rockbox/rockbox-info.txt");
60 if(file.exists())
62 file.open(QIODevice::ReadOnly | QIODevice::Text);
63 QString line = file.readLine();
64 if(line.startsWith("Target: "))
66 line.remove("Target: ");
67 m_device = line.trimmed(); // trim whitespaces
68 m_mountpoint = mountpoints.at(i);
69 qDebug() << "rockbox-info.txt detected:" << m_device << m_mountpoint;
70 return true;
73 // check for some specific files in root folder
74 QDir root(mountpoints.at(i));
75 QStringList rootentries = root.entryList(QDir::Files);
76 if(rootentries.contains("archos.mod", Qt::CaseInsensitive))
78 // archos.mod in root folder -> Archos Player
79 m_device = "player";
80 m_mountpoint = mountpoints.at(i);
81 return true;
83 if(rootentries.contains("ONDIOST.BIN"), Qt::CaseInsensitive)
85 // ONDIOST.BIN in root -> Ondio FM
86 m_device = "ondiofm";
87 m_mountpoint = mountpoints.at(i);
88 return true;
90 if(rootentries.contains("ONDIOSP.BIN"), Qt::CaseInsensitive)
92 // ONDIOSP.BIN in root -> Ondio SP
93 m_device = "ondiosp";
94 m_mountpoint = mountpoints.at(i);
95 return true;
97 if(rootentries.contains("ajbrec.ajz"), Qt::CaseInsensitive)
99 qDebug() << "it's an archos. further detection needed";
101 // detection based on player specific folders
102 QStringList rootfolders = root.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
103 if(rootfolders.contains("GBSYSTEM"), Qt::CaseInsensitive)
105 // GBSYSTEM folder -> Gigabeat
106 m_device = "gigabeatf";
107 m_mountpoint = mountpoints.at(i);
108 return true;
110 qDebug() << rootfolders;
115 int n;
116 //try ipodpatcher
117 struct ipod_t ipod;
118 n = ipod_scan(&ipod);
119 if(n == 1) {
120 qDebug() << "Ipod found:" << ipod.modelstr << "at" << ipod.diskname;
121 m_device = ipod.targetname;
122 m_mountpoint = resolveMountPoint(ipod.diskname);
123 return true;
126 //try sansapatcher
127 struct sansa_t sansa;
128 n = sansa_scan(&sansa);
129 if(n == 1) {
130 qDebug() << "Sansa found:" << "sansae200" << "at" << sansa.diskname;
131 m_device = "sansae200";
132 m_mountpoint = resolveMountPoint(sansa.diskname);
133 return true;
136 return false;
140 QStringList Autodetection::getMountpoints()
142 #if defined(Q_OS_WIN32)
143 QStringList tempList;
144 QFileInfoList list = QDir::drives();
145 for(int i=0; i<list.size();i++)
147 tempList << list.at(i).absolutePath();
149 return tempList;
151 #elif defined(Q_OS_MACX)
152 QDir dir("/Volumes");
153 return dir.entryList();
154 #elif defined(Q_OS_LINUX)
155 QStringList tempList;
157 FILE *mn = setmntent("/etc/mtab", "r");
158 if(!mn)
159 return QStringList("");
161 struct mntent *ent;
162 while((ent = getmntent(mn)))
163 tempList << QString(ent->mnt_dir);
164 endmntent(mn);
166 return tempList;
167 #else
168 #error Unknown Plattform
169 #endif
172 QString Autodetection::resolveMountPoint(QString device)
174 qDebug() << "Autodetection::resolveMountPoint(QString)" << device;
176 #if defined(Q_OS_LINUX)
177 FILE *mn = setmntent("/etc/mtab", "r");
178 if(!mn)
179 return QString("");
181 struct mntent *ent;
182 while((ent = getmntent(mn))) {
183 if(QString(ent->mnt_fsname).startsWith(device)
184 && QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive)) {
185 endmntent(mn);
186 return QString(ent->mnt_dir);
189 endmntent(mn);
191 #endif
192 return QString("");