Add Ipod detection using ipodpatcher.
[Rockbox.git] / rbutil / rbutilqt / autodetection.cpp
blobe8eb498d62080bfb8a8daa2a1113821823adb596
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id: autodetection.cpp 14027 2007-07-27 17:42:49Z domonoky $
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;
56 //try ipodpatcher
57 struct ipod_t ipod;
59 int 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
70 return false;
74 QStringList Autodetection::getMountpoints()
76 #if defined(Q_OS_WIN32)
77 QStringList tempList;
78 QFileInfoList list = QDir::drives();
79 for(int i=0; i<list.size();i++)
81 tempList << list.at(i).absolutePath();
83 return tempList;
85 #elif defined(Q_OS_MACX)
86 QDir dir("/Volumes");
87 return dir.entryList();
88 #elif defined(Q_OS_LINUX)
89 QStringList tempList;
91 FILE *fp = fopen( "/proc/mounts", "r" );
92 if( !fp ) return tempList;
93 char *dev, *dir;
94 while( fscanf( fp, "%as %as %*s %*s %*s %*s", &dev, &dir ) != EOF )
96 tempList << dir;
97 free( dev );
98 free( dir );
100 fclose( fp );
102 return tempList;
103 #else
104 #error Unknown Plattform
105 #endif
108 QString Autodetection::resolveMountPoint(QString device)
110 qDebug() << "Autodetection::resolveMountPoint(QString)" << device;
111 #if defined(Q_OS_LINUX)
112 FILE *fp = fopen( "/proc/mounts", "r" );
113 if( !fp ) return QString("");
114 char *dev, *dir;
115 while( fscanf( fp, "%as %as %*s %*s %*s %*s", &dev, &dir ) != EOF )
117 if( QString(dev).startsWith(device) )
119 QString directory = dir;
120 free( dev );
121 free( dir );
122 return directory;
124 free( dev );
125 free( dir );
127 fclose( fp );
129 #endif
130 return QString("");