Revision 391 src/mh.c
| mh.c (revision 391) | ||
|---|---|---|
| 886 | 886 |
return new_item; |
| 887 | 887 |
} |
| 888 | 888 |
|
| 889 |
static gint mh_rename_folder(Folder *folder, FolderItem *item,
|
|
| 890 |
const gchar *name)
|
|
| 889 |
static gint mh_move_folder_real(Folder *folder, FolderItem *item,
|
|
| 890 |
FolderItem *new_parent, const gchar *name)
|
|
| 891 | 891 |
{
|
| 892 |
gchar *fs_name; |
|
| 893 | 892 |
gchar *oldpath; |
| 894 |
gchar *dirname; |
|
| 895 | 893 |
gchar *newpath; |
| 896 |
gchar *paths[2]; |
|
| 897 |
|
|
| 898 |
g_return_val_if_fail(folder != NULL, -1); |
|
| 899 |
g_return_val_if_fail(item != NULL, -1); |
|
| 900 |
g_return_val_if_fail(item->path != NULL, -1); |
|
| 901 |
g_return_val_if_fail(name != NULL, -1); |
|
| 902 |
|
|
| 903 |
oldpath = folder_item_get_path(item); |
|
| 904 |
dirname = g_dirname(oldpath); |
|
| 905 |
fs_name = g_filename_from_utf8(name, -1, NULL, NULL, NULL); |
|
| 906 |
newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, |
|
| 907 |
fs_name ? fs_name : name, NULL); |
|
| 908 |
g_free(fs_name); |
|
| 909 |
g_free(dirname); |
|
| 910 |
|
|
| 911 |
if (rename(oldpath, newpath) < 0) {
|
|
| 912 |
FILE_OP_ERROR(oldpath, "rename"); |
|
| 913 |
g_free(oldpath); |
|
| 914 |
g_free(newpath); |
|
| 915 |
return -1; |
|
| 916 |
} |
|
| 917 |
|
|
| 918 |
g_free(oldpath); |
|
| 919 |
g_free(newpath); |
|
| 920 |
|
|
| 921 |
if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
|
|
| 922 |
dirname = g_dirname(item->path); |
|
| 923 |
newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL); |
|
| 924 |
g_free(dirname); |
|
| 925 |
} else |
|
| 926 |
newpath = g_strdup(name); |
|
| 927 |
|
|
| 928 |
g_free(item->name); |
|
| 929 |
item->name = g_strdup(name); |
|
| 930 |
|
|
| 931 |
paths[0] = g_strdup(item->path); |
|
| 932 |
paths[1] = newpath; |
|
| 933 |
g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, |
|
| 934 |
mh_rename_folder_func, paths); |
|
| 935 |
|
|
| 936 |
g_free(paths[0]); |
|
| 937 |
g_free(paths[1]); |
|
| 938 |
|
|
| 939 |
return 0; |
|
| 940 |
} |
|
| 941 |
|
|
| 942 |
static gint mh_move_folder(Folder *folder, FolderItem *item, |
|
| 943 |
FolderItem *new_parent) |
|
| 944 |
{
|
|
| 945 |
gchar *oldpath; |
|
| 946 |
gchar *newpath; |
|
| 894 |
gchar *dirname; |
|
| 947 | 895 |
gchar *new_dir; |
| 948 |
gchar *name; |
|
| 896 |
gchar *name_;
|
|
| 949 | 897 |
gchar *utf8_name; |
| 950 | 898 |
gchar *paths[2]; |
| 951 | 899 |
|
| 952 | 900 |
g_return_val_if_fail(folder != NULL, -1); |
| 953 | 901 |
g_return_val_if_fail(item != NULL, -1); |
| 954 |
g_return_val_if_fail(item->path != NULL, -1); |
|
| 955 |
g_return_val_if_fail(new_parent != NULL, -1); |
|
| 956 |
g_return_val_if_fail(item != new_parent, -1); |
|
| 957 |
g_return_val_if_fail(item->parent != new_parent, -1); |
|
| 958 | 902 |
g_return_val_if_fail(folder == item->folder, -1); |
| 959 |
g_return_val_if_fail(item->folder == new_parent->folder, -1); |
|
| 960 |
|
|
| 961 |
if (g_node_is_ancestor(item->node, new_parent->node)) {
|
|
| 962 |
g_warning("folder to be moved is ancestor of new parent\n");
|
|
| 963 |
return -1; |
|
| 903 |
g_return_val_if_fail(item->path != NULL, -1); |
|
| 904 |
g_return_val_if_fail(new_parent != NULL || name != NULL, -1); |
|
| 905 |
if (new_parent) {
|
|
| 906 |
g_return_val_if_fail(item != new_parent, -1); |
|
| 907 |
g_return_val_if_fail(item->parent != new_parent, -1); |
|
| 908 |
g_return_val_if_fail(item->folder == new_parent->folder, -1); |
|
| 909 |
if (g_node_is_ancestor(item->node, new_parent->node)) {
|
|
| 910 |
g_warning("folder to be moved is ancestor of new parent\n");
|
|
| 911 |
return -1; |
|
| 912 |
} |
|
| 964 | 913 |
} |
| 965 | 914 |
|
| 966 | 915 |
oldpath = folder_item_get_path(item); |
| 967 |
name = g_path_get_basename(oldpath); |
|
| 968 |
utf8_name = g_filename_to_utf8(name, -1, NULL, NULL, NULL); |
|
| 969 |
if (!utf8_name) |
|
| 916 |
if (new_parent) {
|
|
| 917 |
if (name) {
|
|
| 918 |
name_ = g_filename_from_utf8(name, -1, NULL, NULL, |
|
| 919 |
NULL); |
|
| 920 |
if (!name_) |
|
| 921 |
name_ = g_strdup(name); |
|
| 922 |
utf8_name = g_strdup(name); |
|
| 923 |
} else {
|
|
| 924 |
name_ = g_path_get_basename(oldpath); |
|
| 925 |
utf8_name = g_filename_to_utf8(name_, -1, NULL, NULL, |
|
| 926 |
NULL); |
|
| 927 |
if (!utf8_name) |
|
| 928 |
utf8_name = g_strdup(name_); |
|
| 929 |
} |
|
| 930 |
new_dir = folder_item_get_path(new_parent); |
|
| 931 |
newpath = g_strconcat(new_dir, G_DIR_SEPARATOR_S, name_, NULL); |
|
| 932 |
g_free(new_dir); |
|
| 933 |
} else {
|
|
| 934 |
name_ = g_filename_from_utf8(name, -1, NULL, NULL, NULL); |
|
| 970 | 935 |
utf8_name = g_strdup(name); |
| 971 |
new_dir = folder_item_get_path(new_parent); |
|
| 972 |
newpath = g_strconcat(new_dir, G_DIR_SEPARATOR_S, name, NULL); |
|
| 973 |
g_free(new_dir); |
|
| 936 |
dirname = g_dirname(oldpath); |
|
| 937 |
newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, |
|
| 938 |
name_ ? name_ : name, NULL); |
|
| 939 |
g_free(dirname); |
|
| 940 |
} |
|
| 941 |
g_free(name_); |
|
| 974 | 942 |
|
| 975 | 943 |
if (is_file_entry_exist(newpath)) {
|
| 976 | 944 |
g_warning("%s already exists\n", newpath);
|
| ... | ... | |
| 981 | 949 |
} |
| 982 | 950 |
|
| 983 | 951 |
debug_print("mh_move_folder: rename(%s, %s)\n", oldpath, newpath);
|
| 984 |
g_free(name); |
|
| 985 | 952 |
|
| 986 | 953 |
if (rename(oldpath, newpath) < 0) {
|
| 987 | 954 |
FILE_OP_ERROR(oldpath, "rename"); |
| ... | ... | |
| 994 | 961 |
g_free(oldpath); |
| 995 | 962 |
g_free(newpath); |
| 996 | 963 |
|
| 997 |
g_node_unlink(item->node); |
|
| 998 |
g_node_append(new_parent->node, item->node); |
|
| 999 |
item->parent = new_parent; |
|
| 964 |
if (new_parent) {
|
|
| 965 |
g_node_unlink(item->node); |
|
| 966 |
g_node_append(new_parent->node, item->node); |
|
| 967 |
item->parent = new_parent; |
|
| 968 |
if (new_parent->path != NULL) {
|
|
| 969 |
newpath = g_strconcat(new_parent->path, |
|
| 970 |
G_DIR_SEPARATOR_S, utf8_name, |
|
| 971 |
NULL); |
|
| 972 |
g_free(utf8_name); |
|
| 973 |
} else |
|
| 974 |
newpath = utf8_name; |
|
| 975 |
} else {
|
|
| 976 |
if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
|
|
| 977 |
dirname = g_dirname(item->path); |
|
| 978 |
newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, |
|
| 979 |
utf8_name, NULL); |
|
| 980 |
g_free(dirname); |
|
| 981 |
g_free(utf8_name); |
|
| 982 |
} else |
|
| 983 |
newpath = utf8_name; |
|
| 984 |
} |
|
| 1000 | 985 |
|
| 1001 |
if (new_parent->path != NULL) {
|
|
| 1002 |
newpath = g_strconcat(new_parent->path, G_DIR_SEPARATOR_S, |
|
| 1003 |
utf8_name, NULL); |
|
| 1004 |
g_free(utf8_name); |
|
| 1005 |
} else |
|
| 1006 |
newpath = utf8_name; |
|
| 986 |
if (name) {
|
|
| 987 |
g_free(item->name); |
|
| 988 |
item->name = g_strdup(name); |
|
| 989 |
} |
|
| 1007 | 990 |
|
| 1008 | 991 |
paths[0] = g_strdup(item->path); |
| 1009 | 992 |
paths[1] = newpath; |
| ... | ... | |
| 1016 | 999 |
return 0; |
| 1017 | 1000 |
} |
| 1018 | 1001 |
|
| 1002 |
static gint mh_move_folder(Folder *folder, FolderItem *item, |
|
| 1003 |
FolderItem *new_parent) |
|
| 1004 |
{
|
|
| 1005 |
return mh_move_folder_real(folder, item, new_parent, NULL); |
|
| 1006 |
} |
|
| 1007 |
|
|
| 1008 |
static gint mh_rename_folder(Folder *folder, FolderItem *item, |
|
| 1009 |
const gchar *name) |
|
| 1010 |
{
|
|
| 1011 |
return mh_move_folder_real(folder, item, NULL, name); |
|
| 1012 |
} |
|
| 1013 |
|
|
| 1019 | 1014 |
static gint mh_remove_folder(Folder *folder, FolderItem *item) |
| 1020 | 1015 |
{
|
| 1021 | 1016 |
gchar *path; |
Also available in: Unified diff