Subversion Repositories mdb

Rev

Rev 14 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8 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
 
34
int deleteQueue(int s1, int id)
35
{
36
	char fname[256], tmpname[256], hv0[256], buffer[8192];
37
	int fd, tmpfd;
38
 
39
	strcpy(fname, configs.home);
40
	strcat(fname, NOWPLAY);
41
	strcpy(tmpname, configs.home);
42
	strcat(tmpname, "/newqueue");
43
 
29 andreas 44
	/* If the id is less than 0, we delete the whole queue. If the audio is
8 andreas 45
	 * playing, it is stopped before. */
46
	if (id < 0)
47
	{
48
		if (playerActive)
49
			nextCommand = PLAY_STOP_NOW;
50
 
51
		unlink(fname);
29 andreas 52
		freeQueue();
8 andreas 53
		return TRUE;
54
	}
55
 
29 andreas 56
	if ((fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP)) <= 0)
8 andreas 57
	{
58
		syslog(LOG_WARNING, "Error opening file %s: %s", fname, strerror(errno));
59
		sprintf(hv0, "ERROR:DELETE:Error opening queue");
60
		write(s1, hv0, strlen(hv0));
61
		return FALSE;
62
	}
63
 
64
	if ((tmpfd = open(tmpname, O_RDWR | O_TRUNC | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) <= 0)
65
	{
66
		syslog(LOG_WARNING, "Error creating a temporary file: %s", strerror(errno));
67
		close(fd);
68
		sprintf(hv0, "ERROR:DELETE:Error creating a temporary file");
69
		write(s1, hv0, strlen(hv0));
70
		return FALSE;
71
	}
72
 
73
	while (readLine(fd, &buffer[0], sizeof(buffer)) != NULL)
74
	{
75
		char *t, *buf;
76
		char path[512], id3_title[256], id3_artist[256], id3_album[256], id3_genre[256];
77
		int x, pid;
78
 
79
		memset(path, 0, sizeof(path));
80
		memset(id3_title, 0, sizeof(id3_title));
81
		memset(id3_artist, 0, sizeof(id3_artist));
82
		memset(id3_album, 0, sizeof(id3_album));
83
		memset(id3_genre, 0, sizeof(id3_genre));
84
 
85
		if ((buf = strdup(buffer)) == NULL)
86
		{
87
			syslog(LOG_DAEMON, "Error duplicating a string: %s", strerror(errno));
88
			close(fd);
89
			close(tmpfd);
90
			return FALSE;
91
		}
92
 
93
		x = 0;
94
		t = strtok(buffer, "\t");
95
 
96
		while (t)
97
		{
98
			switch(x)
99
			{
100
				case 0: strncpy(path, t, sizeof(path)-1); break;
101
				case 1: pid = atoi(t); break;
102
				case 2: strncpy(id3_title, t, sizeof(id3_title) - 1); break;
103
				case 3: strncpy(id3_artist, t, sizeof(id3_artist) - 1); break;
104
				case 4: strncpy(id3_album, t, sizeof(id3_album) - 1); break;
105
				case 5: strncpy(id3_genre, t, sizeof(id3_genre) - 1); break;
106
			}
107
 
108
			x++;
109
			t = strtok(NULL, "\t");
110
		}
111
 
112
		if (id == pid)
113
		{
114
			free (buf);
115
			continue;
116
		}
117
 
118
		/* Write the content into the temporary file */
119
		write (tmpfd, buf, strlen(buf));
120
		strcpy (hv0, "\n");
121
		write (tmpfd, hv0, strlen(hv0));
122
		free (buf);
123
	}
124
 
125
	close (fd);
126
	unlink(fname);
127
	rename(tmpname, fname);
29 andreas 128
	readQueue();
14 andreas 129
	return TRUE;
8 andreas 130
}
131
 
132
/*
133
 * This function deletes a whole playlist from a user. The id is the id of the
134
 * playlist in database table USERS.
135
 */
136
int deletePlaylist(int s1, int id)
137
{
138
	char query[1024];
139
	char fname[256];
140
	sqlite3 *db;
141
	char *zErrMsg = 0;
142
	int rc;
143
 
144
	if (id < 0)
145
		return FALSE;
146
 
147
	strcpy(fname, configs.home);
9 andreas 148
	strcat(fname, MUSICDB);
8 andreas 149
 
150
	rc = sqlite3_open(fname, &db);
151
 
152
	if (rc)
153
	{
154
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
155
		strcpy(query, "ERROR:DELETE:Error opening database;");
156
		write (s1, query, strlen(query));
157
		return FALSE;
158
	}
159
 
160
	sprintf(query, "delete from playlists where userid = %d;delete from users where id = %d;", id, id);
161
 
162
	if ((rc = sqlite3_exec(db, query, NULL, NULL, &zErrMsg)) != SQLITE_OK)
163
	{
164
		syslog(LOG_WARNING, "SQL error [%s]: %s", query, zErrMsg);
165
		sqlite3_free(zErrMsg);
166
		sqlite3_close(db);
167
		return FALSE;
168
	}
169
 
29 andreas 170
	sqlite3_close(db);
171
	scanUser(s1, userchain->uname);
8 andreas 172
	return TRUE;
173
}
174
 
175
int deletePlaylistEntry(int s1, int id)
176
{
177
	char query[1024];
178
	char fname[256];
179
	sqlite3 *db;
180
	char *zErrMsg = 0;
181
	int rc;
182
 
183
	if (id < 0)
184
		return FALSE;
185
 
186
	strcpy(fname, configs.home);
9 andreas 187
	strcat(fname, MUSICDB);
8 andreas 188
 
189
	rc = sqlite3_open(fname, &db);
190
 
191
	if (rc)
192
	{
193
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
194
		strcpy(query, "ERROR:DELETE:Error opening database;");
195
		write (s1, query, strlen(query));
196
		return FALSE;
197
	}
198
 
199
	sprintf(query, "delete from playlists where id = %d", id);
200
 
201
	if ((rc = sqlite3_exec(db, query, NULL, NULL, &zErrMsg)) != SQLITE_OK)
202
	{
203
		syslog(LOG_WARNING, "SQL error [%s]: %s", query, zErrMsg);
204
		sqlite3_free(zErrMsg);
205
		sqlite3_close(db);
206
		return FALSE;
207
	}
208
 
29 andreas 209
	sqlite3_close(db);
8 andreas 210
	return TRUE;
211
}