File indexing completed on 2024-05-19 04:49:12

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Alejandro Wainzinger <aikawarazuni@gmail.com>                     *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #ifndef MEDIADEVICECOLLECTION_H
0018 #define MEDIADEVICECOLLECTION_H
0019 
0020 #include "core/collections/Collection.h"
0021 #include "core-impl/collections/mediadevicecollection/MediaDeviceCollectionLocation.h"
0022 #include "core-impl/collections/mediadevicecollection/support/ConnectionAssistant.h"
0023 #include "core-impl/collections/mediadevicecollection/support/mediadevicecollection_export.h"
0024 #include "core-impl/collections/support/MemoryCollection.h"
0025 
0026 #include <QIcon>
0027 
0028 #include <QtGlobal>
0029 #include <QSharedPointer>
0030 
0031 namespace Collections {
0032 
0033 class MediaDeviceCollection;
0034 
0035 /**
0036  * HACK: Base and Factory are separate because Q_OBJECT does not work directly with templates.
0037  * Templates used to reduce duplicated code in subclasses.
0038  */
0039 class MEDIADEVICECOLLECTION_EXPORT MediaDeviceCollectionFactoryBase : public Collections::CollectionFactory
0040 {
0041     Q_OBJECT
0042 
0043     public:
0044         ~MediaDeviceCollectionFactoryBase() override;
0045         void init() override;
0046 
0047     protected:
0048         MediaDeviceCollectionFactoryBase( ConnectionAssistant* assistant );
0049 
0050     protected Q_SLOTS:
0051         virtual void slotDeviceDetected( MediaDeviceInfo* info ); // detected type of device, connect it
0052 
0053     private Q_SLOTS:
0054         void slotDeviceDisconnected( const QString &udi );
0055 
0056     private:
0057         virtual MediaDeviceCollection* createCollection( MediaDeviceInfo* info ) = 0;
0058 
0059         ConnectionAssistant* m_assistant;
0060         QMap<QString, MediaDeviceCollection*> m_collectionMap;
0061 };
0062 
0063 template <class CollType>
0064 class MediaDeviceCollectionFactory : public MediaDeviceCollectionFactoryBase
0065 {
0066     protected:
0067         MediaDeviceCollectionFactory( ConnectionAssistant *assistant )
0068             : MediaDeviceCollectionFactoryBase( assistant )
0069         {}
0070 
0071         ~MediaDeviceCollectionFactory() override {}
0072 
0073     private:
0074         MediaDeviceCollection* createCollection( MediaDeviceInfo* info ) override
0075         {
0076             return new CollType( info );
0077         }
0078 };
0079 
0080 
0081 class MEDIADEVICECOLLECTION_EXPORT MediaDeviceCollection : public Collections::Collection
0082 {
0083     Q_OBJECT
0084 
0085     public:
0086         /** Collection-related methods */
0087 
0088         ~MediaDeviceCollection() override;
0089 
0090         /**
0091          * url-based methods can be abstracted via use of Amarok URLs
0092          * subclasses simply define a protocol prefix, e.g. ipod
0093          */
0094         bool possiblyContainsTrack( const QUrl &url ) const override { Q_UNUSED(url); return false;} // TODO: NYI
0095         Meta::TrackPtr trackForUrl( const QUrl &url ) override { Q_UNUSED(url); return Meta::TrackPtr();  } // TODO: NYI
0096 
0097         QueryMaker* queryMaker() override;
0098         virtual void startFullScanDevice();
0099 
0100         // NOTE: incrementalscan and stopscan not implemented, might be used by UMS later though
0101 
0102         /**
0103             The protocol of uids coming from this collection.
0104             @return A string of the protocol, without the ://
0105 
0106             This has to be overridden for every device type, e.g. ipod://
0107         */
0108         QString uidUrlProtocol() const override { return QString(); } // TODO: NYI
0109         QString collectionId() const override; // uses udi
0110 
0111         QString prettyName() const override = 0; // NOTE: must be overridden based on device type
0112         QIcon icon() const override = 0; // NOTE: must be overridden based on device type
0113 
0114         bool hasCapacity() const override;
0115         float usedCapacity() const override;
0116         float totalCapacity() const override;
0117 
0118         // NOTE: location will have same method calls always, no need to redo each time
0119         CollectionLocation* location() override { return new MediaDeviceCollectionLocation( this ); }
0120 
0121         /** Capability-related methods */
0122         bool hasCapabilityInterface( Capabilities::Capability::Type type ) const override;
0123         Capabilities::Capability* createCapabilityInterface( Capabilities::Capability::Type type ) override;
0124 
0125         /** MediaDeviceCollection methods */
0126         QString udi() const { return m_udi; }
0127 
0128         /** MediaDeviceCollection-specific */
0129         Meta::MediaDeviceHandler* handler();
0130         void init() { m_handler->init(); } // tell handler to start connection
0131         void emitCollectionReady();
0132 
0133         virtual QAction *ejectAction() const;
0134 
0135         QSharedPointer<MemoryCollection> memoryCollection() const { return m_mc; }
0136         void collectionUpdated() { Q_EMIT updated(); }
0137 
0138     Q_SIGNALS:
0139         void collectionReady( Collections::Collection* );
0140         /** collectionDisconnected is called when ConnectionAssistant
0141           is told it is to be disconnected.  This could be
0142           because another part of Amarok (e.g. applet) told it to
0143           or because the MediaDeviceMonitor noticed it disconnect
0144         */
0145         void collectionDisconnected( const QString &udi );
0146         void deletingCollection();
0147 
0148         void attemptConnectionDone( bool success );
0149 
0150         void copyTracksCompleted( bool success );
0151 
0152     public Q_SLOTS:
0153         void slotAttemptConnectionDone( bool success );
0154 
0155         virtual void eject();
0156         void deleteCollection();
0157 
0158     protected:
0159         MediaDeviceCollection();
0160 
0161         QString m_udi;
0162         Meta::MediaDeviceHandler *m_handler;
0163 
0164         mutable QAction *m_ejectAction;
0165 
0166         QSharedPointer<MemoryCollection> m_mc;
0167 
0168 };
0169 
0170 } //namespace Collections
0171 
0172 #endif