]> git.tdb.fi Git - xinema.git/blob - remote/source/xinemacontrol.cpp
Implement seeking and play/pause controls in the remote
[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         char rbuf[1024];
68         int len = socket.read(rbuf, sizeof(rbuf));
69         if(len<0)
70                 return;
71
72         buffer.append(rbuf, len);
73         unsigned start = 0;
74         while(1)
75         {
76                 int newline = buffer.indexOf('\n', start);
77                 if(newline<0)
78                         break;
79
80                 QString reply = QString::fromUtf8(buffer.mid(start, newline-start));
81                 process_reply(reply);
82
83                 start = newline+1;
84         }
85
86         buffer.remove(0, start);
87 }
88
89 void XinemaControl::process_reply(const QString &reply)
90 {
91         int space = reply.indexOf(' ');
92         QString keyword = reply.mid(0, space);
93         QString args;
94         if(space>=0)
95                 args = reply.mid(space+1);
96
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")
104         {
105                 if(args=="STOPPED")
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);
112         }
113         else if(keyword=="title")
114         {
115                 title = args;
116                 emit title_changed(title);
117         }
118         else if(keyword=="duration")
119         {
120                 duration = args.toFloat();
121                 emit duration_changed(duration);
122         }
123         else if(keyword=="position")
124         {
125                 position = args.toFloat();
126                 emit position_changed(position);
127         }
128 }