aboutsummaryrefslogtreecommitdiff
path: root/lib/cmyth/libcmyth/proglist.c
blob: b0a482070291ec7ff0f8e9c8cdadafefd8fcf1dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
 *  Copyright (C) 2004-2010, Eric Lund
 *  http://www.mvpmc.org/
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * proglist.c - functions to manage MythTV timestamps.  Primarily,
 *               these allocate timestamps and convert between string
 *               and cmyth_proglist_t and between long long and
 *               cmyth_proglist_t.
 */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <cmyth_local.h>

/*
 * cmyth_proglist_destroy(void)
 * 
 * Scope: PRIVATE (static)
 *
 * Description
 *
 * Destroy and free a timestamp structure.  This should only be called
 * by ref_release().  All others should use
 * ref_release() to release references to time stamps.
 *
 * Return Value:
 *
 * None.
 */
static void
cmyth_proglist_destroy(cmyth_proglist_t pl)
{
	int i;

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
	if (!pl) {
		return;
	}
	for (i  = 0; i < pl->proglist_count; ++i) {
		if (pl->proglist_list[i]) {
			ref_release(pl->proglist_list[i]);
		}
		pl->proglist_list[i] = NULL;
	}
	if (pl->proglist_list) {
		free(pl->proglist_list);
	}
}

/*
 * cmyth_proglist_create(void)
 * 
 * Scope: PUBLIC
 *
 * Description
 *
 * Create a timestamp structure and return a pointer to the structure.
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_proglist_t (this type is a pointer)
 *
 * Failure: A NULL cmyth_proglist_t
 */
cmyth_proglist_t
cmyth_proglist_create(void)
{
	cmyth_proglist_t ret;

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
	ret = ref_alloc(sizeof(*ret));
	if (!ret) {
		return(NULL);
	}
	ref_set_destroy(ret, (ref_destroy_t)cmyth_proglist_destroy);

	ret->proglist_list = NULL;
	ret->proglist_count = 0;
	return ret;
}

/*
 * cmyth_proglist_get_item(cmyth_proglist_t pl, int index)
 *
 * Scope: PUBLIC
 *
 * Description:
 *
 * Retrieve the program information structure found at index 'index'
 * in the list in 'pl'.  Return the program information structure
 * held.  Before forgetting the reference to this program info structure
 * the caller must call ref_release().
 *
 * Return Value:
 *
 * Success: A non-null cmyth_proginfo_t (this is a pointer type)
 *
 * Failure: A NULL cmyth_proginfo_t
 */
cmyth_proginfo_t
cmyth_proglist_get_item(cmyth_proglist_t pl, int index)
{
	if (!pl) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL program list\n",
			  __FUNCTION__);
		return NULL;
	}
	if (!pl->proglist_list) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL list\n",
			  __FUNCTION__);
		return NULL;
	}
	if ((index < 0) || (index >= pl->proglist_count)) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: index %d out of range\n",
			  __FUNCTION__, index);
		return NULL;
	}
	ref_hold(pl->proglist_list[index]);
	return pl->proglist_list[index];
}

int
cmyth_proglist_delete_item(cmyth_proglist_t pl, cmyth_proginfo_t prog)
{
	int i;
	cmyth_proginfo_t old;
	int ret = -EINVAL;

	if (!pl) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL program list\n",
			  __FUNCTION__);
		return -EINVAL;
	}
	if (!prog) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL program item\n",
			  __FUNCTION__);
		return -EINVAL;
	}

	pthread_mutex_lock(&mutex);

	for (i=0; i<pl->proglist_count; i++) {
		if (cmyth_proginfo_compare(prog, pl->proglist_list[i]) == 0) {
			old = pl->proglist_list[i];
			memmove(pl->proglist_list+i,
				pl->proglist_list+i+1,
				(pl->proglist_count-i-1)*sizeof(cmyth_proginfo_t));
			pl->proglist_count--;
			ref_release(old);
			ret = 0;
			goto out;
		}
	}

 out:
	pthread_mutex_unlock(&mutex);

	return ret;
}

