/* ** If arg is a folder, look for files inside of it for PDFs / DJVU files and process ** one by one, otherwise just process the passed argument. ** ** Processing file is two steps: ** 1. If auto-rotation is requested, determine the proper rotation of the file. ** 2. Process the file with the determined rotation. ** */ static void k2pdfopt_proc_arg(K2PDFOPT_SETTINGS *k2settings,char *arg,int process, K2PDFOPT_OUTPUT *k2out) { char filename[MAXFILENAMELEN]; int i; double rot; int autorot; #if (WILLUSDEBUGX & 1) printf("@k2pdfopt_proc_arg(%s)\n",arg); printf("wfile_status(%s) = %d\n",arg,wfile_status(arg)); #endif strcpy(filename,arg); if (wfile_status(filename)==0) { #ifdef HAVE_K2GUI if ((!k2gui_active() && process) || (k2gui_active() && !process)) #endif k2printf(TTEXT_WARN "\n** File or folder %s could not be opened. **\n\n" TTEXT_NORMAL,filename); #ifdef HAVE_K2GUI if (process && k2gui_active()) { char buf[512]; k2gui_cbox_increment_error_count(); sprintf(buf,"File %s cannot be opened.",filename); if (k2settings->preview_page>0) k2gui_alertbox(0,"File not found",buf); else k2gui_cbox_set_pages_completed(0,buf); } #endif return; } if (k2settings->info) /* Info only? */ autorot = 0; else if (k2settings->preview_page!=0) autorot = (fabs(k2settings->src_rot - SRCROT_AUTOPREV)<.5); else autorot = (fabs(k2settings->src_rot - SRCROT_AUTOPREV)<.5 || fabs(k2settings->src_rot - SRCROT_AUTO)<.5 || fabs(k2settings->src_rot - SRCROT_AUTOEP)<.5); /* If folder, first process all PDF/DJVU/PS files in the folder */ if (wfile_status(filename)==2) { static char *eolist[]={""}; static char *pdflist[]={"*.pdf","*.djvu","*.djv","*.ps","*.eps",""}; static char *bmplist[]={"*.png","*.jpg",""}; FILELIST *fl,_fl; FILELIST *fl2,_fl2; int nbmp; fl=&_fl; filelist_init(fl); filelist_fill_from_disk(fl,filename,pdflist,eolist,0,0); fl2=&_fl2; filelist_init(fl2); filelist_fill_from_disk(fl2,filename,bmplist,eolist,0,0); nbmp=fl2->n; filelist_free(fl2); if (fl->n>0) { for (i=0;i<fl->n;i++) { char fullname[512]; wfile_fullname(fullname,filename,fl->entry[i].name); if (autorot) { if (process) rot=k2pdfopt_proc_one(k2settings,fullname,SRCROT_AUTO,k2out); else rot=0.; } else rot=k2settings->src_rot < -990. ? 0. : k2settings->src_rot; if (process) k2pdfopt_proc_one(k2settings,fullname,rot,k2out); if (!process || k2out->status==0) k2out->filecount++; #ifdef HAVE_K2GUI if (process && k2gui_active()) { if (k2out->status!=0) k2gui_cbox_error(filename,k2out->status); else k2gui_cbox_set_files_completed(k2out->filecount,NULL); } #endif } } filelist_free(fl); if (nbmp==0) return; } if (autorot) { if (process) rot=k2pdfopt_proc_one(k2settings,filename,SRCROT_AUTO,k2out); else rot=0.; } else rot=k2settings->src_rot < -990. ? 0. : k2settings->src_rot; if (process) k2pdfopt_proc_one(k2settings,filename,rot,k2out); if (!process || k2out->status==0) k2out->filecount++; #ifdef HAVE_K2GUI if (process && k2gui_active()) { if (k2out->status!=0) k2gui_cbox_error(filename,k2out->status); else k2gui_cbox_set_files_completed(k2out->filecount,NULL); } #endif }
/* ** Conversion thread. This is launched as a separate thread to do the ** conversions while the dialog box displays the progress. */ static void k2gui_cbox_start_conversion(void *data) { void **ptrs; int i; K2PDFOPT_CONVERSION *k2conv; K2PDFOPT_SETTINGS *k2settings; K2PDFOPT_FILELIST_PROCESS k2listproc; static char *funcname="k2gui_cbox_start_conversion"; ptrs=(void **)data; k2conv=(K2PDFOPT_CONVERSION *)ptrs[0]; k2settings=&k2conv->k2settings; /* ** Count files */ k2gui_cbox_set_num_files(1); k2gui_cbox_set_files_completed(0,"Counting files..."); overwrite_set(1); for (i=k2listproc.filecount=0;i<k2conv->k2files.n;i++) { k2listproc.bmp=NULL; k2listproc.outname=NULL; k2listproc.mode=K2PDFOPT_FILELIST_PROCESS_MODE_GET_FILECOUNT; k2pdfopt_proc_wildarg(k2settings,k2conv->k2files.file[i],&k2listproc); willus_mem_free((double **)&k2listproc.outname,funcname); } if (k2listproc.filecount==0) { k2gui_cbox_set_files_completed(0,"No files"); k2gui_alertbox(0,"No files","No files can be opened for conversion."); k2gui_cbox_conversion_thread_cleanup(); return; } k2gui_cbox_set_num_files(k2listproc.filecount); k2gui_cbox_set_files_completed(0,NULL); /* ** Process files */ k2gui_cbox_set_error_count(0); k2gui_cbox_freelist(); overwrite_set(0); for (i=k2listproc.filecount=0;i<k2conv->k2files.n;i++) { char *buf; K2PDFOPT_CONVERSION *k2c; STRBUF *cmdline,_cmdline; willus_mem_alloc_warn((void **)&buf,1024,funcname,10); k2gui_cbox_set_num_pages(1); sprintf(buf,"Starting conversion of %s...",k2gui_short_name(k2conv->k2files.file[i])); k2gui_cbox_set_pages_completed(0,buf); willus_mem_alloc_warn((void **)&k2c,sizeof(K2PDFOPT_CONVERSION),funcname,10); k2pdfopt_conversion_init(k2c); cmdline=&_cmdline; strbuf_init(cmdline); k2gui_settings_to_cmdline(cmdline,&k2conv->k2settings); parse_cmd_args(k2c,k2gui->env,cmdline,&k2gui->cmdxtra,1,1); k2listproc.outname=NULL; k2listproc.bmp=NULL; k2listproc.mode=K2PDFOPT_FILELIST_PROCESS_MODE_CONVERT_FILES; k2pdfopt_proc_wildarg(&k2c->k2settings,k2conv->k2files.file[i],&k2listproc); #if (WILLUSDEBUGX & 0x2000) printf("\n\nDone conversion...\n\n"); #endif str0_addstring(&k2gui_cbox->filelist,&k2gui_cbox->filelist_na,k2listproc.outname); willus_mem_free((double **)&k2listproc.outname,funcname); k2pdfopt_conversion_close(k2c); willus_mem_free((double **)&k2c,funcname); willus_mem_free((double **)&buf,funcname); } k2gui_cbox_set_files_completed(k2listproc.filecount,NULL); if (k2listproc.filecount==k2conv->k2files.n && k2gui_cbox->error_count==0) k2gui_cbox->successful=1; k2gui_cbox_conversion_thread_cleanup(); }