static gint hybrid_blist_cache_init(HybridConfig *config) { gchar *cache_file_name; HybridBlistCache *cache; xmlnode *root; gint err; g_return_val_if_fail(config != NULL, HYBRID_ERROR); cache_file_name = g_strdup_printf("%s/blist.xml", config->config_path); hybrid_debug_info("config", "init the blist cache from %s", cache_file_name); cache = g_new0(HybridBlistCache, 1); cache->cache_file_name = cache_file_name; config->blist_cache = cache; if (!(root = xmlnode_root_from_file(cache_file_name))) { const gchar *root_string = "<blist></blist>"; root = xmlnode_root(root_string, strlen(root_string)); cache->root = root; goto blist_cache_init_null; } if (!root) { hybrid_debug_error("config", "FATAL, init blist cache"); return HYBRID_ERROR; } cache->root = root; /* Load the cached buddy list */ goto blist_cache_init_fin; blist_cache_init_null: /* initialize the xml context since we don't have local cache */ xmlnode_new_child(root, "accounts"); xmlnode_save_file(root, cache->cache_file_name); blist_cache_init_fin: /* initialize the icon path. */ config->icon_path = g_strdup_printf("%s/icons", config->config_path); err = mkdir(config->icon_path, S_IRWXU|S_IRWXO|S_IRWXG); if (err && access(config->icon_path, R_OK|W_OK)) { hybrid_debug_error("config", "%s,cannot create, read or write", config->icon_path); g_free(config->icon_path); return HYBRID_ERROR; } return HYBRID_OK; }
gint hybrid_pref_init(void) { const gchar *body; gchar *config_path; hybrid_pref = g_new0(HybridPref, 1); if (!(config_path = hybrid_config_get_path())) { hybrid_debug_error("pref", "get config path error."); return HYBRID_ERROR; } hybrid_pref->filename = g_strdup_printf("%s/pref.xml", config_path); g_free(config_path); if (!(hybrid_pref->root = xmlnode_root_from_file(hybrid_pref->filename))) { body = "<pref></pref>"; hybrid_pref->root = xmlnode_root(body, strlen(body)); hybrid_pref_save(); } return HYBRID_OK; }
GSList* hybrid_logs_read(HybridAccount *account, const gchar *id, const gchar *logname) { gchar *log_path = NULL; gchar *log_name = NULL; gchar *tmp; GSList *list = NULL; xmlnode *root = NULL, *node, *child; HybridLogEntry *entry; log_path = hybrid_logs_get_path(account, id); log_name = g_strdup_printf("%s/%s", log_path, logname); root = xmlnode_root_from_file(log_name); if (!root) { hybrid_debug_error("log", "log %s read error.\n", log_name); goto out; } node = xmlnode_child(root); while (node) { if (!xmlnode_has_prop(node, "type")) goto next; entry = g_new0(HybridLogEntry, 1); tmp = xmlnode_prop(node, "type"); if (g_strcmp0(tmp, "o")) { entry->is_send = 1; } else { entry->is_send = 0; } g_free(tmp); child = xmlnode_find(node, "t"); entry->time = xmlnode_content(child); child = xmlnode_find(node, "n"); entry->name = xmlnode_content(child); child = xmlnode_find(node, "c"); entry->content = xmlnode_content(child); list = g_slist_append(list, entry); next: node = xmlnode_next(node); } out: free(log_path); free(log_name); xmlnode_free(root); return list; }
gchar* get_province_name(const gchar *province) { xmlnode *root; xmlnode *node; gchar *name; gchar *value; g_return_val_if_fail(province != NULL, NULL); if (!(root = xmlnode_root_from_file(FETION_RES_DIR"province.xml"))) { return NULL; } if (!(node = xmlnode_child(root)) || g_strcmp0(node->name, "Province")) { hybrid_debug_error("fetion", "get full province name"); return NULL; } for (; node; node = xmlnode_next(node)) { if (!xmlnode_has_prop(node, "id")) { continue; } value = xmlnode_prop(node, "id"); if (g_strcmp0(value, province) == 0) { name = xmlnode_content(node); /* found, do cleanup. */ g_free(value); xmlnode_free(root); return name; } g_free(value); } xmlnode_free(root); return NULL; }
gchar* get_city_name(const gchar *province, const gchar *city) { xmlnode *root; xmlnode *node; gchar *name; gchar *value; if (!(root = xmlnode_root_from_file(FETION_RES_DIR"city.xml"))) { return NULL; } if (!(node = xmlnode_child(root)) || g_strcmp0(node->name, "Province")) { hybrid_debug_error("fetion", "get full city name"); return NULL; } for (; node; node = xmlnode_next(node)) { if (!xmlnode_has_prop(node, "id")) { continue; } value = xmlnode_prop(node, "id"); if (g_strcmp0(value, province) == 0) { /* found, do cleanup. */ g_free(value); goto province_found; } g_free(value); } xmlnode_free(root); return NULL; province_found: if (!(node = xmlnode_child(node)) || g_strcmp0(node->name, "City")) { hybrid_debug_error("fetion", "get full city name"); xmlnode_free(root); return NULL; } for (; node; node = xmlnode_next(node)) { if (!xmlnode_has_prop(node, "id")) { continue; } value = xmlnode_prop(node, "id"); if (g_strcmp0(value, city) == 0) { name= xmlnode_content(node); /* found, do cleanup. */ g_free(value); xmlnode_free(root); return name; } g_free(value); } xmlnode_free(root); return NULL; }
HybridLogs* hybrid_logs_create(HybridAccount *account, const gchar *id) { HybridLogs *log; const gchar *config_path; gchar *log_path; gchar *account_path; gchar *final_path; gchar *proto_path; struct tm *local_time; time_t now; gint e; g_return_val_if_fail(account != NULL, NULL); g_return_val_if_fail(id != NULL, NULL); now = time(NULL); log = g_new0(HybridLogs, 1); log->id = g_strdup(id); log->time = now; config_path = hybrid_config_get_path(); log_path = g_strdup_printf("%s/logs", config_path); proto_path = g_strdup_printf("%s/%s", log_path, account->proto->info->name); g_free(log_path); e = mkdir(proto_path, S_IRWXU|S_IRWXO|S_IRWXG); if (e && access(proto_path, R_OK|W_OK)) { hybrid_debug_error("logs", "%s,cannot create, read or write", proto_path); g_free(proto_path); return NULL; } /* create log directory for a specified account. */ account_path = g_strdup_printf("%s/%s", proto_path, account->username); g_free(proto_path); e = mkdir(account_path, S_IRWXU|S_IRWXO|S_IRWXG); if (e && access(account_path, R_OK|W_OK)) { hybrid_debug_error("logs", "%s,cannot create, read or write", account_path); g_free(account_path); return NULL; } /* create log directory for a specified account. */ final_path = g_strdup_printf("%s/%s", account_path, id); g_free(account_path); e = mkdir(final_path, S_IRWXU|S_IRWXO|S_IRWXG); if (e && access(final_path, R_OK|W_OK)) { hybrid_debug_error("logs", "%s,cannot create, read or write", final_path); g_free(final_path); return NULL; } /* create/get the log file for a specified chat id, with the name * in form of id_date.xml */ local_time = localtime(&now); log->log_path = g_strdup_printf("%s/%d_%d_%d.xml", final_path, local_time->tm_year + 1900, local_time->tm_mon + 1, local_time->tm_mday); g_free(final_path); log->root = xmlnode_root_from_file(log->log_path); return log; }