/*************************************************************************** source::worx raDIYo Copyright © 2020-2022 c.holzheuer chris@sourceworx.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. ***************************************************************************/ #include #include #include #include #ifdef Q_OS_LINUX #include #include #include #endif SWUSBControl::SWUSBControl( QWidget* parent, QSettings* settings ) : SWSongsControl( parent, settings ) { setHeaderText( "USB Source" ); #ifdef Q_OS_WIN _mountDir = "F:/"; //_mediaDirs = { "D:/", "E:/", "G:/" }; _mediaDirs << "D:/" << "E:/" << "G:/" << "I:/"; #endif #ifdef Q_OS_LINUX // uid ermitteln, für den standard mountpoint // /media/ uid_t uid = ::getuid(); struct passwd * pstruct = getpwuid( uid ); QString uname( pstruct->pw_name ); _mountDir = raDIYo::MountDir + uname; fetchDirs(); #endif _watcher.addPath( _mountDir ); connect( &_watcher, SIGNAL( directoryChanged(QString) ), this, SLOT( onUSBMount(QString) ) ); } /** * @brief Zeigt die gemounteten USB-Laufwerke oder das aktuelle Verzeichnis an. * Wird beim Einblenden aufgerufen. */ void SWUSBControl::onShow() { if( _currentDir.isEmpty() ) showDirs(); else loadFiles( _currentDir ); } /** * @brief SLOT, der ein USB-Laufwerks beim mounten automatisch einbindet. * @param path */ void SWUSBControl::onUSBMount( const QString& ) { _currentDir = ""; fetchDirs(); emit driveMounted( RaDIYo::USB ); } /** * @brief Aktivieren eines Listeneintrags. * Wird zur Navigation innerhalb des Vertzeichnisbaums verwendet. * @param item */ void SWUSBControl::onItemActivated( QListWidgetItem* item ) { QString urlText = item->data( SWListControlRole ).toString(); if( !urlText.startsWith( "dir:") ) { if( urlText.startsWith( "root:") ) return showDirs(); return SWListControl::onItemActivated( item ); } loadFiles( urlText.mid(4) ); } /** * @brief Interne Hilfsfunktion, die potentiellen USB-Laufwerke auflistet. */ void SWUSBControl::fetchDirs() { // alle drives, 'die wo' Sticks sein könnten, also // auf 'media/' gemountet sind _mediaDirs.clear(); for( auto& storage : QStorageInfo::mountedVolumes() ) { if( storage.isValid() && storage.isReady() ) { QString path = storage.rootPath(); if( path.startsWith( _mountDir ) ) { _mediaDirs.append( path ); } } } } /** * @brief Ziegt alle potentiellen USB-Laufwerke an. */ void SWUSBControl::showDirs() { _contentView->clear(); setHeaderText( "USB Source: root" ); QString tpl( "[%1]" ); for( auto& dir : _mediaDirs ) { QListWidgetItem* item = new QListWidgetItem( tpl.arg( dir) ); item->setData( SWListControlRole, "dir:" + dir ); _contentView->addItem( item ); } } /** * @brief SWUSBControl::loadFiles * @param fullPath * - mounts anzeigen */ void SWUSBControl::loadFiles( const QString& fullPath ) { _contentView->clear(); setHeaderText( "USB Source: " + fullPath ); _currentDir = fullPath; bool isRoot = QFileInfo( fullPath ).isRoot(); if( isRoot ) { QListWidgetItem* item = new QListWidgetItem( "[..]" ); item->setData( SWListControlRole, "root:" + fullPath ); _contentView->addItem( item ); } QString tpl( "[%1]" ); QDirIterator it( fullPath, QDir::Files | QDir::Dirs ); while ( it.hasNext() ) { const QFileInfo& entry = it.next(); QString fname = entry.fileName(); if( fname == "." ) continue; QString prefix, dname; if( entry.isFile() ) { if( !fname.endsWith( ".mp3") && !fname.endsWith( ".wav") ) continue; dname = fname; prefix = "file:"; } else { dname = tpl.arg( fname ); prefix = "dir:"; } QListWidgetItem* item = new QListWidgetItem( dname ); item->setData( SWListControlRole, prefix + entry.absoluteFilePath() ); _contentView->addItem( item ); } }