static gint mh_create_tree(Folder *folder) { gchar *rootpath, *f; cm_return_val_if_fail(folder != NULL, -1); CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1); rootpath = LOCAL_FOLDER(folder)->rootpath; MAKE_DIR_IF_NOT_EXIST(rootpath); CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1); /* Create special directories as needed */ if (folder->inbox != NULL && folder->inbox->path != NULL) f = folder->inbox->path; else f = INBOX_DIR; MAKE_DIR_IF_NOT_EXIST(f); if (folder->outbox != NULL && folder->outbox->path != NULL) f = folder->outbox->path; else f = OUTBOX_DIR; MAKE_DIR_IF_NOT_EXIST(f); if (folder->draft != NULL && folder->draft->path != NULL) f = folder->draft->path; else f = DRAFT_DIR; MAKE_DIR_IF_NOT_EXIST(f); if (folder->queue != NULL && folder->queue->path != NULL) f = folder->queue->path; else f = QUEUE_DIR; MAKE_DIR_IF_NOT_EXIST(f); if (folder->trash != NULL && folder->trash->path != NULL) f = folder->trash->path; else f = TRASH_DIR; MAKE_DIR_IF_NOT_EXIST(f); return 0; }
static gchar *mh_item_get_path(Folder *folder, FolderItem *item) { gchar *folder_path, *path; gchar *real_path; cm_return_val_if_fail(folder != NULL, NULL); cm_return_val_if_fail(item != NULL, NULL); folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath); cm_return_val_if_fail(folder_path != NULL, NULL); /* FIXME: [W32] The code below does not correctly merge relative filenames; there should be a function to handle this. */ if ( !is_relative_filename (folder_path) ) { if (item->path) path = g_strconcat(folder_path, G_DIR_SEPARATOR_S, item->path, NULL); else path = g_strdup(folder_path); } else { if (item->path) path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, folder_path, G_DIR_SEPARATOR_S, item->path, NULL); else path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, folder_path, NULL); } g_free(folder_path); real_path = mh_filename_from_utf8(path); if (!is_dir_exist(real_path) && is_dir_exist(path)) { /* mmh, older version did put utf8 filenames instead of * the correct encoding */ if (g_rename(path, real_path) == 0) folder_item_scan(item); } g_free(path); return real_path; }
static void mh_scan_tree_recursive(FolderItem *item) { Folder *folder; #ifdef G_OS_WIN32 GDir *dir; #else DIR *dp; struct dirent *d; #endif const gchar *dir_name; struct stat s; gchar *real_path, *entry, *utf8entry, *utf8name; gint n_msg = 0; cm_return_if_fail(item != NULL); cm_return_if_fail(item->folder != NULL); folder = item->folder; real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup("."); #ifdef G_OS_WIN32 dir = g_dir_open(real_path, 0, NULL); if (!dir) { g_warning("failed to open directory: %s\n", real_path); g_free(real_path); return; } #else dp = opendir(real_path); if (!dp) { FILE_OP_ERROR(real_path, "opendir"); return; } #endif g_free(real_path); debug_print("scanning %s ...\n", item->path ? item->path : LOCAL_FOLDER(item->folder)->rootpath); if (folder->ui_func) folder->ui_func(folder, item, folder->ui_func_data); #ifdef G_OS_WIN32 while ((dir_name = g_dir_read_name(dir)) != NULL) { #else while ((d = readdir(dp)) != NULL) { dir_name = d->d_name; #endif if (dir_name[0] == '.') continue; utf8name = mh_filename_to_utf8(dir_name); if (item->path) utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S, utf8name, NULL); else utf8entry = g_strdup(utf8name); entry = mh_filename_from_utf8(utf8entry); if ( #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE) d->d_type == DT_DIR || (d->d_type == DT_UNKNOWN && #endif g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode) #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE) ) #endif ) { FolderItem *new_item = NULL; GNode *node; node = item->node; for (node = node->children; node != NULL; node = node->next) { FolderItem *cur_item = FOLDER_ITEM(node->data); gchar *curpath = mh_filename_from_utf8(cur_item->path); if (!strcmp2(curpath, entry)) { new_item = cur_item; g_free(curpath); break; } g_free(curpath); } if (!new_item) { debug_print("new folder '%s' found.\n", entry); new_item = folder_item_new(folder, utf8name, utf8entry); folder_item_append(item, new_item); } if (!item->path) { if (!folder->inbox && !strcmp(dir_name, INBOX_DIR)) { new_item->stype = F_INBOX; folder->inbox = new_item; } else if (!folder->outbox && !strcmp(dir_name, OUTBOX_DIR)) { new_item->stype = F_OUTBOX; folder->outbox = new_item; } else if (!folder->draft && !strcmp(dir_name, DRAFT_DIR)) { new_item->stype = F_DRAFT; folder->draft = new_item; } else if (!folder->queue && !strcmp(dir_name, QUEUE_DIR)) { new_item->stype = F_QUEUE; folder->queue = new_item; } else if (!folder->trash && !strcmp(dir_name, TRASH_DIR)) { new_item->stype = F_TRASH; folder->trash = new_item; } } mh_scan_tree_recursive(new_item); } else if (to_number(dir_name) > 0) n_msg++; g_free(entry); g_free(utf8entry); g_free(utf8name); } #ifdef G_OS_WIN32 g_dir_close(dir); #else closedir(dp); #endif mh_set_mtime(folder, item); } static gboolean mh_rename_folder_func(GNode *node, gpointer data) { FolderItem *item = node->data; gchar **paths = data; const gchar *oldpath = paths[0]; const gchar *newpath = paths[1]; gchar *base; gchar *new_itempath; gint oldpathlen; oldpathlen = strlen(oldpath); if (strncmp(oldpath, item->path, oldpathlen) != 0) { g_warning("path doesn't match: %s, %s\n", oldpath, item->path); return TRUE; } base = item->path + oldpathlen; while (*base == G_DIR_SEPARATOR) base++; if (*base == '\0') new_itempath = g_strdup(newpath); else new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base, NULL); g_free(item->path); item->path = new_itempath; return FALSE; }
static void mh_folder_destroy(Folder *folder) { folder_local_folder_destroy(LOCAL_FOLDER(folder)); }
static void mh_scan_tree_recursive(FolderItem *item) { Folder *folder; GDir *dir; const gchar *dir_name; gchar *real_path, *entry, *utf8entry, *utf8name; gint n_msg = 0; GError *error = NULL; cm_return_if_fail(item != NULL); cm_return_if_fail(item->folder != NULL); folder = item->folder; real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup("."); dir = g_dir_open(real_path, 0, &error); if (!dir) { g_warning("failed to open directory '%s': %s (%d)", real_path, error->message, error->code); g_error_free(error); g_free(real_path); return; } g_free(real_path); debug_print("scanning %s ...\n", item->path ? item->path : LOCAL_FOLDER(item->folder)->rootpath); if (folder->ui_func) folder->ui_func(folder, item, folder->ui_func_data); while ((dir_name = g_dir_read_name(dir)) != NULL) { if (dir_name[0] == '.') continue; utf8name = mh_filename_to_utf8(dir_name); if (item->path) utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S, utf8name, NULL); else utf8entry = g_strdup(utf8name); entry = mh_filename_from_utf8(utf8entry); if (g_file_test(entry, G_FILE_TEST_IS_DIR)) { FolderItem *new_item = NULL; GNode *node; node = item->node; for (node = node->children; node != NULL; node = node->next) { FolderItem *cur_item = FOLDER_ITEM(node->data); gchar *curpath = mh_filename_from_utf8(cur_item->path); if (!strcmp2(curpath, entry)) { new_item = cur_item; g_free(curpath); break; } g_free(curpath); } if (!new_item) { debug_print("new folder '%s' found.\n", entry); new_item = folder_item_new(folder, utf8name, utf8entry); folder_item_append(item, new_item); } if (!item->path) { if (!folder->inbox && !strcmp(dir_name, INBOX_DIR)) { new_item->stype = F_INBOX; folder->inbox = new_item; } else if (!folder->outbox && !strcmp(dir_name, OUTBOX_DIR)) { new_item->stype = F_OUTBOX; folder->outbox = new_item; } else if (!folder->draft && !strcmp(dir_name, DRAFT_DIR)) { new_item->stype = F_DRAFT; folder->draft = new_item; } else if (!folder->queue && !strcmp(dir_name, QUEUE_DIR)) { new_item->stype = F_QUEUE; folder->queue = new_item; } else if (!folder->trash && !strcmp(dir_name, TRASH_DIR)) { new_item->stype = F_TRASH; folder->trash = new_item; } } mh_scan_tree_recursive(new_item); } else if (to_number(dir_name) > 0) n_msg++; g_free(entry); g_free(utf8entry); g_free(utf8name); } g_dir_close(dir); mh_set_mtime(folder, item); }