-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg.h
204 lines (173 loc) · 5.73 KB
/
img.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
void add_images_from_directory(GtkWidget *widget, gpointer user_data)
{
gtk_list_store_clear(GTK_LIST_STORE(imglist_store));
gchar dir_path[8192];
snprintf(dir_path, sizeof(dir_path), "%s%s%s/%s_files", home_dir, notes_dir, current_workspace, current_file);
GFile *directory = g_file_new_for_path(dir_path);
if (!g_file_query_exists(directory, NULL))
{
g_print("This note don't have pictures.\n");
g_object_unref(directory);
return;
}
GError *currenterror = NULL;
GFileEnumerator *enumerator = g_file_enumerate_children(directory, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NONE, NULL, ¤terror);
if (currenterror != NULL)
{
g_print("Failed to enum files on directory: %s\n", error->message);
g_error_free(error);
g_object_unref(directory);
return;
}
GFileInfo *fileinfo;
while ((fileinfo = g_file_enumerator_next_file(enumerator, NULL, &error)) != NULL)
{
const gchar *filename = g_file_info_get_name(fileinfo);
gchar *file_path = g_build_filename(dir_path, filename, NULL);
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(file_path, NULL);
if (pixbuf != NULL)
{
gint original_width = gdk_pixbuf_get_width(pixbuf);
gint target_width = 90;
gint target_height = gdk_pixbuf_get_height(pixbuf) * target_width / original_width;
GdkPixbuf *scaled_pixbuf = gdk_pixbuf_scale_simple(pixbuf, target_width, target_height, GDK_INTERP_BILINEAR);
GtkTreeIter iter;
gtk_list_store_append(imglist_store, &iter);
gtk_list_store_set(imglist_store, &iter, 0, scaled_pixbuf, -1);
g_object_unref(pixbuf);
g_object_unref(scaled_pixbuf);
}
g_free(file_path);
g_object_unref(fileinfo);
}
g_file_enumerator_close(enumerator, NULL, NULL);
g_object_unref(enumerator);
g_object_unref(directory);
}
void add_image(GtkWidget *widget, gpointer user_data)
{
GtkTreeIter iter;
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
gint res;
dialog = gtk_file_chooser_dialog_new("Open File",
GTK_WINDOW(window),
action,
"_Cancel",
GTK_RESPONSE_CANCEL,
"_Open",
GTK_RESPONSE_ACCEPT,
NULL);
res = gtk_dialog_run(GTK_DIALOG(dialog));
if (res == GTK_RESPONSE_ACCEPT)
{
char *filename;
GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog);
filename = gtk_file_chooser_get_filename(chooser);
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
if (pixbuf != NULL)
{
int original_width = gdk_pixbuf_get_width(pixbuf);
int target_width = 100;
int target_height = gdk_pixbuf_get_height(pixbuf) * target_width / original_width;
char file_path[6144];
snprintf(file_path, sizeof(file_path), "%s%s%s/%s_files", home_dir, notes_dir, current_workspace, current_file);
g_mkdir_with_parents(file_path, 0755);
// Always use .png extension for the copied files
char unique_filename[8192];
int counter = 0;
do
{
snprintf(unique_filename, sizeof(unique_filename), "%s/%d.png", file_path, counter);
counter++;
} while (g_file_test(unique_filename, G_FILE_TEST_EXISTS));
GFile *source_file = g_file_new_for_path(filename);
GFile *destination_file = g_file_new_for_path(unique_filename);
if (g_file_copy(source_file, destination_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, NULL))
{
GdkPixbuf *scaled_pixbuf = gdk_pixbuf_scale_simple(pixbuf, target_width, target_height, GDK_INTERP_BILINEAR);
gtk_list_store_append(imglist_store, &iter);
gtk_list_store_set(imglist_store, &iter, 0, scaled_pixbuf, -1);
g_object_unref(pixbuf);
g_object_unref(scaled_pixbuf);
}
else
{
g_print("Error can't open output folder.\n");
}
g_object_unref(source_file);
g_object_unref(destination_file);
}
}
gtk_widget_destroy(dialog);
}
static void on_submenu_imglist_item1_selected(void)
{
char file_path[8192];
file_path[0] = '\0';
snprintf(file_path, sizeof(file_path), "%s%s%s/%s_files/%d.png", home_dir, notes_dir, current_workspace, current_file, selfromtreeview);
char *command = g_strdup_printf("xdg-open \"%s\"", file_path);
system(command);
g_free(command);
}
int numeric_file_compare(const void *a, const void *b)
{
const char *str1 = *(const char **)a;
const char *str2 = *(const char **)b;
int num1 = atoi(str1);
int num2 = atoi(str2);
return num1 - num2;
}
static void on_submenu_imglist_item2_selected(GtkWidget *widget, gpointer user_data)
{
char file_path[8192];
file_path[0] = '\0';
snprintf(file_path, sizeof(file_path), "%s%s%s/%s_files/%d.png", home_dir, notes_dir, current_workspace, current_file, selfromtreeview);
if (remove(file_path) == 0)
{
g_print("deleted file:%s\n", file_path );
DIR *dir;
struct dirent *entry;
char folder_path[4096];
snprintf(folder_path, sizeof(folder_path), "%s%s%s/%s_files", home_dir, notes_dir, current_workspace, current_file);
dir = opendir(folder_path);
if (dir != NULL)
{
char **file_names = NULL;
int file_count = 0;
while ((entry = readdir(dir)) != NULL)
{
if (entry->d_type == DT_REG)
{
if (file_count == 0)
{
file_names = (char **)malloc(sizeof(char *));
}
else
{
file_names = (char **)realloc(file_names, (file_count + 1) * sizeof(char *));
}
file_names[file_count] = strdup(entry->d_name);
file_count++;
}
}
closedir(dir);
qsort(file_names, file_count, sizeof(char *), numeric_file_compare);
for (int i = 0; i < file_count; i++)
{
char old_name[8192];
char new_name[8192];
snprintf(old_name, sizeof(old_name), "%s/%s", folder_path, file_names[i]);
snprintf(new_name, sizeof(new_name), "%s/%d.png", folder_path, i);
rename(old_name, new_name);
free(file_names[i]);
}
free(file_names);
}
gtk_list_store_clear(GTK_LIST_STORE(imglist_store));
add_images_from_directory(GTK_WIDGET(treeview), user_data);
}
else
{
g_print("Error deleting file:%s\n", file_path );
}
}