3a37b7b0d2577c0828fed25f2e2dbdc94cca3776
[claws.git] / src / gtk / w32_filesel.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2016 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 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #define UNICODE
25 #define _UNICODE
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gdk/gdkwin32.h>
30 #include <pthread.h>
31
32 #include <windows.h>
33 #include <shlobj.h>
34
35 #include "claws.h"
36 #include "alertpanel.h"
37 #include "manage_window.h"
38 #include "utils.h"
39
40 static OPENFILENAME o;
41 static BROWSEINFO b;
42
43 /* Since running the native dialogs in the same thread stops GTK+
44  * loop from redrawing other windows on the background, we need
45  * to run the dialogs in a separate thread. */
46
47 /* TODO: There's a lot of code repeat in this file, it could be
48  * refactored to be neater. */
49
50 struct _WinChooserCtx {
51         void *data;
52         gboolean return_value;
53         PIDLIST_ABSOLUTE return_value_pidl;
54         gboolean done;
55 };
56
57 typedef struct _WinChooserCtx WinChooserCtx;
58
59 static void *threaded_GetOpenFileName(void *arg)
60 {
61         WinChooserCtx *ctx = (WinChooserCtx *)arg;
62
63         g_return_val_if_fail(ctx != NULL, NULL);
64         g_return_val_if_fail(ctx->data != NULL, NULL);
65
66         ctx->return_value = GetOpenFileName(ctx->data);
67         ctx->done = TRUE;
68
69         return NULL;
70 }
71
72 static void *threaded_GetSaveFileName(void *arg)
73 {
74         WinChooserCtx *ctx = (WinChooserCtx *)arg;
75
76         g_return_val_if_fail(ctx != NULL, NULL);
77         g_return_val_if_fail(ctx->data != NULL, NULL);
78
79         ctx->return_value = GetSaveFileName(ctx->data);
80         ctx->done = TRUE;
81
82         return NULL;
83 }
84
85 static void *threaded_SHBrowseForFolder(void *arg)
86 {
87         WinChooserCtx *ctx = (WinChooserCtx *)arg;
88
89         g_return_val_if_fail(ctx != NULL, NULL);
90         g_return_val_if_fail(ctx->data != NULL, NULL);
91
92         ctx->return_value_pidl = SHBrowseForFolder(ctx->data);
93
94         ctx->done = TRUE;
95
96         return NULL;
97 }
98
99 /* This function handles calling GetOpenFilename(), using
100  * global static variable o.
101  * It expects o.lpstrFile to point to an already allocated buffer,
102  * of size at least MAXPATHLEN. */
103 static const gboolean _file_open_dialog(const gchar *path, const gchar *title,
104                 const gchar *filter, const gboolean multi)
105 {
106         gboolean ret;
107         gunichar2 *path16 = NULL;
108         gunichar2 *title16 = NULL;
109         gunichar2 *filter16 = NULL;
110         gunichar2 *win_filter16 = NULL;
111         glong conv_items, sz;
112         GError *error = NULL;
113         WinChooserCtx *ctx;
114 #ifdef USE_PTHREAD
115         pthread_t pt;
116 #endif
117
118         /* Path needs to be converted to UTF-16, so that the native chooser
119          * can understand it. */
120         path16 = g_utf8_to_utf16(path ? path : "",
121                         -1, NULL, NULL, &error);
122         if (error != NULL) {
123                 alertpanel_error(_("Could not convert file path to UTF-16:\n\n%s"),
124                                 error->message);
125                 debug_print("file path '%s' conversion to UTF-16 failed\n", path);
126                 g_error_free(error);
127                 error = NULL;
128                 return FALSE;
129         }
130
131         /* Chooser dialog title needs to be UTF-16 as well. */
132         title16 = g_utf8_to_utf16(title ? title : "",
133                         -1, NULL, NULL, &error);
134         if (error != NULL) {
135                 debug_print("dialog title '%s' conversion to UTF-16 failed\n", title);
136                 g_error_free(error);
137                 error = NULL;
138         }
139
140         o.lStructSize = sizeof(OPENFILENAME);
141         if (focus_window != NULL)
142                 o.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(focus_window));
143         else
144                 o.hwndOwner = NULL;
145         o.hInstance = NULL;
146         o.lpstrFilter = NULL;
147         o.lpstrCustomFilter = NULL;
148         o.nFilterIndex = 0;
149         o.nMaxFile = MAXPATHLEN;
150         o.lpstrFileTitle = NULL;
151         o.lpstrInitialDir = path16;
152         o.lpstrTitle = title16;
153         if (multi)
154                 o.Flags = OFN_LONGNAMES | OFN_EXPLORER | OFN_ALLOWMULTISELECT;
155         else
156                 o.Flags = OFN_LONGNAMES | OFN_EXPLORER;
157
158         if (filter != NULL && strlen(filter) > 0) {
159                 debug_print("Setting filter '%s'\n", filter);
160                 filter16 = g_utf8_to_utf16(filter, -1, NULL, &conv_items, &error);
161                 /* We're creating a UTF16 (2 bytes for each character) string:
162                  * "filter\0filter\0\0"
163                  * As g_utf8_to_utf16() will stop on first null byte, even if
164                  * we pass string length in its second argument, we have to
165                  * construct this string manually.
166                  * conv_items contains number of UTF16 characters of our filter.
167                  * Therefore we need enough bytes to store the filter string twice
168                  * and three null chars. */
169                 sz = sizeof(gunichar2);
170                 win_filter16 = g_malloc0(conv_items*sz*2 + sz*3);
171                 memcpy(win_filter16, filter16, conv_items*sz);
172                 memcpy(win_filter16 + conv_items + 1, filter16, conv_items*sz);
173
174                 if (error != NULL) {
175                         debug_print("dialog title '%s' conversion to UTF-16 failed\n", title);
176                         g_error_free(error);
177                         error = NULL;
178                 }
179                 o.lpstrFilter = (LPCTSTR)win_filter16;
180                 o.nFilterIndex = 1;
181         }
182
183         ctx = g_new0(WinChooserCtx, 1);
184         ctx->data = &o;
185         ctx->done = FALSE;
186
187 #ifdef USE_PTHREAD
188         if (pthread_create(&pt, NULL, threaded_GetOpenFileName,
189                                 (void *)ctx) != 0) {
190                 debug_print("Couldn't run in a thread, continuing unthreaded.\n");
191                 threaded_GetOpenFileName(ctx);
192         } else {
193                 while (!ctx->done) {
194                         claws_do_idle();
195                 }
196                 pthread_join(pt, NULL);
197         }
198         ret = ctx->return_value;
199 #else
200         debug_print("No threads available, continuing unthreaded.\n");
201         ret = GetOpenFileName(&o);
202 #endif
203
204         g_free(win_filter16);
205         if (path16 != NULL) {
206                 g_free(path16);
207         }
208         g_free(title16);
209         g_free(ctx);
210
211         return ret;
212 }
213
214 gchar *filesel_select_file_open_with_filter(const gchar *title, const gchar *path,
215                               const gchar *filter)
216 {
217         gchar *str = NULL;
218         GError *error = NULL;
219
220         o.lpstrFile = g_malloc0(MAXPATHLEN);
221         if (!_file_open_dialog(path, title, filter, FALSE)) {
222                 g_free(o.lpstrFile);
223                 return NULL;
224         }
225
226         /* Now convert the returned file path back from UTF-16. */
227         str = g_utf16_to_utf8(o.lpstrFile, o.nMaxFile, NULL, NULL, &error);
228         if (error != NULL) {
229                 alertpanel_error(_("Could not convert file path back to UTF-8:\n\n%s"),
230                                 error->message);
231                 debug_print("returned file path conversion to UTF-8 failed\n");
232                 g_error_free(error);
233         }
234
235         g_free(o.lpstrFile);
236         return str;
237 }
238
239 GList *filesel_select_multiple_files_open_with_filter(const gchar *title,
240                 const gchar *path, const gchar *filter)
241 {
242         GList *file_list = NULL;
243         gchar *str = NULL;
244         gchar *dir = NULL;
245         gunichar2 *f;
246         GError *error = NULL;
247         glong n, items_read;
248
249         o.lpstrFile = g_malloc0(MAXPATHLEN);
250         if (!_file_open_dialog(path, title, filter, TRUE)) {
251                 g_free(o.lpstrFile);
252                 return NULL;
253         }
254
255         /* Now convert the returned directory and file names back from UTF-16.
256          * The content of o.lpstrFile is:
257          * "directory0file0file0...0file00" for multiple files selected,
258          * "fullfilepath0" for single file. */
259         str = g_utf16_to_utf8(o.lpstrFile, -1, &items_read, NULL, &error);
260
261         if (error != NULL) {
262                 alertpanel_error(_("Could not convert file path back to UTF-8:\n\n%s"),
263                                 error->message);
264                 debug_print("returned file path conversion to UTF-8 failed\n");
265                 g_error_free(error);
266                 return NULL;
267         }
268
269         /* The part before the first null char is always a full path. If it is
270          * a path to a file, then only this one file has been selected,
271          * and we can bail out early. */
272         if (g_file_test(str, G_FILE_TEST_IS_REGULAR)) {
273                 debug_print("Selected one file: '%s'\n", str);
274                 file_list = g_list_append(file_list, g_strdup(str));
275                 g_free(str);
276                 return file_list;
277         }
278
279         /* So the path was to a directory. We need to parse more after
280          * the fist null char, until we get to two null chars in a row. */
281         dir = g_strdup(str);
282         g_free(str);
283         debug_print("Selected multiple files in dir '%s'\n", dir);
284
285         n = items_read + 1;
286         f = &o.lpstrFile[n];
287         while (items_read > 0) {
288                 str = g_utf16_to_utf8(f, -1, &items_read, NULL, &error);
289                 if (error != NULL) {
290                         alertpanel_error(_("Could not convert file path back to UTF-8:\n\n%s"),
291                                         error->message);
292                         debug_print("returned file path conversion to UTF-8 failed\n");
293                         g_error_free(error);
294                         g_free(o.lpstrFile);
295                         return NULL;
296                 }
297
298                 if (items_read > 0) {
299                         debug_print("selected file '%s'\n", str);
300                         file_list = g_list_append(file_list,
301                                         g_strconcat(dir, G_DIR_SEPARATOR_S, str, NULL));
302                 }
303                 g_free(str);
304
305                 n += items_read + 1;
306                 f = &o.lpstrFile[n];
307         }
308
309         g_free(o.lpstrFile);
310         return file_list;
311 }
312
313 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
314 {
315         return filesel_select_file_open_with_filter(title, path, NULL);
316 }
317
318 GList *filesel_select_multiple_files_open(const gchar *title, const gchar *path)
319 {
320         return filesel_select_multiple_files_open_with_filter(title, path, NULL);
321 }
322
323 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
324 {
325         gboolean ret;
326         gchar *str, *filename = NULL;
327         gunichar2 *filename16, *path16, *title16;
328         glong conv_items;
329         GError *error = NULL;
330         WinChooserCtx *ctx;
331 #ifdef USE_PTHREAD
332         pthread_t pt;
333 #endif
334
335         /* Find the filename part, if any */
336         if (path == NULL || path[strlen(path)-1] == G_DIR_SEPARATOR) {
337                 filename = "";
338         } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
339                 filename++;
340         } else {
341                 filename = (char *) path;
342         }
343
344         /* Convert it to UTF-16. */
345         filename16 = g_utf8_to_utf16(filename, -1, NULL, &conv_items, &error);
346         if (error != NULL) {
347                 alertpanel_error(_("Could not convert attachment name to UTF-16:\n\n%s"),
348                                 error->message);
349                 debug_print("filename '%s' conversion to UTF-16 failed\n", filename);
350                 g_error_free(error);
351                 return NULL;
352         }
353
354         /* Path needs to be converted to UTF-16, so that the native chooser
355          * can understand it. */
356         path16 = g_utf8_to_utf16(path, -1, NULL, NULL, &error);
357         if (error != NULL) {
358                 alertpanel_error(_("Could not convert file path to UTF-16:\n\n%s"),
359                                 error->message);
360                 debug_print("file path '%s' conversion to UTF-16 failed\n", path);
361                 g_error_free(error);
362                 g_free(filename16);
363                 return NULL;
364         }
365
366         /* Chooser dialog title needs to be UTF-16 as well. */
367         title16 = g_utf8_to_utf16(title, -1, NULL, NULL, &error);
368         if (error != NULL) {
369                 debug_print("dialog title '%s' conversion to UTF-16 failed\n", title);
370                 g_error_free(error);
371         }
372
373         o.lStructSize = sizeof(OPENFILENAME);
374         if (focus_window != NULL)
375                 o.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(focus_window));
376         else
377                 o.hwndOwner = NULL;
378         o.lpstrFilter = NULL;
379         o.lpstrCustomFilter = NULL;
380         o.lpstrFile = g_malloc0(MAXPATHLEN);
381         if (path16 != NULL)
382                 memcpy(o.lpstrFile, filename16, conv_items * sizeof(gunichar2));
383         o.nMaxFile = MAXPATHLEN;
384         o.lpstrFileTitle = NULL;
385         o.lpstrInitialDir = path16;
386         o.lpstrTitle = title16;
387         o.Flags = OFN_LONGNAMES | OFN_EXPLORER;
388
389         ctx = g_new0(WinChooserCtx, 1);
390         ctx->data = &o;
391         ctx->return_value = FALSE;
392         ctx->done = FALSE;
393
394 #ifdef USE_PTHREAD
395         if (pthread_create(&pt, NULL, threaded_GetSaveFileName,
396                                 (void *)ctx) != 0) {
397                 debug_print("Couldn't run in a thread, continuing unthreaded.\n");
398                 threaded_GetSaveFileName(ctx);
399         } else {
400                 while (!ctx->done) {
401                         claws_do_idle();
402                 }
403                 pthread_join(pt, NULL);
404         }
405         ret = ctx->return_value;
406 #else
407         debug_print("No threads available, continuing unthreaded.\n");
408         ret = GetSaveFileName(&o);
409 #endif
410
411         g_free(filename16);
412         g_free(path16);
413         g_free(title16);
414         g_free(ctx);
415
416         if (!ret) {
417                 g_free(o.lpstrFile);
418                 return NULL;
419         }
420
421         /* Now convert the returned file path back from UTF-16. */
422         str = g_utf16_to_utf8(o.lpstrFile, o.nMaxFile, NULL, NULL, &error);
423         if (error != NULL) {
424                 alertpanel_error(_("Could not convert file path back to UTF-8:\n\n%s"),
425                                 error->message);
426                 debug_print("returned file path conversion to UTF-8 failed\n");
427                 g_error_free(error);
428         }
429
430         g_free(o.lpstrFile);
431         return str;
432 }
433
434 /* This callback function is used to set the folder browse dialog
435  * selection from filesel_select_file_open_folder() to set
436  * chosen starting folder ("path" argument to that function. */
437 static int CALLBACK _open_folder_callback(HWND hwnd, UINT uMsg,
438                 LPARAM lParam, LPARAM lpData)
439 {
440         if (uMsg != BFFM_INITIALIZED)
441                 return 0;
442
443         SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
444         return 0;
445 }
446
447 gchar *filesel_select_file_open_folder(const gchar *title, const gchar *path)
448 {
449         PIDLIST_ABSOLUTE pidl;
450         gchar *str;
451         gunichar2 *path16, *title16;
452         glong conv_items;
453         GError *error = NULL;
454         WinChooserCtx *ctx;
455 #ifdef USE_PTHREAD
456         pthread_t pt;
457 #endif
458
459         /* Path needs to be converted to UTF-16, so that the native chooser
460          * can understand it. */
461         path16 = g_utf8_to_utf16(path ? path : "",
462                         -1, NULL, &conv_items, &error);
463         if (error != NULL) {
464                 alertpanel_error(_("Could not convert file path to UTF-16:\n\n%s"),
465                                 error->message);
466                 debug_print("file path '%s' conversion to UTF-16 failed\n", path);
467                 g_error_free(error);
468                 return NULL;
469         }
470
471         /* Chooser dialog title needs to be UTF-16 as well. */
472         title16 = g_utf8_to_utf16(title ? title : "",
473                         -1, NULL, NULL, &error);
474         if (error != NULL) {
475                 debug_print("dialog title '%s' conversion to UTF-16 failed\n", title);
476                 g_error_free(error);
477         }
478
479         if (focus_window != NULL)
480                 b.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(focus_window));
481         else
482                 b.hwndOwner = NULL;
483         b.pszDisplayName = g_malloc(MAXPATHLEN);
484         b.lpszTitle = title16;
485         b.ulFlags = 0;
486         b.pidlRoot = NULL;
487         b.lpfn = _open_folder_callback;
488         b.lParam = (LPARAM)path16;
489
490         CoInitialize(NULL);
491
492         ctx = g_new0(WinChooserCtx, 1);
493         ctx->data = &b;
494         ctx->done = FALSE;
495
496 #ifdef USE_PTHREAD
497         if (pthread_create(&pt, NULL, threaded_SHBrowseForFolder,
498                                 (void *)ctx) != 0) {
499                 debug_print("Couldn't run in a thread, continuing unthreaded.\n");
500                 threaded_SHBrowseForFolder(ctx);
501         } else {
502                 while (!ctx->done) {
503                         claws_do_idle();
504                 }
505                 pthread_join(pt, NULL);
506         }
507         pidl = ctx->return_value_pidl;
508 #else
509         debug_print("No threads available, continuing unthreaded.\n");
510         pidl = SHBrowseForFolder(&b);
511 #endif
512
513         g_free(b.pszDisplayName);
514         g_free(title16);
515         g_free(path16);
516
517         if (pidl == NULL) {
518                 CoUninitialize();
519                 g_free(ctx);
520                 return NULL;
521         }
522
523         path16 = malloc(MAX_PATH);
524         if (!SHGetPathFromIDList(pidl, path16)) {
525                 CoTaskMemFree(pidl);
526                 CoUninitialize();
527                 g_free(path16);
528                 g_free(ctx);
529                 return NULL;
530         }
531
532         /* Now convert the returned file path back from UTF-16. */
533         /* Unfortunately, there is no field in BROWSEINFO struct to indicate
534          * actual length of string in pszDisplayName, so we have to assume
535          * the string is null-terminated. */
536         str = g_utf16_to_utf8(path16, -1, NULL, NULL, &error);
537         if (error != NULL) {
538                 alertpanel_error(_("Could not convert file path back to UTF-8:\n\n%s"),
539                                 error->message);
540                 debug_print("returned file path conversion to UTF-8 failed\n");
541                 g_error_free(error);
542         }
543         CoTaskMemFree(pidl);
544         CoUninitialize();
545         g_free(ctx);
546         g_free(path16);
547
548         return str;
549 }
550
551 gchar *filesel_select_file_save_folder(const gchar *title, const gchar *path)
552 {
553         return filesel_select_file_open_folder(title, path);
554 }