/*
 * cmyth_proglist_get_count(cmyth_proglist_t pl)
 *
 * Scope: PUBLIC
 *
 * Description:
 *
 * Retrieve the number of elements in the program information
 * structure in 'pl'.
 *
 * Return Value:
 *
 * Success: A number >= 0 indicating the number of items in 'pl'
 *
 * Failure: -errno
 */
int
cmyth_proglist_get_count(cmyth_proglist_t pl)
{
	if (!pl) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL program list\n",
			  __FUNCTION__);
		return -EINVAL;
	}
	return pl->proglist_count;
}

/*
 * cmyth_proglist_get_list(cmyth_conn_t conn,
 *                         cmyth_proglist_t proglist,
 *                         char *msg, char *func)
 * 
 * Scope: PRIVATE (static)
 *
 * Description
 *
 * Obtain a program list from the query specified in 'msg' from the
 * function 'func'.  Make the query on 'conn' and put the results in
 * 'proglist'.
 *
 * Return Value:
 *
 * Success: 0
 *
 * Failure: -(ERRNO)
 */
static int
cmyth_proglist_get_list(cmyth_conn_t conn,
			cmyth_proglist_t proglist,
			char *msg, const char *func)
{
	int err = 0;
	int count;
	int ret;

	if (!conn) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: no connection\n", func);
		return -EINVAL;
	}
	if (!proglist) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: no program list\n", func);
		return -EINVAL;
	}

	pthread_mutex_lock(&mutex);

	if ((err = cmyth_send_message(conn, msg)) < 0) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_send_message() failed (%d)\n",
			  func, err);
		ret = err;
		goto out;
	}
	count = cmyth_rcv_length(conn);
	if (count < 0) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_rcv_length() failed (%d)\n",
			  func, count);
		ret = count;
		goto out;
	}
	if (strcmp(msg, "QUERY_GETALLPENDING") == 0) {
		long c;
		int r;
		if ((r=cmyth_rcv_long(conn, &err, &c, count)) < 0) {
			cmyth_dbg(CMYTH_DBG_ERROR,
				  "%s: cmyth_rcv_length() failed (%d)\n",
				  __FUNCTION__, r);
			ret = err;
			goto out;
		}
		count -= r;
	}
	if (cmyth_rcv_proglist(conn, &err, proglist, count) != count) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_rcv_proglist() < count\n",
			  func);
	}
	if (err) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_rcv_proglist() failed (%d)\n",
			  func, err);
		ret = -1 * err;
		goto out;
	}

	ret = 0;

    out:
	pthread_mutex_unlock(&mutex);

	return ret;
}

/*
 * cmyth_proglist_get_all_recorded(cmyth_conn_t control,
 *                                 cmyth_proglist_t *proglist)
 * 
 * Scope: PUBLIC
 *
 * Description
 *
 * Make a request on the control connection 'control' to obtain a list
 * of completed or in-progress recordings.  Build a list of program
 * information structures and put a malloc'ed pointer to the list (an
 * array of pointers) in proglist.
 *
 * Return Value:
 *
 * Success: A held, noon-NULL cmyth_proglist_t
 *
 * Failure: NULL
 */
cmyth_proglist_t
cmyth_proglist_get_all_recorded(cmyth_conn_t control)
{
	char query[32];
	cmyth_proglist_t proglist = cmyth_proglist_create();

	if (proglist == NULL) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_proglist_create() failed\n",
			  __FUNCTION__);
		return NULL;
	}

	if (control->conn_version < 65) {
		strcpy(query, "QUERY_RECORDINGS Play");
	}
	else {
		strcpy(query, "QUERY_RECORDINGS Ascending");
	}
	if (cmyth_proglist_get_list(control, proglist,
				    query,
				    __FUNCTION__) < 0) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_proglist_get_list() failed\n",
			  __FUNCTION__);
		ref_release(proglist);
		return NULL;
	}
	return proglist;
}

