-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.c
231 lines (196 loc) · 6.6 KB
/
admin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright (C) 2009-2016 Christian Heckendorf <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "admin.h"
#include "defs.h"
#include "dbact.h"
#include "util.h"
#include "portal.h"
#include <glob.h>
#if 0
static int addPlugin(char *args, void *data){
char lib[200];
int size,x;
printf("Library (e.g., libharpmp3): ");
size=sprintf(lib,"%s/harp/",SHARE_PATH);
if(!fgets(&lib[size],sizeof(lib)-(size+4),stdin))return PORTAL_RET_PREV;
for(x=size;lib[x]!='\n' && lib[x];x++);
strcpy(&lib[x],".sql");
debug(1,"Adding information from from file:");
debug(1,lib);
if(db_exec_file(lib)){
fprintf(stderr,"Error adding plugin from file:\n\t%s\n",lib);
}
return PORTAL_RET_PREV;
}
int autoAddPlugins(){
char *lib;
glob_t pglob;
int i;
if(glob(SHARE_PATH "/harp/libharp*.sql",0,experr,&pglob)){
return HARP_RET_ERR;
}
for(i=0;i<pglob.gl_pathc;i++){
lib=pglob.gl_pathv[i];
if(db_exec_file(lib)){
fprintf(stderr,"Error adding plugin from file:\n\t%s\n",lib);
}
}
globfree(&pglob);
return HARP_RET_OK;
}
static int listPlugins(char *args, void *data){
int exception[10];
exception[0]=exception[1]=exception[2]=exception[3]=1;
doTitleQuery("SELECT FileType.Name AS Format, PluginType.PluginTypeID AS PluginID, PluginType.Active AS Active, Plugin.Library AS Library FROM FileType NATURAL JOIN PluginType NATURAL JOIN Plugin ORDER BY Format ASC, Active DESC",exception,1);
return PORTAL_RET_PREV;
}
static int togglePlugin(char *args, void *data){
char pid[50];
char query[300];
int x,id;
printf("PluginID(e.g., 5): "); // Is actually PluginTypeID but this will be easier for the user to understand.
for(x=0;args[x] && (args[x]<'0' || args[x]>'9');x++); // See if id was given as an arg
if(!args[x]){ // Get from prompt
if(!fgets(pid,sizeof(pid),stdin))return PORTAL_RET_PREV;
id=(int)strtol(pid,NULL,10); // In case a number was not entered. ID 0 should not exist in the database.
}
else{ // Get from arg
id=(int)strtol(&args[x],NULL,10);
printf("%d\n",id);
}
sprintf(query,"UPDATE PluginType SET Active=NOT(Active) WHERE PluginTypeID=%d",id);
harp_sqlite3_exec(conn,query,NULL,NULL,NULL);
return PORTAL_RET_PREV;
}
static int removePlugin(char *args, void *data){
char lib[200];
char query[300];
int x,id;
printf("Library (e.g., libharpmp3): ");
if(!fgets(lib,sizeof(lib)-3,stdin))return PORTAL_RET_PREV;
for(x=0;lib[x]!='\n' && lib[x];x++);
strcpy(&lib[x],".so");
sprintf(query,"SELECT PluginID FROM Plugin WHERE Library='%s' LIMIT 1",lib);
id=0;
harp_sqlite3_exec(conn,query,uint_return_cb,&id,NULL);
if(!id){
printf("Library not found\n");
return PORTAL_RET_PREV;
}
sprintf(query,"DELETE FROM Plugin WHERE PluginID=%d",id);
harp_sqlite3_exec(conn,query,NULL,NULL,NULL);
sprintf(query,"DELETE FROM PluginType WHERE PluginID=%d",id);
harp_sqlite3_exec(conn,query,NULL,NULL,NULL);
return PORTAL_RET_PREV;
}
#endif
int write_stats_cb(void *data, int col_count, char **row, char **titles){
FILE *ffd=(FILE*)data;
int x;
for(x=0;x<col_count;x++){
fputs(row[x],ffd);
fputc('\t',ffd);
}
fputc('\n',ffd);
return 0;
}
static int exportStats(char *args, void *data){
char *num,filename[30];
FILE *ffd;
int x,limit;
if((x=getStdArgs(args,"Number of songs (* for all): "))<0)return PORTAL_RET_PREV;
num=args+x;
debug(2,args);
debug(2,num);
if(*num=='*')
snprintf(args,PORTAL_ARG_LEN,"SELECT SongID,Title,Location,Rating,PlayCount,SkipCount,LastPlay,Active FROM Song ORDER BY Location");
else{
if((limit=strtol(num,NULL,10))<1)return PORTAL_RET_PREV;
snprintf(args,PORTAL_ARG_LEN,"SELECT SongID,Title,Location,Rating,PlayCount,SkipCount,LastPlay,Active FROM Song ORDER BY Location LIMIT %d",limit);
}
snprintf(filename,30,"harp_stats_%d.csv",(int)time(NULL));
if((ffd=fopen(filename,"w"))==NULL){
fprintf(stderr,"Failed to open file\n");
return PORTAL_RET_PREV;
}
fputs("ID\tTITLE\tLOCATION\tRATING\tPLAYCOUNT\tSKIPCOUNT\tLASTPLAY\tACTIVE\n",ffd);
debug(3,args);
harp_sqlite3_exec(conn,args,write_stats_cb,ffd,NULL);
printf("Stats exported to: %s\n",filename);
fclose(ffd);
return PORTAL_RET_PREV;
}
static int resetAll(char *args, void *data){
harp_sqlite3_exec(conn,"UPDATE Song SET Rating=3,PlayCount=0,SkipCount=0,LastPlay=0",NULL,NULL,NULL);
return PORTAL_RET_PREV;
}
static int resetRating(char *args, void *data){
harp_sqlite3_exec(conn,"UPDATE Song SET Rating=3",NULL,NULL,NULL);
return PORTAL_RET_PREV;
}
static int resetPlayCount(char *args, void *data){
harp_sqlite3_exec(conn,"UPDATE Song SET PlayCount=0",NULL,NULL,NULL);
return PORTAL_RET_PREV;
}
static int resetSkipCount(char *args, void *data){
harp_sqlite3_exec(conn,"UPDATE Song SET SkipCount=0",NULL,NULL,NULL);
return PORTAL_RET_PREV;
}
static int resetLastPlay(char *args, void *data){
harp_sqlite3_exec(conn,"UPDATE Song SET LastPlay=0",NULL,NULL,NULL);
return PORTAL_RET_PREV;
}
static int resetPortal(char *args, void *data){
struct commandOption portalOptions[]={
{'a',resetAll,"Reset all stats",NULL},
{'r',resetRating,"Reset ratings",NULL},
{'d',resetPlayCount,"Reset play count",NULL},
{'s',resetSkipCount,"Reset skip count",NULL},
{'l',resetLastPlay,"Reset last play time",NULL},
{0,NULL,NULL}
};
return portal(portalOptions,"Reset Stats");
}
static int statsPortal(char *args, void *data){
struct commandOption portalOptions[]={
{'e',exportStats,"Export stats",NULL},
{'r',resetPortal,"Reset stats",NULL},
{0,NULL,NULL}
};
return portal(portalOptions,"Stats");
}
#if 0
static int pluginPortal(char *args, void *data){
struct commandOption portalOptions[]={
{'a',addPlugin,"Add plugin",NULL},
{'l',listPlugins,"List plugins",NULL},
{'t',togglePlugin,"Toggle activation of plugin",NULL},
{'r',removePlugin,"Remove plugin",NULL},
{0,NULL,NULL}
};
return portal(portalOptions,"Plugins");
}
#endif
void adminPortal(){
struct commandOption portalOptions[]={
{'s',statsPortal,"Manage stats",NULL},
//{'p',pluginPortal,"Manage plugins",NULL},
{0,NULL,NULL}
};
printf("Enter a command. ? for command list.\n");
while(portal(portalOptions,"Admin"));
}