10 |
andreas |
1 |
/*
|
|
|
2 |
* Copyright (C) 2015 by Andreas Theofilu <andreas@theosys.at>
|
|
|
3 |
*
|
|
|
4 |
* All rights reserved. No warranty, explicit or implicit, provided.
|
|
|
5 |
*
|
|
|
6 |
* NOTICE: All information contained herein is, and remains
|
|
|
7 |
* the property of Andreas Theofilu and his suppliers, if any.
|
|
|
8 |
* The intellectual and technical concepts contained
|
|
|
9 |
* herein are proprietary to Andreas Theofilu and its suppliers and
|
|
|
10 |
* may be covered by European and Foreign Patents, patents in process,
|
|
|
11 |
* and are protected by trade secret or copyright law.
|
|
|
12 |
* Dissemination of this information or reproduction of this material
|
|
|
13 |
* is strictly forbidden unless prior written permission is obtained
|
|
|
14 |
* from Andreas Theofilu.
|
|
|
15 |
*/
|
|
|
16 |
#include <stdio.h>
|
|
|
17 |
#include <string.h>
|
|
|
18 |
#include <strings.h>
|
|
|
19 |
#include <unistd.h>
|
|
|
20 |
#include <stdlib.h>
|
|
|
21 |
#include <ctype.h>
|
|
|
22 |
#include <math.h>
|
|
|
23 |
#include <syslog.h>
|
|
|
24 |
#include <errno.h>
|
|
|
25 |
#include <sys/stat.h>
|
|
|
26 |
#include <sys/types.h>
|
|
|
27 |
#include <fcntl.h>
|
|
|
28 |
#include <sqlite3.h>
|
|
|
29 |
#include "config.h"
|
|
|
30 |
#include "helplib.h"
|
|
|
31 |
#include "user.h"
|
|
|
32 |
#include "play.h"
|
|
|
33 |
#include "search.h"
|
|
|
34 |
|
|
|
35 |
int searchTerm(int s1, const char *type, const char *term, int start, int length)
|
|
|
36 |
{
|
|
|
37 |
char query[1024], hv0[128], buffer[8192];
|
|
|
38 |
char fname[256];
|
|
|
39 |
sqlite3 *db;
|
|
|
40 |
int rc, id, total, pos;
|
|
|
41 |
sqlite3_stmt *res;
|
|
|
42 |
char id3_title[256], id3_artist[256], id3_album[256], id3_genre[256];
|
14 |
andreas |
43 |
char *title, *artist, *album;
|
10 |
andreas |
44 |
|
|
|
45 |
strcpy(fname, configs.home);
|
|
|
46 |
strcat(fname, MUSICDB);
|
|
|
47 |
|
|
|
48 |
rc = sqlite3_open(fname, &db);
|
|
|
49 |
|
|
|
50 |
if (rc)
|
|
|
51 |
{
|
|
|
52 |
syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
|
|
|
53 |
strcpy(query, "ERROR:USER:Error opening database;");
|
|
|
54 |
write (s1, query, strlen(query));
|
|
|
55 |
return FALSE;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
strcpy (query, "select id, title, interpret, album, genre from musicdb where ");
|
|
|
59 |
|
|
|
60 |
if (!strcasecmp(type, "TITLE"))
|
|
|
61 |
strcat (query, "title like \"%");
|
|
|
62 |
else if (!strcasecmp(type, "ARTIST"))
|
|
|
63 |
strcat (query, "interpret like \"%");
|
|
|
64 |
else if (!strcasecmp(type, "ALBUM"))
|
|
|
65 |
strcat (query, "album like \"%");
|
|
|
66 |
else if (!strcasecmp(type, "GENRE"))
|
|
|
67 |
strcat (query, "genre like \"%");
|
|
|
68 |
|
|
|
69 |
strcat (query, term);
|
|
|
70 |
strcat (query, "%\"");
|
|
|
71 |
|
|
|
72 |
if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
|
|
|
73 |
{
|
|
|
74 |
syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
|
|
|
75 |
strcpy(query, "ERROR:SEARCH:Error preparing a SQL statement;");
|
|
|
76 |
write(s1, query, strlen(query));
|
|
|
77 |
return FALSE;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
total = sqlite3_column_count(res);
|
|
|
81 |
sprintf(hv0, "TOTAL:%d;", total);
|
|
|
82 |
write (s1, hv0, strlen(hv0));
|
|
|
83 |
pos = 0;
|
|
|
84 |
|
|
|
85 |
while ((rc = sqlite3_step(res)) == SQLITE_ROW)
|
|
|
86 |
{
|
|
|
87 |
if (pos < (start - 1))
|
|
|
88 |
{
|
|
|
89 |
pos++;
|
|
|
90 |
continue;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
if (pos >= ((start - 1) + length))
|
|
|
94 |
break;
|
|
|
95 |
|
|
|
96 |
memset(id3_title, 0, sizeof(id3_title));
|
|
|
97 |
memset(id3_artist, 0, sizeof(id3_artist));
|
|
|
98 |
memset(id3_album, 0, sizeof(id3_album));
|
|
|
99 |
memset(id3_genre, 0, sizeof(id3_genre));
|
|
|
100 |
id = sqlite3_column_int(res, 0);
|
|
|
101 |
strncpy(id3_title, (const char *)sqlite3_column_text(res, 1), sizeof(id3_title));
|
|
|
102 |
strncpy(id3_artist, (const char *)sqlite3_column_text(res, 2), sizeof(id3_artist));
|
|
|
103 |
strncpy(id3_album, (const char *)sqlite3_column_text(res, 3), sizeof(id3_album));
|
|
|
104 |
strncpy(id3_genre, (const char *)sqlite3_column_text(res, 4), sizeof(id3_genre));
|
|
|
105 |
title = urlencode(id3_title);
|
|
|
106 |
artist = urlencode(id3_artist);
|
|
|
107 |
album = urlencode(id3_album);
|
|
|
108 |
|
|
|
109 |
if (title != NULL)
|
|
|
110 |
{
|
|
|
111 |
strncpy(id3_title, title, sizeof(id3_title));
|
|
|
112 |
free(title);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
if (artist != NULL)
|
|
|
116 |
{
|
|
|
117 |
strncpy(id3_artist, artist, sizeof(id3_artist));
|
|
|
118 |
free(artist);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
if (album != NULL)
|
|
|
122 |
{
|
|
|
123 |
strncpy(id3_album, album, sizeof(id3_album));
|
|
|
124 |
free(album);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
sprintf (buffer, "SEARCH:%s:%d:%s:%s:%s:%s;", type, id, id3_title, id3_artist, id3_album, id3_genre);
|
|
|
128 |
write (s1, buffer, strlen(buffer));
|
|
|
129 |
pos++;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
sqlite3_finalize(res);
|
|
|
133 |
sqlite3_close(db);
|
|
|
134 |
return TRUE;
|
|
|
135 |
}
|