1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2004-2018 the Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #include "defs.h"
21 
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 
26 #include "foldersort.h"
27 #include "inc.h"
28 #include "utils.h"
29 #include "prefs_common.h"
30 
31 enum {
32 	FOLDERSORT_COL_NAME,
33 	FOLDERSORT_COL_PTR,
34 	N_FOLDERSORT_COLS
35 };
36 
37 typedef struct _FolderSortDialog FolderSortDialog;
38 
39 struct _FolderSortDialog
40 {
41 	GtkWidget *window;
42 	GtkWidget *moveup_btn;
43 	GtkWidget *movedown_btn;
44 	GtkWidget *folderlist;
45 	gint rows;
46 };
47 
destroy_dialog(FolderSortDialog * dialog)48 static void destroy_dialog(FolderSortDialog *dialog)
49 {
50 	inc_unlock();
51 	gtk_widget_destroy(dialog->window);
52 
53 	g_free(dialog);
54 }
55 
ok_clicked(GtkWidget * widget,FolderSortDialog * dialog)56 static void ok_clicked(GtkWidget *widget, FolderSortDialog *dialog)
57 {
58 	Folder *folder;
59 	GtkTreeModel *model;
60 	GtkTreeIter iter;
61 	int i;
62 
63 	model = gtk_tree_view_get_model(GTK_TREE_VIEW(dialog->folderlist));
64 
65 	for (i = 0; i < dialog->rows; i++) {
66 		if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, i))
67 			continue;
68 
69 		gtk_tree_model_get(model, &iter, FOLDERSORT_COL_PTR, &folder, -1);
70 
71 		folder_set_sort(folder, dialog->rows - i);
72 	}
73 
74 	destroy_dialog(dialog);
75 }
76 
cancel_clicked(GtkWidget * widget,FolderSortDialog * dialog)77 static void cancel_clicked(GtkWidget *widget, FolderSortDialog *dialog)
78 {
79 	destroy_dialog(dialog);
80 }
81 
set_selected(FolderSortDialog * dialog)82 static void set_selected(FolderSortDialog *dialog)
83 {
84 	GtkTreeSelection *sel;
85 	GtkTreeModel *model;
86 	GtkTreeIter iter;
87 	GtkTreePath *path;
88 	gint *indices;
89 	gint selected;
90 
91 	/* Get row number of the selected row */
92 	sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->folderlist));
93 	if (!gtk_tree_selection_get_selected(sel, &model, &iter))
94 		return;
95 	path = gtk_tree_model_get_path(model, &iter);
96 	indices = gtk_tree_path_get_indices(path);
97 	selected = indices[0];
98 	gtk_tree_path_free(path);
99 
100 	if (selected >= 0) {
101 		gtk_widget_set_sensitive(dialog->moveup_btn, selected > 0);
102 		gtk_widget_set_sensitive(dialog->movedown_btn, selected < (dialog->rows - 1));
103 	} else {
104 		gtk_widget_set_sensitive(dialog->moveup_btn, FALSE);
105 		gtk_widget_set_sensitive(dialog->movedown_btn, FALSE);
106 	}
107 }
108 
moveup_clicked(GtkWidget * widget,FolderSortDialog * dialog)109 static void moveup_clicked(GtkWidget *widget, FolderSortDialog *dialog)
110 {
111 	GtkTreeSelection *sel;
112 	GtkTreeModel *model;
113 	GtkTreeIter iter, previter;
114 	GtkTreePath *path;
115 
116 	/* Get currently selected iter */
117 	sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->folderlist));
118 	if (!gtk_tree_selection_get_selected(sel, &model, &iter))
119 		return;
120 
121 	/* Now get the iter above it, if any */
122 	/* GTK+2 does not have gtk_tree_model_iter_previous(), so
123 	 * we have to get through GtkPath */
124 	path = gtk_tree_model_get_path(model, &iter);
125 	if (!gtk_tree_path_prev(path)) {
126 		/* No previous path, are we already on top? */
127 		gtk_tree_path_free(path);
128 		return;
129 	}
130 
131 	if (!gtk_tree_model_get_iter(model, &previter, path)) {
132 		/* Eh? */
133 		gtk_tree_path_free(path);
134 		return;
135 	}
136 
137 	gtk_tree_path_free(path);
138 
139 	gtk_list_store_move_before(GTK_LIST_STORE(model), &iter, &previter);
140 
141 	set_selected(dialog);
142 }
143 
movedown_clicked(GtkWidget * widget,FolderSortDialog * dialog)144 static void movedown_clicked(GtkWidget *widget, FolderSortDialog *dialog)
145 {
146 	GtkTreeSelection *sel;
147 	GtkTreeModel *model;
148 	GtkTreeIter iter, nextiter;
149 
150 	/* Get currently selected iter */
151 	sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->folderlist));
152 	if (!gtk_tree_selection_get_selected(sel, &model, &iter))
153 		return;
154 
155 	/* Now get the iter above it, if any */
156 	nextiter = iter;
157 	if (!gtk_tree_model_iter_next(model, &nextiter)) {
158 		/* No next iter, are we already on the bottom? */
159 		return;
160 	}
161 
162 	gtk_list_store_move_after(GTK_LIST_STORE(model), &iter, &nextiter);
163 
164 	set_selected(dialog);
165 }
166 
folderlist_cursor_changed_cb(GtkTreeView * view,gpointer user_data)167 static void folderlist_cursor_changed_cb(GtkTreeView *view, gpointer user_data)
168 {
169 	FolderSortDialog *dialog = (FolderSortDialog *)user_data;
170 	set_selected(dialog);
171 }
172 
folderlist_row_inserted_cb(GtkTreeModel * model,GtkTreePath * path,GtkTreeIter * iter,gpointer user_data)173 static void folderlist_row_inserted_cb(GtkTreeModel *model,
174 		GtkTreePath *path, GtkTreeIter *iter,
175 		gpointer user_data)
176 {
177 	FolderSortDialog *dialog = (FolderSortDialog *)user_data;
178 	GtkTreeSelection *sel;
179 
180 	/* The only time "row-inserted" signal should be fired is when
181 	 * a row is reordered using drag&drop. So we want to select
182 	 * the recently moved row, and call set_selected(), to update
183 	 * the up/down button sensitivity. */
184 	sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->folderlist));
185 	gtk_tree_selection_select_iter(sel, iter);
186 
187 	set_selected(dialog);
188 }
189 
delete_event(GtkWidget * widget,GdkEventAny * event,FolderSortDialog * dialog)190 static gint delete_event(GtkWidget *widget, GdkEventAny *event, FolderSortDialog *dialog)
191 {
192 	destroy_dialog(dialog);
193 	return TRUE;
194 }
195 
key_pressed(GtkWidget * widget,GdkEventKey * event,FolderSortDialog * dialog)196 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, FolderSortDialog *dialog)
197 {
198 	if (event && event->keyval == GDK_KEY_Escape)
199 		destroy_dialog(dialog);
200 	return FALSE;
201 }
202 
foldersort_size_allocate_cb(GtkWidget * widget,GtkAllocation * allocation)203 static void foldersort_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation)
204 {
205 	cm_return_if_fail(allocation != NULL);
206 
207 	prefs_common.foldersortwin_width = allocation->width;
208 	prefs_common.foldersortwin_height = allocation->height;
209 }
210 
foldersort_open()211 void foldersort_open()
212 {
213 	FolderSortDialog *dialog = g_new0(FolderSortDialog, 1);
214 	GList *flist;
215 
216 	GtkWidget *window;
217 	GtkWidget *vbox;
218 	GtkWidget *vbox1;
219 	GtkWidget *label1;
220 	GtkWidget *hbox;
221 	GtkWidget *hbox2;
222 	GtkWidget *ok_btn;
223 	GtkWidget *cancel_btn;
224 	GtkWidget *confirm_area;
225 	GtkWidget *moveup_btn;
226 	GtkWidget *movedown_btn;
227 	GtkWidget *btn_vbox;
228 	GtkWidget *scrolledwindow1;
229 	GtkWidget *folderlist;
230 	GtkTreeViewColumn *col;
231 	GtkListStore *store;
232 	GtkCellRenderer *rdr;
233 	GtkTreeSelection *selector;
234 	GtkTreeIter iter;
235 	static GdkGeometry geometry;
236 
237 	window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "foldersort");
238 	g_object_set_data(G_OBJECT(window), "window", window);
239 	gtk_container_set_border_width(GTK_CONTAINER(window), 8);
240 	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
241 	gtk_window_set_title(GTK_WINDOW(window), _("Set mailbox order"));
242 	gtk_window_set_modal(GTK_WINDOW(window), TRUE);
243 	gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
244 	g_signal_connect(G_OBJECT(window), "delete_event",
245 			 G_CALLBACK(delete_event), dialog);
246 	g_signal_connect (G_OBJECT(window), "size_allocate",
247 			 G_CALLBACK (foldersort_size_allocate_cb), NULL);
248 	g_signal_connect(G_OBJECT(window), "key_press_event",
249 			 G_CALLBACK(key_pressed), dialog);
250 
251 	vbox = gtk_vbox_new(FALSE, 6);
252 	gtk_widget_show(vbox);
253 	gtk_container_add(GTK_CONTAINER(window), vbox);
254 
255 	gtkut_stock_button_set_create(&confirm_area, &cancel_btn, GTK_STOCK_CANCEL,
256 				      &ok_btn, GTK_STOCK_OK,
257 				      NULL, NULL);
258 	gtk_widget_show(confirm_area);
259 	gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
260 	gtk_widget_grab_focus(ok_btn);
261 
262 	g_signal_connect(G_OBJECT(ok_btn), "clicked",
263                          G_CALLBACK(ok_clicked), dialog);
264 	g_signal_connect(G_OBJECT(cancel_btn), "clicked",
265                          G_CALLBACK(cancel_clicked), dialog);
266 
267 	vbox1 = gtk_vbox_new(FALSE, 8);
268 	gtk_widget_show(vbox1);
269 	gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
270 	gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
271 
272 	hbox = gtk_hbox_new(FALSE, 8);
273 	gtk_widget_show(hbox);
274 	gtk_box_pack_start(GTK_BOX(vbox1), hbox, FALSE, FALSE, 0);
275 
276 	label1 = gtk_label_new(_
277 		("Move mailboxes up or down to change the sort order "
278 		 "in the Folder list."));
279 	gtk_widget_show(label1);
280 	gtk_widget_set_size_request(GTK_WIDGET(label1), 392, -1);
281 	gtk_label_set_line_wrap(GTK_LABEL(label1), TRUE);
282 	gtk_box_pack_start(GTK_BOX(hbox), label1, FALSE, FALSE, 0);
283 
284 	hbox2 = gtk_hbox_new(FALSE, 8);
285 	gtk_widget_show(hbox2);
286 	gtk_box_pack_start(GTK_BOX(vbox1), hbox2, TRUE, TRUE, 0);
287 
288 	scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
289 	gtk_widget_show(scrolledwindow1);
290 	gtk_widget_set_size_request(scrolledwindow1, -1, 150);
291 	gtk_box_pack_start(GTK_BOX(hbox2), scrolledwindow1,
292 			   TRUE, TRUE, 0);
293 	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwindow1),
294 				       GTK_POLICY_AUTOMATIC,
295 				       GTK_POLICY_AUTOMATIC);
296 
297 	/* Create the list store */
298 	store = gtk_list_store_new(N_FOLDERSORT_COLS,
299 			G_TYPE_STRING, G_TYPE_POINTER, -1);
300 
301 	/* Create the view widget */
302 	folderlist = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
303 	g_object_unref(store);
304 	gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(folderlist), TRUE);
305 	gtk_tree_view_set_reorderable(GTK_TREE_VIEW(folderlist), TRUE);
306 	selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(folderlist));
307 	gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
308 
309 	/* The only column for the view widget */
310 	rdr = gtk_cell_renderer_text_new();
311 	col = gtk_tree_view_column_new_with_attributes(_("Mailboxes"),
312 			rdr, "markup", FOLDERSORT_COL_NAME, NULL);
313 	gtk_tree_view_column_set_min_width(col, 80);
314 	gtk_tree_view_append_column(GTK_TREE_VIEW(folderlist), col);
315 
316 	gtk_widget_show(folderlist);
317 	gtk_container_add(GTK_CONTAINER(scrolledwindow1), folderlist);
318 
319 	btn_vbox = gtk_vbox_new(FALSE, 8);
320 	gtk_widget_show(btn_vbox);
321 	gtk_box_pack_start(GTK_BOX(hbox2), btn_vbox, FALSE, FALSE, 0);
322 
323 	moveup_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
324 	gtk_widget_show(moveup_btn);
325 	gtk_box_pack_start(GTK_BOX(btn_vbox), moveup_btn, FALSE, FALSE, 0);
326 
327 	movedown_btn =  gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
328 	gtk_widget_show(movedown_btn);
329 	gtk_box_pack_start(GTK_BOX(btn_vbox), movedown_btn, FALSE, FALSE, 0);
330 
331 	if (!geometry.min_height) {
332 		geometry.min_width = 400;
333 		geometry.min_height = 300;
334 	}
335 
336 	gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
337 				      GDK_HINT_MIN_SIZE);
338 	gtk_widget_set_size_request(window, prefs_common.foldersortwin_width,
339 				    prefs_common.foldersortwin_height);
340 
341 	dialog->window = window;
342 	dialog->moveup_btn = moveup_btn;
343 	dialog->movedown_btn = movedown_btn;
344 	dialog->folderlist = folderlist;
345 
346 	gtk_widget_show(window);
347 	gtk_widget_set_sensitive(moveup_btn, FALSE);
348 	gtk_widget_set_sensitive(movedown_btn, FALSE);
349 
350 	/* Connect up the signals for the up/down buttons */
351 	g_signal_connect(G_OBJECT(moveup_btn), "clicked",
352                          G_CALLBACK(moveup_clicked), dialog);
353 	g_signal_connect(G_OBJECT(movedown_btn), "clicked",
354                          G_CALLBACK(movedown_clicked), dialog);
355 
356 //	g_signal_connect(G_OBJECT(folderlist), "select-row",
357 //			 G_CALLBACK(row_selected), dialog);
358 //	g_signal_connect(G_OBJECT(folderlist), "unselect-row",
359 //			 G_CALLBACK(row_unselected), dialog);
360 //	g_signal_connect(G_OBJECT(folderlist), "row-move",
361 //			 G_CALLBACK(row_moved), dialog);
362 
363 	/* Populate the list with mailboxes */
364 	dialog->rows = 0;
365 	for (flist = folder_get_list(); flist != NULL; flist = g_list_next(flist)) {
366 		Folder *folder = flist->data;
367 
368 		gtk_list_store_append(store, &iter);
369 		gtk_list_store_set(store, &iter,
370 				FOLDERSORT_COL_NAME, folder->name,
371 				FOLDERSORT_COL_PTR, folder,
372 				-1);
373 		dialog->rows++;
374 	}
375 
376 	/* Connect up the signals for the folderlist */
377 	g_signal_connect(G_OBJECT(folderlist), "cursor-changed",
378 			G_CALLBACK(folderlist_cursor_changed_cb), dialog);
379 	g_signal_connect(G_OBJECT(store), "row-inserted",
380 			G_CALLBACK(folderlist_row_inserted_cb), dialog);
381 
382 	/* Select the first row and adjust the sensitivity of
383 	 * the up/down buttons */
384 	if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) {
385 		gtk_tree_selection_select_iter(selector, &iter);
386 		set_selected(dialog);
387 	}
388 
389 	inc_lock();
390 }
391