-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.c
74 lines (62 loc) · 2.18 KB
/
search.c
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
#include "search.h"
#include <mpd/client.h>
#include <stdio.h>
#include "constants.h"
#include "log.h"
#include "util.h"
void tab_complete_list(struct mpd_connection *connection) {
/* Display all artists, albums, and titles for tab completion when adding */
long unsigned int i;
enum mpd_tag_type mpd_tags[3] = {MPD_TAG_ARTIST, MPD_TAG_ALBUM, MPD_TAG_TITLE};
for (i = 0; i < sizeof(mpd_tags) / sizeof(mpd_tags[0]); ++i) {
tag_search(connection, mpd_tags[i]);
}
}
void tag_search(struct mpd_connection *connection, enum mpd_tag_type tag) {
struct mpd_pair *pair;
mpd_search_db_tags(connection, tag);
mpd_search_commit(connection);
while ((pair = mpd_recv_pair_tag(connection, tag)) != NULL) {
mpd_check_error(connection, NULL, NULL);
if (pair->value) {
printf("%s\n", pair->value);
mpd_return_pair(connection, pair);
}
}
#ifdef DEBUG
log_info("Searched for tag %d", tag);
#endif
}
void search_all_tags(struct mpd_connection *connection, char *query, bool play) {
int i;
struct mpd_song *song;
if (play) {
mpd_search_add_db_songs(connection, false);
} else {
mpd_search_db_songs(connection, false);
}
mpd_check_error(connection, NULL, NULL);
mpd_search_add_any_tag_constraint(connection, MPD_OPERATOR_DEFAULT, query);
mpd_check_error(connection, NULL, NULL);
mpd_search_commit(connection);
mpd_check_error(connection, NULL, NULL);
if (play) { /* actually play the songs */
if (!mpd_response_finish(connection)) {
mpd_check_error(connection, NULL, NULL);
}
} else { /* print the songs */
i = 0;
while ((song = mpd_recv_song(connection)) != NULL) {
mpd_check_error(connection, NULL, song);
const char *artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
const char *title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
printf("%d - %s - %s\n", i, artist ? artist : NULL_STRING,
title ? title : NULL_STRING);
mpd_song_free(song);
i++;
}
}
#ifdef DEBUG
log_info("Searched all tags for query %s", query);
#endif
}