File: recommend.c

package info (click to toggle)
shell-fm 0.7%2Bgit20100414-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 444 kB
  • ctags: 305
  • sloc: ansic: 4,422; makefile: 135; python: 80; haskell: 76; sh: 67; perl: 19
file content (123 lines) | stat: -rw-r--r-- 2,417 bytes parent folder | download | duplicates (2)
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
/*
	Copyright (C) 2006 by Jonas Kramer
	Published under the terms of the GNU General Public License (GPL).
*/

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <assert.h>

#include "xmlrpc.h"
#include "feeds.h"
#include "hash.h"
#include "completion.h"
#include "readline.h"
#include "interface.h"
#include "settings.h"
#include "getln.h"

static char ** users = NULL;
static int usercomplete(char *, const unsigned, int);

void recommend(struct hash track) {
	char key, * message = NULL, * recipient = NULL;
	unsigned result = 0;

	struct prompt setup = {
		.prompt = "Recipient: ",
		.line = NULL,
		.history = NULL,
		.callback = usercomplete,
	};

	struct prompt comment = {
		.prompt = "Comment: ",
		.line = NULL,
		.history = NULL,
		.callback = NULL,
	};

	if(!track.content)
		return;

	fputs("Recommend (a)rtist, a(l)bum, (t)rack or (c)ancel?\n", stderr);

	while(!strchr("altc", (key = fetchkey(2))));

	if(key == 'c')
		return;

	users = neighbors(value(& rc, "username"));
	users = merge(users, friends(value(& rc, "username")), 0);

	recipient = readline(& setup);

	purge(users);
	users = NULL;

	message = readline(& comment);

	switch(key) {
		case 'a':
			result = xmlrpc(
				"recommendArtist", "ssss",
				value(& track, "creator"),
				recipient, message, "en"
			);
			break;

		case 'l':
			result = xmlrpc(
				"recommendAlbum", "sssss",
				value(& track, "creator"),
				value(& track, "album"),
				recipient, message, "en"
			);
			break;

		case 't':
			result = xmlrpc(
				"recommendTrack", "sssss",
				value(& track, "creator"),
				value(& track, "title"),
				recipient, message, "en"
			);
			break;
	}

	puts(result ? "Recommended." : "Sorry, failed.");
}


static int usercomplete(char * line, const unsigned max, int changed) {
	unsigned length, nres = 0;
	int retval = 0;
	const char * match = NULL;

	assert(line != NULL);
	length = strlen(line);

	/* Remove spaces at the beginning of the string. */
	while(isspace(line[0])) {
		retval = !0;
		memmove(line, line + 1, strlen(line + 1));
		line[--length] = 0;
	}

	/* Remove spaces at the end of the string. */
	while((length = strlen(line)) > 0 && isspace(line[length - 1])) {
		retval = !0;
		line[--length] = 0;
	}

	if(!users || !users[0])
		return retval;

	if((match = nextmatch(users, changed ? line : NULL, & nres)) != NULL) {
		snprintf(line, max, "%s", match);
		retval = !0;
	}

	return retval;
}