static void create_bookmark(const char *guessed_alias) { char *default_alias = 0; char *prompt; url_t *url; listitem *li; int a; char *alias; alias = xstrdup(guessed_alias); while(true) { if(!alias) { default_alias = guess_alias(ftp->url); if(default_alias) asprintf(&prompt, "alias (%s): ", default_alias); else prompt = xstrdup("alias: "); force_completion_type = cpBookmark; alias = input_read_string(prompt); free(prompt); if(!alias || !*alias) alias = default_alias; else free(default_alias); } url = url_clone(ftp->url); url_setalias(url, alias); free(alias); alias = 0; li = list_search(gvBookmarks, (listsearchfunc)urlcmp, url); if(li) { a = ask(ASKYES|ASKNO|ASKCANCEL, ASKYES, _("a bookmark named '%s' already exists, overwrite?"), url->alias ? url->alias : url->hostname); if(a == ASKCANCEL) { url_destroy(url); return; } if(a == ASKYES) break; } else break; } /* password is automatically saved if anonymous login */ if(!url_isanon(url) && url->password) { a = ask(ASKYES|ASKNO, ASKNO, _("Save password?")); if(a == ASKNO) url_setpassword(url, 0); } create_the_bookmark(url); printf(_("created bookmark %s\n"), url->alias); }
void auto_create_bookmark(void) { listitem *li; int a; bool auto_create, had_passwd = false; bool update = false; if(gvAutoBookmark == 0 && gvAutoBookmarkUpdate == 0) return; if(!ftp_loggedin() || gvSighupReceived) return; auto_create = (gvAutoBookmark == 1); /* check if bookmark already exists */ li = list_search(gvBookmarks, (listsearchfunc)urlcmp, ftp->url); if(li) { if(!should_update_bookmark((url_t *)li->data)) return; /* bookmark already exist, update it */ if(gvAutoBookmarkUpdate == 2) { a = ask(ASKYES|ASKNO, ASKYES, _("Do you want to update the bookmark for this site?")); if(a == ASKNO) return; } update = true; auto_create = true; had_passwd = (((url_t *)li->data)->password != 0); } if(gvAutoBookmark == 2 && !auto_create) { a = ask(ASKYES|ASKNO, ASKYES, _("Do you want to create a bookmark for this site?")); if(a == ASKNO) return; create_bookmark(0); } else { /* auto autobookmark... */ url_t* url = url_clone(ftp->url); char* a = guess_alias(ftp->url); url_setalias(url, a); free(a); li = list_search(gvBookmarks, (listsearchfunc)urlcmp, url); if(li) { /* bookmark already exist, overwrite it */ while(true) { url_t *xurl = (url_t *)li->data; if(xurl->alias && (strcmp(xurl->alias, url->alias) == 0) && (strcmp(xurl->hostname, url->hostname) != 0)) { /* create a new version of the alias, since guessed alias * already exists but for another host */ update = false; char* par = strrchr(url->alias, '('); int ver = 1; if (par) { *par = 0; ver = atoi(par + 1); } if (asprintf(&a, "%s(%d)", url->alias, ver + 1) == -1) { fprintf(stderr, _("Failed to allocate memory.\n")); url_destroy(url); return; } url_setalias(url, a); free(a); } else { update = true; had_passwd = (xurl->password != 0); break; } li = list_search(gvBookmarks, (listsearchfunc)urlcmp, url); if(li == 0) break; } if(li) /* overwrite bookmark (ie, delete old and create a new) */ list_delitem(gvBookmarks, li); if(!url_isanon(url) && gvAutoBookmarkSavePasswd == false && !had_passwd) url_setpassword(url, 0); } else { if(!url_isanon(url) && gvAutoBookmarkSavePasswd == false) url_setpassword(url, 0); } create_the_bookmark(url); if(!gvAutoBookmarkSilent) { if(update) printf(_("updated bookmark %s\n"), url->alias); else printf(_("created bookmark %s\n"), url->alias); } } }