]> git.tdb.fi Git - xinema.git/blob - remote/source/xinemacontrol.cpp
Fix a bug in receiving data from the server
[xinema.git] / remote / source / xinemacontrol.cpp
1 #include "xinemacontrol.h"
2
3 XinemaControl::XinemaControl():
4         playback_state(STOPPED),
5         duration(0.0f),
6         position(0.0f)
7 {
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);
11 }
12
13 void XinemaControl::connect(const QHostAddress &addr)
14 {
15         socket.connectToHost(addr, 34588);
16 }
17
18 bool XinemaControl::is_connected() const
19 {
20         return socket.state()==QAbstractSocket::ConnectedState;
21 }
22
23 void XinemaControl::list_directory(const QString &dir)
24 {
25         if(!is_connected())
26                 return;
27
28         send_request("list_directory "+dir);
29 }
30
31 void XinemaControl::play_file(const QString &fn)
32 {
33         if(!is_connected())
34                 return;
35
36         send_request("play_file "+fn);
37 }
38
39 void XinemaControl::play()
40 {
41         send_request("play");
42 }
43
44 void XinemaControl::seek(float time)
45 {
46         send_request(QString("seek %1").arg(time));
47 }
48
49 void XinemaControl::pause()
50 {
51         send_request("pause");
52 }
53
54 void XinemaControl::stop()
55 {
56         send_request("stop");
57 }
58
59 void XinemaControl::send_request(const QString &req)
60 {
61         socket.write(req.toUtf8());
62         socket.write("\n", 1);
63 }
64
65 void XinemaControl::data_available()
66 {
67         while(socket.bytesAvailable())
68         {
69                 char rbuf[1024];
70                 int len = socket.read(rbuf, sizeof(rbuf));
71                 if(len<0)
72                         break;
73
74                 buffer.append(rbuf, len);
75         }
76
77         unsigned start = 0;
78         while(1)
79         {
80                 int newline = buffer.indexOf('\n', start);
81                 if(newline<0)
82                         break;
83
84                 QString reply = QString::fromUtf8(buffer.mid(start, newline-start));
85                 process_reply(reply);
86
87                 start = newline+1;
88         }
89
90         buffer.remove(0, start);
91 }
92
93 void XinemaControl::process_reply(const QString &reply)
94 {
95         int space = reply.indexOf(' ');
96         QString keyword = reply.mid(0, space);
97         QString args;
98         if(space>=0)
99                 args = reply.mid(space+1);
100
101         if(keyword=="directory")
102                 emit directory_started(args);
103         else if(keyword=="subdir")
104                 emit subdirectory_added(args);
105         else if(keyword=="file")
106                 emit file_added(args);
107         else if(keyword=="state")
108         {
109                 if(args=="STOPPED")
110                         playback_state = STOPPED;
111                 else if(args=="PAUSED")
112                         playback_state = PAUSED;
113                 else if(args=="PLAYING")
114                         playback_state = PLAYING;
115                 emit playback_state_changed(playback_state);
116         }
117         else if(keyword=="title")
118         {
119                 title = args;
120                 emit title_changed(title);
121         }
122         else if(keyword=="duration")
123         {
124                 duration = args.toFloat();
125                 emit duration_changed(duration);
126         }
127         else if(keyword=="position")
128         {
129                 position = args.toFloat();
130                 emit position_changed(position);
131         }
132 }