~ubuntu-branches/ubuntu/lucid/lastfm/lucid

« back to all changes in this revision

Viewing changes to src/Twiddly/common/ITunesComWrapper.h

  • Committer: Bazaar Package Importer
  • Author(s): Devid Filoni
  • Date: 2008-07-14 16:46:20 UTC
  • mfrom: (1.1.7 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080714164620-67hoz9fs177wpgmr
Tags: 1:1.5.1.31879.dfsg-1ubuntu1
* Merge from Debian unstable (LP: #248100), remaining changes:
  - debian/rules: add dh_icons call
  + debian/control:
    - switch iceweasel to firefox in Recommends field
    - modify debhelper version to >= 5.0.51~
    - modify Maintainer to Ubuntu MOTU Developers

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008 by                                                 *
 
3
 *      Last.fm Ltd. <client@last.fm>                                      *
 
4
 *                                                                         *
 
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.                                   *
 
9
 *                                                                         *
 
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.                          *
 
14
 *                                                                         *
 
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
 ***************************************************************************/
 
20
 
 
21
#ifndef ITUNESCOMWRAPPER_H
 
22
#define ITUNESCOMWRAPPER_H
 
23
 
 
24
#ifdef WIN32
 
25
 
 
26
#include "ITunesTrack.h"
 
27
#include "ITunesEventInterface.h"
 
28
 
 
29
// Needed for CoInitializeEx
 
30
#ifndef _WIN32_DCOM 
 
31
    #define _WIN32_DCOM 
 
32
#endif    
 
33
 
 
34
#include <windows.h>
 
35
 
 
36
// Must include objbase before iTunes interface or it won't compile
 
37
#include <objbase.h>
 
38
#include <atlbase.h>
 
39
#include <atlsafe.h>
 
40
#include <atlcom.h>
 
41
#include "../iTunesCOMAPI/iTunesCOMInterface.h"
 
42
 
 
43
#include <string>
 
44
#include <vector>
 
45
 
 
46
class ITunesComEventSink;
 
47
 
 
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.
 
52
  */
 
53
class ITunesComWrapper
 
54
{
 
55
public:
 
56
 
 
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 );
 
60
    ~ITunesComWrapper();
 
61
 
 
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; }
 
65
 
 
66
    /// Call libraryTrackCount first...
 
67
    long libraryTrackCount();
 
68
 
 
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();
 
74
 
 
75
    /// Then pass an index in the range 0..libraryTrackCount to this function
 
76
    ITunesTrack track( long idx );
 
77
 
 
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 );
 
82
 
 
83
    /// Get currently playing track in iTunes
 
84
    ITunesTrack currentTrack();
 
85
 
 
86
    /// Get state of player (stopped/running etc)
 
87
    ITPlayerState playerState();
 
88
    
 
89
    /// Get player position in seconds
 
90
    long playerPosition();
 
91
 
 
92
    /// Search library. ITPlaylistSearchField is an iTunes type.
 
93
    std::vector<ITunesTrack>
 
94
    searchForTrack( ITPlaylistSearchField searchField, std::wstring searchTerm );
 
95
 
 
96
private:
 
97
    void initialiseCom();
 
98
    void uninitialiseCom();
 
99
 
 
100
    /// Connects iTunes events to our event sink
 
101
    void connectSink( ITunesEventInterface* handler );
 
102
 
 
103
    /// Throws if no iPod found in iTunes
 
104
    IITPlaylist* iPodLibraryPlaylist();
 
105
 
 
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 );
 
109
 
 
110
    IiTunes* m_iTunesApp;
 
111
    IITTrackCollection* m_allTracks;
 
112
    long m_trackCount;
 
113
 
 
114
    CComObject<ITunesComEventSink>* m_sink;
 
115
    DWORD m_sinkId; // needed to close the connection
 
116
    IUnknown* m_sinkAsUnknown;
 
117
    
 
118
    friend ITunesTrack;
 
119
};
 
120
 
 
121
 
 
122
/** @author Erik Jalevik <erik@last.fm>
 
123
  * @brief Internal handler for COM events fired by iTunes.
 
124
  */
 
125
class ATL_NO_VTABLE ITunesComEventSink : 
 
126
    public CComObjectRootEx<CComObjectThreadModel>,
 
127
    public IDispatch
 
128
{
 
129
public:
 
130
 
 
131
    BEGIN_COM_MAP(ITunesComEventSink)
 
132
        COM_INTERFACE_ENTRY( IDispatch )
 
133
        COM_INTERFACE_ENTRY_IID( DIID__IiTunesEvents, IDispatch )
 
134
    END_COM_MAP()
 
135
 
 
136
    ITunesComEventSink() : m_handler( 0 ) { }
 
137
 
 
138
    /// You must call this function with a valid handler after initialising this class.
 
139
    void setHandler( ITunesEventInterface* handler ) { m_handler = handler; }
 
140
 
 
141
    // IDispatch stuff
 
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; }
 
147
 
 
148
private:
 
149
 
 
150
    ITunesEventInterface* m_handler;
 
151
 
 
152
    void onDatabaseChangedEvent( VARIANT deletedObjectIDs, VARIANT changedObjectIDs );
 
153
 
 
154
    ITunesEventInterface::ITunesIdSet
 
155
    getIdsFromSafeArray( long index, CComSafeArray<VARIANT>& array );
 
156
 
 
157
};
 
158
 
 
159
 
 
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> { };
 
164
 
 
165
 
 
166
#endif // WIN32
 
167
 
 
168
#endif // ITUNESCOMTHREAD_H
 
 
b'\\ No newline at end of file'