/* WebDownloader for X-Window * Copyright (C) 1999-2002 Koshelev Maxim * This Program is free but not GPL!!! You can't modify it * without agreement with author. You can't distribute modified * program but you can distribute unmodified program. * * 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. */ #include #include #include #include #include #include #include "schedule.h" #include "dbc.h" #include "locstr.h" #include "main.h" #include "savedvar.h" #include "var.h" #include "face/list.h" #include "face/fsched.h" #include "savelog.h" d4xScheduler *MainScheduler=(d4xScheduler *)NULL; d4xSchedAction::~d4xSchedAction(){ if (iter) gtk_tree_iter_free(iter); }; int d4xSchedAction::load(int fd){ tSavedVar table_of_fields[]={ {"start_time", SV_TYPE_TIME, &start_time}, {"period", SV_TYPE_TIME, &period}, {"retries", SV_TYPE_INT, &retries}, {"end.", SV_TYPE_END, NULL} }; char buf[MAX_LEN]; while(f_rstr(fd,buf,MAX_LEN)>0){ unsigned int i; for (i=0;i0){ unsigned int i; for (i=0;iset_speed(speed); }; /*------------------------------------------------------------*/ int d4xSAPopup::type(){ return(SACT_POPUP_WINDOW); }; int d4xSAPopup::save(int fd){ if (f_wstr_lf(fd,"d4x_sa_popup:")<=0) return(-1); return(d4xSchedAction::save(fd)); }; void d4xSAPopup::run(tMain *papa){ main_window_popup(); }; /*-----------------------------------------------------------*/ int d4xSAExit::type(){ return(SACT_EXIT); }; int d4xSAExit::save(int fd){ if (f_wstr_lf(fd,"d4x_sa_exit:")<=0) return(-1); return(d4xSchedAction::save(fd)); }; void d4xSAExit::run(tMain *papa){ //hehe nothing to do here :-) }; /*------------------------------------------------------------*/ int d4xSADelCompleted::type(){ return(SACT_DEL_COMPLETED); }; int d4xSADelCompleted::save(int fd){ if (f_wstr_lf(fd,"d4x_sa_del_completed:")<=0) return(-1); return(d4xSchedAction::save(fd)); }; void d4xSADelCompleted::run(tMain *papa){ papa->del_completed(); }; /*------------------------------------------------------------*/ int d4xSADelFailed::type(){ return(SACT_DEL_FAILED); }; int d4xSADelFailed::save(int fd){ if (f_wstr_lf(fd,"d4x_sa_del_failed:")<=0) return(-1); return(d4xSchedAction::save(fd)); }; void d4xSADelFailed::run(tMain *papa){ papa->del_fataled(); }; /*------------------------------------------------------------*/ d4xSAAddDownload::d4xSAAddDownload():d4xSchedAction(){ dwn=NULL; }; int d4xSAAddDownload::type(){ return(SACT_ADD_DOWNLOAD); }; int d4xSAAddDownload::load(int fd){ tSavedVar table_of_fields[]={ {"start_time", SV_TYPE_TIME, &start_time}, {"period", SV_TYPE_TIME, &period}, {"retries", SV_TYPE_INT, &retries}, {"Download:", SV_TYPE_DOWNLOAD, &dwn}, {"end.", SV_TYPE_END, NULL} }; char buf[MAX_LEN]; while(f_rstr(fd,buf,MAX_LEN)>0){ unsigned int i; for (i=0;isave_to_config(fd); return(d4xSchedAction::save(fd)); }; void d4xSAAddDownload::run(tMain *papa){ if (dwn){ tDownload *dwn_copy=new tDownload; dwn_copy->copy(dwn); if (papa->add_downloading(dwn_copy)) delete(dwn_copy); }; }; d4xSAAddDownload::~d4xSAAddDownload(){ if (dwn) delete(dwn); }; /*------------------------------------------------------------*/ int d4xSAUrl::load(int fd){ tSavedVar table_of_fields[]={ {"start_time", SV_TYPE_TIME, &start_time}, {"period", SV_TYPE_TIME, &period}, {"retries", SV_TYPE_INT, &retries}, {"URL:", SV_TYPE_URL, &url}, {"end.", SV_TYPE_END, NULL} }; char buf[MAX_LEN]; while(f_rstr(fd,buf,MAX_LEN)>0){ unsigned int i; for (i=0;idelete_download_url(url); }; /*------------------------------------------------------------*/ d4xSADelIfCompleted::d4xSADelIfCompleted():d4xSAUrl(){ }; int d4xSADelIfCompleted::type(){ return(SACT_DEL_IF_COMPLETED); }; int d4xSADelIfCompleted::save(int fd){ if (f_wstr_lf(fd,"d4x_sa_del_if_completed:")<=0) return(-1); return(d4xSAUrl::save(fd)); }; void d4xSADelIfCompleted::run(tMain *papa){ tDownload *tmp=papa->find_url(url); if (tmp && tmp->owner()==DL_COMPLETE) papa->delete_download(tmp); }; /*------------------------------------------------------------*/ d4xSARunDownload::d4xSARunDownload():d4xSAUrl(){ }; int d4xSARunDownload::type(){ return(SACT_RUN_DOWNLOAD); }; int d4xSARunDownload::save(int fd){ if (f_wstr_lf(fd,"d4x_sa_run_download:")<=0) return(-1); return(d4xSAUrl::save(fd)); }; void d4xSARunDownload::run(tMain *papa){ papa->continue_download_url(url); }; /*------------------------------------------------------------*/ d4xSAStopDownload::d4xSAStopDownload():d4xSAUrl(){ }; int d4xSAStopDownload::type(){ return(SACT_PAUSE_DOWNLOAD); }; int d4xSAStopDownload::save(int fd){ if (f_wstr_lf(fd,"d4x_sa_stop_download:")<=0) return(-1); return(d4xSAUrl::save(fd)); }; void d4xSAStopDownload::run(tMain *papa){ papa->stop_download_url(url); }; /*------------------------------------------------------------*/ int d4xSASaveList::type(){ return(SACT_SAVE_LIST); }; int d4xSASaveList::load(int fd){ tSavedVar table_of_fields[]={ {"start_time", SV_TYPE_TIME, &start_time}, {"period", SV_TYPE_TIME, &period}, {"retries", SV_TYPE_INT, &retries}, {"path", SV_TYPE_PSTR, &path}, {"end.", SV_TYPE_END, NULL} }; char buf[MAX_LEN]; while(f_rstr(fd,buf,MAX_LEN)>0){ unsigned int i; for (i=0;i0){ unsigned int i; for (i=0;icommand.get()); pthread_exit(NULL); return(NULL); }; void d4xSAExecute::run(tMain *papa){ if (command.get()){ pthread_attr_t attr_p; pthread_t thread_id; pthread_attr_init(&attr_p); pthread_attr_setdetachstate(&attr_p,PTHREAD_CREATE_DETACHED); pthread_attr_setscope(&attr_p,PTHREAD_SCOPE_SYSTEM); pthread_create(&thread_id,&attr_p, _sa_exec_run_,(void *)this); }; }; /**************************************************************/ d4xScheduler::d4xScheduler(){ FIRST=NULL; changed=0; }; d4xScheduler::~d4xScheduler(){ while (FIRST){ d4xSchedAction *tmp=FIRST; del_action(tmp); delete(tmp); }; }; void d4xScheduler::add_scheduled(tDownload *what){ d4xSAAddDownload *act=new d4xSAAddDownload; act->dwn=what; act->start_time=what->ScheduleTime; act->period=0; act->retries=0; add_action(act); }; void d4xScheduler::add_action(d4xSchedAction *act){ DBC_RETURN_IF_FAIL(act!=NULL); changed=1; act->lock=0; if (FIRST){ d4xSchedAction *tmp=FIRST; while (tmp->next){ if (tmp->start_time>act->start_time) break; tmp=tmp->next; }; if (tmp->start_time>act->start_time){ act->next=tmp; if ((act->prev=tmp->prev)) act->prev->next=act; else FIRST=act; tmp->prev=act; }else{ act->prev=tmp; tmp->next=act; act->next=NULL; }; d4x_scheduler_insert(act,act->prev); }else{ FIRST=act; act->next=act->prev=NULL; d4x_scheduler_insert(act,NULL); }; }; void d4xScheduler::del_action(d4xSchedAction *act){ DBC_RETURN_IF_FAIL(act!=NULL); changed=1; if (act->prev) act->prev->next=act->next; else FIRST=act->next; if (act->next) act->next->prev=act->prev; d4x_scheduler_remove(act); }; void d4xScheduler::redraw(){ d4xSchedAction *tmp=FIRST; while (tmp){ d4x_scheduler_insert(tmp,tmp->prev); tmp=tmp->next; }; }; int d4xScheduler::save(int fd){ d4xSchedAction *tmp=FIRST; while (tmp){ if (tmp->save(fd)) return(-1); tmp=tmp->next; }; return(0); }; void d4xScheduler::clear_old(){ time_t now=time(NULL); d4xSchedAction *tmp=FIRST; while(tmp){ d4xSchedAction *next=tmp->next; if (now>tmp->start_time){ del_action(tmp); while (tmp->start_timeretries!=0){ tmp->start_time+=tmp->period; if (tmp->retries>0) tmp->retries-=1; }; if (tmp->retries==0) delete(tmp); else add_action(tmp); }else{ break; }; tmp=next; }; }; void d4xScheduler::run(tMain *papa){ time_t now=time(NULL); int a=0; //counter d4xSchedAction *tmp=FIRST; int exit_flag=0; int se=0; while (tmp){ if (now>tmp->start_time){ d4xSchedAction *next=tmp->next; se=1; if (tmp->lock==0){ //if no editors opened tmp->run(papa); del_action(tmp); while (tmp->start_timeretries!=0){ tmp->start_time+=tmp->period; if (tmp->retries>0) tmp->retries-=1; }; if (tmp->type()==SACT_EXIT) exit_flag=1; if (tmp->retries==0) delete(tmp); else add_action(tmp); }; tmp=next; if (a++>10) break; }else break; if (exit_flag) break; }; if (se && changed) save(); if (exit_flag){ my_main_quit(); // _aa_.run_after_quit(); exit(0); }; }; int d4xScheduler::load(int fd){ char *table_of_fields[]={ "d4x_sa_speed:", "d4x_sa_popup:", "d4x_sa_exit:", "d4x_sa_del_completed:", "d4x_sa_del_failed:", "d4x_sa_run_download:", "d4x_sa_stop_download:", "d4x_sa_del_download:", "d4x_sa_del_if_completed:", "d4x_sa_add_download:", "d4x_sa_save_list:", "d4x_sa_execute:" }; char buf[MAX_LEN]; d4xSchedAction *action=(d4xSchedAction *)NULL; while(f_rstr(fd,buf,MAX_LEN)>0){ unsigned int i; for (i=0;iload(fd)==0) add_action(action); else delete(action); action=NULL; }; }; return(0); }; void d4xScheduler::save(){ if (!HOME_VARIABLE) return; char *path=sum_strings(HOME_VARIABLE,"/",CFG_DIR,"/","Scheduler",NULL); int fd=open(path,O_TRUNC | O_CREAT |O_RDWR,S_IRUSR | S_IWUSR); if (fd>=0){ save(fd); close(fd); }; delete[] path; }; void d4xScheduler::load(){ if (!HOME_VARIABLE) return; char *path=sum_strings(HOME_VARIABLE,"/",CFG_DIR,"/","Scheduler",NULL); int fd=open(path,O_RDONLY,S_IRUSR | S_IWUSR); if (fd>=0){ load(fd); close(fd); }; delete[] path; };