'C' reloads configuration.
[shell-fm.git] / source / recommend.c
blobcd88d08bda3586b723ead968774bfbbb7ef27819
1 /*
2 Copyright (C) 2006 by Jonas Kramer
3 Published under the terms of the GNU General Public License (GPL).
4 */
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <ctype.h>
9 #include <assert.h>
11 #include "xmlrpc.h"
12 #include "feeds.h"
13 #include "hash.h"
14 #include "completion.h"
15 #include "readline.h"
16 #include "interface.h"
17 #include "settings.h"
18 #include "getln.h"
20 static char ** users = NULL;
21 static int usercomplete(char *, const unsigned, int);
23 void recommend(struct hash track) {
24 char key, * message = NULL, * recipient = NULL;
25 unsigned result = 0;
27 struct prompt setup = {
28 .prompt = "Recipient: ",
29 .line = NULL,
30 .history = NULL,
31 .callback = usercomplete,
34 struct prompt comment = {
35 .prompt = "Comment: ",
36 .line = NULL,
37 .history = NULL,
38 .callback = NULL,
41 if(!track.content)
42 return;
44 fputs("Recommend (a)rtist, a(l)bum, (t)rack or (c)ancel?\n", stderr);
46 while(!strchr("altc", (key = fetchkey(2))));
48 if(key == 'c')
49 return;
51 users = neighbors(value(& rc, "username"));
52 users = merge(users, friends(value(& rc, "username")), 0);
54 recipient = readline(& setup);
56 purge(users);
57 users = NULL;
59 message = readline(& comment);
61 switch(key) {
62 case 'a':
63 result = xmlrpc(
64 "recommendArtist", "ssss",
65 value(& track, "creator"),
66 recipient, message, "en"
68 break;
70 case 'l':
71 result = xmlrpc(
72 "recommendAlbum", "sssss",
73 value(& track, "creator"),
74 value(& track, "album"),
75 recipient, message, "en"
77 break;
79 case 't':
80 result = xmlrpc(
81 "recommendTrack", "sssss",
82 value(& track, "creator"),
83 value(& track, "title"),
84 recipient, message, "en"
86 break;
89 puts(result ? "Recommended." : "Sorry, failed.");
93 static int usercomplete(char * line, const unsigned max, int changed) {
94 unsigned length, nres = 0;
95 int retval = 0;
96 const char * match = NULL;
98 assert(line != NULL);
99 length = strlen(line);
101 /* Remove spaces at the beginning of the string. */
102 while(isspace(line[0])) {
103 retval = !0;
104 memmove(line, line + 1, strlen(line + 1));
105 line[--length] = 0;
108 /* Remove spaces at the end of the string. */
109 while((length = strlen(line)) > 0 && isspace(line[length - 1])) {
110 retval = !0;
111 line[--length] = 0;
114 if(!users || !users[0])
115 return retval;
117 if((match = nextmatch(users, changed ? line : NULL, & nres)) != NULL) {
118 snprintf(line, max, "%s", match);
119 retval = !0;
122 return retval;