28#include <sys/socket.h>
35#include <FL/fl_draw.H>
36#include <FL/Fl_File_Chooser.H>
37#include <FL/Fl_Window.H>
38#include <FL/Fl_Widget.H>
39#include <FL/Fl_Group.H>
40#include <FL/Fl_Scroll.H>
41#include <FL/Fl_Pack.H>
43#include <FL/Fl_Button.H>
47#include "../dpip/dpip.h"
53#define MSG(...) printf("[downloads dpi]: " __VA_ARGS__)
61class ProgressBar :
public Fl_Box {
67 bool mShowPct, mShowMsg;
72 ProgressBar(
int x,
int y,
int w,
int h,
const char *lbl = 0);
73 void range(
double min,
double max,
double step = 1) {
74 mMin = min; mMax = max; mStep = step;
76 void step(
double step) { mPresent += step; redraw(); };
77 void move(
double step);
78 double minimum() {
return mMin; }
79 double maximum() {
return mMax; }
80 void minimum(
double nm) { mMin = nm; };
81 void maximum(
double nm) { mMax = nm; };
82 double position () {
return mPresent; }
83 double step() {
return mStep; }
84 void position(
double pos) { mPresent = pos; redraw(); }
85 void showtext(
bool st) { mShowPct = st; }
86 void message(
char *msg) { mShowMsg =
true; strncpy(mMsg,msg,63); redraw(); }
87 bool showtext() {
return mShowPct; }
88 void text_color(Fl_Color col) { mTextColor = col; }
89 Fl_Color text_color() {
return mTextColor; }
96 ST_newline, ST_number, ST_discard, ST_copy
101 char *shortname, *fullname;
105 size_t log_len, log_max;
110 time_t twosec_time, onesec_time;
111 int twosec_bytesize, onesec_bytesize;
112 int init_bytesize, curr_bytesize, total_bytesize;
113 int DataDone, LogDone, ForkDone, UpdatesDone, WidgetDone;
120 Fl_Widget *prTitle, *prGot, *prSize, *prRate, *pr_Rate, *prETA, *prETAt;
123 DLItem(
const char *full_filename,
const char *url);
127 void update_size(
int new_sz);
128 void log_text_add(
const char *buf, ssize_t st);
129 void log_text_show();
132 pid_t pid() {
return mPid; }
133 void pid(pid_t p) { mPid = p; }
134 void child_finished(
int status);
135 void status_msg(
const char *msg) { prBar->message((
char*)msg); }
136 Fl_Widget *get_widget() {
return group; }
137 int widget_done() {
return WidgetDone; }
138 void widget_done(
int val) { WidgetDone = val; }
139 int updates_done() {
return UpdatesDone; }
140 void updates_done(
int val) { UpdatesDone = val; }
141 int fork_done() {
return ForkDone; }
142 void fork_done(
int val) { ForkDone = val; }
143 int log_done() {
return LogDone; }
144 void log_done(
int val) { LogDone = val; }
145 int wget_status() {
return WgetStatus; }
146 void wget_status(
int val) { WgetStatus = val; }
147 void update_prSize(
int newsize);
159 DLItemList() { mNum = 0; mMax = 32; }
161 int num() {
return mNum; }
162 void add(DLItem *i) {
if (mNum < mMax) mList[mNum++] = i; }
163 DLItem *get(
int n) {
return (n >= 0 && n < mNum) ? mList[n] : NULL; }
164 void del(
int n) {
if (n >= 0 && n < mNum) mList[n] = mList[--mNum]; }
176 DLWin(
int ww,
int wh);
177 void add(
const char *full_filename,
const char *url);
178 void del(
int n_item);
181 void listen(
int req_fd);
182 void show() { mWin->show(); }
183 void hide() { mWin->hide(); }
196 char *ret = (
char *) malloc(2 * len + 1);
227void ProgressBar::move(
double step)
235ProgressBar::ProgressBar(
int x,
int y,
int w,
int h,
const char *lbl)
236: Fl_Box(x, y, w, h, lbl)
246void ProgressBar::draw()
254 Rectangle r = {x(), y(), w(), h()};
259 double pct = (mPresent - mMin) / mMax;
261 r.w = r.w * pct + .5;
262 fl_rectf(r.x, r.y, r.w, r.h, FL_BLUE);
266 fl_font(this->labelfont(), this->labelsize());
267 fl_draw(mMsg, x(), y(), w(), h(), FL_ALIGN_CENTER);
268 }
else if (mShowPct) {
270 sprintf(buffer,
"%d%%",
int (pct * 100 + .5));
272 fl_font(this->labelfont(), this->labelsize());
273 fl_draw(buffer, x(), y(), w(), h(), FL_ALIGN_CENTER);
282 DLItem *i = (DLItem *)cb_data;
287DLItem::DLItem(
const char *full_filename,
const char *url)
292 if (pipe(LogPipe) < 0) {
297 fcntl(LogPipe[0], F_SETFL,
298 O_NONBLOCK | fcntl(LogPipe[0], F_GETFL));
300 fullname =
dStrdup(full_filename);
301 p = strrchr(fullname,
'/');
303 p = strrchr(full_filename,
'/');
304 target_dir= p ?
dStrndup(full_filename,p-full_filename+1) :
dStrdup(
"??");
308 log_state = ST_newline;
310 onesec_bytesize = twosec_bytesize = curr_bytesize = init_bytesize = 0;
314 init_time = time(NULL);
316 twosec_time = onesec_time = init_time;
327 const char *user_agent =
"Dillo/" VERSION;
329 if (stat(fullname, &ss) == 0)
330 init_bytesize = (
int)ss.st_size;
332 dl_argv[i++] = (
char*) user_agent;
334 dl_argv[i++] = (
char*)
"--load-cookies";
349 group =
new Fl_Group(0,0,gw,gh);
351 prTitle =
new Fl_Box(24, 7, 290, 23);
352 prTitle->box(FL_RSHADOW_BOX);
353 prTitle->color(FL_WHITE);
354 prTitle->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE|FL_ALIGN_CLIP);
355 prTitle->copy_label(shortname);
357 log_text_add(
"Target File: ", 13);
358 log_text_add(fullname, strlen(fullname));
359 log_text_add(
"\n\n", 2);
361 prBar =
new ProgressBar(24, 40, 92, 20);
362 prBar->box(FL_THIN_UP_BOX);
363 prBar->tooltip(
"Progress Status");
365 int ix = 122, iy = 37, iw = 50, ih = 14;
366 Fl_Widget *o =
new Fl_Box(ix,iy,iw,ih,
"Got");
367 o->box(FL_RFLAT_BOX);
370 o->tooltip(
"Downloaded Size");
371 prGot =
new Fl_Box(ix,iy+14,iw,ih,
"0KB");
372 prGot->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
373 prGot->labelcolor(FL_BLUE);
374 prGot->labelsize(12);
375 prGot->box(FL_NO_BOX);
378 o =
new Fl_Box(ix,iy,iw,ih,
"Size");
379 o->box(FL_RFLAT_BOX);
382 o->tooltip(
"Total Size");
383 prSize =
new Fl_Box(ix,iy+14,iw,ih,
"??");
384 prSize->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
385 prSize->labelsize(12);
386 prSize->box(FL_NO_BOX);
389 o =
new Fl_Box(ix,iy,iw,ih,
"Rate");
390 o->box(FL_RFLAT_BOX);
393 o->tooltip(
"Current transfer Rate (KBytes/sec)");
394 prRate =
new Fl_Box(ix,iy+14,iw,ih,
"??");
395 prRate->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
396 prRate->labelsize(12);
397 prRate->box(FL_NO_BOX);
400 o =
new Fl_Box(ix,iy,iw,ih,
"~Rate");
401 o->box(FL_RFLAT_BOX);
404 o->tooltip(
"Average transfer Rate (KBytes/sec)");
405 pr_Rate =
new Fl_Box(ix,iy+14,iw,ih,
"??");
406 pr_Rate->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
407 pr_Rate->labelsize(12);
408 pr_Rate->box(FL_NO_BOX);
411 prETAt = o =
new Fl_Box(ix,iy,iw,ih,
"ETA");
412 o->box(FL_RFLAT_BOX);
415 o->tooltip(
"Estimated Time of Arrival");
416 prETA =
new Fl_Box(ix,iy+14,iw,ih,
"??");
417 prETA->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
418 prETA->labelsize(12);
419 prETA->box(FL_NO_BOX);
421 prButton =
new Fl_Button(326, 9, 44, 19,
"Stop");
422 prButton->tooltip(
"Stop this transfer");
423 prButton->box(FL_UP_BOX);
424 prButton->clear_visible_focus();
427 group->box(FL_ROUNDED_BOX);
447void DLItem::abort_dl()
451 Fl::remove_fd(LogPipe[0]);
455 kill(pid(), SIGTERM);
460void DLItem::prButton_cb()
462 prButton->deactivate();
466void DLItem::child_init()
473 setenv(
"LC_ALL",
"C", 1);
481void DLItem::update_prSize(
int newsize)
485 if (newsize > 1024 * 1024)
486 snprintf(num, 64,
"%.1fMB", (
float)newsize / (1024*1024));
488 snprintf(num, 64,
"%.0fKB", (
float)newsize / 1024);
489 prSize->copy_label(num);
492void DLItem::log_text_add(
const char *buf, ssize_t st)
495 char *esc_str, *q, *d, num[64];
500 esc_len = strlen(esc_str);
503 if (log_len + esc_len >= log_max) {
504 log_max = log_len + esc_len + 1024;
505 log_text = (
char *)
dRealloc (log_text, log_max);
506 log_text[log_len] = 0;
507 prTitle->tooltip(log_text);
511 q = log_text + log_len;
512 for (p = esc_str; (size_t)(p - esc_str) < esc_len; ++p) {
516 log_state = ST_discard;
517 }
else if (isdigit(*p)) {
518 *q++ = *p; log_state = ST_number;
519 }
else if (*p ==
'\n') {
522 *q++ = *p; log_state = ST_copy;
526 if (isdigit(*q++ = *p)) {
528 }
else if (*p ==
'K') {
529 for (--q; isdigit(q[-1]); --q) ;
530 log_state = ST_discard;
537 log_state = ST_newline;
540 if ((*q++ = *p) ==
'\n')
541 log_state = ST_newline;
546 log_len = strlen(log_text);
551 if (total_bytesize == -1) {
552 p = strstr(log_text,
"\nLength: ");
553 if (p && isdigit(p[9]) && strchr(p + 9,
' ')) {
554 for (p += 9, d = &num[0]; *p !=
' '; ++p)
558 total_bytesize = strtol (num, NULL, 10);
560 update_prSize(total_bytesize);
569 if (curr_bytesize == 0) {
570 prTitle->copy_label(
"Connecting...");
575void DLItem::log_text_show()
577 MSG(
"\nStored Log:\n%s", log_text);
580void DLItem::update_size(
int new_sz)
584 if (curr_bytesize == 0 && new_sz) {
586 init_time = time(NULL);
588 prTitle->copy_label(shortname);
594 curr_bytesize = new_sz;
595 if (curr_bytesize > 1024 * 1024)
596 snprintf(buf, 64,
"%.1fMB", (
float)curr_bytesize / (1024*1024));
598 snprintf(buf, 64,
"%.0fKB", (
float)curr_bytesize / 1024);
599 prGot->copy_label(buf);
600 if (total_bytesize == -1) {
601 prBar->showtext(
false);
604 prBar->showtext(
true);
606 if (total_bytesize > 0)
607 pos *= (double)curr_bytesize / total_bytesize;
608 prBar->position(pos);
614 DLItem *dl_item = (DLItem *)data;
615 const int BufLen = 4096;
620 st = read(fd_in, Buf, BufLen);
622 if (errno == EAGAIN) {
627 }
else if (st == 0) {
629 Fl::remove_fd(fd_in, 1);
630 dl_item->log_done(1);
633 dl_item->log_text_add(Buf, st);
638void DLItem::father_init()
651void DLItem::child_finished(
int status)
656 prButton->label(
"Done");
657 prButton->tooltip(
"Close this information panel");
659 prButton->label(
"Close");
660 prButton->tooltip(
"Close this information panel");
661 status_msg(
"ABORTED");
662 if (curr_bytesize == 0) {
664 prTitle->copy_label(shortname);
667 prButton->activate();
669 MSG(
"wget status %d\n", status);
679 eh = et / 3600; em = (et % 3600) / 60; es = et % 60;
682 snprintf(str, 16,
"%ds", es);
684 snprintf(str, 16,
"%dm%ds", em, es);
686 snprintf(str, 16,
"%dh%dm", eh, em);
697 float csec, tsec, rate, _rate = 0;
705 if (stat(fullname, &ss) == -1) {
709 update_size((
int)ss.st_size);
713 csec = (float) (curr_time - init_time);
717 tsec = (float) (curr_time - twosec_time);
718 rate = ((float)(curr_bytesize-twosec_bytesize) / 1024) / tsec;
719 snprintf(str, 64, (rate < 100) ?
"%.1fK/s" :
"%.0fK/s", rate);
720 prRate->copy_label(str);
724 _rate = ((float)(curr_bytesize-init_bytesize) / 1024) / csec;
725 snprintf(str, 64, (_rate < 100) ?
"%.1fK/s" :
"%.0fK/s", _rate);
726 pr_Rate->copy_label(str);
732 prETAt->label(
"Time");
733 prETAt->tooltip(
"Download Time");
736 prETA->copy_label(str);
737 if (total_bytesize == -1) {
738 update_prSize(curr_bytesize);
739 if (wget_status() == 0)
743 if (_rate > 0 && total_bytesize > 0 && curr_bytesize > 0) {
744 et = (int)((total_bytesize-curr_bytesize) / (_rate * 1024));
746 prETA->copy_label(str);
751 twosec_time = onesec_time;
752 onesec_time = curr_time;
753 twosec_bytesize = onesec_bytesize;
754 onesec_bytesize = curr_bytesize;
769 struct sigaction sigact;
772 (void) sigemptyset(&set);
774 sigact.sa_mask = set;
775 sigact.sa_flags = SA_NOCLDSTOP;
776 if (sigaction(SIGCHLD, &sigact, NULL) == -1) {
787 DLItemList *list = (DLItemList *)data;
793 for (i = 0; i < list->num(); ++i) {
794 if (!list->get(i)->fork_done() &&
795 waitpid(list->get(i)->pid(), &status, WNOHANG) > 0) {
796 list->get(i)->child_finished(status);
797 list->get(i)->fork_done(1);
813 static int cb_used = 0;
815 DLItemList *list = (DLItemList *)data;
818 for (
int i = 0; i < list->num(); ++i) {
819 if (!list->get(i)->widget_done()) {
820 list->get(i)->update();
821 }
else if (list->get(i)->fork_done()) {
828 if (cb_used && list->num() == 0)
843 struct sockaddr_un clnt_addr;
847 char *dpip_tag = NULL, *cmd = NULL, *url = NULL, *dl_dest = NULL;
850 csz =
sizeof(
struct sockaddr_un);
853 sock_fd = accept(req_fd, (
struct sockaddr *) &clnt_addr, &csz);
854 }
while (sock_fd == -1 && errno == EINTR);
866 MSG(
"can't authenticate request: %s fd=%d\n",
dStrerror(errno), sock_fd);
874 MSG(
"can't read request: %s fd=%d\n",
dStrerror(errno), sock_fd);
879 _MSG(
"Received tag={%s}\n", dpip_tag);
882 MSG(
"Failed to parse 'cmd' in {%s}\n", dpip_tag);
885 if (strcmp(cmd,
"DpiBye") == 0) {
886 MSG(
"got DpiBye, ignoring...\n");
889 if (strcmp(cmd,
"download") != 0) {
890 MSG(
"unknown command: '%s'. Aborting.\n", cmd);
894 MSG(
"Failed to parse 'url' in {%s}\n", dpip_tag);
898 MSG(
"Failed to parse 'destination' in {%s}\n", dpip_tag);
901 dl_win->add(dl_dest, url);
916 const char *msg =
"There are running downloads.\n"
917 "ABORT them and EXIT anyway?";
920 fl_message_title(
"Dillo Downloads: Abort downloads?");
921 int ch = fl_choice(
"%s",
"Cancel",
"*No",
"Yes", msg);
922 if (ch == 0 || ch == 1)
934void DLWin::add(
const char *full_filename,
const char *url)
936 DLItem *dl_item =
new DLItem(full_filename, url);
937 mDList->add(dl_item);
938 mPG->insert(*dl_item->get_widget(), 0);
940 _MSG(
"Child index = %d\n", mPG->find(dl_item->get_widget()));
943 pid_t f_pid = fork();
946 dl_item->child_init();
948 }
else if (f_pid < 0) {
953 dl_item->get_widget()->show();
956 dl_item->father_init();
963void DLWin::del(
int n_item)
965 DLItem *dl_item = mDList->get(n_item);
968 mPG->remove(dl_item->get_widget());
979 return mDList->num();
985int DLWin::num_running()
989 for (i = nr = 0; i < mDList->num(); ++i)
990 if (!mDList->get(i)->fork_done())
998void DLWin::listen(
int req_fd)
1006void DLWin::abort_all()
1008 for (
int i = 0; i < mDList->num(); ++i)
1009 mDList->get(i)->abort_dl();
1016class DlScroll :
public Fl_Scroll
1019 void resize(
int x_,
int y_,
int w_,
int h_)
1021 Fl_Scroll::resize(x_, y_, w_, h_);
1022 Fl_Widget *resizable_ = resizable();
1024 resizable_->h() <= h() ? 0 :
1025 scrollbar_size() ? scrollbar_size() :
1026 Fl::scrollbar_size();
1028 resizable_->resize(resizable_->x(),
1033 DlScroll(
int x,
int y,
int w,
int h,
const char *l = 0)
1034 : Fl_Scroll(x, y, w, h, l)
1042DLWin::DLWin(
int ww,
int wh) {
1045 mDList =
new DLItemList();
1048 mWin =
new Fl_Window(ww, wh,
"Dillo Downloads");
1050 mScroll =
new DlScroll(0,0,ww,wh);
1052 mPG =
new Fl_Pack(0,0,ww-18,wh);
1055 mScroll->type(Fl_Scroll::VERTICAL);
1056 mScroll->resizable(mPG);
1058 mWin->resizable(mScroll);
1067 fl_message_title_default(
"Dillo Downloads: Message");
1072 Fl::add_timeout(1.0,
update_cb, mDList);
1084 const int interpret_symbols = 0;
1086 fl_draw_shortcut = 0;
1087 fl_font(o->font, o->size);
1088 fl_color((Fl_Color)o->color);
1089 fl_draw(o->value, X, Y, W,
H, align, o->image, interpret_symbols);
1094 const int interpret_symbols = 0;
1096 fl_draw_shortcut = 0;
1097 fl_font(o->font, o->size);
1098 fl_measure(o->value, W,
H, interpret_symbols);
1106 int ww = 420, wh = 85;
1116 dl_win =
new DLWin(ww, wh);
1119 dl_win->listen(STDIN_FILENO);
1121 MSG(
"started...\n");
char * dStrconcat(const char *s1,...)
Concatenate a NULL-terminated list of strings.
char * dStrdup(const char *s)
int dStrnAsciiCasecmp(const char *s1, const char *s2, size_t n)
int dClose(int fd)
Close a FD handling EINTR.
char * dStrndup(const char *s, size_t sz)
void * dRealloc(void *mem, size_t size)
char * dGethomedir(void)
Return the home directory in a static string (don't free)
static void custLabelMeasure(const Fl_Label *o, int &W, int &H)
static void read_log_cb(int fd_in, void *data)
static void raw_sigchld(int)
static void est_sigchld(void)
static void custLabelDraw(const Fl_Label *o, int X, int Y, int W, int H, Fl_Align align)
volatile sig_atomic_t caught_sigchld
static void secs2timestr(int et, char *str)
static char * escape_tooltip(const char *buf, ssize_t len)
static void cleanup_cb(void *data)
static void read_req_cb(int req_fd, void *)
static void prButton_scb(Fl_Widget *, void *cb_data)
static class DLWin * dl_win
static void update_cb(void *data)
static void dlwin_esc_cb(Fl_Widget *, void *)
void a_Dpip_dsh_free(Dsh *dsh)
Free the SockHandler structure.
char * a_Dpip_get_attr(const char *tag, const char *attrname)
Task: given a tag and an attribute name, return its value.
char * a_Dpip_dsh_read_token(Dsh *dsh, int blocking)
Return a newlly allocated string with the next dpip token in the socket.
void a_Dpip_dsh_close(Dsh *dsh)
Close this socket for reading and writing.
int a_Dpip_check_auth(const char *auth_tag)
Check whether the given 'auth' string equals what dpid saved.
Dsh * a_Dpip_dsh_new(int fd_in, int fd_out, int flush_sz)
Create and initialize a dpip socket handler.
char * Filter_smtp_hack(char *url)
char * Escape_uri_str(const char *str, const char *p_esc_set)
Dpip socket handler type.