ifpmediadevice.cpp

来自「Amarok是一款在LINUX或其他类UNIX操作系统中运行的音频播放器软件。 」· C++ 代码 · 共 715 行 · 第 1/2 页

CPP
715
字号
/*************************************************************************** * copyright            : (C) 2005-2006 Seb Ruiz <me@sebruiz.net>          * *                                                                         * * With some code helpers from KIO_IFP                                     * *                        (c) 2004 Thomas Loeber <ifp@loeber1.de>          * ***************************************************************************//*************************************************************************** *                                                                         * *   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.                                   * *                                                                         * ***************************************************************************/ /**  *  iRiver ifp media device code  *  @author Seb Ruiz <me@sebruiz.net>  *  @see http://ifp-driver.sourceforge.net/libifp/docs/ifp_8h.html  *  @note ifp uses a backslash '\' as a directory delimiter for _remote_ files  */#define DEBUG_PREFIX "IfpMediaDevice"#include "ifpmediadevice.h"AMAROK_EXPORT_PLUGIN( IfpMediaDevice )#include "debug.h"#include "metabundle.h"#include "collectiondb.h"#include "statusbar/statusbar.h"#include "transferdialog.h"#include <kapplication.h>#include <kconfig.h>           //download saveLocation#include <kiconloader.h>       //smallIcon#include <kmessagebox.h>#include <kpopupmenu.h>#include <kurlrequester.h>     //downloadSelectedItems()#include <kurlrequesterdlg.h>  //downloadSelectedItems()#include <qfile.h>#include <qcstring.h>#include <qregexp.h>namespace Amarok {    extern KConfig *config( const QString& );    extern QString cleanPath( const QString&, bool );}/** * IfpMediaItem Class */class IfpMediaItem : public MediaItem{    public:        IfpMediaItem( QListView *parent, QListViewItem *after = 0 )            : MediaItem( parent, after )        {}        IfpMediaItem( QListViewItem *parent, QListViewItem *after = 0 )            : MediaItem( parent, after )        {}        void        setEncodedName( QString &name )        {            m_encodedName = QFile::encodeName( name );        }        void        setEncodedName( QCString &name ) { m_encodedName = name; }        QCString        encodedName() { return m_encodedName; }        // List directories first, always        int        compare( QListViewItem *i, int col, bool ascending ) const        {            #define i static_cast<IfpMediaItem *>(i)            switch( type() )            {                case MediaItem::DIRECTORY:                    if( i->type() == MediaItem::DIRECTORY )                        break;                    return -1;                default:                    if( i->type() == MediaItem::DIRECTORY )                        return 1;            }            #undef i            return MediaItem::compare(i, col, ascending);        }    private:        bool     m_dir;        QCString m_encodedName;};/** * IfpMediaDevice Class */IfpMediaDevice::IfpMediaDevice()    : MediaDevice()    , m_dev( 0 )    , m_dh( 0 )    , m_connected( false )    , m_last( 0 )    , m_tmpParent( 0 )    , m_td( 0 ){    m_name = "iRiver";    m_hasMountPoint = false;    m_spacesToUnderscores = configBool("spacesToUnderscores");    m_firstSort           = configString( "firstGrouping", i18n("None") );    m_secondSort          = configString( "secondGrouping", i18n("None") );    m_thirdSort           = configString( "thirdGrouping", i18n("None") );}voidIfpMediaDevice::init( MediaBrowser* parent ){    MediaDevice::init( parent );}IfpMediaDevice::~IfpMediaDevice(){    setConfigString( "firstGrouping"    , m_firstSort );    setConfigString( "secondGrouping"   , m_secondSort );    setConfigString( "thirdGrouping"    , m_thirdSort );    setConfigBool( "spacesToUnderscores", m_spacesToUnderscores );    closeDevice();}boolIfpMediaDevice::checkResult( int result, QString message ){    if( result == 0 )        return true;    error() << result << ": " << message << endl;    return false;}boolIfpMediaDevice::openDevice( bool /*silent*/ ){    DEBUG_BLOCK    usb_init();    m_dh = (usb_dev_handle*)ifp_find_device();    QString genericError = i18n( "Could not connect to iFP device" );    if( m_dh == NULL )    {        error() << "A suitable iRiver iFP device couldn't be found" << endl;        Amarok::StatusBar::instance()->shortLongMessage( genericError,                                        i18n("iFP: A suitable iRiver iFP device could not be found")                                        , KDE::StatusBar::Error );        return false;    }    m_dev = usb_device( m_dh );    if( m_dev == NULL )    {        error() << "Could not get usb_device()" << endl;        Amarok::StatusBar::instance()->shortLongMessage( genericError,                                        i18n("iFP: Could not get a USB device handle"), KDE::StatusBar::Error );        if( ifp_release_device( m_dh ) )            error() << "warning: release_device failed." << endl;        return false;    }    /* "must be called" written in the libusb documentation */    if( usb_claim_interface( m_dh, m_dev->config->interface->altsetting->bInterfaceNumber ) )    {        error() << "Device is busy.  (I was unable to claim its interface.)" << endl;        Amarok::StatusBar::instance()->shortLongMessage( genericError,                                        i18n("iFP: Device is busy"), KDE::StatusBar::Error );        if( ifp_release_device( m_dh ) )            error() << "warning: release_device failed." << endl;        return false;    }    int i = ifp_init( &m_ifpdev, m_dh );    if( i )    {        error() << "iFP device: Device cannot be opened." << endl;        Amarok::StatusBar::instance()->shortLongMessage( genericError,                                        i18n("iFP: Could not open device"), KDE::StatusBar::Error );        usb_release_interface( m_dh, m_dev->config->interface->altsetting->bInterfaceNumber );        return false;    }    m_connected = true;    char info[20];    ifp_model( &m_ifpdev, info, 20 );    m_transferDir = QString(info);    debug() << "Successfully connected to: " << info << endl;    listDir( "" );    return true;}boolIfpMediaDevice::closeDevice()  //SLOT{    DEBUG_BLOCK    if( m_connected )    {        if( m_dh )        {            usb_release_interface( m_dh, m_dev->config->interface->altsetting->bInterfaceNumber );            if( ifp_release_device( m_dh ) )                error() << "warning: release_device failed." << endl;            ifp_finalize( &m_ifpdev );            m_dh = 0;        }        m_view->clear();        m_connected = false;    }    return true;}voidIfpMediaDevice::runTransferDialog(){    m_td = new TransferDialog( this );    m_td->exec();}/// RenamingvoidIfpMediaDevice::renameItem( QListViewItem *item ) // SLOT{    if( !item )        return;    #define item static_cast<IfpMediaItem*>(item)    QCString src  = QFile::encodeName( getFullPath( item, false ) );    src.append( item->encodedName() );     //the rename line edit has already changed the QListViewItem text    QCString dest = QFile::encodeName( getFullPath( item ) );    debug() << "Renaming " << src << " to: " << dest << endl;    if( ifp_rename( &m_ifpdev, src, dest ) ) //success == 0        //rename failed        item->setText( 0, item->encodedName() );    #undef item}/// Creating a directoryMediaItem *IfpMediaDevice::newDirectory( const QString &name, MediaItem *parent ){    if( !m_connected || name.isEmpty() ) return 0;    QString cleanedName = cleanPath( name );    const QCString dirPath = QFile::encodeName( getFullPath( parent ) + "\\" + cleanedName );    debug() << "Creating directory: " << dirPath << endl;    int err = ifp_mkdir( &m_ifpdev, dirPath );    if( err ) //failed        return 0;    m_tmpParent = parent;    addTrackToList( IFP_DIR, cleanedName );    return m_last;}MediaItem *IfpMediaDevice::newDirectoryRecursive( const QString &name, MediaItem *parent ){    QStringList folders = QStringList::split( '\\', name );    QString progress = "";    if( parent )        progress += getFullPath( parent ) + "\\";    else        progress += "\\";    foreach( folders )    {        debug() << "Checking folder: " << progress << endl;        progress += *it;        const QCString dirPath = QFile::encodeName( progress );        if( ifp_exists( &m_ifpdev, dirPath ) == IFP_DIR )        {            m_tmpParent = parent;            parent = findChildItem( *it, parent );            if( !parent )            {                addTrackToList( IFP_DIR, *it );                parent = m_last;            }        }        else        {            parent = newDirectory( *it, parent );            if( !parent ) //failed                return 0;        }        progress += "\\";    }    return parent;}MediaItem *IfpMediaDevice::findChildItem( const QString &name, MediaItem *parent ){    QListViewItem *child;    parent ?        child = parent->firstChild():        child = m_view->firstChild();    while( child )    {        if( child->text(0) == name )            return static_cast<MediaItem*>(child);        child = child->nextSibling();    }    return 0;}voidIfpMediaDevice::addToDirectory( MediaItem *directory, QPtrList<MediaItem> items ){    if( !directory || items.isEmpty() ) return;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?