~julienxx/castor9

e29ac5e534dc9ec859b5698331fc4d6cf3ef191c — Julien Blanchard 3 years ago 69cadae
Add/Show bookmarks
1 files changed, 67 insertions(+), 4 deletions(-)

M castor.c
M castor.c => castor.c +67 -4
@@ 44,6 44,7 @@ void gemini_get(Url *u);
void gemini_put(Response *r);
void texthit(Panel *p, int b, Rtext *t);
void addbookmark(void);
void showbookmarks(void);
void message(char *s, ...);

Panel *root;


@@ 569,7 570,7 @@ menuhit(int button, int item)
		search();
		break;
	case Mbookmarks:
		//showbookmarks();
		showbookmarks();
		break;
	case Maddbookmark:
		addbookmark();


@@ 580,13 581,75 @@ menuhit(int button, int item)
	}
}

void
addbookmark(void)
char*
getbookmarkspath(void)
{
	char *home, *bpath;
	home = getenv("home");
	if(home==0)
		sysfatal("getenv(home): %r");

	bpath = smprint("%s/lib/castorbookmarks", home);
	return bpath;
}

int
createbookmarks(void)
{
	int fd;
	if((fd = create("bookmarks", OWRITE, 0600 | DMAPPEND)) < 0)
	if((fd = create(getbookmarkspath(), OWRITE, 0600 | DMAPPEND)) < 0)
		sysfatal("create(bookmarks): %r");
	return fd;
}

void
showbookmarks(void)
{
	char *line;
	Biobuf *bfile;

	bfile = Bopen(getbookmarkspath(), OREAD);
	if(bfile==nil){
		message("You must bookmark a page first!");
		return;
	}

	Ctx *c;
	c = malloc(sizeof *c);
	if(c==nil)
		sysfatal("malloc: %r");
	c->text = nil;
	c->url = urlparse(nil, "file://bookmarks");

	Hist *h;
	h = malloc(sizeof *h);
	if(h == nil)
		sysfatal("malloc: %r");

	plrtstr(&c->text, 1000000, 0, 0, font, strdup(" "), 0, 0);

	message("loading bookmarks...");
	
	while((line = Brdstr(bfile, '\n', 0)) != nil){
		render_link(c, line);
		free(line);
	}

	Bflush(bfile);

	h->p = hist;
	h->n = nil;
	h->c = c;
	hist = h;

	show(c);
}

void
addbookmark(void)
{
	int fd;
	fd = createbookmarks();
	fprint(fd, "=> %s\n", hist->c->url->raw);
	close(fd);
	message("Bookmark added!");