/
file_dialog.cpp
510 lines (420 loc) · 15.3 KB
/
file_dialog.cpp
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
Copyright (C) 2011, 2016 by Ignacio R. Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program 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 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/dialogs/file_dialog.hpp"
#include "cursor.hpp"
#include "filesystem.hpp"
#include "formula/string_utils.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/dialogs/folder_create.hpp"
#include "gui/dialogs/helper.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "gui/widgets/button.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else
#include "gui/widgets/listbox.hpp"
#endif
#include "gui/widgets/settings.hpp"
#include "gui/widgets/text_box.hpp"
#include "gui/widgets/toggle_panel.hpp"
#include "gui/widgets/window.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include <functional>
static lg::log_domain log_filedlg{"gui/dialogs/file_dialog"};
#define ERR_FILEDLG LOG_STREAM(err, log_filedlg)
#define WRN_FILEDLG LOG_STREAM(warn, log_filedlg)
#define LOG_FILEDLG LOG_STREAM(info, log_filedlg)
#define DBG_FILEDLG LOG_STREAM(debug, log_filedlg)
namespace fs = filesystem;
namespace
{
const std::string icon_dir = "misc/folder-icon.png";
const std::string icon_file = "";
const std::string icon_parent = "";
// NOTE: Does not need to be the same as PARENT_DIR! Use PARENT_DIR to build
// relative paths for non-presentational purposes instead.
const std::string label_parent = "..";
const std::string CURRENT_DIR = ".";
const std::string PARENT_DIR = "..";
const int FILE_DIALOG_ITEM_RETVAL = 9001;
const int FILE_DIALOG_MAX_ENTRY_LENGTH = 42;
inline std::string concat_path(const std::string& a, const std::string& b)
{
//
// As of Boost 1.61, normalize_path() displays unusual behavior when passing
// it paths with extra path separators (e.g. //opt becomes
// //opt/home/shadowm/src/wesnoth, where the extraneous sequence is probably
// the current working dir), so avoid leaving those around.
//
// TODO: Maybe handle this corner case in filesystem::normalize_path()
// instead, really.
//
if((a.empty() || !fs::is_path_sep(a.back())) && (b.empty() || !fs::is_path_sep(b.front()))) {
return a + fs::path_separator() + b;
} else {
return a + b;
}
}
inline std::string filesystem_root()
{
// TODO: Multiple drives support (may require cooperation from the caller).
return std::string(1, fs::path_separator());
}
} // unnamed namespace
namespace gui2
{
REGISTER_DIALOG(file_dialog)
tfile_dialog::tfile_dialog()
: title_(_("Find File"))
, msg_()
, ok_label_()
, current_entry_()
, current_dir_()
, read_only_(false)
, save_mode_(false)
, dir_files_()
, dir_subdirs_()
{
}
std::string tfile_dialog::path() const
{
const std::string& dir_norm = fs::normalize_path(current_dir_, true);
if(current_entry_.empty() || current_entry_ == CURRENT_DIR) {
return dir_norm;
} else if(current_entry_ == PARENT_DIR) {
return fs::directory_name(dir_norm);
}
return concat_path(dir_norm, current_entry_);
}
tfile_dialog& tfile_dialog::set_path(const std::string& value)
{
if(value.empty()) {
current_dir_ = filesystem_root();
}
const std::string& norm = fs::normalize_path(value, true);
if(fs::is_directory(norm)) {
current_dir_ = norm;
} else {
current_dir_ = fs::nearest_extant_parent(norm);
if(current_dir_.empty()) {
current_dir_ = filesystem_root();
}
// The file may or may not exist. We'll find out eventually when setting up
// the dialog.
current_entry_ = fs::base_name(norm);
}
return *this;
}
tfile_dialog& tfile_dialog::set_filename(const std::string& value)
{
current_entry_ = value;
return *this;
}
void tfile_dialog::pre_show(twindow& window)
{
tcontrol& title = find_widget<tcontrol>(&window, "title", false);
tcontrol& message = find_widget<tcontrol>(&window, "message", false);
tcontrol& ok = find_widget<tcontrol>(&window, "ok", false);
title.set_label(title_);
if(msg_.empty()) {
message.set_visible(gui2::twidget::tvisible::invisible);
} else {
message.set_label(msg_);
message.set_use_markup(true);
}
if(ok_label_.empty()) {
ok.set_label(save_mode_ ? _("Save") : _("Open"));
} else {
ok.set_label(ok_label_);
}
tlistbox& filelist = find_widget<tlistbox>(&window, "filelist", false);
#ifdef GUI2_EXPERIMENTAL_LISTBOX
connect_signal_notify_modified(filelist, std::bind(
&tfile_dialog::on_row_selected
, *this
, std::ref(window)));
#else
filelist.set_callback_value_change(
dialog_callback<tfile_dialog, &tfile_dialog::on_row_selected>);
#endif
tbutton& mkdir_button = find_widget<tbutton>(&window, "new_dir", false);
tbutton& rm_button = find_widget<tbutton>(&window, "delete_file", false);
connect_signal_mouse_left_click(mkdir_button,
std::bind(&tfile_dialog::on_dir_create_cmd, this, std::ref(window)));
connect_signal_mouse_left_click(rm_button,
std::bind(&tfile_dialog::on_file_delete_cmd, this, std::ref(window)));
if(read_only_) {
mkdir_button.set_active(false);
rm_button.set_active(false);
mkdir_button.set_visible(twidget::tvisible::hidden);
rm_button.set_visible(twidget::tvisible::hidden);
}
refresh_fileview(window);
window.keyboard_capture(&filelist);
window.set_exit_hook(std::bind(&tfile_dialog::on_exit, this, std::ref(window)));
}
bool tfile_dialog::on_exit(twindow& window)
{
if(window.get_retval() == FILE_DIALOG_ITEM_RETVAL) {
// Attempting to exit by double clicking items -- only proceeds if the item
// was a file.
if(process_fileview_submit(window)) {
window.set_retval(twindow::OK, false);
return true;
} else {
return false;
}
}
if(window.get_retval() == twindow::OK) {
// Attempting to exit by pressing Enter/clicking OK -- only proceeds if the
// textbox was not altered by the user to point to a different directory.
return process_textbox_submit(window);
}
return true;
}
bool tfile_dialog::is_selection_type_acceptable(tfile_dialog::SELECTION_TYPE stype) const
{
// TODO: Adapt for implementing directory selection mode.
return save_mode_
? stype != SELECTION_IS_DIR && stype != SELECTION_PARENT_NOT_FOUND
: stype == SELECTION_IS_FILE;
}
bool tfile_dialog::process_submit_common(twindow& window, const std::string& name)
{
const auto stype = register_new_selection(name);
//DBG_FILEDLG << "current_dir_=" << current_dir_ << " current_entry_=" << current_entry_ << '\n';
if(is_selection_type_acceptable(stype)) {
return true;
}
switch(stype) {
case SELECTION_IS_DIR:
// TODO: Adapt for implementing directory selection mode.
refresh_fileview(window);
break;
case SELECTION_PARENT_NOT_FOUND:
// We get here in save mode or not. Use the file creation language only in
// save mode.
if(save_mode_) {
show_transient_error_message(window.video(), vgettext("The file or folder $path cannot be created.", {{"path", name}}));
break;
}
case SELECTION_NOT_FOUND:
// We only get here if we aren't in save mode.
show_transient_error_message(window.video(), vgettext("The file or folder $path does not exist.", {{"path", name}}));
break;
case SELECTION_IS_FILE:
// TODO: Adapt for implementing directory selection mode.
default:
assert(false && "Unimplemented selection mode or semantics");
}
return false;
}
bool tfile_dialog::process_fileview_submit(twindow& window)
{
tlistbox& filelist = find_widget<tlistbox>(&window, "filelist", false);
const std::string& selected_name = get_filelist_selection(filelist);
return process_submit_common(window, selected_name);
}
bool tfile_dialog::process_textbox_submit(twindow& window)
{
ttext_box& file_textbox = find_widget<ttext_box>(&window, "filename", false);
const std::string& input_name = file_textbox.get_value();
return !input_name.empty() && process_submit_common(window, input_name);
}
std::string tfile_dialog::get_filelist_selection(tlistbox& filelist)
{
const int row = filelist.get_selected_row();
if(row == -1) {
// Shouldn't happen...
return "";
}
const bool i_am_root = fs::is_root(current_dir_);
if(row == 0 && !i_am_root) {
return PARENT_DIR;
} else {
size_t n = i_am_root ? row : row - 1;
if(n < dir_subdirs_.size()) {
return dir_subdirs_[n];
} else {
n -= dir_subdirs_.size();
if(n < dir_files_.size()) {
return dir_files_[n];
} else {
assert(false && "File list selection is out of range!");
}
}
}
return "";
}
tfile_dialog::SELECTION_TYPE tfile_dialog::register_new_selection(const std::string& name)
{
std::string new_path, new_parent;
if(fs::is_relative(name)) {
// On Windows, \ represents a path relative to the root of the process'
// current working drive specified by the current working dir, so we get
// here. This makes it the only platform where is_relative() and is_root()
// aren't mutually exclusive.
if(fs::is_root(name)) {
DBG_FILEDLG << "register_new_selection(): new selection '" << name << "' is relative to a root resource\n";
// Using the browsed dir's root drive instead of the cwd's makes the most
// sense for users.
new_parent = fs::root_name(current_dir_);
new_path = fs::normalize_path(concat_path(new_parent, name), true, true);
} else {
DBG_FILEDLG << "register_new_selection(): new selection '" << name << "' seems relative\n";
new_parent = current_dir_;
new_path = fs::normalize_path(concat_path(current_dir_, name), true, true);
}
} else {
DBG_FILEDLG << "register_new_selection(): new selection '" << name << "' seems absolute\n";
new_parent = fs::directory_name(name);
new_path = fs::normalize_path(name, true, true);
DBG_FILEDLG << "register_new_selection(): new selection is " << new_path << '\n';
}
if(!new_path.empty()) {
if(fs::is_directory(new_path)) {
DBG_FILEDLG << "register_new_selection(): new selection '" << name << "' is a directory: " << new_path << '\n';
current_dir_ = new_path;
current_entry_.clear();
return SELECTION_IS_DIR;
} else if(fs::file_exists(new_path)) {
// FIXME: Perhaps redundant since the three-params call to normalize_path()
// above necessarily validates existence.
DBG_FILEDLG << "register_new_selection(): new selection '" << name << "' is a file, symbolic link, or special: " << new_path << '\n';
current_dir_ = fs::directory_name(new_path);
current_entry_ = fs::base_name(new_path);
return SELECTION_IS_FILE;
}
}
// The path does not exist, at least not entirely. See if the parent does
// (in save mode non-existent files are accepted as long as the parent dir
// exists).
const std::string& absolute_parent = fs::normalize_path(new_parent, true, true);
if(!absolute_parent.empty()) {
DBG_FILEDLG << "register_new_selection(): new selection '" << name << "' does not exist or is not accessible, but parent exists\n";
current_dir_ = absolute_parent;
current_entry_ = fs::base_name(name);
return SELECTION_NOT_FOUND;
}
DBG_FILEDLG << "register_new_selection(): new selection '" << name << "' does not exist or is not accessible\n";
return SELECTION_PARENT_NOT_FOUND;
}
void tfile_dialog::refresh_fileview(twindow& window)
{
cursor::setter cur{cursor::WAIT};
dir_files_.clear();
dir_subdirs_.clear();
// TODO: Need to detect and handle cases where we don't have search permission
// on current_dir_, otherwise things may get weird.
filesystem::get_files_in_dir(current_dir_, &dir_files_, &dir_subdirs_, filesystem::FILE_NAME_ONLY);
//
// Clear and refill the filelist box.
//
tlistbox& filelist = find_widget<tlistbox>(&window, "filelist", false);
filelist.clear();
// Parent entry
if(!fs::is_root(current_dir_)) {
// label_parent may not necessarily be always ".." in the future, so push
// with check_selection = false and check the selection ourselves here.
push_fileview_row(filelist, label_parent, icon_parent, false);
if(current_entry_ == PARENT_DIR || current_entry_.empty()) {
filelist.select_row(0, true);
}
}
for(const auto& dir : dir_subdirs_) {
push_fileview_row(filelist, dir, icon_dir);
}
for(const auto& file : dir_files_) {
push_fileview_row(filelist, file, icon_file);
}
find_widget<tcontrol>(&window, "current_dir", false).set_label(current_dir_);
find_widget<ttext_box>(&window, "filename", false).set_value(current_entry_);
}
void tfile_dialog::push_fileview_row(tlistbox& filelist, const std::string& name, const std::string& icon, bool check_selection)
{
// TODO: Hopefully some day GUI2 will allow us to make labels be ellipsized
// dynamically at layout/rendering time.
std::string label = name;
utils::ellipsis_truncate(label, FILE_DIALOG_MAX_ENTRY_LENGTH);
std::map<std::string, string_map> data;
data["icon"]["label"] = icon;
data["file"]["label"] = label;
tgrid& last_grid = filelist.add_row(data);
const unsigned last_pos = filelist.get_item_count() - 1;
//
// Crummy hack around the lack of an option to hook into row double click
// events for all rows using the GUI2 listbox API. Assign a special retval to
// each row that triggers a special check during dialog exit.
//
find_widget<ttoggle_panel>(&last_grid, "item_panel", false)
.set_retval(FILE_DIALOG_ITEM_RETVAL);
if(check_selection && name == current_entry_) {
filelist.select_row(last_pos, true);
}
}
void tfile_dialog::on_row_selected(twindow& window)
{
tlistbox& filelist = find_widget<tlistbox>(&window, "filelist", false);
ttext_box& file_textbox = find_widget<ttext_box>(&window, "filename", false);
// Don't use register_new_selection() here, we don't want any parsing to be
// performed at this point.
current_entry_ = get_filelist_selection(filelist);
// Clear the textbox when selecting ..
if(current_entry_ != PARENT_DIR) {
file_textbox.set_value(current_entry_);
} else {
file_textbox.clear();
}
}
void tfile_dialog::on_dir_create_cmd(twindow& window)
{
std::string new_dir_name;
if(tfolder_create::execute(new_dir_name, window.video())) {
const std::string& new_path = concat_path(current_dir_, new_dir_name);
if(!fs::make_directory(new_path)) {
show_transient_error_message(window.video(),
vgettext("Could not create a new folder at $path|. Make sure you have the appropriate permissions to write to this location.",
{{"path", new_path}}));
} else {
refresh_fileview(window);
}
}
}
void tfile_dialog::on_file_delete_cmd(twindow& window)
{
if(current_entry_.empty()) {
return;
}
const std::string& selection = concat_path(current_dir_, current_entry_);
const bool is_dir = fs::is_directory(selection);
const std::string& message = (is_dir
? _("The following folder and its contents will be permanently deleted:")
: _("The following file will be permanently deleted:"))
+ "\n\n" + selection + "\n\n" + _("Do you wish to continue?");
if(gui2::show_message(window.video(), _("Confirm"), message, gui2::tmessage::yes_no_buttons) == gui2::twindow::CANCEL) {
return;
}
const bool result = is_dir
? fs::delete_directory(selection)
: fs::delete_file(selection);
if(!result) {
show_transient_error_message(window.video(),
vgettext("Could not delete $path|. Make sure you have the appropriate permissions to write to this location.",
{{"path", selection}}));
} else {
refresh_fileview(window);
}
}
} // namespace gui2