ipodmediadevice.cpp

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

CPP
2,141
字号
/***************************************************************************    copyright            : (C) 2005, 2006 by Martin Aumueller    email                : aumuell@reserv.at    copyright            : (C) 2004 by Christian Muehlhaeuser    email                : chris@chris.de ***************************************************************************//*************************************************************************** *   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                                                   * ***************************************************************************/#define DEBUG_PREFIX "IpodMediaDevice"#include <config.h>#include "ipodmediadevice.h"AMAROK_EXPORT_PLUGIN( IpodMediaDevice )#include <debug.h>#include <metabundle.h>#include <collectiondb.h>#include <statusbar/statusbar.h>#include <k3bexporter.h>#include <playlist.h>#include <collectionbrowser.h>#include <playlistbrowser.h>#include <tagdialog.h>#include <threadmanager.h>#include <metadata/tplugins.h>#include <hintlineedit.h>#include <kactionclasses.h>#include <kapplication.h>#include <kmountpoint.h>#include <kpushbutton.h>#include <kprogress.h>#include <kmessagebox.h>#include <kiconloader.h>#include <kpopupmenu.h>#include <qcheckbox.h>#include <qdir.h>#include <qfileinfo.h>#include <qlabel.h>#include <qlineedit.h>#include <qregexp.h>#include <qtimer.h>#include <qtooltip.h>#ifdef HAVE_STATVFS#include <stdint.h>#include <sys/statvfs.h>#endif#include <cstdlib>#include <unistd.h>#ifndef HAVE_ITDB_MEDIATYPE#define mediatype unk208#endif#include "metadata/audible/taglib_audiblefile.h"struct PodcastInfo{    // per show    QString url;    QString description;    QDateTime date;    QString author;    bool listened;    // per channel    QString rss;    PodcastInfo() { listened = false; }};class TrackList : public QPtrList<Itdb_Track>{    int compareItems ( QPtrCollection::Item track1, QPtrCollection::Item track2 )    {        Itdb_Track *t1 = (Itdb_Track *)track1;        Itdb_Track *t2 = (Itdb_Track *)track2;        if(t1->track_nr != t2->track_nr)            return t1->track_nr - t2->track_nr;        return strcasecmp(t1->title, t2->title);    }};class IpodMediaItem : public MediaItem{    public:        IpodMediaItem( QListView *parent, MediaDevice *dev )            : MediaItem( parent ) { init( dev ); }        IpodMediaItem( QListViewItem *parent, MediaDevice *dev )            : MediaItem( parent ) { init( dev ); }        IpodMediaItem( QListView *parent, QListViewItem *after, MediaDevice *dev )            : MediaItem( parent, after ) { init( dev ); }        IpodMediaItem( QListViewItem *parent, QListViewItem *after, MediaDevice *dev )            : MediaItem( parent, after ) { init( dev ); }        virtual ~IpodMediaItem() { delete m_podcastInfo; }        void init( MediaDevice *dev )        {            m_track       = 0;            m_playlist    = 0;            m_device      = dev;            m_podcastInfo = 0;        }        void bundleFromTrack( Itdb_Track *track, const QString& path )        {            MetaBundle *bundle = new MetaBundle();            bundle->setArtist    ( QString::fromUtf8( track->artist ) );            bundle->setComposer  ( QString::fromUtf8( track->composer ) );            bundle->setAlbum     ( QString::fromUtf8( track->album ) );            bundle->setTitle     ( QString::fromUtf8( track->title ) );            bundle->setComment   ( QString::fromUtf8( track->comment ) );            bundle->setGenre     ( QString::fromUtf8( track->genre ) );            bundle->setYear      ( track->year );            bundle->setTrack     ( track->track_nr );            bundle->setDiscNumber( track->cd_nr );            bundle->setBpm       ( track->BPM );            bundle->setLength    ( track->tracklen/1000 );            bundle->setBitrate   ( track->bitrate );            bundle->setSampleRate( track->samplerate );            bundle->setPath      ( path );            bundle->setFilesize  ( track->size );            QString rss( track->podcastrss );            QString url( track->podcasturl );            QString desc( track->description );            QString subtitle( track->subtitle );            QDateTime date;            date.setTime_t( itdb_time_mac_to_host( track->time_released) );            if( !rss.isEmpty() || !url.isEmpty() )            {                PodcastEpisodeBundle peb( KURL::fromPathOrURL(url), KURL::fromPathOrURL(rss),                        track->title, track->artist, desc, date.toString(Qt::ISODate), QString::null /*type*/,                        bundle->length(), QString::null /*guid*/, track->playcount<=0 );                bundle->setPodcastBundle( peb );            }            setBundle( bundle );        }        Itdb_Track      *m_track;        Itdb_Playlist   *m_playlist;        PodcastInfo     *m_podcastInfo;        int played()         const { return m_track ? m_track->playcount        : 0; }        int recentlyPlayed() const { return m_track ? m_track->recent_playcount : 0; }        int rating()         const { return m_track ? m_track->rating           : 0; }        void setRating( int rating )        {            if( m_track ) m_track->rating = m_track->app_rating = rating;            if( dynamic_cast<IpodMediaDevice *>(device()) )                static_cast<IpodMediaDevice *>(device())->m_dbChanged = true;        }        void setPlayCount( int playcount )        {            if ( m_track )                m_track->playcount = playcount;            if( dynamic_cast<IpodMediaDevice *>(device()) )                static_cast<IpodMediaDevice *>(device())->m_dbChanged = true;        }        void setLastPlayed( uint lastplay )        {            if ( m_track )                m_track->time_played = itdb_time_host_to_mac( lastplay );            if( dynamic_cast<IpodMediaDevice *>(device()) )                static_cast<IpodMediaDevice *>(device())->m_dbChanged = true;        }        bool ratingChanged() const { return m_track ? m_track->rating != m_track->app_rating : false; }        void setListened( bool l )        {            MediaItem::setListened( l );            if( type() == PODCASTITEM )            {                if( m_podcastInfo )                    m_podcastInfo->listened = listened();                if( m_track )                    m_track->mark_unplayed = listened() ? 0x01 : 0x02;            }        }        QDateTime playTime() const        {            QDateTime t;            if( m_track )                t.setTime_t( itdb_time_mac_to_host( m_track->time_played ) );            return t;        }        IpodMediaItem *findTrack( Itdb_Track *track )        {            if( m_track == track )                return this;            for( IpodMediaItem *it = dynamic_cast<IpodMediaItem *>( firstChild() );                    it;                    it = dynamic_cast<IpodMediaItem *>( it->nextSibling()) )            {                IpodMediaItem *found = it->findTrack(track);                if( found )                    return found;            }            return 0;        }};IpodMediaDevice::IpodMediaDevice()    : MediaDevice()    , m_masterPlaylist( 0 )    , m_podcastPlaylist( 0 )    , m_lockFile( 0 )    , m_customAction( 0 ){    registerTaglibPlugins();    m_podcastItem = 0;    m_staleItem = 0;    m_orphanedItem = 0;    m_invisibleItem = 0;    m_playlistItem = 0;    m_dbChanged = false;    m_itdb = 0;    m_podcastItem = 0;    m_staleItem = 0;    m_orphanedItem = 0;    m_invisibleItem = 0;    m_playlistItem = 0;    m_supportsArtwork = true;    m_supportsVideo = false;    m_rockboxFirmware = false;    m_isShuffle = false;    m_isMobile = false;    m_isIPhone = false;    m_needsFirewireGuid = false;    m_requireMount = true;    m_name = "iPod";    // config stuff    m_autoConnect = true;    m_syncStatsCheck = 0;    m_autoDeletePodcastsCheck = 0;    KActionCollection *ac = new KActionCollection( this );    KActionMenu *am = new KActionMenu( i18n( "iPod" ), Amarok::icon( "device" ), ac );    m_customAction = am;    m_customAction->setEnabled( false );    am->setDelayed( false );    KPopupMenu *menu = am->popupMenu();    connect( menu, SIGNAL(activated(int)), SLOT(slotIpodAction(int)) );    menu->insertItem( i18n( "Stale and Orphaned" ), CHECK_INTEGRITY );    menu->insertItem( i18n( "Update Artwork" ), UPDATE_ARTWORK );    KPopupMenu *ipodGen = new KPopupMenu( menu );    menu->insertItem( i18n( "Set iPod Model" ), ipodGen );    const Itdb_IpodInfo *table = itdb_info_get_ipod_info_table();    if( !table )        return;    bool infoFound = false;    int generation = ITDB_IPOD_GENERATION_FIRST;    do    {        const Itdb_IpodInfo *info = table;        infoFound = false;        KPopupMenu *gen = 0;        int index = SET_IPOD_MODEL;        while( info->model_number )        {            if( info->ipod_generation == generation )            {                if (!infoFound)                {                    infoFound = true;                    gen = new KPopupMenu( ipodGen );                    connect( gen, SIGNAL(activated(int)), SLOT(slotIpodAction(int)) );                    ipodGen->insertItem(                            itdb_info_get_ipod_generation_string( info->ipod_generation),                            gen );                }                if( info->capacity > 0.f )                    gen->insertItem( i18n( "%1 GB %2 (x%3)" )                            .arg( QString::number( info->capacity ),                                itdb_info_get_ipod_model_name_string( info->ipod_model ),                                info->model_number ),                            index );                else                    gen->insertItem( i18n( "%1 (x%2)" )                            .arg( itdb_info_get_ipod_model_name_string( info->ipod_model ),                                info->model_number ),                             index );            }            ++info;            ++index;        }        ++generation;    }    while( infoFound );}voidIpodMediaDevice::slotIpodAction( int id ){    switch( id )    {        case CHECK_INTEGRITY:            checkIntegrity();            break;        case UPDATE_ARTWORK:            updateArtwork();            break;        default:            if( const Itdb_IpodInfo *table = itdb_info_get_ipod_info_table() )            {                int index = id - SET_IPOD_MODEL;                if( m_itdb && m_itdb->device )                {                    gchar model[PATH_MAX];                    g_snprintf (model, PATH_MAX, "x%s", table[index].model_number);                    itdb_device_set_sysinfo( m_itdb->device, "ModelNumStr", model );                    detectModel();                    if( m_isIPhone )                    {                       m_autoConnect = false;                       setConfigBool( "AutoConnect", m_autoConnect );                    }                    // try to make sure that the Device directory exists                    QDir dir;                    QString realPath;                    if(!pathExists( itunesDir(), &realPath) )                    {                        dir.setPath(realPath);                        dir.mkdir(dir.absPath());                    }                    if(!pathExists( itunesDir( "Device" ), &realPath) )                    {                        dir.setPath(realPath);                        dir.mkdir(dir.absPath());                    }                    GError *err = 0;                     gboolean success = itdb_device_write_sysinfo(m_itdb->device, &err);                    debug() << "success writing sysinfo to ipod? (return value " << success << ")" << endl;                    if( !success && err )                    {                        g_error_free(err);                        //FIXME: update i18n files for next message                        Amarok::StatusBar::instance()->longMessage(                                 i18n( "Could not write SysInfo file to iPod (check the permissions of the file \"%1\" on your iPod)" ).arg( itunesDir( "Device:SysInfo" ) ) );                        //FIXME: update i18n files for next message                        Amarok::StatusBar::instance()->shortMessage(                                i18n( "Unable to set iPod model to %1 GB %2 (x%3)" )                                .arg( QString::number( table[index].capacity ),                                    itdb_info_get_ipod_model_name_string( table[index].ipod_model ),                                    table[index].model_number ) );                    }                     else                    {                        Amarok::StatusBar::instance()->shortMessage(                                i18n( "Setting iPod model to %1 GB %2 (x%3)" )                                .arg( QString::number( table[index].capacity ),                                    itdb_info_get_ipod_model_name_string( table[index].ipod_model ),                                    table[index].model_number ) );                    }                    MediaBrowser::instance()->updateDevices();                }            }            break;    }}voidIpodMediaDevice::init( MediaBrowser* parent ){    MediaDevice::init( parent );}IpodMediaDevice::~IpodMediaDevice(){    if( m_itdb )        itdb_free(m_itdb);    m_files.clear();}boolIpodMediaDevice::isConnected(){    return ( m_itdb != 0 );

⌨️ 快捷键说明

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