📄 bookmarks.c
字号:
/*************************************************************************** * Copyright (C) 2003 by zhong * * zhongz@163.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ #include <string.h>#include <stdio.h>#include <time.h>#include <gtk/gtk.h>#include "interface.h"// 设定书签个数#define ITEMMAX 500typedef struct _BookMarks BookMarks;struct _BookMarks { ChmSee *default_window; GtkWidget *ok_button;};struct bookmark_info { gchar *b_id; gchar *b_chm; gchar *b_title; gchar *b_path;} bm_info[ITEMMAX];static void chmsee_delete_bookmarks_item_go(const gchar *path, int lastitem);static void chmsee_delete_bookmarks_item(const gchar *path, gchar *item_id);static void chmsee_bookmark_create_id(ChmSee *window);static void chmsee_save_bookmarks(ChmSee *window);static void dialog_ok_button(GtkWidget *widget, gpointer data);static void dialog_bookmark_entry_changed (GtkEntry *entry, BookMarks *bookmark_window);static void chmsee_delete_bookmarks_item_go(const gchar *path, int lastitem) { int i; FILE *fp; g_print("have %d item\n", lastitem); fp = fopen(path, "w"); if (lastitem > ITEMMAX) { lastitem = ITEMMAX; } for (i=0; i<lastitem; i++) { fprintf(fp, bm_info[i].b_id); fprintf(fp, bm_info[i].b_chm); fprintf(fp, bm_info[i].b_title); fprintf(fp, bm_info[i].b_path); } fclose(fp);}static void chmsee_delete_bookmarks_item(const gchar *path, gchar *item_id) { gchar *get_tmp; gchar *ch; gchar *get_id = NULL; FILE *fp; char lineBuf[256]; int label = 0; int lastitem = 0; fp = fopen(path, "r"); if (!fp) { return; } while(fgets(lineBuf,512,fp)!=NULL) { if (lastitem < ITEMMAX) { if (( ch = g_strrstr (lineBuf,"["))) { get_tmp = g_strdup( ch + 1); get_id = g_strndup(get_tmp, strlen(get_tmp)-2); bm_info[lastitem].b_id = g_strdup(ch); label = 1; } if (!(g_strcasecmp(get_id, item_id) == 0)) { if ((ch = g_strrstr(lineBuf, "CHM="))) { bm_info[lastitem].b_chm = g_strdup(ch); label++; } else if ((ch = g_strrstr(lineBuf, "TITLE="))) { bm_info[lastitem].b_title = g_strdup(ch); label++; } else if ((ch = g_strrstr(lineBuf, "PATH="))) { bm_info[lastitem].b_path = g_strdup(ch); label++; } } } if (label ==4 ) { get_id = NULL; label = 0; lastitem++; } } fclose(fp); g_free(get_id); chmsee_delete_bookmarks_item_go(path, lastitem);}void chmsee_load_bookmarks(ChmSee *window) { gchar *get_tmp; gchar *get_filename = NULL; gchar *get_title = NULL; gchar *get_path = NULL; gchar *get_id = NULL; gchar *ch; FILE *fp; char lineBuf[256]; int label = 0; fp = fopen(window->bookmarks, "r"); if (!fp) { return; } while(fgets(lineBuf,512,fp)!=NULL) { if (( ch = g_strrstr (lineBuf,"["))) { get_tmp = g_strdup( ch + 1); get_id = g_strndup(get_tmp, strlen(get_tmp)-2); label = 1; get_filename = NULL; get_title = NULL; get_path = NULL; } if ((ch = g_strrstr(lineBuf, "CHM="))) { get_tmp = g_strdup(ch + 4); get_filename = g_strndup(get_tmp, strlen(get_tmp)-1); label = 2; } else if ((ch = g_strrstr(lineBuf, "TITLE="))) { get_tmp = g_strdup(ch + 6); get_title = g_strndup(get_tmp, strlen(get_tmp)-1); label = 3; } else if ((ch = g_strrstr(lineBuf, "PATH="))) { get_tmp = g_strdup(ch + 5); get_path = g_strndup(get_tmp, strlen(get_tmp)-1); label = 4; } if (label ==4 ) { if (g_strcasecmp(window->chm_name, get_filename) == 0) { GtkTreeView *tree = GTK_TREE_VIEW(window->nb_bookmark_treeview); GtkTreeModel *model = gtk_tree_view_get_model (tree); GtkTreeIter iter; gtk_tree_store_append(GTK_TREE_STORE (model), &iter, NULL); gtk_tree_store_set (GTK_TREE_STORE (model), &iter, TITLE_COLUMN, get_title, FILENAME_COLUMN, get_path, ID_COLUMN, get_id, -1); } label = 0; } } fclose(fp); g_free(get_filename); g_free(get_id); g_free(get_title); g_free(get_path);}static voidchmsee_bookmark_create_id(ChmSee *window) { struct tm *ptr; time_t lt; lt = time(NULL); ptr = localtime(<); window->bookmark_id = g_strdup_printf("%d%d%d%d%d%d", ptr->tm_year, ptr->tm_mon, ptr->tm_mday, ptr->tm_hour, ptr->tm_min, ptr->tm_sec);}static void chmsee_save_bookmarks(ChmSee *window) { FILE *file; gchar *filepath; gchar *title; gchar *path; gchar *timeid; timeid = g_strdup_printf("[%s]\n", window->bookmark_id); filepath = g_strdup_printf("CHM=%s\n", window->chm_name); title = g_strdup_printf("TITLE=%s\n", window->bookmark_title); path = g_strdup_printf("PATH=%s\n", window->bookmark_uri); file = fopen(window->bookmarks, "a+"); fprintf(file, timeid); fprintf(file, filepath); fprintf(file, title); fprintf(file, path); fclose(file);}void chmsee_bookmark_treeview_add(GtkWidget *widget, gpointer data) { ChmSee *window = (ChmSee *) data; GtkTreeView *tree = GTK_TREE_VIEW(window->nb_bookmark_treeview); GtkTreeModel *model = gtk_tree_view_get_model (tree); GtkTreeIter iter; if (window->bookmark_uri) { chmsee_bookmark_create_id(window); gtk_tree_store_append(GTK_TREE_STORE (model), &iter, NULL); gtk_tree_store_set (GTK_TREE_STORE (model), &iter, TITLE_COLUMN, window->bookmark_title, FILENAME_COLUMN, window->bookmark_uri, ID_COLUMN, window->bookmark_id, -1); chmsee_save_bookmarks(window); }}voidchmsee_bookmark_treeview_remove (GtkWidget *widget, gpointer data){ ChmSee *window = (ChmSee *) data; GtkTreeView *treeview = GTK_TREE_VIEW(window->nb_bookmark_treeview); GtkTreeIter iter; GtkTreeModel *model = gtk_tree_view_get_model (treeview); GtkTreeSelection *selection = gtk_tree_view_get_selection (treeview); GValue id_value = {0,}; const gchar *path; gchar *item_id; if (gtk_tree_selection_get_selected (selection, NULL, &iter)) { path = (const gchar *) window->bookmarks; gtk_tree_model_get_value (model, &iter, ID_COLUMN, &id_value); item_id = g_strdup_printf("%s", g_value_get_string (&id_value)); gtk_tree_store_remove (GTK_TREE_STORE (model), &iter); chmsee_delete_bookmarks_item(path, item_id); g_value_unset (&id_value); }}static void dialog_ok_button(GtkWidget *widget, gpointer data) { ChmSee *window = (ChmSee *) data; chmsee_bookmark_treeview_add ( window->nb_bookmark_treeview, window ); gtk_widget_destroy (gtk_widget_get_toplevel (widget));}static void dialog_bookmark_entry_changed (GtkEntry *entry, BookMarks *bookmark_window) { const gchar *text; gint text_length; ChmSee *window = (ChmSee *) bookmark_window->default_window; text = gtk_entry_get_text (GTK_ENTRY (entry)); text_length = strlen(text); window->bookmark_title = (gchar *) text; if (text_length >= 2) { gtk_widget_set_sensitive (bookmark_window->ok_button, TRUE); } else { gtk_widget_set_sensitive (bookmark_window->ok_button, FALSE); }}void create_bookmark_dialog (ChmSee *window){ GtkWidget *bookmark_dialog; GtkWidget *dialog_vbox; GtkWidget *bookmark_dialog_entry; GtkWidget *dialog_action_area; GtkWidget *cancel_button; BookMarks *bookmark_window; bookmark_window = g_new0(BookMarks, 1); bookmark_window->default_window = window; bookmark_dialog = gtk_dialog_new (); gtk_container_set_border_width (GTK_CONTAINER (bookmark_dialog), 5); gtk_window_set_title (GTK_WINDOW (bookmark_dialog), "输入书签名称"); gtk_window_set_position (GTK_WINDOW (bookmark_dialog), GTK_WIN_POS_CENTER); gtk_window_set_resizable (GTK_WINDOW (bookmark_dialog), FALSE); dialog_vbox = GTK_DIALOG (bookmark_dialog)->vbox; gtk_widget_show (dialog_vbox); bookmark_dialog_entry = gtk_entry_new (); gtk_entry_set_text(GTK_ENTRY(bookmark_dialog_entry), window->bookmark_title); gtk_widget_show (bookmark_dialog_entry); gtk_box_pack_start (GTK_BOX (dialog_vbox), bookmark_dialog_entry, TRUE, TRUE, 5); g_signal_connect (bookmark_dialog_entry, "changed", G_CALLBACK (dialog_bookmark_entry_changed), bookmark_window); dialog_action_area = GTK_DIALOG (bookmark_dialog)->action_area; gtk_widget_show (dialog_action_area); gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area), GTK_BUTTONBOX_END); cancel_button = gtk_button_new_from_stock ("gtk-cancel"); gtk_widget_show (cancel_button); gtk_dialog_add_action_widget (GTK_DIALOG (bookmark_dialog), cancel_button, GTK_RESPONSE_CANCEL); GTK_WIDGET_SET_FLAGS (cancel_button, GTK_CAN_DEFAULT); bookmark_window->ok_button = gtk_button_new_from_stock ("gtk-ok"); gtk_widget_show (bookmark_window->ok_button); gtk_dialog_add_action_widget (GTK_DIALOG (bookmark_dialog), bookmark_window->ok_button, GTK_RESPONSE_OK); GTK_WIDGET_SET_FLAGS (bookmark_window->ok_button, GTK_CAN_DEFAULT); g_signal_connect_swapped ((gpointer) bookmark_dialog, "destroy", G_CALLBACK (gtk_widget_destroy), GTK_OBJECT (bookmark_dialog)); g_signal_connect_swapped ((gpointer) cancel_button, "clicked", G_CALLBACK (gtk_widget_destroy), GTK_OBJECT (bookmark_dialog)); g_signal_connect ((gpointer) bookmark_window->ok_button, "clicked", G_CALLBACK (dialog_ok_button), window); gtk_widget_show(bookmark_dialog);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -