3
* Copyright (C) 2006 Remko Troncon
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (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 library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
#include <CoreFoundation/CoreFoundation.h>
28
#include "itunescontroller.h"
31
* \class ITunesController
32
* \brief A controller for the Mac OS X version of iTunes.
35
static QString CFStringToQString(CFStringRef s)
40
CFIndex length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(s), kCFStringEncodingUTF8) + 1;
41
char* buffer = new char[length];
42
if (CFStringGetCString(s, buffer, length, kCFStringEncodingUTF8)) {
43
result = QString::fromUtf8(buffer);
46
qWarning("itunesplayer.cpp: CFString conversion failed.");
54
ITunesController::ITunesController()
56
// TODO: Poll iTunes for current playing tune
57
CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter();
58
CFNotificationCenterAddObserver(center, this, ITunesController::iTunesCallback, CFSTR("com.apple.iTunes.playerInfo"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
61
Tune ITunesController::currentTune()
66
void ITunesController::iTunesCallback(CFNotificationCenterRef,void* observer,CFStringRef,const void*, CFDictionaryRef info)
69
ITunesController* controller = (ITunesController*) observer;
71
CFStringRef cf_state = (CFStringRef) CFDictionaryGetValue(info, CFSTR("Player State"));
72
if (CFStringCompare(cf_state,CFSTR("Paused"),0) == kCFCompareEqualTo) {
73
//qDebug() << "itunesplayer.cpp: Paused";
74
emit controller->stopped();
76
else if (CFStringCompare(cf_state,CFSTR("Stopped"),0) == kCFCompareEqualTo) {
77
//qDebug() << "itunesplayer.cpp: Stopped";
78
emit controller->stopped();
80
else if (CFStringCompare(cf_state,CFSTR("Playing"),0) == kCFCompareEqualTo) {
81
//qDebug() << "itunesplayer.cpp: Playing";
82
tune.setArtist(CFStringToQString((CFStringRef) CFDictionaryGetValue(info, CFSTR("Artist"))));
83
tune.setName(CFStringToQString((CFStringRef) CFDictionaryGetValue(info, CFSTR("Name"))));
84
tune.setAlbum(CFStringToQString((CFStringRef) CFDictionaryGetValue(info, CFSTR("Album"))));
86
CFNumberRef cf_track = (CFNumberRef) CFDictionaryGetValue(info, CFSTR("Track Number"));
89
if (!CFNumberGetValue(cf_track,kCFNumberIntType,&tracknr)) {
90
qWarning("itunesplayer.cpp: Number value conversion failed.");
92
tune.setTrack(QString::number(tracknr));
95
CFNumberRef cf_time = (CFNumberRef) CFDictionaryGetValue(info, CFSTR("Total Time"));
97
if (cf_time && !CFNumberGetValue(cf_time,kCFNumberIntType,&time)) {
98
qWarning("itunesplayer.cpp: Number value conversion failed.");
100
tune.setTime((unsigned int) (time / 1000));
101
controller->currentTune_ = tune;
102
emit controller->playing(tune);
105
qWarning("itunesplayer.cpp: Unknown state.");