File center move files

This commit is contained in:
Andrew Pamment 2018-05-03 15:14:03 +10:00
parent a981864237
commit 69a3fdcdba

View File

@ -13,6 +13,8 @@ struct files {
char *name;
char *description;
int approved;
int dlcount;
time_t uploadtime;
};
struct file_directory **file_directories;
@ -323,6 +325,66 @@ static int approveFile(EObjectType cdktype, void *object, void *clientData, chty
return FALSE;
}
static int approveAll(EObjectType cdktype, void *object, void *clientData, chtype input) {
CDKSCROLL *s = (CDKSCROLL *)object;
int i;
for (i=0;i<fcount;i++) {
if (f[i]->approved == 0) {
doApprove(i);
}
}
setCDKScrollItems(s, filenames, fcount, FALSE);
refreshCDKScreen(cdkscreen);
return FALSE;
}
static int disapproveAll(EObjectType cdktype, void *object, void *clientData, chtype input) {
CDKSCROLL *s = (CDKSCROLL *)object;
int i;
for (i=0;i<fcount;i++) {
if (f[i]->approved == 1) {
doDisapprove(i);
}
}
setCDKScrollItems(s, filenames, fcount, FALSE);
refreshCDKScreen(cdkscreen);
return FALSE;
}
int copy_file(char *src, char *dest) {
FILE *src_file;
FILE *dest_file;
char c;
src_file = fopen(src, "rb");
if (!src_file) {
return -1;
}
dest_file = fopen(dest, "wb");
if (!dest_file) {
fclose(src_file);
return -1;
}
while(1) {
c = fgetc(src_file);
if (!feof(src_file)) {
fputc(c, dest_file);
} else {
break;
}
}
fclose(src_file);
fclose(dest_file);
return 0;
}
int recursive_delete(const char *dir) {
int ret = 0;
FTS *ftsp = NULL;
@ -371,6 +433,189 @@ finish:
return ret;
}
static int moveFile(EObjectType cdktype, void *object, void *clientData, chtype input) {
CDKSCROLL *s = (CDKSCROLL *)object;
CDKSCROLL *dirScrollList;
CDKSCROLL *subScrollList;
int dir_sel;
int sub_sel;
char **filedirs;
char **filesubs;
char dest_file[PATH_MAX];
int index = getCDKScrollCurrent(s);
char title[42];
sqlite3 *db;
sqlite3_stmt *res;
int rc;
struct stat st;
char database[PATH_MAX];
int i;
char create_sql[] = "CREATE TABLE IF NOT EXISTS files ("
"Id INTEGER PRIMARY KEY,"
"filename TEXT,"
"description TEXT,"
"size INTEGER,"
"dlcount INTEGER,"
"uploaddate INTEGER,"
"approved INTEGER);";
char sql_delete[] = "DELETE FROM files WHERE filename LIKE ?";
char sql_insert[] = "INSERT INTO files (filename, description, size, dlcount, uploaddate, approved) VALUES(?, ?, ?, ?, ?, ?)";
char *err_msg = 0;
if (index >= fcount) {
return FALSE;
}
dirScrollList = newCDKScroll(cdkscreen, 9, 6, 1, 36, 36, "</48>File Directories<!48>", NULL, 0, FALSE, A_REVERSE, TRUE, TRUE);
filedirs = (char **)malloc(sizeof(char *) * file_directory_count);
for (i=0;i<file_directory_count;i++) {
filedirs[i] = strdup(file_directories[i]->name);
}
setCDKScrollItems(dirScrollList, filedirs, file_directory_count, FALSE);
while(1) {
dir_sel = activateCDKScroll(dirScrollList, 0);
if (dirScrollList->exitType == vESCAPE_HIT) {
break;
} else if (dirScrollList->exitType == vNORMAL) {
snprintf(title, 42, "</48>%s<!48>", file_directories[dir_sel]->name);
subScrollList = newCDKScroll(cdkscreen, 12, 8, 1, 36, 36, title, NULL, 0, FALSE, A_REVERSE, TRUE, TRUE);
filesubs = (char **)malloc(sizeof(char *) * file_directory_count);
for (i=0;i<file_directories[dir_sel]->file_sub_count;i++) {
filesubs[i] = strdup(file_directories[dir_sel]->file_subs[i]->name);
}
setCDKScrollItems(subScrollList, filesubs, file_directories[dir_sel]->file_sub_count, FALSE);
while (1) {
sub_sel = activateCDKScroll(subScrollList, 0);
if (subScrollList->exitType == vESCAPE_HIT) {
for (i=0;i<file_directories[dir_sel]->file_sub_count;i++) {
free(filesubs[i]);
}
free(filesubs);
destroyCDKScroll(subScrollList);
break;
} else if (subScrollList->exitType == vNORMAL) {
snprintf(dest_file, PATH_MAX, "%s/%s", file_directories[dir_sel]->file_subs[sub_sel]->upload_path, basename(f[index]->name));
if (stat(dest_file, &st) != 0) {
// move file
if (copy_file(f[index]->name, dest_file) == 0) {
// success
unlink(f[index]->name);
// remove from database
snprintf(database, PATH_MAX, "%s/%s.sq3", bbspath, file_directories[current_dir]->file_subs[current_sub]->database);
rc = sqlite3_open(database, &db);
if (rc != SQLITE_OK) {
break;
}
sqlite3_busy_timeout(db, 5000);
rc = sqlite3_prepare_v2(db, sql_delete, -1, &res, 0);
if (rc != SQLITE_OK) {
sqlite3_close(db);
break;
}
sqlite3_bind_text(res, 1, f[index]->name, -1, 0);
sqlite3_step(res);
sqlite3_finalize(res);
sqlite3_close(db);
// add to dest database
snprintf(database, PATH_MAX, "%s/%s.sq3", bbspath, file_directories[dir_sel]->file_subs[sub_sel]->database);
rc = sqlite3_open(database, &db);
if (rc != SQLITE_OK) {
break;
}
sqlite3_busy_timeout(db, 5000);
rc = sqlite3_exec(db, create_sql, 0, 0, &err_msg);
if (rc != SQLITE_OK ) {
sqlite3_free(err_msg);
sqlite3_close(db);
return FALSE;
}
rc = sqlite3_prepare_v2(db, sql_insert, -1, &res, 0);
if (rc != SQLITE_OK) {
sqlite3_close(db);
return FALSE;
}
stat(dest_file, &st);
sqlite3_bind_text(res, 1, dest_file, -1, 0);
sqlite3_bind_text(res, 2, f[index]->description, -1, 0);
sqlite3_bind_int(res, 3, st.st_size);
sqlite3_bind_int(res, 4, f[index]->dlcount);
sqlite3_bind_int(res, 5, f[index]->uploadtime);
sqlite3_bind_int(res, 6, f[index]->approved);
sqlite3_step(res);
sqlite3_finalize(res);
sqlite3_close(db);
// remove from current memory
free(filenames[index]);
free(f[index]->name);
free(f[index]->description);
free(f[index]);
for (i=index;i<fcount-1;i++) {
f[i] = f[i+1];
filenames[i] = filenames[i+1];
}
fcount--;
f = (struct files **)realloc(f, sizeof(struct files *) * fcount);
filenames = (char **)realloc(filenames, sizeof(char *) * fcount);
}
}
break;
}
for (i=0;i<file_directories[dir_sel]->file_sub_count;i++) {
free(filesubs[i]);
}
free(filesubs);
eraseCDKScroll(subScrollList);
destroyCDKScroll(subScrollList);
}
for (i=0;i<file_directory_count;i++) {
free(filedirs[i]);
}
free(filedirs);
eraseCDKScroll(dirScrollList);
destroyCDKScroll(dirScrollList);
break;
}
}
setCDKScrollItems(s, filenames, fcount, FALSE);
eraseCDKScroll(s);
drawCDKScroll(s, TRUE);
refreshCDKScreen(cdkscreen);
}
static int scanFiles(EObjectType cdktype, void *object, void *clientData, chtype input) {
DIR *ind = opendir(file_directories[current_dir]->file_subs[current_sub]->upload_path);
FILE *fptr;
@ -590,7 +835,7 @@ void list_files(int dir, int sub) {
sqlite3_stmt *res;
int rc;
struct stat s;
char sql_read[] = "SELECT filename, description, approved FROM files";
char sql_read[] = "SELECT filename, description, approved, dlcount, uploaddate FROM files";
char create_sql[] = "CREATE TABLE IF NOT EXISTS files ("
"Id INTEGER PRIMARY KEY,"
"filename TEXT,"
@ -642,7 +887,8 @@ void list_files(int dir, int sub) {
f[fcount]->name = strdup((char *)sqlite3_column_text(res, 0));
f[fcount]->description = strdup((char *)sqlite3_column_text(res, 1));
f[fcount]->approved = sqlite3_column_int(res, 2);
f[fcount]->dlcount = sqlite3_column_int(res, 3);
f[fcount]->uploadtime = sqlite3_column_int(res, 4);
filenames[fcount] = (char *)malloc(strlen(basename(f[fcount]->name)) + 30);
if (stat(f[fcount]->name, &s) != 0) {
sprintf(filenames[fcount], "</16>%s (missing)<!16>", basename(f[fcount]->name));
@ -661,7 +907,7 @@ void list_files(int dir, int sub) {
sqlite3_finalize(res);
sqlite3_close(db);
scrollList = newCDKScroll(cdkscreen, 6, 4, 1, 36, 32, title, NULL, 0, FALSE, A_REVERSE, TRUE, TRUE);
scrollList = newCDKScroll(cdkscreen, 6, 4, 1, 36, 36, title, NULL, 0, FALSE, A_REVERSE, TRUE, TRUE);
if (!scrollList) {
for (i=0;i<fcount;i++) {
free(f[i]->name);
@ -678,7 +924,10 @@ void list_files(int dir, int sub) {
setCDKScrollItems(scrollList, filenames, fcount, FALSE);
bindCDKObject (vSCROLL, scrollList, 'a', approveFile, NULL);
bindCDKObject (vSCROLL, scrollList, 'm', moveFile, NULL);
bindCDKObject (vSCROLL, scrollList, 'u', disapproveAll, NULL);
bindCDKObject (vSCROLL, scrollList, 'a', approveAll, NULL);
bindCDKObject (vSCROLL, scrollList, 't', approveFile, NULL);
bindCDKObject (vSCROLL, scrollList, 'd', deleteFile, NULL);
bindCDKObject (vSCROLL, scrollList, 's', scanFiles, NULL);
@ -716,7 +965,7 @@ void list_subdirs(int selected) {
filesubs[i] = strdup(file_directories[selected]->file_subs[i]->name);
}
scrollList = newCDKScroll(cdkscreen, 4, 3, 1, 36, 34, title, NULL, 0, FALSE, A_REVERSE, TRUE, TRUE);
scrollList = newCDKScroll(cdkscreen, 4, 3, 1, 36, 36, title, NULL, 0, FALSE, A_REVERSE, TRUE, TRUE);
if (!scrollList) {
fprintf(stderr, "Unable to make scrolllist!");
destroyCDKScreen(cdkscreen);