dialogs.c

来自「一个很有名的浏览器」· C语言 代码 · 共 692 行 · 第 1/2 页

C
692
字号
/* Bookmarks dialogs *//* $Id: dialogs.c,v 1.194.2.8 2005/04/06 09:02:09 jonas Exp $ */#ifndef _GNU_SOURCE#define _GNU_SOURCE /* XXX: we _WANT_ strcasestr() ! */#endif#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <string.h>#include "elinks.h"#include "bfu/dialog.h"#include "bookmarks/bookmarks.h"#include "bookmarks/dialogs.h"#include "dialogs/edit.h"#include "intl/gettext/libintl.h"#include "protocol/uri.h"#include "sched/session.h"#include "terminal/terminal.h"#include "util/conv.h"#include "util/error.h"#include "util/memory.h"#include "util/object.h"#include "util/ttime.h"/* Whether to save bookmarks after each modification of their list * (add/modify/delete). */#define BOOKMARKS_RESAVE	1/* Last searched values */unsigned char *bm_last_searched_name = NULL;unsigned char *bm_last_searched_url = NULL;static voidlock_bookmark(struct listbox_item *item){	object_lock((struct bookmark *) item->udata);}static voidunlock_bookmark(struct listbox_item *item){	object_unlock((struct bookmark *) item->udata);}static intis_bookmark_used(struct listbox_item *item){	return is_object_used((struct bookmark *) item->udata);}static unsigned char *get_bookmark_text(struct listbox_item *item, struct terminal *term){	struct bookmark *bookmark = item->udata;	return stracpy(bookmark->title);}static unsigned char *get_bookmark_info(struct listbox_item *item, struct terminal *term){	struct bookmark *bookmark = item->udata;	struct string info;	if (item->type == BI_FOLDER) return NULL;	if (!init_string(&info)) return NULL;	add_format_to_string(&info, "%s: %s", _("Title", term), bookmark->title);	add_format_to_string(&info, "\n%s: %s", _("URL", term), bookmark->url);	return info.source;}static struct uri *get_bookmark_uri(struct listbox_item *item){	struct bookmark *bookmark = item->udata;	return bookmark->url && *bookmark->url		? get_translated_uri(bookmark->url, 0) : NULL;}static struct listbox_item *get_bookmark_root(struct listbox_item *item){	struct bookmark *bookmark = item->udata;	return bookmark->root ? bookmark->root->box_item : NULL;}static intcan_delete_bookmark(struct listbox_item *item){	return 1;}static voiddelete_bookmark_item(struct listbox_item *item, int last){	struct bookmark *bookmark = item->udata;	assert(!is_object_used(bookmark));	delete_bookmark(bookmark);#ifdef BOOKMARKS_RESAVE	if (last) write_bookmarks();#endif}static struct listbox_ops_messages bookmarks_messages = {	/* cant_delete_item */	N_("Sorry, but the bookmark \"%s\" cannot be deleted."),	/* cant_delete_used_item */	N_("Sorry, but the bookmark \"%s\" is being used by something else."),	/* cant_delete_folder */	N_("Sorry, but the folder \"%s\" cannot be deleted."),	/* cant_delete_used_folder */	N_("Sorry, but the folder \"%s\" is being used by something else."),	/* delete_marked_items_title */	N_("Delete marked bookmarks"),	/* delete_marked_items */	N_("Delete marked bookmarks?"),	/* delete_folder_title */	N_("Delete folder"),	/* delete_folder */	N_("Delete the folder \"%s\" and all bookmarks in it?"),	/* delete_item_title */	N_("Delete bookmark"),	/* delete_item */	N_("Delete this bookmark?"),	/* clear_all_items_title */	N_("Clear all bookmarks"),	/* clear_all_items_title */	N_("Do you really want to remove all bookmarks?"),};static struct listbox_ops bookmarks_listbox_ops = {	lock_bookmark,	unlock_bookmark,	is_bookmark_used,	get_bookmark_text,	get_bookmark_info,	get_bookmark_uri,	get_bookmark_root,	NULL,	can_delete_bookmark,	delete_bookmark_item,	NULL,	&bookmarks_messages,};/****************************************************************************  Bookmark manager stuff.****************************************************************************/void launch_bm_add_doc_dialog(struct terminal *, struct dialog_data *,			      struct session *);/* Callback for the "add" button in the bookmark manager */static t_handler_event_statuspush_add_button(struct dialog_data *dlg_data, struct widget_data *widget_data){	launch_bm_add_doc_dialog(dlg_data->win->term, dlg_data,				 (struct session *) dlg_data->dlg->udata);	return EVENT_PROCESSED;}staticvoid launch_bm_search_doc_dialog(struct terminal *, struct dialog_data *,				 struct session *);/* Callback for the "search" button in the bookmark manager */static t_handler_event_statuspush_search_button(struct dialog_data *dlg_data, struct widget_data *widget_data){	launch_bm_search_doc_dialog(dlg_data->win->term, dlg_data,				    (struct session *) dlg_data->dlg->udata);	return EVENT_PROCESSED;}static voidmove_bookmark_after_selected(struct bookmark *bookmark, struct bookmark *selected){	if (selected == bookmark->root	    || !selected	    || !selected->box_item	    || !bookmark->box_item)		return;	del_from_list(bookmark->box_item);	del_from_list(bookmark);	add_at_pos(selected, bookmark);	add_at_pos(selected->box_item, bookmark->box_item);}/**** ADD FOLDER *****************************************************/static voidfocus_bookmark(struct widget_data *box_widget_data, struct listbox_data *box,		struct bookmark *bm){	/* Infinite loop protector. Maximal safety. It will protect your system	 * from 100% CPU time. Buy it now. Only from Sirius Labs. */	struct listbox_item *sel2 = NULL;	do {		sel2 = box->sel;		listbox_sel_move(box_widget_data, 1);	} while (box->sel->udata != bm && box->sel != sel2);}/* TODO: merge with bookmark_add_add() ? --Zas */static voiddo_add_special_bookmark(struct dialog_data *dlg_data, unsigned char *name, unsigned char *url){	struct widget_data *widget_data = dlg_data->widgets_data;	struct listbox_data *box = get_dlg_listbox_data(dlg_data);	struct bookmark *bm = NULL;	struct bookmark *selected = NULL;	if (box->sel) {		selected = box->sel ? box->sel->udata : NULL;		if (box->sel->type == BI_FOLDER && box->sel->expanded) {			bm = selected;		} else {			bm = selected->root;		}	}	bm = add_bookmark(bm, 1, name, url);	if (!bm) return;	move_bookmark_after_selected(bm, selected);#ifdef BOOKMARKS_RESAVE	write_bookmarks();#endif	/* We touch only the actual bookmark dialog, not all of them;	 * that's right, right? ;-) --pasky */	focus_bookmark(widget_data, box, bm);}static voiddo_add_folder(struct dialog_data *dlg_data, unsigned char *name){	do_add_special_bookmark(dlg_data, name, NULL);}static t_handler_event_statuspush_add_separator_button(struct dialog_data *dlg_data, struct widget_data *widget_data){	do_add_special_bookmark(dlg_data, "-", "");	redraw_dialog(dlg_data, 1);	return EVENT_PROCESSED;}static t_handler_event_statuspush_add_folder_button(struct dialog_data *dlg_data, struct widget_data *widget_data){	input_dialog(dlg_data->win->term, NULL,		     N_("Add folder"), N_("Folder name"),		     dlg_data, NULL,		     MAX_STR_LEN, NULL, 0, 0, NULL,		     (void (*)(void *, unsigned char *)) do_add_folder,		     NULL);	return EVENT_PROCESSED;}/**** EDIT ***********************************************************//* Called when an edit is complete. */static voidbookmark_edit_done(void *data) {	struct dialog *dlg = data;	struct bookmark *bm = (struct bookmark *) dlg->udata2;	update_bookmark(bm, dlg->widgets[0].data, dlg->widgets[1].data);	object_unlock(bm);#ifdef BOOKMARKS_RESAVE	write_bookmarks();#endif}static voidbookmark_edit_cancel(struct dialog *dlg) {	struct bookmark *bm = (struct bookmark *) dlg->udata2;	object_unlock(bm);}/* Called when the edit button is pushed */static t_handler_event_statuspush_edit_button(struct dialog_data *dlg_data, struct widget_data *edit_btn){	struct listbox_data *box = get_dlg_listbox_data(dlg_data);	/* Follow the bookmark */	if (box->sel) {		struct bookmark *bm = (struct bookmark *) box->sel->udata;		const unsigned char *name = bm->title;		const unsigned char *url = bm->url;		object_lock(bm);		do_edit_dialog(dlg_data->win->term, 1, N_("Edit bookmark"),			       name, url,			       (struct session *) dlg_data->dlg->udata, dlg_data,			       bookmark_edit_done, bookmark_edit_cancel,			       (void *) bm, EDIT_DLG_ADD);	}	return EVENT_PROCESSED;}/**** MOVE ***********************************************************/static struct bookmark *move_cache_root_avoid;static voidupdate_depths(struct listbox_item *parent){	struct listbox_item *item;	foreach (item, parent->child) {		item->depth = parent->depth + 1;		if (item->type == BI_FOLDER)			update_depths(item);	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?