Subversion Repositories mdb

Rev

Rev 9 | Go to most recent revision | Details | 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);
127
}
128
 
129
/*
130
 * This function deletes a whole playlist from a user. The id is the id of the
131
 * playlist in database table USERS.
132
 */
133
int deletePlaylist(int s1, int id)
134
{
135
	char query[1024];
136
	char fname[256];
137
	sqlite3 *db;
138
	char *zErrMsg = 0;
139
	int rc;
140
 
141
	if (id < 0)
142
		return FALSE;
143
 
144
	strcpy(fname, configs.home);
145
	strcat(fname, "/music.db");
146
 
147
	rc = sqlite3_open(fname, &db);
148
 
149
	if (rc)
150
	{
151
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
152
		strcpy(query, "ERROR:DELETE:Error opening database;");
153
		write (s1, query, strlen(query));
154
		return FALSE;
155
	}
156
 
157
	sprintf(query, "delete from playlists where userid = %d;delete from users where id = %d;", id, id);
158
 
159
	if ((rc = sqlite3_exec(db, query, NULL, NULL, &zErrMsg)) != SQLITE_OK)
160
	{
161
		syslog(LOG_WARNING, "SQL error [%s]: %s", query, zErrMsg);
162
		sqlite3_free(zErrMsg);
163
		sqlite3_close(db);
164
		return FALSE;
165
	}
166
 
167
	return TRUE;
168
}
169
 
170
int deletePlaylistEntry(int s1, int id)
171
{
172
	char query[1024];
173
	char fname[256];
174
	sqlite3 *db;
175
	char *zErrMsg = 0;
176
	int rc;
177
 
178
	if (id < 0)
179
		return FALSE;
180
 
181
	strcpy(fname, configs.home);
182
	strcat(fname, "/music.db");
183
 
184
	rc = sqlite3_open(fname, &db);
185
 
186
	if (rc)
187
	{
188
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
189
		strcpy(query, "ERROR:DELETE:Error opening database;");
190
		write (s1, query, strlen(query));
191
		return FALSE;
192
	}
193
 
194
	sprintf(query, "delete from playlists where id = %d", id);
195
 
196
	if ((rc = sqlite3_exec(db, query, NULL, NULL, &zErrMsg)) != SQLITE_OK)
197
	{
198
		syslog(LOG_WARNING, "SQL error [%s]: %s", query, zErrMsg);
199
		sqlite3_free(zErrMsg);
200
		sqlite3_close(db);
201
		return FALSE;
202
	}
203
 
204
	return TRUE;
205
}