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

« back to all changes in this revision

Viewing changes to src/libUnicorn/CocoaWatcher.mm

  • Committer: Bazaar Package Importer
  • Author(s): Pedro Fragoso
  • Date: 2007-12-31 09:49:54 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071231094954-ix1amvcsj9pk61ya
Tags: 1:1.4.1.57486.dfsg-1ubuntu1
* Merge from Debian unstable (LP: #180254), remaining changes:
  - debian/rules;
    - Added dh_icons
  - Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2007 by                                                         *
 
3
 *      Philipp Maihart, Last.fm Ltd <phil@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  02111-1307, USA.          *
 
19
 ***************************************************************************/
 
20
 
 
21
#import <Cocoa/Cocoa.h>
 
22
#import "CocoaWatcher.h"
 
23
 
 
24
 
 
25
// Friend functions
 
26
void setiPodMountState( CocoaWatcher* cocoa, bool mounted, QString path )
 
27
{
 
28
        if ( mounted == true )
 
29
                emit cocoa->iPodMounted( path );        
 
30
        else
 
31
                emit cocoa->iPodUnMounted();
 
32
        
 
33
        cocoa->m_iPodMountState = mounted;
 
34
        cocoa->m_iPodPath = path;
 
35
        
 
36
        qDebug() << "iPod state changed: Mounted =" << mounted << "Path =" << path;
 
37
        emit cocoa->iPodMountStateChanged( mounted, path );
 
38
}
 
39
 
 
40
void emitiTunesLaunched( CocoaWatcher* cocoa )
 
41
{
 
42
        qDebug() << "iTunes launched";
 
43
        emit cocoa->iTunesLaunched();
 
44
}
 
45
 
 
46
 
 
47
// Objective-C Interfaces
 
48
@interface IPodWatcher : NSObject
 
49
{
 
50
        NSString *iPodMountPath;
 
51
        CocoaWatcher *cUtils;
 
52
}
 
53
- (id)init;
 
54
- (void)connect:(CocoaWatcher*)parent;
 
55
- (void)volumeDidMount:(NSNotification*)notification;
 
56
- (void)volumeDidUnmount:(NSNotification*)notification;
 
57
@end
 
58
 
 
59
@interface ITunesWatcher : NSObject
 
60
{
 
61
    CocoaWatcher *cUtils;
 
62
}
 
63
- (id)init;
 
64
- (void)connect:(CocoaWatcher*)parent;
 
65
- (void)appLaunched:(NSNotification*)notification;
 
66
@end
 
67
 
 
68
 
 
69
// Global variables
 
70
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
71
IPodWatcher *iPodWatcher;
 
72
ITunesWatcher *iTunesWatcher;
 
73
 
 
74
 
 
75
// Constructor/Destructor
 
76
CocoaWatcher::CocoaWatcher() :
 
77
    m_messagePumpRunning( false )
 
78
{}
 
79
 
 
80
CocoaWatcher::~CocoaWatcher()
 
81
{
 
82
    stopWatchers();
 
83
        [pool release];
 
84
    instanceFlag = false;
 
85
}
 
86
 
 
87
bool CocoaWatcher::instanceFlag = false;
 
88
CocoaWatcher* CocoaWatcher::single = NULL;
 
89
 
 
90
CocoaWatcher* CocoaWatcher::getInstance()
 
91
{
 
92
    if( !instanceFlag )
 
93
    {
 
94
        single = new CocoaWatcher();
 
95
        instanceFlag = true;
 
96
        return single;
 
97
    }
 
98
    else
 
99
    {
 
100
        return single;
 
101
    }
 
102
}
 
103
 
 
104
// Some Notifications need a running Cocoa Message Pump - be careful!
 
105
// Only call if you need that for your notification...
 
106
void
 
107
CocoaWatcher::startCocoaMessagePump()
 
108
{
 
109
    if ( m_messagePumpRunning == true ) return;
 
110
    
 
111
    m_messagePumpRunning = true;
 
112
 
 
113
    [NSApp run];
 
114
}
 
115
 
 
116
void
 
117
CocoaWatcher::stopCocoaMessagePump()
 
118
{
 
119
    if ( m_messagePumpRunning == false ) return;
 
120
 
 
121
    [NSApp terminate];
 
122
 
 
123
    m_messagePumpRunning = false;
 
124
}
 
125
 
 
126
 
 
127
// Start watchers
 
128
void
 
129
CocoaWatcher::watchOutForiPod()
 
130
{
 
131
        iPodWatcher = [[IPodWatcher alloc] init];
 
132
        CocoaWatcher* me = this; // C++
 
133
        [iPodWatcher connect:me];
 
134
}
 
135
 
 
136
 
 
137
void
 
138
CocoaWatcher::watchOutForiTunes()
 
139
{
 
140
        iTunesWatcher = [[ITunesWatcher alloc] init];
 
141
        CocoaWatcher* me = this; // C++
 
142
        [iTunesWatcher connect:me];
 
143
}
 
144
 
 
145
 
 
146
// Stops all observing watchers
 
147
void
 
148
CocoaWatcher::stopWatchers()
 
149
{
 
150
    [iPodWatcher release];
 
151
        [iTunesWatcher release];
 
152
        stopCocoaMessagePump();
 
153
}
 
154
 
 
155
 
 
156
// Implementations for Obj-C classes
 
157
 
 
158
// Emits a signals when mounting/unmounting an iPod
 
159
@implementation IPodWatcher
 
160
- (id)init
 
161
{               
 
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];
 
167
 
 
168
        return self;
 
169
}
 
