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