added most of the remaining commands.
[simple-xmbc-rem] / src / xbmc.cpp
1 #include "xbmc.h"
2 #include "constants.h"
3 #include "genericnotify.h"
4
5 #include <QSettings>
6 #include <QTextStream>
7 #include <QNetworkReply>
8
9 // XBMC constants -- from https://github.com/xbmc/xbmc/blob/master/xbmc/guilib/Key.h
10 #define ACTION_MOVE_LEFT                1
11 #define ACTION_MOVE_RIGHT               2
12 #define ACTION_MOVE_UP                  3
13 #define ACTION_MOVE_DOWN                4
14 #define ACTION_NEXT_SUBTITLE            26 // switch to next subtitle of movie. Can b used in videoFullScreen.xml window id=2005
15 #define ACTION_SUBTITLE_DELAY_MIN       52 // Decrease subtitle/movie Delay. Can b used in videoFullScreen.xml window id=2005
16 #define ACTION_SUBTITLE_DELAY_PLUS      53 // Increase subtitle/movie Delay. Can b used in videoFullScreen.xml window id=2005
17 #define ACTION_AUDIO_DELAY_MIN          54 // Increase avsync delay. Can b used in videoFullScreen.xml window id=2005
18 #define ACTION_AUDIO_DELAY_PLUS         55 // Decrease avsync delay. Can b used in videoFullScreen.xml window id=2005
19 #define ACTION_AUDIO_NEXT_LANGUAGE      56 // Select next language in movie. Can b used in videoFullScreen.xml window id=2005
20 #define ACTION_ENTER                    135
21 #define ACTION_SHOW_GUI                 18 // toggle between GUI and movie or GUI and visualisation.
22 #define ACTION_STEP_FORWARD             20 // seek +1% in the movie. Can b used in videoFullScreen.xml window id=2005
23 #define ACTION_STEP_BACK                21 // seek -1% in the movie. Can b used in videoFullScreen.xml window id=2005
24 #define ACTION_BIG_STEP_FORWARD         22 // seek +10% in the movie. Can b used in videoFullScreen.xml window id=2005
25 #define ACTION_BIG_STEP_BACK            23 // seek -10% in the movie. Can b used in videoFullScreen.xml window id=2005
26
27 #define ACTION_PLAYER_PLAY              79 // Play current song. Unpauses song and sets playspeed to 1x. global action, can be used anywhere
28 #define ACTION_SELECT_ITEM              7
29 #define ACTION_HIGHLIGHT_ITEM           8
30 #define ACTION_PARENT_DIR               9
31
32 #define ACTION_VOLUME_UP                88
33 #define ACTION_VOLUME_DOWN              89
34 #define ACTION_MUTE                     91
35
36 #define ACTION_PAUSE                    12
37 #define ACTION_STOP                     13
38 #define ACTION_NEXT_ITEM                14
39 #define ACTION_PREV_ITEM                15
40 #define ACTION_FORWARD                  16 // Can be used to specify specific action in a window, Playback control is handled in ACTION_PLAYER_*
41 #define ACTION_REWIND                   17 // Can be used to specify specific action in a window, Playback control is handled in ACTION_PLAYER_*
42
43 Xbmc::Xbmc(QObject *parent) : QObject(parent)
44 {
45     m_manager = new QNetworkAccessManager(this);
46 }
47
48 Xbmc::~Xbmc()
49 {
50     delete m_manager;
51 }
52
53 void Xbmc::commandActionFinished()
54 {
55     QNetworkReply* reply = qobject_cast<QNetworkReply *>(sender());
56     if (reply) {
57         if (reply->error() == QNetworkReply::NoError) {
58             QTextStream stream(reply);
59             QString msg = stream.readAll();
60             qDebug("Xbmc::commandActionFinished: %s", qPrintable(msg));
61         } else {
62             notify::notify(reply->errorString());
63         }
64         reply->deleteLater();
65     }
66 }
67
68 void Xbmc::do_command_action(int action)
69 {
70     QSettings settings;
71     QString server = settings.value(SETUP_XBMC_SERVER, SETUP_XBMC_SERVER_DEFAULT).toString();
72     QString port = settings.value(SETUP_XBMC_PORT, SETUP_XBMC_PORT_DEFAULT).toString();
73
74     QUrl url = QUrl(QString("http://%1:%2/xbmcCmds/xbmcHttp?command=Action(%3)").arg(server).arg(port).arg(action));
75
76     QNetworkRequest request;
77     request.setUrl(url);
78
79     QNetworkReply *reply = m_manager->get(request);
80     connect(reply, SIGNAL(finished()), this, SLOT(commandActionFinished()));
81 }
82
83 void Xbmc::actionRight()
84 {
85     do_command_action(ACTION_MOVE_RIGHT);
86     do_command_action(ACTION_STEP_FORWARD);
87 }
88
89 void Xbmc::actionLeft()
90 {
91     do_command_action(ACTION_MOVE_LEFT);
92     do_command_action(ACTION_STEP_BACK);
93 }
94
95 void Xbmc::actionUp()
96 {
97     do_command_action(ACTION_MOVE_UP);
98     do_command_action(ACTION_BIG_STEP_FORWARD);
99 }
100
101 void Xbmc::actionDown()
102 {
103     do_command_action(ACTION_MOVE_DOWN);
104     do_command_action(ACTION_BIG_STEP_BACK);
105 }
106
107 void Xbmc::actionMute()
108 {
109     do_command_action(ACTION_MUTE);
110 }
111
112 void Xbmc::actionVolumeUp()
113 {
114     do_command_action(ACTION_VOLUME_UP);
115 }
116
117 void Xbmc::actionVolumeDown()
118 {
119     do_command_action(ACTION_VOLUME_DOWN);
120 }
121
122 void Xbmc::actionNextSubtitle()
123 {
124     do_command_action(ACTION_NEXT_SUBTITLE);
125 }
126
127 void Xbmc::actionNextLanguage()
128 {
129     do_command_action(ACTION_AUDIO_NEXT_LANGUAGE);
130 }
131
132 void Xbmc::actionPlay()
133 {
134     do_command_action(ACTION_PLAYER_PLAY);
135 }
136
137 void Xbmc::actionParentDir()
138 {
139     do_command_action(ACTION_PARENT_DIR);
140 }
141
142 void Xbmc::actionSelect()
143 {
144     do_command_action(ACTION_SELECT_ITEM);
145 }
146
147 void Xbmc::actionStop()
148 {
149     do_command_action(ACTION_STOP);
150 }