Subversion Repositories mdb

Rev

Rev 29 | 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];
47 andreas 77
		int x, type, pid;
8 andreas 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;
47 andreas 101
				case 1: type = atoi(t); break;
102
				case 2: pid = atoi(t); break;
103
				case 3: strncpy(id3_title, t, sizeof(id3_title) - 1); break;
104
				case 4: strncpy(id3_artist, t, sizeof(id3_artist) - 1); break;
105
				case 5: strncpy(id3_album, t, sizeof(id3_album) - 1); break;
106
				case 6: strncpy(id3_genre, t, sizeof(id3_genre) - 1); break;
8 andreas 107
			}
108
 
109
			x++;
110
			t = strtok(NULL, "\t");
111
		}
112
 
113
		if (id == pid)
114
		{
115
			free (buf);
116
			continue;
117
		}
118
 
119
		/* Write the content into the temporary file */
120
		write (tmpfd, buf, strlen(buf));
121
		strcpy (hv0, "\n");
122
		write (tmpfd, hv0, strlen(hv0));
123
		free (buf);
124
	}
125
 
126
	close (fd);
127
	unlink(fname);
128
	rename(tmpname, fname);
29 andreas 129
	readQueue();
14 andreas 130
	return TRUE;
8 andreas 131
}
132
 
133
/*
134
 * This function deletes a whole playlist from a user. The id is the id of the
135
 * playlist in database table USERS.
136
 */
137
int deletePlaylist(int s1, int id)
138
{
139
	char query[1024];
140
	char fname[256];
141
	sqlite3 *db;
142
	char *zErrMsg = 0;
143
	int rc;
144
 
145
	if (id < 0)
146
		return FALSE;
147
 
148
	strcpy(fname, configs.home);
9 andreas 149
	strcat(fname, MUSICDB);
8 andreas 150
 
151
	rc = sqlite3_open(fname, &db);
152
 
153
	if (rc)
154
	{
155
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
156
		strcpy(query, "ERROR:DELETE:Error opening database;");
157
		write (s1, query, strlen(query));
158
		return FALSE;
159
	}
160
 
161
	sprintf(query, "delete from playlists where userid = %d;delete from users where id = %d;", id, id);
162
 
163
	if ((rc = sqlite3_exec(db, query, NULL, NULL, &zErrMsg)) != SQLITE_OK)
164
	{
165
		syslog(LOG_WARNING, "SQL error [%s]: %s", query, zErrMsg);
166
		sqlite3_free(zErrMsg);
167
		sqlite3_close(db);
168
		return FALSE;
169
	}
170
 
29 andreas 171
	sqlite3_close(db);
172
	scanUser(s1, userchain->uname);
8 andreas 173
	return TRUE;
174
}
175
 
176
int deletePlaylistEntry(int s1, int id)
177
{
178
	char query[1024];
179
	char fname[256];
180
	sqlite3 *db;
181
	char *zErrMsg = 0;
182
	int rc;
183
 
184
	if (id < 0)
185
		return FALSE;
186
 
187
	strcpy(fname, configs.home);
9 andreas 188
	strcat(fname, MUSICDB);
8 andreas 189
 
190
	rc = sqlite3_open(fname, &db);
191
 
192
	if (rc)
193
	{
194
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
195
		strcpy(query, "ERROR:DELETE:Error opening database;");
196
		write (s1, query, strlen(query));
197
		return FALSE;
198
	}
199
 
200
	sprintf(query, "delete from playlists where id = %d", id);
201
 
202
	if ((rc = sqlite3_exec(db, query, NULL, NULL, &zErrMsg)) != SQLITE_OK)
203
	{
204
		syslog(LOG_WARNING, "SQL error [%s]: %s", query, zErrMsg);
205
		sqlite3_free(zErrMsg);
206
		sqlite3_close(db);
207
		return FALSE;
208
	}
209
 
29 andreas 210
	sqlite3_close(db);
8 andreas 211
	return TRUE;
212
}