move x outside a loop, its value has to be in memory
[scrobby.git] / src / mpdpp.cpp
blobfc7333274412722f5c092629f1638936081785d5
1 /***************************************************************************
2 * Copyright (C) 2008 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
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. *
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 program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "mpdpp.h"
23 using std::string;
25 MPD::Connection::Connection() : isConnected(0),
26 itsErrorCode(0),
27 itsHost("localhost"),
28 itsPort(6600),
29 itsTimeout(15),
30 itsUpdater(0),
31 itsErrorHandler(0)
33 itsConnection = 0;
34 itsCurrentStatus = 0;
35 itsOldStatus = 0;
38 MPD::Connection::~Connection()
40 if (itsConnection)
41 mpd_closeConnection(itsConnection);
42 if (itsOldStatus)
43 mpd_freeStatus(itsOldStatus);
44 if (itsCurrentStatus)
45 mpd_freeStatus(itsCurrentStatus);
48 bool MPD::Connection::Connect()
50 if (!isConnected && !itsConnection)
52 itsConnection = mpd_newConnection(itsHost.c_str(), itsPort, itsTimeout);
53 isConnected = 1;
54 if (CheckForErrors())
55 return false;
56 if (!itsPassword.empty())
57 SendPassword();
58 return !CheckForErrors();
60 else
61 return true;
64 bool MPD::Connection::Connected() const
66 return isConnected;
69 void MPD::Connection::Disconnect()
71 if (itsConnection)
72 mpd_closeConnection(itsConnection);
73 if (itsOldStatus)
74 mpd_freeStatus(itsOldStatus);
75 if (itsCurrentStatus)
76 mpd_freeStatus(itsCurrentStatus);
77 itsConnection = 0;
78 itsCurrentStatus = 0;
79 itsOldStatus = 0;
80 isConnected = 0;
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);
91 else
92 itsHost = host;
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()
115 CheckForErrors();
117 if (itsOldStatus)
118 mpd_freeStatus(itsOldStatus);
119 itsOldStatus = itsCurrentStatus;
120 mpd_sendStatusCommand(itsConnection);
121 itsCurrentStatus = mpd_getStatus(itsConnection);
123 if (CheckForErrors())
124 return;
126 if (itsCurrentStatus && itsUpdater)
128 if (itsOldStatus == NULL)
130 itsChanges.SongID = 1;
131 itsChanges.ElapsedTime = 1;
132 itsChanges.State = 1;
134 else
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);
151 if (item)
153 mpd_Song *result = item->info.song;
154 item->info.song = 0;
155 mpd_freeInfoEntity(item);
156 return result;
158 mpd_finishCommand(itsConnection);
160 return NULL;
163 int MPD::Connection::CheckForErrors()
165 itsErrorCode = 0;
166 if (itsConnection->error)
168 itsErrorMessage = itsConnection->errorStr;
169 if (itsConnection->error == MPD_ERROR_ACK)
171 if (itsErrorHandler)
172 itsErrorHandler(this, itsConnection->errorCode, itsErrorMessage, itsErrorHandlerUserdata);
173 itsErrorCode = itsConnection->errorCode;
175 else
177 isConnected = 0; // the rest of errors are fatal to connection
178 if (itsErrorHandler)
179 itsErrorHandler(this, itsConnection->error, itsErrorMessage, itsErrorHandlerUserdata);
180 itsErrorCode = itsConnection->error;
182 mpd_clearError(itsConnection);
184 return itsErrorCode;