summaryrefslogtreecommitdiffstats
blob: 0843aadf83c3ac77a69cf78dbbe558f975f25637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * Copyright (c) 2014 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Sluift/ITunesInterface.h>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wfour-char-constants"
#import <Sluift/iTunes.h>
#pragma clang diagnostic pop
#include <ScriptingBridge/ScriptingBridge.h>

#include <boost/smart_ptr/make_shared.hpp>
#include <boost/optional.hpp>
#include <SwifTools/Cocoa/CocoaUtil.h>

using namespace Swift;

struct ITunesInterface::Private {
	Private() : iTunes(nil) {
	}

	iTunesApplication* iTunes;
};

ITunesInterface::ITunesInterface() : p(boost::make_shared<Private>()) {
}

ITunesInterface::~ITunesInterface() {
}

boost::optional<ITunesInterface::Track> ITunesInterface::getCurrentTrack() const {
	if (!haveApplication()) {
		return boost::optional<ITunesInterface::Track>();
	}
	iTunesTrack* currentTrack = p->iTunes.currentTrack;
	if (!currentTrack) {
		return boost::optional<ITunesInterface::Track>();
	}
	ITunesInterface::Track result;
	result.name = NS2STDSTRING(currentTrack.name);
	result.artist = NS2STDSTRING(currentTrack.artist);
	result.album = NS2STDSTRING(currentTrack.album);
	result.trackNumber = currentTrack.trackNumber;
	result.duration = currentTrack.duration;
	result.rating = currentTrack.rating;
	return result;
}


bool ITunesInterface::haveApplication() const {
	if (!p->iTunes) {
		p->iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
	}
	return p->iTunes != nil && [p->iTunes isRunning];
}