Subversion Repositories mdb

Rev

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