forked from hrkfdn/mpdas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpd.cpp
More file actions
103 lines (92 loc) · 2.09 KB
/
mpd.cpp
File metadata and controls
103 lines (92 loc) · 2.09 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "mpdas.h"
CMPD* MPD = 0;
void
CMPD::SetSong(mpd_Song* song)
{
_cached = false;
if(song && song->artist && song->title && !Config->IsArtistExcluded(song->artist)) {
_song.artist = song->artist;
_song.title = song->title;
if(song->album)
_song.album = song->album;
else
_song.album = "";
_song.time = song->time;
_gotsong = true;
iprintf("New song: %s - %s", _song.artist.c_str(), _song.title.c_str());
}
else {
_gotsong = false;
return;
}
_start = mpd_stats_get_playtime(_obj);
_starttime = time(NULL);
AudioScrobbler->SendNowPlaying(song);
}
void
CMPD::CheckSubmit()
{
if(!_gotsong || _cached || (!_song.artist.size() || !_song.title.size())) return;
int curplaytime = mpd_stats_get_playtime(_obj);
if(curplaytime - _start >= 240 || curplaytime - _start >= _song.time/2) {
Cache->AddToCache(_song.time, _song.artist, _song.title, _song.album, _starttime, false);
_cached = true;
}
}
void
CMPD::StatusChanged(MpdObj* obj, ChangedStatusType what)
{
mpd_Song* song = 0;
if(what & MPD_CST_SONGID) {
song = mpd_playlist_get_current_song(obj);
if(song)
MPD->SetSong(song);
}
else if(what & MPD_CST_STATE) {
if(mpd_player_get_state(obj) == MPD_PLAYER_STOP)
MPD->SetSong(0);
}
else if(what & MPD_CST_ELAPSED_TIME) {
MPD->CheckSubmit();
}
}
CMPD::CMPD()
{
_gotsong = false;
_connected = false;
_cached = false;
_obj = mpd_new((char*)Config->getMHost().c_str(), Config->getMPort(), (char*)Config->getMPassword().c_str());
mpd_signal_connect_status_changed(_obj, (StatusChangedCallback)&StatusChanged, NULL);
if(Connect())
iprintf("%s", "Connected to MPD.");
else
eprintf("%s", "Could not connect to MPD.");
}
CMPD::~CMPD()
{
if(_obj)
mpd_free(_obj);
}
bool
CMPD::Connect()
{
_connected = true;
if(mpd_connect(_obj)) {
_connected = false;
return _connected;
}
if(mpd_send_password(_obj))
_connected = false;
return (_connected == true);
}
void
CMPD::Update()
{
if(!_connected || !mpd_check_connected(_obj)) {
eprintf("%s", "Disconnected from MPD. Reconnecting ..");
Connect();
return;
}
mpd_status_update(_obj);
mpd_stats_update(_obj);
}