1
/***************************************************************************
2
* Copyright (C) 2008 by *
3
* Last.fm Ltd. <client@last.fm> *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. *
19
***************************************************************************/
21
#ifndef ITUNESCOMWRAPPER_H
22
#define ITUNESCOMWRAPPER_H
26
#include "ITunesTrack.h"
27
#include "ITunesEventInterface.h"
29
// Needed for CoInitializeEx
36
// Must include objbase before iTunes interface or it won't compile
41
#include "../iTunesCOMAPI/iTunesCOMInterface.h"
46
class ITunesComEventSink;
48
/** @author Erik Jalevik <erik@last.fm>
49
* @brief Uses COM to query iTunes for track information.
50
* All functions that access COM directly will throw an ITunesException
51
* if something went wrong.
53
class ITunesComWrapper
57
/// If you want events, pass in an implementation of the event handler interface,
58
/// leave it as 0 and the event sink won't get initialised.
59
ITunesComWrapper( ITunesEventInterface* handler = 0 );
62
/// If this returns false, we didn't manage to initialise COM and all the
63
/// other malarkey required for this to work.
64
bool isValid() { return m_iTunesApp != 0 && m_trackCount != -1 && m_allTracks != 0; }
66
/// Call libraryTrackCount first...
67
long libraryTrackCount();
69
/// if you call this instead of the libraryTrackCount() equivalent
70
/// all other functions that involve m_allTracks will reference the iPod
71
/// instead FIXME sucks!
72
/// NOTE as above, call this before calling track()
73
long iPodLibraryTrackCount();
75
/// Then pass an index in the range 0..libraryTrackCount to this function
76
ITunesTrack track( long idx );
78
/// Or an ITunesIdSet to this. An ITunesIdSet does not necessarily represent
79
/// a track, it could be any iTunes object. If the set of IDs passed in does
80
/// not represent a track, an ITunesException will be thrown.
81
ITunesTrack track( const ITunesEventInterface::ITunesIdSet& ids );
83
/// Get currently playing track in iTunes
84
ITunesTrack currentTrack();
86
/// Get state of player (stopped/running etc)
87
ITPlayerState playerState();
89
/// Get player position in seconds
90
long playerPosition();
92
/// Search library. ITPlaylistSearchField is an iTunes type.
93
std::vector<ITunesTrack>
94
searchForTrack( ITPlaylistSearchField searchField, std::wstring searchTerm );
98
void uninitialiseCom();
100
/// Connects iTunes events to our event sink
101
void connectSink( ITunesEventInterface* handler );
103
/// Throws if no iPod found in iTunes
104
IITPlaylist* iPodLibraryPlaylist();
106
static std::wstring bstrToWString( BSTR bstr );
107
static bool handleComResult( HRESULT res, std::wstring err = L"" );
108
static void logComError( HRESULT res, std::wstring err );
110
IiTunes* m_iTunesApp;
111
IITTrackCollection* m_allTracks;
114
CComObject<ITunesComEventSink>* m_sink;
115
DWORD m_sinkId; // needed to close the connection
116
IUnknown* m_sinkAsUnknown;
122
/** @author Erik Jalevik <erik@last.fm>
123
* @brief Internal handler for COM events fired by iTunes.
125
class ATL_NO_VTABLE ITunesComEventSink :
126
public CComObjectRootEx<CComObjectThreadModel>,
131
BEGIN_COM_MAP(ITunesComEventSink)
132
COM_INTERFACE_ENTRY( IDispatch )
133
COM_INTERFACE_ENTRY_IID( DIID__IiTunesEvents, IDispatch )
136
ITunesComEventSink() : m_handler( 0 ) { }
138
/// You must call this function with a valid handler after initialising this class.
139
void setHandler( ITunesEventInterface* handler ) { m_handler = handler; }
142
HRESULT __stdcall Invoke( DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags,
143
DISPPARAMS* pdispparams, VARIANT* pvarResult, EXCEPINFO* pexcepinfo, UINT* puArgErr );
144
HRESULT __stdcall GetTypeInfoCount( UINT* ) { return E_NOTIMPL; }
145
HRESULT __stdcall GetTypeInfo( UINT, LCID, ITypeInfo** ) { return E_NOTIMPL; }
146
HRESULT __stdcall GetIDsOfNames( REFIID, LPOLESTR*, UINT, LCID, DISPID* ) { return E_NOTIMPL; }
150
ITunesEventInterface* m_handler;
152
void onDatabaseChangedEvent( VARIANT deletedObjectIDs, VARIANT changedObjectIDs );
154
ITunesEventInterface::ITunesIdSet
155
getIdsFromSafeArray( long index, CComSafeArray<VARIANT>& array );
160
// We need one of these things hanging around for the ATL calls to work.
161
// It's initialised in connectSink.
162
class TheAtlModule : public CAtlDllModuleT<TheAtlModule> { };
163
//class TheAtlModule : public CAtlExeModuleT<TheAtlModule> { };
168
#endif // ITUNESCOMTHREAD_H
b'\\ No newline at end of file'