Subversion Repositories mdb

Rev

Rev 14 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2015 by Andreas Theofilu <andreas@theosys.at>
 *
 * All rights reserved. No warranty, explicit or implicit, provided.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Andreas Theofilu and his suppliers, if any.
 * The intellectual and technical concepts contained
 * herein are proprietary to Andreas Theofilu and its suppliers and
 * may be covered by European and Foreign Patents, patents in process,
 * and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Andreas Theofilu.
 */
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <syslog.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sqlite3.h>
#include "config.h"
#include "helplib.h"
#include "user.h"
#include "play.h"
#include "search.h"

int searchTerm(int s1, const char *type, const char *term, int start, int length)
{
        char query[1024], field[128], hv0[128], buffer[8192];
        char fname[256];
        sqlite3 *db;
        int rc, id, total, pos, line;
        sqlite3_stmt *res;
        char id3_title[256], id3_artist[256], id3_album[256], id3_genre[256];
        char *title, *artist, *album;

        strcpy(fname, configs.home);
        strcat(fname, MUSICDB);
        
        rc = sqlite3_open(fname, &db);
        
        if (rc)
        {
                syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
                strcpy(query, "ERROR:USER:Error opening database;");
                write (s1, query, strlen(query));
                return FALSE;
        }

        memset(field, 0, sizeof(field));

        if (!strcasecmp(type, "TITLE"))
                sprintf(field, "title like \"%%%s%%\"", term);
        else if (!strcasecmp(type, "ARTIST"))
                sprintf(field, "interpret like \"%%%s%%\"", term);
        else if (!strcasecmp(type, "ALBUM"))
                sprintf(field, "album like \"%%%s%%\"", term);
        else if (!strcasecmp(type, "GENRE"))
                sprintf(field, "genre like \"%%%s%%\"", term);

        strcpy (query, "select count(*) from musicdb where ");
        strcat (query, field);

        if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
        {
                syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
                strcpy(query, "ERROR:SEARCH:Error preparing a SQL statement;");
                write(s1, query, strlen(query));
                return FALSE;
        }
        
        if ((rc = sqlite3_step(res)) == SQLITE_ROW)
        {
                total = sqlite3_column_int(res, 0);
                sprintf(hv0, "TOTAL:%d;", total);
                write (s1, hv0, strlen(hv0));
        }
        else
                total = 0;

        strcpy (query, "select id, title, interpret, album, genre from musicdb where ");
        strcat (query, field);

        if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
        {
                syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
                strcpy(query, "ERROR:SEARCH:Error preparing a SQL statement;");
                write(s1, query, strlen(query));
                return FALSE;
        }

        pos = 0;
        line = 1;

        while ((rc = sqlite3_step(res)) == SQLITE_ROW)
        {
                if (pos < (start - 1))
                {
                        pos++;
                        continue;
                }
                
                if (pos >= ((start - 1) + length))
                        break;
                
                memset(id3_title, 0, sizeof(id3_title));
                memset(id3_artist, 0, sizeof(id3_artist));
                memset(id3_album, 0, sizeof(id3_album));
                memset(id3_genre, 0, sizeof(id3_genre));
                id = sqlite3_column_int(res, 0);
                strncpy(id3_title, (const char *)sqlite3_column_text(res, 1), sizeof(id3_title));
                strncpy(id3_artist, (const char *)sqlite3_column_text(res, 2), sizeof(id3_artist));
                strncpy(id3_album, (const char *)sqlite3_column_text(res, 3), sizeof(id3_album));
                strncpy(id3_genre, (const char *)sqlite3_column_text(res, 4), sizeof(id3_genre));
                title = urlencode(id3_title);
                artist = urlencode(id3_artist);
                album = urlencode(id3_album);

                if (title != NULL)
                {
                        strncpy(id3_title, title, sizeof(id3_title));
                        free(title);
                }

                if (artist != NULL)
                {
                        strncpy(id3_artist, artist, sizeof(id3_artist));
                        free(artist);
                }

                if (album != NULL)
                {
                        strncpy(id3_album, album, sizeof(id3_album));
                        free(album);
                }

                sprintf (buffer, "SEARCH:%s:%d:%d:%s:%s:%s:%s;", type, id, line, id3_title, id3_artist, id3_album, id3_genre);
                write (s1, buffer, strlen(buffer));
                pos++;
                line++;
        }
        
        sqlite3_finalize(res);
        sqlite3_close(db);
        return TRUE;
}