Subversion Repositories mdb

Rev

Rev 29 | 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"

int deleteQueue(int s1, int id)
{
        char fname[256], tmpname[256], hv0[256], buffer[8192];
        int fd, tmpfd;

        strcpy(fname, configs.home);
        strcat(fname, NOWPLAY);
        strcpy(tmpname, configs.home);
        strcat(tmpname, "/newqueue");

        /* If the id is less than 0, we delete the whole queue. If the audio is
         * playing, it is stopped before. */
        if (id < 0)
        {
                if (playerActive)
                        nextCommand = PLAY_STOP_NOW;

                unlink(fname);
                freeQueue();
                return TRUE;
        }

        if ((fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP)) <= 0)
        {
                syslog(LOG_WARNING, "Error opening file %s: %s", fname, strerror(errno));
                sprintf(hv0, "ERROR:DELETE:Error opening queue");
                write(s1, hv0, strlen(hv0));
                return FALSE;
        }

        if ((tmpfd = open(tmpname, O_RDWR | O_TRUNC | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) <= 0)
        {
                syslog(LOG_WARNING, "Error creating a temporary file: %s", strerror(errno));
                close(fd);
                sprintf(hv0, "ERROR:DELETE:Error creating a temporary file");
                write(s1, hv0, strlen(hv0));
                return FALSE;
        }

        while (readLine(fd, &buffer[0], sizeof(buffer)) != NULL)
        {
                char *t, *buf;
                char path[512], id3_title[256], id3_artist[256], id3_album[256], id3_genre[256];
                int x, type, pid;

                memset(path, 0, sizeof(path));
                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));

                if ((buf = strdup(buffer)) == NULL)
                {
                        syslog(LOG_DAEMON, "Error duplicating a string: %s", strerror(errno));
                        close(fd);
                        close(tmpfd);
                        return FALSE;
                }

                x = 0;
                t = strtok(buffer, "\t");

                while (t)
                {
                        switch(x)
                        {
                                case 0: strncpy(path, t, sizeof(path)-1); break;
                                case 1: type = atoi(t); break;
                                case 2: pid = atoi(t); break;
                                case 3: strncpy(id3_title, t, sizeof(id3_title) - 1); break;
                                case 4: strncpy(id3_artist, t, sizeof(id3_artist) - 1); break;
                                case 5: strncpy(id3_album, t, sizeof(id3_album) - 1); break;
                                case 6: strncpy(id3_genre, t, sizeof(id3_genre) - 1); break;
                        }

                        x++;
                        t = strtok(NULL, "\t");
                }

                if (id == pid)
                {
                        free (buf);
                        continue;
                }
                
                /* Write the content into the temporary file */
                write (tmpfd, buf, strlen(buf));
                strcpy (hv0, "\n");
                write (tmpfd, hv0, strlen(hv0));
                free (buf);
        }
        
        close (fd);
        unlink(fname);
        rename(tmpname, fname);
        readQueue();
        return TRUE;
}

/*
 * This function deletes a whole playlist from a user. The id is the id of the
 * playlist in database table USERS.
 */
int deletePlaylist(int s1, int id)
{
        char query[1024];
        char fname[256];
        sqlite3 *db;
        char *zErrMsg = 0;
        int rc;

        if (id < 0)
                return FALSE;

        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:DELETE:Error opening database;");
                write (s1, query, strlen(query));
                return FALSE;
        }

        sprintf(query, "delete from playlists where userid = %d;delete from users where id = %d;", id, id);

        if ((rc = sqlite3_exec(db, query, NULL, NULL, &zErrMsg)) != SQLITE_OK)
        {
                syslog(LOG_WARNING, "SQL error [%s]: %s", query, zErrMsg);
                sqlite3_free(zErrMsg);
                sqlite3_close(db);
                return FALSE;
        }

        sqlite3_close(db);
        scanUser(s1, userchain->uname);
        return TRUE;
}

int deletePlaylistEntry(int s1, int id)
{
        char query[1024];
        char fname[256];
        sqlite3 *db;
        char *zErrMsg = 0;
        int rc;
        
        if (id < 0)
                return FALSE;
        
        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:DELETE:Error opening database;");
                write (s1, query, strlen(query));
                return FALSE;
        }
        
        sprintf(query, "delete from playlists where id = %d", id);
        
        if ((rc = sqlite3_exec(db, query, NULL, NULL, &zErrMsg)) != SQLITE_OK)
        {
                syslog(LOG_WARNING, "SQL error [%s]: %s", query, zErrMsg);
                sqlite3_free(zErrMsg);
                sqlite3_close(db);
                return FALSE;
        }

        sqlite3_close(db);
        return TRUE;
}