Merge pull request #1129 from jmarshallnz/remove_smb_auth_details_in_add_source
[vuplus_xbmc] / lib / cmyth / libcmyth / bookmark.c
1 /*
2  *  Copyright (C) 2005-2009, Jon Gettler
3  *  http://www.mvpmc.org/
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2.1 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <inttypes.h>
24 #include <errno.h>
25 #include <cmyth_local.h>
26
27
28 long long cmyth_get_bookmark(cmyth_conn_t conn, cmyth_proginfo_t prog)
29 {
30         char *buf;
31         unsigned int len = CMYTH_TIMESTAMP_LEN + CMYTH_LONGLONG_LEN + 18;
32         int err;
33         long long ret;
34         int count;
35         int64_t ll;
36         int r;
37         char start_ts_dt[CMYTH_TIMESTAMP_LEN + 1];
38         cmyth_datetime_to_string(start_ts_dt, prog->proginfo_rec_start_ts);
39         buf = alloca(len);
40         if (!buf) {
41                 return -ENOMEM;
42         }
43         sprintf(buf,"%s %ld %s","QUERY_BOOKMARK",prog->proginfo_chanId,
44                 start_ts_dt);
45         pthread_mutex_lock(&mutex);
46         if ((err = cmyth_send_message(conn,buf)) < 0) {
47                 cmyth_dbg(CMYTH_DBG_ERROR,
48                         "%s: cmyth_send_message() failed (%d)\n",
49                         __FUNCTION__, err);
50                 ret = err;
51                 goto out;
52         }
53         count = cmyth_rcv_length(conn);
54         if (count < 0) {
55                 cmyth_dbg(CMYTH_DBG_ERROR,
56                         "%s: cmyth_rcv_length() failed (%d)\n",
57                         __FUNCTION__, count);
58                 ret = count;
59                 goto out;
60         }
61         if ((r=cmyth_rcv_int64(conn, &err, &ll, count)) < 0) {
62                 cmyth_dbg(CMYTH_DBG_ERROR,
63                         "%s: cmyth_rcv_int64() failed (%d)\n",
64                         __FUNCTION__, r);
65                 ret = err;
66                 goto out;
67         }
68
69         ret = ll;
70    out:
71         pthread_mutex_unlock(&mutex);
72         return ret;
73 }
74         
75 int cmyth_set_bookmark(cmyth_conn_t conn, cmyth_proginfo_t prog, long long bookmark)
76 {
77         char *buf;
78         unsigned int len = CMYTH_TIMESTAMP_LEN + CMYTH_LONGLONG_LEN * 2 + 18;
79         char resultstr[3];
80         int r,err;
81         int ret;
82         int count;
83         char start_ts_dt[CMYTH_TIMESTAMP_LEN + 1];
84         cmyth_datetime_to_string(start_ts_dt, prog->proginfo_rec_start_ts);
85         buf = alloca(len);
86         if (!buf) {
87                 return -ENOMEM;
88         }
89         if (conn->conn_version >= 66) {
90                 /*
91                  * Since protocol 66 mythbackend expects a single 64 bit integer rather than two 32 bit
92                  * hi and lo integers.
93                  */
94                 sprintf(buf, "SET_BOOKMARK %ld %s %"PRIu64, prog->proginfo_chanId,
95                                 start_ts_dt, (int64_t)bookmark);
96         }
97         else {
98                 sprintf(buf, "SET_BOOKMARK %ld %s %d %d", prog->proginfo_chanId,
99                                 start_ts_dt, (int32_t)(bookmark >> 32), (int32_t)(bookmark & 0xffffffff));
100         }
101         pthread_mutex_lock(&mutex);
102         if ((err = cmyth_send_message(conn,buf)) < 0) {
103                 cmyth_dbg(CMYTH_DBG_ERROR,
104                         "%s: cmyth_send_message() failed (%d)\n",
105                         __FUNCTION__, err);
106                 ret = err;
107                 goto out;
108         }
109         count = cmyth_rcv_length(conn);
110         if ((r=cmyth_rcv_string(conn,&err,resultstr,sizeof(resultstr),count)) < 0) {
111                 cmyth_dbg(CMYTH_DBG_ERROR,
112                         "%s: cmyth_rcv_string() failed (%d)\n",
113                         __FUNCTION__, count);
114                 ret = count;
115                 goto out;
116         }
117         ret = (strncmp(resultstr,"OK",2) == 0);
118    out:
119         pthread_mutex_unlock(&mutex);
120         return ret;
121 }