/*
 * cmyth_proglist_get_all_pending(cmyth_conn_t control,
 *                                cmyth_proglist_t *proglist)
 * 
 * Scope: PUBLIC
 *
 * Description
 *
 * Make a request on the control connection 'control' to obtain a list
 * of pending recordings.  Build a list of program information
 * structures and put a malloc'ed pointer to the list (an array of
 * pointers) in proglist.
 *
 * Return Value:
 *
 * Success: A held, noon-NULL cmyth_proglist_t
 *
 * Failure: NULL
 */
cmyth_proglist_t
cmyth_proglist_get_all_pending(cmyth_conn_t control)
{
	cmyth_proglist_t proglist = cmyth_proglist_create();

	if (proglist == NULL) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_proglist_create() failed\n",
			  __FUNCTION__);
		return NULL;
	}

	if (cmyth_proglist_get_list(control, proglist,
				    "QUERY_GETALLPENDING",
				    __FUNCTION__) < 0) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_proglist_get_list() failed\n",
			  __FUNCTION__);
		ref_release(proglist);
		return NULL;
	}
	return proglist;
}

/*
 * cmyth_proglist_get_all_scheduled(cmyth_conn_t control,
 *                                  cmyth_proglist_t *proglist)
 * 
 * Scope: PUBLIC
 *
 * Description
 *
 * Make a request on the control connection 'control' to obtain a list
 * of scheduled recordings.  Build a list of program information
 * structures and put a malloc'ed pointer to the list (an array of
 * pointers) in proglist.
 *
 * Return Value:
 *
 * Success: A held, noon-NULL cmyth_proglist_t
 *
 * Failure: NULL
 */
cmyth_proglist_t
cmyth_proglist_get_all_scheduled(cmyth_conn_t control)
{
	cmyth_proglist_t proglist = cmyth_proglist_create();

	if (proglist == NULL) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_proglist_create() failed\n",
			  __FUNCTION__);
		return NULL;
	}

	if (cmyth_proglist_get_list(control, proglist,
				    "QUERY_GETALLSCHEDULED",
				    __FUNCTION__) < 0) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_proglist_get_list() failed\n",
			  __FUNCTION__);
		ref_release(proglist);
		return NULL;
	}
	return proglist;
}

/*
 * cmyth_proglist_get_conflicting(cmyth_conn_t control,
 *                                cmyth_proglist_t *proglist)
 * 
 * Scope: PUBLIC
 *
 * Description
 *
 * Make a request on the control connection 'control' to obtain a list
 * of conflicting recordings.  Build a list of program information
 * structures and put a malloc'ed pointer to the list (an array of
 * pointers) in proglist.
 *
 * Return Value:
 *
 * Success: A held, noon-NULL cmyth_proglist_t
 *
 * Failure: NULL
 */
cmyth_proglist_t
cmyth_proglist_get_conflicting(cmyth_conn_t control)
{
	cmyth_proglist_t proglist = cmyth_proglist_create();

	if (proglist == NULL) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_proglist_create() failed\n",
			  __FUNCTION__);
		return NULL;
	}

	if (cmyth_proglist_get_list(control, proglist,
				    "QUERY_GETCONFLICTING",
				    __FUNCTION__) < 0) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_proglist_get_list() failed\n",
			  __FUNCTION__);
		ref_release(proglist);
		return NULL;
	}
	return proglist;
}

/*
 * sort_timestamp(const void *a, const void *b)
 *
 * Scope: PRIVATE
 *
 * Description
 *
 * Return an integer value to specify the relative position of the timestamp
 * This is a helper function for the sort function called by qsort.  It will
 * sort any of the timetstamps for the qsort functions 
 *
 * Return Value:
 *
 * Same Date/Time: 0
 * Date a > b: 1
 * Date a < b: -1
 *
 */
