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
/*
* This file is part of YAD.
*
* YAD is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* YAD is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with YAD. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2008-2021, Victor Ananjevsky <ananasik@gmail.com>
*/
#include "yad.h"
static GtkWidget *filechooser;
static void
file_activated_cb (GtkFileChooser * chooser, gpointer data)
{
if (options.plug == -1)
yad_exit (options.data.def_resp);
}
gboolean
file_confirm_overwrite (GtkWidget * dlg)
{
if (options.file_data.save && options.file_data.confirm_overwrite && !options.common_data.multi)
{
gchar *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (filechooser));
if (g_file_test (filename, G_FILE_TEST_EXISTS))
return yad_confirm_dlg (GTK_WINDOW (dlg), options.file_data.confirm_text);
}
return TRUE;
}
GtkWidget *
file_create_widget (GtkWidget * dlg)
{
GtkWidget *w;
GList *filt;
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
if (options.file_data.directory)
{
if (options.file_data.save)
action = GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
else
action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
}
else
{
if (options.file_data.save)
action = GTK_FILE_CHOOSER_ACTION_SAVE;
}
w = filechooser = gtk_file_chooser_widget_new (action);
gtk_widget_set_name (w, "yad-file-widget");
if (options.common_data.uri)
{
if (!options.file_data.directory && g_file_test (options.common_data.uri, G_FILE_TEST_IS_DIR))
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (w), options.common_data.uri);
else
{
gchar *dir = g_path_get_dirname (options.common_data.uri);
if (g_path_is_absolute (options.common_data.uri) == TRUE)
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (w), dir);
if (options.common_data.uri[strlen (options.common_data.uri) - 1] != '/')
{
gchar *basename = g_path_get_basename (options.common_data.uri);
if (options.file_data.save)
gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (w), basename);
else
gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (w), options.common_data.uri);
g_free (basename);
}
g_free (dir);
}
}
else
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (w), g_get_current_dir ());
if (options.common_data.multi)
gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (w), TRUE);
/* add preview */
if (options.common_data.preview)
{
GtkWidget *p = gtk_image_new ();
gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER (w), p);
g_signal_connect (w, "update-preview", G_CALLBACK (update_preview), p);
}
/* add filters */
for (filt = options.common_data.filters; filt; filt = filt->next)
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (w), GTK_FILE_FILTER (filt->data));
g_signal_connect (w, "map", G_CALLBACK (gtk_file_chooser_set_show_hidden), GINT_TO_POINTER (options.common_data.show_hidden));
g_signal_connect (w, "file-activated", G_CALLBACK (file_activated_cb), dlg);
return w;
}
void
file_print_result (void)
{
GSList *selections, *iter;
selections = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (filechooser));
for (iter = selections; iter != NULL; iter = iter->next)
{
if (options.common_data.quoted_output)
{
gchar *buf = g_shell_quote (g_filename_to_utf8 ((gchar *) iter->data, -1, NULL, NULL, NULL));
g_printf ("%s", buf);
g_free (buf);
}
else
g_printf ("%s", g_filename_to_utf8 ((gchar *) iter->data, -1, NULL, NULL, NULL));
g_free (iter->data);
if (iter->next != NULL)
g_printf ("%s", options.common_data.separator);
}
g_printf ("\n");
g_slist_free (selections);
}