1 #include "xinemacontrol.h"
3 XinemaControl::XinemaControl()
5 QObject::connect(&socket, &QAbstractSocket::connected, this, &XinemaControl::connected);
6 QObject::connect(&socket, &QAbstractSocket::disconnected, this, &XinemaControl::disconnected);
7 QObject::connect(&socket, &QIODevice::readyRead, this, &XinemaControl::data_available);
10 void XinemaControl::connect(const QHostAddress &addr)
12 socket.connectToHost(addr, 34588);
15 bool XinemaControl::is_connected() const
17 return socket.state()==QAbstractSocket::ConnectedState;
20 void XinemaControl::list_directory(const QString &dir)
25 send_request("list_directory "+dir);
28 void XinemaControl::play_file(const QString &fn)
33 send_request("play_file "+fn);
36 void XinemaControl::send_request(const QString &req)
38 socket.write(req.toUtf8());
39 socket.write("\n", 1);
42 void XinemaControl::data_available()
45 int len = socket.read(rbuf, sizeof(rbuf));
49 buffer.append(rbuf, len);
53 int newline = buffer.indexOf('\n', start);
57 QString reply = QString::fromUtf8(buffer.mid(start, newline-start));
63 buffer.remove(0, start);
66 void XinemaControl::process_reply(const QString &reply)
68 int space = reply.indexOf(' ');
69 QString keyword = reply.mid(0, space);
72 args = reply.mid(space+1);
74 if(keyword=="directory")
75 emit directory_started(args);
76 else if(keyword=="subdir")
77 emit subdirectory_added(args);
78 else if(keyword=="file")
79 emit file_added(args);
80 else if(keyword=="title")
83 emit title_changed(title);
85 else if(keyword=="duration")
87 duration = args.toFloat();
88 emit duration_changed(duration);
90 else if(keyword=="position")
92 position = args.toFloat();
93 emit position_changed(position);