static int sort_timestamp(cmyth_timestamp_t X, cmyth_timestamp_t Y)
{

	if (X->timestamp_year > Y->timestamp_year)
		return 1;
	else if (X->timestamp_year < Y->timestamp_year)
		return -1;
	else /* X->timestamp_year == Y->timestamp_year */
	{
		if (X->timestamp_month > Y->timestamp_month)
			return 1;
		else if (X->timestamp_month < Y->timestamp_month)
			return -1;
		else /* X->timestamp_month == Y->timestamp_month */
		{
			if (X->timestamp_day > Y->timestamp_day)
				return 1;
			else if (X->timestamp_day < Y->timestamp_day)
				return -1;
			else /* X->timestamp_day == Y->timestamp_day */ 
			{
				if (X->timestamp_hour > Y->timestamp_hour)
					return 1;
				else if (X->timestamp_hour < Y->timestamp_hour)
					return -1;
				else /* X->timestamp_hour == Y->timestamp_hour */
				{
					if (X->timestamp_minute > Y->timestamp_minute)
						return 1;
					else if (X->timestamp_minute < Y->timestamp_minute)
						return -1;
					else /* X->timestamp_minute == Y->timestamp_minute */
					{
						if (X->timestamp_second > Y->timestamp_second)
							return 1;
						else if (X->timestamp_second < Y->timestamp_second)
							return -1;
						else /* X->timestamp_second == Y->timestamp_second */
							return 0;
					}
				}
			}
		}
	}
}

/*
 * recorded_compare(const void *a, const void *b)
 *
 * Scope: PRIVATE
 *
 * Description
 *
 * Return an integer value to a qsort function to specify the relative
 * position of the recorded date 
 *
 * Return Value:
 * 
 * Same Day: 0
 * Date a > b: 1
 * Date a < b: -1
 *
 */
static int
recorded_compare(const void *a, const void *b)
{
	const cmyth_proginfo_t x = *(cmyth_proginfo_t *)a;
	const cmyth_proginfo_t y = *(cmyth_proginfo_t *)b;
	cmyth_timestamp_t X = x->proginfo_rec_start_ts;
	cmyth_timestamp_t Y = y->proginfo_rec_start_ts;

	return sort_timestamp(X, Y);
}

/*
 * airdate_compare(const void *a, const void *b)
 *
 * Scope: PRIVATE
 *
 * Description
 *
 * Return an integer value to a qsort function to specify the relative
 * position of the original airdate
 *
 * Return Value:
 * 
 * Same Day: 0
 * Date a > b: 1
 * Date a < b: -1
 *
 */
static int
airdate_compare(const void *a, const void *b)
{
	const cmyth_proginfo_t x = *(cmyth_proginfo_t *)a;
	const cmyth_proginfo_t y = *(cmyth_proginfo_t *)b;
	const cmyth_timestamp_t X = x->proginfo_originalairdate;
	const cmyth_timestamp_t Y = y->proginfo_originalairdate;

	return sort_timestamp(X, Y);
}

/*
 * cmyth_proglist_sort(cmyth_proglist_t pl, int count, int sort)
 *
 * Scope: PUBLIC
 *
 * Description
 *
 * Sort the epispde list by mythtv_sort setting. Check to ensure that the 
 * program list is not null and pass the proglist_list to the qsort function
 *
 * Return Value:
 * 
 * Success = 0
 * Failure = -1
 */ 
int 
cmyth_proglist_sort(cmyth_proglist_t pl, int count, cmyth_proglist_sort_t sort)
{
        if (!pl) {
                cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL program list\n",
                          __FUNCTION__);
                return -1;
        }
        if (!pl->proglist_list) {
                cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL list\n",
                          __FUNCTION__);
                return -1;
        }

	cmyth_dbg(CMYTH_DBG_ERROR,
                          "cmyth_proglist_sort\n");

	switch (sort) {
		case MYTHTV_SORT_DATE_RECORDED:	/* Default Date Recorded */
			qsort((cmyth_proginfo_t)pl->proglist_list, count, sizeof(pl->proglist_list) , recorded_compare);
			break;
		case MYTHTV_SORT_ORIGINAL_AIRDATE: /*Default Date Recorded */
			qsort((cmyth_proginfo_t)pl->proglist_list, count, sizeof(pl->proglist_list) , airdate_compare);
			break;
		default: 
			printf("Unsupported MythTV sort type\n");
	}

	cmyth_dbg(CMYTH_DBG_ERROR,
                          "end cmyth_proglist_sort\n");
	return 0;
}