1 /***************************************************************************
2 * Copyright (C) 2008 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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 program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 MPD::Connection::Connection() : isConnected(0),
38 MPD::Connection::~Connection()
41 mpd_closeConnection(itsConnection
);
43 mpd_freeStatus(itsOldStatus
);
45 mpd_freeStatus(itsCurrentStatus
);
48 bool MPD::Connection::Connect()
50 if (!isConnected
&& !itsConnection
)
52 itsConnection
= mpd_newConnection(itsHost
.c_str(), itsPort
, itsTimeout
);
56 if (!itsPassword
.empty())
58 return !CheckForErrors();
64 bool MPD::Connection::Connected() const
69 void MPD::Connection::Disconnect()
72 mpd_closeConnection(itsConnection
);
74 mpd_freeStatus(itsOldStatus
);
76 mpd_freeStatus(itsCurrentStatus
);
83 void MPD::Connection::SetHostname(const string
&host
)
85 size_t at
= host
.find("@");
86 if (at
!= string::npos
)
88 itsPassword
= host
.substr(0, at
);
89 itsHost
= host
.substr(at
+1);
95 void MPD::Connection::SendPassword() const
97 mpd_sendPasswordCommand(itsConnection
, itsPassword
.c_str());
98 mpd_finishCommand(itsConnection
);
101 void MPD::Connection::SetStatusUpdater(StatusUpdater updater
, void *data
)
103 itsUpdater
= updater
;
104 itsStatusUpdaterUserdata
= data
;
107 void MPD::Connection::SetErrorHandler(ErrorHandler handler
, void *data
)
109 itsErrorHandler
= handler
;
110 itsErrorHandlerUserdata
= data
;
113 void MPD::Connection::UpdateStatus()
118 mpd_freeStatus(itsOldStatus
);
119 itsOldStatus
= itsCurrentStatus
;
120 mpd_sendStatusCommand(itsConnection
);
121 itsCurrentStatus
= mpd_getStatus(itsConnection
);
123 if (CheckForErrors())
126 if (itsCurrentStatus
&& itsUpdater
)
128 if (itsOldStatus
== NULL
)
130 itsChanges
.SongID
= 1;
131 itsChanges
.ElapsedTime
= 1;
132 itsChanges
.State
= 1;
136 itsChanges
.SongID
= itsOldStatus
->songid
!= itsCurrentStatus
->songid
;
137 itsChanges
.ElapsedTime
= itsOldStatus
->elapsedTime
!= itsCurrentStatus
->elapsedTime
;
138 itsChanges
.State
= itsOldStatus
->state
!= itsCurrentStatus
->state
;
140 itsUpdater(this, itsChanges
, itsErrorHandlerUserdata
);
144 mpd_Song
* MPD::Connection::CurrentSong() const
146 if (isConnected
&& (GetState() == psPlay
|| GetState() == psPause
))
148 mpd_sendCurrentSongCommand(itsConnection
);
149 mpd_InfoEntity
*item
= NULL
;
150 item
= mpd_getNextInfoEntity(itsConnection
);
153 mpd_Song
*result
= item
->info
.song
;
155 mpd_freeInfoEntity(item
);
158 mpd_finishCommand(itsConnection
);
163 int MPD::Connection::CheckForErrors()
166 if (itsConnection
->error
)
168 itsErrorMessage
= itsConnection
->errorStr
;
169 if (itsConnection
->error
== MPD_ERROR_ACK
)
172 itsErrorHandler(this, itsConnection
->errorCode
, itsErrorMessage
, itsErrorHandlerUserdata
);
173 itsErrorCode
= itsConnection
->errorCode
;
177 isConnected
= 0; // the rest of errors are fatal to connection
179 itsErrorHandler(this, itsConnection
->error
, itsErrorMessage
, itsErrorHandlerUserdata
);
180 itsErrorCode
= itsConnection
->error
;
182 mpd_clearError(itsConnection
);