?? addressbook.c
字號:
#include <stdlib.h>#include <string.h>#include "glib.h"#include "emtk.h"#include "laf/laf.h"#include "glist_stable.h"#include "db.h"#include "common_util.h"#include "addressbook.h"#include "addressbook_edit.h"#include "i18n.h"#ifdef FONT_TABLE #include "font_table.h"#else #define lookupFont(a, b) (FONT1) #define init_font_table(a) #define unload_font_table()#endif#ifdef LAUNCHER_SUPPORT #include "deskmsg.h"#endif#define MAX_SEARCH_CHARS 20typedef struct { int address_id; char *title; char *phone;} AddressListItem;#define ADDRESSLISTITEM(p) ((AddressListItem *) p)int font1_idx;int app_window_id, table_id, entry_id, new_btn_id;GList *address_list;int database_context;int edit_window = 0;void addressbook_ime_activate (int flag){#ifdef LAUNCHER_SUPPORT DESKTOP_CHANNEL channel; MSG msg; int msgLen; msgLen = msg_command_input_control( (flag) ? MSG_COMMAND_INPUT_ACTIVATE : MSG_COMMAND_INPUT_DEACTIVATE, &msg); desktop_channel_connect_specified_channel_name(&channel, "/tmp/ime"); desktop_channel_send(&channel, &msg, msgLen); desktop_channel_disconnect(&channel);#endif }static gint addressListCompareFunc(gconstpointer a, gconstpointer b);static void makeItemFromAddress(AddressListItem *item, Address *address);static void destroyAddressListItem(AddressListItem *item) { g_return_if_fail(item != NULL); g_free(item->title); g_free(item->phone); g_free(item);}static int addresslistIdCompareFunc(gconstpointer a, gconstpointer b) { AddressListItem *item = ADDRESSLISTITEM(a); return !(item->address_id == (int) b);}static void updateView() { GList *item; emtk_table_remove_all(table_id); for (item = address_list; item != NULL; item = g_list_next(item)) { emtk_table_append_label_new(table_id, lookupFont("en_US", SMALL_FONT), FALSE, ADDRESSLISTITEM(item->data)->title, 0); emtk_table_append_label_new(table_id, lookupFont("en_US", SMALL_FONT), FALSE, ADDRESSLISTITEM(item->data)->phone, 1); }}static void editWndCallback(AddressbookOperation operation, Address *address) { GList *item; AddressListItem *addr_item; edit_window = 0; switch (operation) { case ADDRESS_NEW: g_warning("%s Address book entry is not new, but new operation is invoked\n", __FUNCTION__); if (address->new && address->dirty && !addressbook_record_empty(address)) { addressbook_record_save(database_context, address); } addr_item = g_new0(AddressListItem, 1); makeItemFromAddress(addr_item, address); address_list = g_list_append(address_list, addr_item); address_list = g_list_sort_stable(address_list, addressListCompareFunc); break; case ADDRESS_MODIFIED: if (!addressbook_record_empty(address) && address->dirty) { addressbook_record_save(database_context, address); } else { break; } item = g_list_find_custom(address_list, (gpointer) address->address_id, addresslistIdCompareFunc); addr_item = ADDRESSLISTITEM(item->data); makeItemFromAddress(addr_item, address); printf("Empty address delete\n"); // empty addressbook entry, delete it. break; case ADDRESS_DELETE: item = g_list_find_custom(address_list, (gpointer) address->address_id, addresslistIdCompareFunc); g_return_if_fail(item != NULL); addr_item = ADDRESSLISTITEM(item->data); destroyAddressListItem(addr_item); address_list = g_list_remove(address_list, item->data); addressbook_record_delete(database_context, address); break;#if 0 case ADDRESS_DUPLICATE: //addressbook_record break;#endif default: g_warning("unknown operation\n"); } updateView();}static gint addressListCompareFunc(gconstpointer a, gconstpointer b) { return strcasecmp(ADDRESSLISTITEM(a)->title, ADDRESSLISTITEM(b)->title);}static void closeApp(int wid, void *data){ int *feedback = (int*) data; GList *item; AddressListItem *addritem; for (item = address_list; item != NULL; item = g_list_next(item)) { addritem = ADDRESSLISTITEM(item->data); destroyAddressListItem(addritem); } g_list_free(address_list); addressbook_ime_activate (FALSE); *feedback = EMTK_APP_WINDOW_DESTROY_WITH_EMTK_CLOSE;}static void tableRowSelected(int id, void *data, int row) { Address *addr; AddressListItem *addr_item; addr_item = ADDRESSLISTITEM(g_list_nth_data(address_list, row)); g_return_if_fail(addr_item != NULL); addr = addressbook_record_load(database_context, addr_item->address_id); edit_window = addressbook_edit_window(addr, editWndCallback);}static int lookup(const char *term) { GList *item; AddressListItem *addr; int length; length = strlen(term); for(item = address_list; item != NULL; item = g_list_next(item)) { addr = ADDRESSLISTITEM(item->data); if (strncasecmp(term, addr->title, length) == 0) return g_list_position(address_list, item); } return -1;}static void search_entry_changed(int eid, void *data, void *userData) { char *term; char *newTerm; int position; term = g_new0(char, MAX_SEARCH_CHARS + 1); emtk_entry_get_value(eid, term, MAX_SEARCH_CHARS); position = lookup(term); if (position >= 0) emtk_table_set_focus(table_id, position); else { newTerm = g_strndup(term, strlen(term) - 1); emtk_entry_set_value(eid, newTerm, strlen(newTerm)); g_free(newTerm); } g_free(term);}static void new_address(int bid, void *data) { Address *addr; addr = addressbook_record_new(); edit_window = addressbook_edit_window(addr, editWndCallback);}static void drawWnd(int window, WindowRect *clientRect) { table_id = emtk_table_new(window, 0, 0, APP_WIN_W - SCROLLBAR_WIDTH, ADDRESSBOOK_LIST_HEIGHT, 2, ADDRESSBOOK_LIST_SIZE+1, 0); emtk_table_add_column_new(table_id, 200); emtk_table_add_column_new(table_id, 120 - SCROLLBAR_WIDTH); emtk_table_set_highlight_color(table_id, emtk_app_get_hl_color()); emtk_window_add_object(window, table_id); emtk_table_add_vscrollbar_new(table_id, SCROLLBAR_WIDTH, VSCROLLBAR_ALIGN_RIGHT | VSCROLLBAR_ALWAYS_SHOWN); emtk_table_set_one_click_activate(table_id, TRUE); emtk_table_set_selection_callback(table_id, tableRowSelected, NULL); entry_id = emtk_entry_new(window, 0, clientRect->h - 8, emtk_string_display_length(_("Look Up"), lookupFont("en_US", SMALL_FONT)), KEY_TYPE_ANY, 90, MAX_SEARCH_CHARS + 1, lookupFont("en_US", SMALL_FONT), lookupFont("en_US", SMALL_FONT), _("Look Up")); emtk_entry_set_value_change_callback(entry_id, search_entry_changed, NULL); emtk_window_add_object(window, entry_id); new_btn_id = emtk_button_new(window, 145, clientRect->h - 20, 40, 19, lookupFont("en_US", SMALL_FONT), _("New"), 0, new_address, NULL); emtk_window_add_object(window, new_btn_id); #if 0 emtk_window_add_object(window, emtk_button_new(window, 200, clientRect->h - 20, 40, 19, FONT1, "se", 0, searchAddr, NULL));#endif addressbook_ime_activate (TRUE);}static void makeItemFromAddress(AddressListItem *item, Address *addr) { g_return_if_fail(item != NULL); g_return_if_fail(addr != NULL); if (!strEmpty(addr->personal->lastName)) { if (!strEmpty(addr->personal->firstName)) { item->title = g_strconcat(addr->personal->lastName, ", ", addr->personal->firstName, NULL); } else { item->title = g_strdup(addr->personal->lastName); } } else { if (!strEmpty(addr->personal->firstName)) { item->title = g_strdup(addr->personal->firstName); } else { if (!strEmpty(addr->business->company)) { item->title = g_strdup(addr->business->company); } else { item->title = g_strdup(_("-Unnamed-")); } } } if (!strEmpty(addr->personal->phone)) { item->phone = g_strconcat(addr->personal->phone, "P", NULL); } else if (!strEmpty(addr->personal->mobile)) { item->phone = g_strconcat(addr->personal->mobile, "MP", NULL); } else if (!strEmpty(addr->personal->fax)) { item->phone = g_strconcat(addr->personal->fax, "FP", NULL); } else if (!strEmpty(addr->business->phone)) { item->phone = g_strconcat(addr->business->phone, "B", NULL); } else if (!strEmpty(addr->business->mobile)) { item->phone = g_strconcat(addr->business->mobile, "MB", NULL); } else if (!strEmpty(addr->business->fax)) { item->phone = g_strconcat(addr->business->fax, "FB", NULL); } else { item->phone = g_strdup(""); } item->address_id = addr->address_id; }static int addItem(int key, char *data) { Address *addr; AddressListItem *item; item = g_new0(AddressListItem, 1); deserialize(&addr, data); makeItemFromAddress(item, addr); address_list = g_list_append(address_list, item); addressbook_record_destroy(addr); return TRUE;}static void init() { app_window_id = emtk_app_window_new(_("Address"), lookupFont("en_US", SMALL_FONT), drawWnd, closeApp, NULL); db_iterate(database_context, addItem); address_list = g_list_sort_stable(address_list, addressListCompareFunc); updateView();}#ifdef LAUNCHER_SUPPORTstatic void addressbook_desktop_msg(MSG *msg, gpointer user_data) { g_return_if_fail(msg != NULL); if (msg->type == MSG_TYPE_COMMAND && msg->subtype == MSG_COMMAND_CLOSE) { if (edit_window) emtk_app_close_window(edit_window); emtk_app_close_window(app_window_id); }}#endifint main(int argc, char **argv) { #ifdef LAUNCHER_SUPPORT DESKTOP_CHANNEL channel; if(!desktop_channel_listen_connect(&channel, addressbook_desktop_msg, NULL)) { printf("Failed opening channel for listening\n"); return 1; }#endif init_font_table(NULL); i18n_init(); database_context = addressbook_record_db_open(); if (!(database_context > 0)) { fprintf(stderr, "Error opening addressbook database, exiting\n"); exit(1); } emtk_app_init(&argc, &argv); emtk_app_root(APP_WIN_X, APP_WIN_Y, APP_WIN_W, APP_WIN_H); init(); emtk_app_run(); addressbook_record_db_close(database_context); unload_font_table(); #ifdef LAUNCHER_SUPPORT desktop_channel_listen_disconnect(&channel);#endif exit(0); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -