1 #include "xinemacontrol.h"
3 XinemaControl::XinemaControl():
4 playback_state(STOPPED),
8 QObject::connect(&socket, &QAbstractSocket::connected, this, &XinemaControl::connected);
9 QObject::connect(&socket, &QAbstractSocket::disconnected, this, &XinemaControl::disconnected);
10 QObject::connect(&socket, &QIODevice::readyRead, this, &XinemaControl::data_available);
13 void XinemaControl::connect(const QHostAddress &addr)
15 socket.connectToHost(addr, 34588);
18 bool XinemaControl::is_connected() const
20 return socket.state()==QAbstractSocket::ConnectedState;
23 void XinemaControl::list_directory(const QString &dir)
28 send_request("list_directory "+dir);
31 void XinemaControl::play_file(const QString &fn)
36 send_request("play_file "+fn);
39 void XinemaControl::play()
44 void XinemaControl::seek(float time)
46 send_request(QString("seek %1").arg(time));
49 void XinemaControl::pause()
51 send_request("pause");
54 void XinemaControl::stop()
59 void XinemaControl::send_request(const QString &req)
61 socket.write(req.toUtf8());
62 socket.write("\n", 1);
65 void XinemaControl::data_available()
68 int len = socket.read(rbuf, sizeof(rbuf));
72 buffer.append(rbuf, len);
76 int newline = buffer.indexOf('\n', start);
80 QString reply = QString::fromUtf8(buffer.mid(start, newline-start));
86 buffer.remove(0, start);
89 void XinemaControl::process_reply(const QString &reply)
91 int space = reply.indexOf(' ');
92 QString keyword = reply.mid(0, space);
95 args = reply.mid(space+1);
97 if(keyword=="directory")
98 emit directory_started(args);
99 else if(keyword=="subdir")
100 emit subdirectory_added(args);
101 else if(keyword=="file")
102 emit file_added(args);
103 else if(keyword=="state")
106 playback_state = STOPPED;
107 else if(args=="PAUSED")
108 playback_state = PAUSED;
109 else if(args=="PLAYING")
110 playback_state = PLAYING;
111 emit playback_state_changed(playback_state);
113 else if(keyword=="title")
116 emit title_changed(title);
118 else if(keyword=="duration")
120 duration = args.toFloat();
121 emit duration_changed(duration);
123 else if(keyword=="position")
125 position = args.toFloat();
126 emit position_changed(position);