[general] "poor programming style" fix
[libxmms2client-qt.git] / client.cpp
blob7e4107bb38303eea178501278701f75cb4b93189
1 //
2 // C++ Implementation: client
3 //
4 // Author: Oliver Groß <z.o.gross@gmx.de>, (C) 2008
5 //
6 // Copyright: See COPYING file that comes with this distribution
7 //
8 #include "client.h"
10 namespace XmmsQt {
11 QClient::QClient(QString name, QObject * parent) : QObject(parent),
12 m_Name(name), m_Connected(false), m_Connection(NULL), m_ReadNotifier(NULL), m_WriteNotifier(NULL)
13 // m_Playback(this), m_Playlist(this)
15 m_Connection = xmmsc_init(name.toAscii().data());
18 QClient::~QClient() {
19 if( m_Connection ) {
20 xmmsc_unref(m_Connection);
24 void QClient::disconnectCallbackHandler(void * instance) {
25 if (instance) {
26 QClient * client = static_cast<QClient *>(instance);
27 xmmsc_unref(client->m_Connection);
28 client->m_Connection = 0;
30 if (client->m_ReadNotifier)
31 delete client->m_ReadNotifier;
32 if (client->m_WriteNotifier)
33 delete client->m_WriteNotifier;
35 emit client->connectionChanged(NULL); //TODO: check if this is possible
39 void QClient::outputCheck(int, void * instance) {
40 if (instance) {
41 QClient * client = static_cast<QClient *>(instance);
42 if (xmmsc_io_want_out(client->m_Connection))
43 client->m_WriteNotifier->setEnabled(true);
44 else
45 client->m_WriteNotifier->setEnabled(false);
49 void QClient::handleIn(int) {
50 xmmsc_io_in_handle(m_Connection);
53 void QClient::handleOut(int) {
54 xmmsc_io_out_handle(m_Connection);
57 void QClient::connectToServer(QString ipcPath) {
58 if(!m_Connected) {
59 if(!m_Connection) {
60 m_Connection = xmmsc_init(m_Name.toAscii().data());
62 m_Connected = xmmsc_connect(m_Connection, (ipcPath.isEmpty()) ? NULL : ipcPath.toAscii().data());
64 if (!m_Connected)
65 return;
67 xmmsc_disconnect_callback_set(m_Connection, disconnectCallbackHandler, this);
68 xmmsc_io_need_out_callback_set(m_Connection, outputCheck, this);
70 //don't think we need a quit handler as we have a disconnect handler
71 //xmmsc_result_notifier_set();
73 //xmmsc_io_need_out_callback_set();
75 if (m_ReadNotifier)
76 delete m_ReadNotifier;
77 if (m_WriteNotifier)
78 delete m_WriteNotifier;
80 m_ReadNotifier = new QSocketNotifier(xmmsc_io_fd_get(m_Connection), QSocketNotifier::Read, this);
81 m_WriteNotifier = new QSocketNotifier(xmmsc_io_fd_get(m_Connection), QSocketNotifier::Write, this);
82 connect(m_ReadNotifier, SIGNAL(activated(int)), this, SLOT(handleIn(int)));
83 connect(m_WriteNotifier, SIGNAL(activated(int)), this, SLOT(handleOut(int)));
84 m_ReadNotifier->setEnabled(true);
85 m_WriteNotifier->setEnabled(false);
87 emit connectionChanged(m_Connection);
90 bool QClient::isConnectedToServer() const
92 return m_Connected;
95 void QClient::quitServer() {
96 if(m_Connected) {
97 xmmsc_result_t * result = xmmsc_quit(m_Connection);
98 xmmsc_result_unref(result);
99 m_Connected = false;
103 QString QClient::lastError() const {
104 return QString(xmmsc_get_last_error(m_Connection));