170
 
 
171
- (void)connect:(CocoaWatcher*)parent
 
172
{
 
173
        cUtils = parent; // C++
 
174
 
 
175
        if (!iPodMountPath) {
 
176
        // Simulate mount events for current mounts so that any mounted iPod is found
 
177
        NSEnumerator *en = [[[NSWorkspace sharedWorkspace] mountedLocalVolumePaths] objectEnumerator];
 
178
        NSString *path;
 
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];
 
184
        }
 
185
    }   
 
186
}
 
187
 
 
188
- (void)volumeDidMount:(NSNotification*)notification
 
189
{
 
190
    NSDictionary *info = [notification userInfo];
 
191
        NSString *mountPath = [info objectForKey:@"NSDevicePath"];
 
192
    NSString *iPodControlPath = [mountPath stringByAppendingPathComponent:@"iPod_Control"];
 
193
 
 
194
    BOOL isDir = NO;
 
195
    if ([[NSFileManager defaultManager] fileExistsAtPath:iPodControlPath isDirectory:&isDir] && isDir) {
 
196
        [self setValue:mountPath forKey:@"iPodMountPath"];
 
197
 
 
198
                setiPodMountState( cUtils, true, QString::fromStdString( std::string( [mountPath cString] ) ) ); // C++
 
199
    }
 
200
}
 
201
 
 
202
- (void)volumeDidUnmount:(NSNotification*)notification
 
203
{
 
204
        if ( cUtils == NULL ) return;
 
205
        
 
206
    NSDictionary *info = [notification userInfo];
 
207
        NSString *mountPath = [info objectForKey:@"NSDevicePath"];
 
208
    
 
209
    if ([iPodMountPath isEqualToString:mountPath]) {
 
210
                setiPodMountState( cUtils, false, "" ); // C++
 
211
    }
 
212
}
 
213
@end
 
214
 
 
215
 
 
216
// Emits a signal when launching iTunes
 
217
@implementation ITunesWatcher
 
218
- (id)init
 
219
{
 
220
    // Register for application launches
 
221
        [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
 
222
        selector:@selector(appLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
 
223
    
 
224
    return self;
 
225
}
 
226
 
 
227
- (void)connect:(CocoaWatcher*)parent
 
228
{
 
229
        cUtils = parent; // C++    
 
230
    cUtils->startCocoaMessagePump(); // Need message pump for this notification
 
231
}
 
232
 
 
233
- (void)appLaunched:(NSNotification*)notification
 
234
{
 
235
        if ( cUtils == NULL ) return;
 
236
    
 
237
    NSDictionary *info = [notification userInfo];
 
238
 
 
239
        if ( [[info objectForKey:@"NSApplicationName"] isEqual:@"iTunes"] )
 
240
        {
 
241
                emitiTunesLaunched( cUtils ); // C++
 
242
    }
 
243
}
 
244
@end