1
/***************************************************************************
2
* Copyright (C) 2007 by *
3
* Philipp Maihart, Last.fm Ltd <phil@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 02111-1307, USA. *
19
***************************************************************************/
21
#import <Cocoa/Cocoa.h>
22
#import "CocoaWatcher.h"
26
void setiPodMountState( CocoaWatcher* cocoa, bool mounted, QString path )
28
if ( mounted == true )
29
emit cocoa->iPodMounted( path );
31
emit cocoa->iPodUnMounted();
33
cocoa->m_iPodMountState = mounted;
34
cocoa->m_iPodPath = path;
36
qDebug() << "iPod state changed: Mounted =" << mounted << "Path =" << path;
37
emit cocoa->iPodMountStateChanged( mounted, path );
40
void emitiTunesLaunched( CocoaWatcher* cocoa )
42
qDebug() << "iTunes launched";
43
emit cocoa->iTunesLaunched();
47
// Objective-C Interfaces
48
@interface IPodWatcher : NSObject
50
NSString *iPodMountPath;
54
- (void)connect:(CocoaWatcher*)parent;
55
- (void)volumeDidMount:(NSNotification*)notification;
56
- (void)volumeDidUnmount:(NSNotification*)notification;
59
@interface ITunesWatcher : NSObject
64
- (void)connect:(CocoaWatcher*)parent;
65
- (void)appLaunched:(NSNotification*)notification;
70
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
71
IPodWatcher *iPodWatcher;
72
ITunesWatcher *iTunesWatcher;
75
// Constructor/Destructor
76
CocoaWatcher::CocoaWatcher() :
77
m_messagePumpRunning( false )
80
CocoaWatcher::~CocoaWatcher()
87
bool CocoaWatcher::instanceFlag = false;
88
CocoaWatcher* CocoaWatcher::single = NULL;
90
CocoaWatcher* CocoaWatcher::getInstance()
94
single = new CocoaWatcher();
104
// Some Notifications need a running Cocoa Message Pump - be careful!
105
// Only call if you need that for your notification...
107
CocoaWatcher::startCocoaMessagePump()
109
if ( m_messagePumpRunning == true ) return;
111
m_messagePumpRunning = true;
117
CocoaWatcher::stopCocoaMessagePump()
119
if ( m_messagePumpRunning == false ) return;
123
m_messagePumpRunning = false;
129
CocoaWatcher::watchOutForiPod()
131
iPodWatcher = [[IPodWatcher alloc] init];
132
CocoaWatcher* me = this; // C++
133
[iPodWatcher connect:me];
138
CocoaWatcher::watchOutForiTunes()
140
iTunesWatcher = [[ITunesWatcher alloc] init];
141
CocoaWatcher* me = this; // C++
142
[iTunesWatcher connect:me];
146
// Stops all observing watchers
148
CocoaWatcher::stopWatchers()
150
[iPodWatcher release];
151
[iTunesWatcher release];
152
stopCocoaMessagePump();
156
// Implementations for Obj-C classes
158
// Emits a signals when mounting/unmounting an iPod
159
@implementation IPodWatcher
162
// Register for mounts and unmounts (iPod support)
163
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
164
selector:@selector(volumeDidMount:) name:NSWorkspaceDidMountNotification object:nil];
165
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
166
selector:@selector(volumeDidUnmount:) name:NSWorkspaceDidUnmountNotification object:nil];
171
- (void)connect:(CocoaWatcher*)parent
173
cUtils = parent; // C++
175
if (!iPodMountPath) {
176
// Simulate mount events for current mounts so that any mounted iPod is found
177
NSEnumerator *en = [[[NSWorkspace sharedWorkspace] mountedLocalVolumePaths] objectEnumerator];
179
while ((path = [en nextObject])) {
180
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:path, @"NSDevicePath", nil];
181
NSNotification *note = [NSNotification notificationWithName:NSWorkspaceDidMountNotification
182
object:[NSWorkspace sharedWorkspace] userInfo:dict];
183
[self volumeDidMount:note];
188
- (void)volumeDidMount:(NSNotification*)notification
190
NSDictionary *info = [notification userInfo];
191
NSString *mountPath = [info objectForKey:@"NSDevicePath"];
192
NSString *iPodControlPath = [mountPath stringByAppendingPathComponent:@"iPod_Control"];
195
if ([[NSFileManager defaultManager] fileExistsAtPath:iPodControlPath isDirectory:&isDir] && isDir) {
196
[self setValue:mountPath forKey:@"iPodMountPath"];
198
setiPodMountState( cUtils, true, QString::fromStdString( std::string( [mountPath cString] ) ) ); // C++
202
- (void)volumeDidUnmount:(NSNotification*)notification
204
if ( cUtils == NULL ) return;
206
NSDictionary *info = [notification userInfo];
207
NSString *mountPath = [info objectForKey:@"NSDevicePath"];
209
if ([iPodMountPath isEqualToString:mountPath]) {
210
setiPodMountState( cUtils, false, "" ); // C++
216
// Emits a signal when launching iTunes
217
@implementation ITunesWatcher
220
// Register for application launches
221
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
222
selector:@selector(appLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
227
- (void)connect:(CocoaWatcher*)parent
229
cUtils = parent; // C++
230
cUtils->startCocoaMessagePump(); // Need message pump for this notification
233
- (void)appLaunched:(NSNotification*)notification
235
if ( cUtils == NULL ) return;
237
NSDictionary *info = [notification userInfo];
239
if ( [[info objectForKey:@"NSApplicationName"] isEqual:@"iTunes"] )
241
emitiTunesLaunched( cUtils ); // C++