Oops, part 2: I shouldn't remove an icon that is still in use. Also convert some...
[Rockbox.git] / rbutil / rbutilqt / autodetection.cpp
blob08c471718da3f5b73e6b3dc075169041c175c834
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 Autodetection::Autodetection(QObject* parent): QObject(parent)
27 bool Autodetection::detect()
29 m_device = "";
30 m_mountpoint = "";
32 // Try detection via rockbox.info
33 QStringList mountpoints = getMountpoints();
35 for(int i=0; i< mountpoints.size();i++)
37 QDir dir(mountpoints.at(i));
38 if(dir.exists())
40 QFile file(mountpoints.at(i) + "/.rockbox/rockbox-info.txt");
41 if(file.exists())
43 file.open(QIODevice::ReadOnly | QIODevice::Text);
44 QString line = file.readLine();
45 if(line.startsWith("Target: "))
47 line.remove("Target: ");
48 m_device = line;
49 m_mountpoint = mountpoints.at(i);
50 return true;
55 int n;
57 //try ipodpatcher
58 struct ipod_t ipod;
59 n = ipod_scan(&ipod);
60 if(n == 1) {
61 qDebug() << "Ipod found:" << ipod.modelstr << "at" << ipod.diskname;
62 m_device = ipod.targetname;
63 m_mountpoint = resolveMountPoint(ipod.diskname);
64 return true;
67 //try sansapatcher
68 struct sansa_t sansa;
69 n = sansa_scan(&sansa);
70 if(n == 1) {
71 qDebug() << "Sansa found:" << "sansae200" << "at" << sansa.diskname;
72 m_device = "sansae200";
73 m_mountpoint = resolveMountPoint(sansa.diskname);
74 return true;
77 return false;
81 QStringList Autodetection::getMountpoints()
83 #if defined(Q_OS_WIN32)
84 QStringList tempList;
85 QFileInfoList list = QDir::drives();
86 for(int i=0; i<list.size();i++)
88 tempList << list.at(i).absolutePath();
90 return tempList;
92 #elif defined(Q_OS_MACX)
93 QDir dir("/Volumes");
94 return dir.entryList();
95 #elif defined(Q_OS_LINUX)
96 QStringList tempList;
98 FILE *fp = fopen( "/proc/mounts", "r" );
99 if( !fp ) return tempList;
100 char *dev, *dir;
101 while( fscanf( fp, "%as %as %*s %*s %*s %*s", &dev, &dir ) != EOF )
103 tempList << dir;
104 free( dev );
105 free( dir );
107 fclose( fp );
109 return tempList;
110 #else
111 #error Unknown Plattform
112 #endif
115 QString Autodetection::resolveMountPoint(QString device)
117 qDebug() << "Autodetection::resolveMountPoint(QString)" << device;
118 #if defined(Q_OS_LINUX)
119 FILE *fp = fopen( "/proc/mounts", "r" );
120 if( !fp ) return QString("");
121 char *dev, *dir;
122 while( fscanf( fp, "%as %as %*s %*s %*s %*s", &dev, &dir ) != EOF )
124 if( QString(dev).startsWith(device) )
126 QString directory = dir;
127 free( dev );
128 free( dir );
129 fclose(fp);
130 return directory;
132 free( dev );
133 free( dir );
135 fclose( fp );
137 #endif
138 return QString("");