~ubuntu-branches/ubuntu/quantal/psi/quantal

« back to all changes in this revision

Viewing changes to src/tools/tunecontroller/plugins/itunes/itunescontroller.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * itunescontroller.cpp 
 
3
 * Copyright (C) 2006  Remko Troncon
 
4
 *
 
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.
 
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 library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include <QtGlobal>
 
22
#include <QString>
 
23
#include <QTime>
 
24
#include <QDebug>
 
25
 
 
26
#include <CoreFoundation/CoreFoundation.h>
 
27
 
 
28
#include "itunescontroller.h"
 
29
 
 
30
/**
 
31
 * \class ITunesController
 
32
 * \brief A controller for the Mac OS X version of iTunes.
 
33
 */
 
34
 
 
35
static QString CFStringToQString(CFStringRef s)
 
36
{
 
37
        QString result;
 
38
 
 
39
        if (s != NULL) {
 
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);
 
44
                }
 
45
                else {
 
46
                        qWarning("itunesplayer.cpp: CFString conversion failed.");
 
47
                }
 
48
                delete buffer;
 
49
        } 
 
50
    return result;
 
51
}                         
 
52
 
 
53
 
 
54
ITunesController::ITunesController()
 
55
{
 
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); 
 
59
}
 
60
 
 
61
Tune ITunesController::currentTune() 
 
62
{
 
63
        return currentTune_;
 
64
}
 
65
 
 
66
void ITunesController::iTunesCallback(CFNotificationCenterRef,void* observer,CFStringRef,const void*, CFDictionaryRef info)
 
67
{
 
68
        Tune tune;
 
69
        ITunesController* controller = (ITunesController*) observer;
 
70
        
 
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();
 
75
        }
 
76
        else if (CFStringCompare(cf_state,CFSTR("Stopped"),0) == kCFCompareEqualTo) {
 
77
                //qDebug() << "itunesplayer.cpp: Stopped";
 
78
                emit controller->stopped();
 
79
        }
 
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"))));
 
85
                
 
86
                CFNumberRef cf_track = (CFNumberRef) CFDictionaryGetValue(info, CFSTR("Track Number"));
 
87
                if (cf_track) {
 
88
                        int tracknr;
 
89
                        if (!CFNumberGetValue(cf_track,kCFNumberIntType,&tracknr)) {
 
90
                                qWarning("itunesplayer.cpp: Number value conversion failed.");
 
91
                        }
 
92
                        tune.setTrack(QString::number(tracknr));
 
93
                }
 
94
                
 
95
                CFNumberRef cf_time = (CFNumberRef) CFDictionaryGetValue(info, CFSTR("Total Time"));
 
96
                int time = 0;
 
97
                if (cf_time && !CFNumberGetValue(cf_time,kCFNumberIntType,&time)) {
 
98
                        qWarning("itunesplayer.cpp: Number value conversion failed.");
 
99
                }
 
100
                tune.setTime((unsigned int) (time / 1000));
 
101
                controller->currentTune_ = tune;
 
102
                emit controller->playing(tune);
 
103
        }
 
104
        else {
 
105
                qWarning("itunesplayer.cpp: Unknown state.");
 
106
        }
 
107
}