njbmediadevice.cpp

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

CPP
913
字号
/***************************************************************************    copyright            : (C) 2006 by Andres Oton    email                : andres.oton@gmail.com    copyright            : (C) 2006 by T.R.Shashwath    email                : trshash84@gmail.com ***************************************************************************//*************************************************************************** *   This library is free software; you can redistribute it and/or modify  * *   it  under the terms of the GNU General Public License version 2 as    * *   published by the Free Software Foundation.                            * *                                                                         * *   This library is distributed in the hope that it will be useful, but   * *   WITHOUT ANY WARRANTY; without even the implied warranty of            * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     * *   Lesser General Public License for more details.                       * *                                                                         * *   You should have received a copy of the GNU General Public License     * *   along with this library; if not, write to the Free Software           * *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,            * *   MA  02110-1301  USA                                                   * ***************************************************************************/#include "njbmediadevice.h"AMAROK_EXPORT_PLUGIN( NjbMediaDevice )// Amarok#include <collectiondb.h>#include <collectionbrowser.h>#include <debug.h>#include <metabundle.h>#include <statusbar/statusbar.h>#include <statusbar/popupMessage.h>// KDE#include <kapplication.h>#include <kdebug.h>#include <kfiledialog.h>#include <kiconloader.h>       //smallIcon#include <kinstance.h>#include <klocale.h>#include <kmessagebox.h>#include <kpopupmenu.h>#include <ktempdir.h>#include <ktoolbarbutton.h>#include <kurl.h>#include <kurlrequester.h>     //downloadSelectedItems()#include <kurlrequesterdlg.h>  //downloadSelectedItems()// Qt#include <qdir.h>#include <qlistview.h>#include <qregexp.h>#include <qtooltip.h>#include <quuid.h>// posix#include <stdint.h>#include <sys/stat.h>#include <time.h>#include <unistd.h>namespace Amarok { extern KConfig *config( const QString& ); }njb_t *NjbMediaDevice::m_njb = 0;// This function has NOT handled the request, so other functions may be called// upon to do soconst int NJB_NOTHANDLED = 0;// This function has handled the request, so no further processing is needed.const int NJB_HANDLED = -1;NjbMediaDevice::NjbMediaDevice(): MediaDevice(){    //	listAmarokPlayLists = new QListView();    m_name = i18n("NJB Media device");    m_njb = 0;    m_connected = false;    m_libcount = 0;    m_connected = false;    m_customButton = true;    m_td = 0;    NJB_Set_Debug(0); // or try DD_SUBTRACE    KToolBarButton* customButton = MediaBrowser::instance()->getToolBar()->getButton( MediaBrowser::CUSTOM );    customButton->setText( i18n("Special device functions") );    QToolTip::remove( customButton );    QToolTip::add( customButton, i18n( "Special functions of your jukebox" ) );}NjbMediaDevice::~NjbMediaDevice(){}boolNjbMediaDevice::closeDevice(){    DEBUG_BLOCK    if(m_connected)    {        NJB_Release( m_njb);        m_connected = false;    }    m_connected = false;    if( m_njb )    {        NJB_Close( m_njb);        m_njb = 0;    }    debug()<< "Disconnected NJB device" << endl;    clearItems();    m_name = i18n("NJB Media device");    debug() << "Done" << endl;    return true;}voidNjbMediaDevice::unlockDevice(){}boolNjbMediaDevice::getCapacity(KIO::filesize_t* total, KIO::filesize_t* available){    if(!m_connected)	return false;    u_int64_t itotal;    u_int64_t ifree;    if(NJB_Get_Disk_Usage(m_njb, &itotal, &ifree) == -1)	return false;    *total = itotal;    *available = ifree;    return true;}boolNjbMediaDevice::isConnected(){    return m_connected;}boolNjbMediaDevice::isPlayable(const MetaBundle& bundle){    DEBUG_BLOCK            ;    if(bundle.fileType() == MetaBundle::mp3       || bundle.fileType() == MetaBundle::wma)        return true;    return false;}boolNjbMediaDevice::isPreferredFormat(const MetaBundle& bundle){    DEBUG_BLOCK    if(bundle.fileType() == MetaBundle::mp3)        return true;    else        return false;}boolNjbMediaDevice::lockDevice(bool tryOnly){    // The device is "locked" upon connection - there's very little else we can do here.    Q_UNUSED(tryOnly);    return true;}boolNjbMediaDevice::openDevice(bool){    DEBUG_BLOCK    if( m_njb )        return true;    QString genericError = i18n( "Could not connect to Nomad device" );    NJB_Set_Unicode( NJB_UC_UTF8 ); // I assume that UTF-8 is fine with everyone...    int n;    if( NJB_Discover( njbs, 0, &n) == -1 || n == 0 )    {        Amarok::StatusBar::instance()->shortLongMessage( genericError, i18n("A suitable Nomad device could not be found"), KDE::StatusBar::Error );        debug() << ": no NJBs found\n";        return false;    }    m_njb = &njbs[0];    if( NJB_Open( m_njb ) == -1 )    {        Amarok::StatusBar::instance()->shortLongMessage( genericError, i18n("Nomad device could not be opened"), KDE::StatusBar::Error );        return false;    }    QString deviceName = NJB_Get_Device_Name( m_njb, 1 );    QString owner = NJB_Get_Owner_String( m_njb );    m_name = deviceName + " (Owned by " + owner + ')';    if( NJB_Capture(m_njb) == -1)    {        debug() << ": couldn't capture\n";        m_connected = false;    }    else    {        m_connected = true;        readJukeboxMusic();    }    return true;}intNjbMediaDevice::deleteFromDevice(unsigned id){    int status = NJB_Delete_Track( m_njb, id );    if( status != NJB_SUCCESS)    {        debug() << ": NJB_Delete_Track failed" << endl;        return -1;    }    // remove from the cache    trackList.remove(trackList.findTrackById( id ) );    return 1;}intNjbMediaDevice::deleteItemFromDevice(MediaItem* item, int flags ){    DEBUG_BLOCK    int result = 0;    if ( isCanceled() || !item )    {        return -1;    }    MediaItem *next = 0;    switch( item->type() )    {        case MediaItem::TRACK:            if( isCanceled() )                break;            if( item )            {                deleteTrack( dynamic_cast<NjbMediaItem *> (item) );                result++;            }            break;        case MediaItem::ALBUM:        case MediaItem::ARTIST:            // Recurse through the lists, slashing and burning.            if( isCanceled() )                break;            expandItem( dynamic_cast<QListViewItem *>(item) );            for( MediaItem *it = dynamic_cast<MediaItem *>( item->firstChild() ); it ; it = next )            {                next = dynamic_cast<MediaItem *>(it->nextSibling());                int res = deleteItemFromDevice( it, flags );                if( res >= 0 && result >= 0 )                    result += res;                else                    result = -1;            }            if( item )                delete dynamic_cast<MediaItem *>( item );            break;        default:            result = 0;    }    return result;}intNjbMediaDevice::deleteTrack(NjbMediaItem *trackItem){    int status = NJB_Delete_Track( m_njb, trackItem->track()->id() );    if( status != NJB_SUCCESS)    {        debug() << ": NJB_Delete_Track failed" << endl;        Amarok::StatusBar::instance()->shortLongMessage( i18n( "Deleting failed" ), i18n( "Deleting track(s) failed." ), KDE::StatusBar::Error );        return -1;    }    debug() << ": NJB_Delete_Track track deleted" << endl;    // remove from the cache    trackList.remove(trackList.findTrackById( trackItem->track()->id() ) );    delete trackItem;    return 1;}intNjbMediaDevice::downloadSelectedItems(){    /* Copied from ifpmediadevice */    QString save = QString::null;    KURLRequesterDlg dialog( save, 0, 0 );    dialog.setCaption( kapp->makeStdCaption( i18n( "Choose a Download Directory" ) ) );    dialog.urlRequester()->setMode( KFile::Directory | KFile::ExistingOnly );    dialog.exec();    KURL destDir = dialog.selectedURL();    if( destDir.isEmpty() )	    return -1;    destDir.adjustPath( 1 ); //add trailing slash    QDir dir;    QString path;    QPtrList<MediaItem> items;    m_view->getSelectedLeaves( 0, &items );    int result = 0;    for( MediaItem *it = items.first(); it && !(m_canceled); it = items.next() )    {        path = destDir.path();        if( it->type() == MediaItem::TRACK )        {            dynamic_cast<MediaBrowser *>( parent() )->queue()->addURL(path, dynamic_cast<MediaItem *>(it) );        }    }    return result;}/** * Download the selected items and put them into the collection DB. * @return The number of files downloaded.*/intNjbMediaDevice::downloadToCollection(){    // We will first download all files into a temp dir, and then call move to collection.    QPtrList<MediaItem> items;    m_view->getSelectedLeaves( 0, &items );    KTempDir tempdir( QString::null ); // Default prefix is fine with us    tempdir.setAutoDelete( true ); // We don't need it once the work is done.    QString path = tempdir.name(), filepath;    KURL::List urls;    for( MediaItem *it = items.first(); it && !(m_canceled); it = items.next() )    {        if( (it->type() == MediaItem::TRACK) )        {            NjbMediaItem* auxItem = dynamic_cast<NjbMediaItem *>( (it) );	    if (!auxItem) {		debug() << "Dynamic cast to NJB media item failed. " << endl;		return -1;	    }            QString track_id;            track_id.setNum( auxItem->track()->id() );            filepath = path + auxItem->bundle()->url().path();            if( NJB_Get_Track( m_njb, auxItem->track()->id(), auxItem->bundle()->filesize(), filepath.utf8(), progressCallback, this)                != NJB_SUCCESS )            {                debug() << "Get Track failed. " << endl;                if( NJB_Error_Pending(m_njb) )                {                    const char *njbError;                    while( (njbError = NJB_Error_Geterror(m_njb) ) )                        error() << njbError << endl;                }                else                    debug() << "No reason to report for failure" << endl;            }            urls << filepath;        }    }    // Now, call the collection organizer.    CollectionView::instance()->organizeFiles( urls, i18n( "Move Files To Collection" ), false );    return 0;}MediaItem*NjbMediaDevice::copyTrackToDevice(const MetaBundle& bundle){    DEBUG_BLOCK    if( m_canceled )            return 0;    trackValueList::const_iterator it_track = trackList.findTrackByName( bundle.filename() );    if( it_track != trackList.end() )    {        deleteFromDevice( (*it_track)->id() );    }    // read the mp3 header    int duration = bundle.length();    if( !duration )    {        m_errMsg = i18n( "Not a valid mp3 file");        return 0;    }    MetaBundle temp( bundle );    NjbTrack *taggedTrack = new NjbTrack();    taggedTrack->setBundle( temp );    u_int32_t id;    m_progressStart = time( 0);    m_progressMessage = i18n("Copying / Sent %1%...");    njb_songid_t* songid = NJB_Songid_New();    taggedTrack->writeToSongid( songid );    m_busy = true;    kapp->processEvents( 100 );    if(NJB_Send_Track (m_njb, bundle.url().path().utf8(), songid, progressCallback, this, &id) != NJB_SUCCESS)    {

⌨️ 快捷键说明

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