-
-
Notifications
You must be signed in to change notification settings - Fork 989
/
file_dialog.cpp
753 lines (615 loc) · 22.9 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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*
Copyright (C) 2011, 2017 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 "desktop/paths.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 "serialization/unicode.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());
}
inline void isort_dir_entries(std::vector<std::string>& entries)
{
// Yes, this uses Wesnoth's locale and not the filesystem/OS locale. Yes, this
// isn't ideal. No, we don't really need to worry about it. It's just a
// cosmetic procedure anyway.
std::sort(entries.begin(), entries.end(),
[](const std::string& a, const std::string& b) { return translation::icompare(a, b) < 0; });
}
} // unnamed namespace
namespace gui2
{
namespace dialogs
{
REGISTER_DIALOG(file_dialog)
file_dialog::file_dialog()
: title_(_("Find File"))
, msg_()
, ok_label_()
, extension_()
, current_entry_()
, current_dir_()
, read_only_(false)
, save_mode_(false)
, dir_files_()
, dir_subdirs_()
, bookmark_paths_()
, current_bookmark_()
, user_bookmarks_begin_()
{
set_restore(true);
}
std::string file_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_);
}
file_dialog& file_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;
}
file_dialog& file_dialog::set_filename(const std::string& value)
{
current_entry_ = value;
return *this;
}
void file_dialog::pre_show(window& window)
{
styled_widget& title = find_widget<styled_widget>(&window, "title", false);
styled_widget& message = find_widget<styled_widget>(&window, "message", false);
styled_widget& ok = find_widget<styled_widget>(&window, "ok", false);
title.set_label(title_);
if(msg_.empty()) {
message.set_visible(gui2::widget::visibility::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_);
}
listbox& bookmarks_bar = find_widget<listbox>(&window, "bookmarks", false);
find_widget<styled_widget>(&window, "current_dir", false).set_text_ellipse_mode(PANGO_ELLIPSIZE_START);
//
// Push hard-coded bookmarks.
//
std::vector<desktop::path_info> bookmarks = desktop::game_paths();
const auto& sys_paths = desktop::system_paths();
bookmarks.insert(bookmarks.end(), sys_paths.begin(), sys_paths.end());
bookmark_paths_.clear();
current_bookmark_ = user_bookmarks_begin_ = -1;
std::map<std::string, string_map> data;
for(const auto& pinfo : bookmarks) {
bookmark_paths_.push_back(pinfo.path);
data["bookmark"]["label"] = pinfo.display_name();
bookmarks_bar.add_row(data);
}
//
// Push user-defined bookmarks.
//
const std::vector<desktop::bookmark_info>& user_bookmarks = desktop::user_bookmarks();
if(!user_bookmarks.empty()) {
user_bookmarks_begin_ = bookmark_paths_.size();
}
for(const auto& bookmark : user_bookmarks) {
bookmark_paths_.push_back(bookmark.path);
data["bookmark"]["label"] = bookmark.label;
bookmarks_bar.add_row(data);
}
sync_bookmarks_bar(window);
listbox& filelist = find_widget<listbox>(&window, "filelist", false);
connect_signal_notify_modified(filelist,
std::bind(&file_dialog::on_row_selected, this, std::ref(window)));
connect_signal_notify_modified(bookmarks_bar,
std::bind(&file_dialog::on_bookmark_selected, this, std::ref(window)));
button& mkdir_button = find_widget<button>(&window, "new_dir", false);
button& rm_button = find_widget<button>(&window, "delete_file", false);
button& bookmark_add_button = find_widget<button>(&window, "add_bookmark", false);
button& bookmark_del_button = find_widget<button>(&window, "remove_bookmark", false);
connect_signal_mouse_left_click(mkdir_button,
std::bind(&file_dialog::on_dir_create_cmd, this, std::ref(window)));
connect_signal_mouse_left_click(rm_button,
std::bind(&file_dialog::on_file_delete_cmd, this, std::ref(window)));
connect_signal_mouse_left_click(bookmark_add_button,
std::bind(&file_dialog::on_bookmark_add_cmd, this, std::ref(window)));
connect_signal_mouse_left_click(bookmark_del_button,
std::bind(&file_dialog::on_bookmark_del_cmd, this, std::ref(window)));
if(read_only_) {
mkdir_button.set_active(false);
rm_button.set_active(false);
mkdir_button.set_visible(widget::visibility::invisible);
rm_button.set_visible(widget::visibility::invisible);
}
refresh_fileview(window);
window.keyboard_capture(find_widget<text_box>(&window, "filename", false, true));
window.add_to_keyboard_chain(&filelist);
window.set_exit_hook(std::bind(&file_dialog::on_exit, this, std::ref(window)));
}
bool file_dialog::on_exit(window& 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(window::OK, false);
return true;
} else {
return false;
}
}
if(window.get_retval() == window::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 file_dialog::is_selection_type_acceptable(file_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 file_dialog::confirm_overwrite(window& window, file_dialog::SELECTION_TYPE stype)
{
// TODO: Adapt for implementing directory selection mode.
if(stype != SELECTION_IS_FILE) {
return true;
}
const std::string& message
= _("The file already exists. Do you wish to overwrite it?");
return gui2::show_message(window.video(), _("Confirm"), message, message::yes_no_buttons) != gui2::window::CANCEL;
}
bool file_dialog::process_submit_common(window& 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 save_mode_ ? confirm_overwrite(window, stype) : true;
}
switch(stype) {
case SELECTION_IS_DIR:
// TODO: Adapt for implementing directory selection mode.
sync_bookmarks_bar(window);
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;
}
FALLTHROUGH;
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 file_dialog::process_fileview_submit(window& window)
{
listbox& filelist = find_widget<listbox>(&window, "filelist", false);
const std::string& selected_name = get_filelist_selection(filelist);
return process_submit_common(window, selected_name);
}
bool file_dialog::process_textbox_submit(window& window)
{
text_box& file_textbox = find_widget<text_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 file_dialog::get_filelist_selection(listbox& 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 "";
}
file_dialog::SELECTION_TYPE file_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 file_dialog::set_input_text(text_box& t, const std::string& value)
{
if(value.empty()) {
clear_input_text(t);
return;
}
t.set_value(value);
const size_t vallen = t.get_length();
const size_t extlen = utf8::size(extension_);
if(save_mode_ && extlen && vallen > extlen) {
// Highlight everything but the extension if it matches
if(value.substr(vallen - extlen) == extension_) {
t.set_selection(0, vallen - extlen);
}
}
}
void file_dialog::clear_input_text(text_box& t)
{
if(save_mode_ && !extension_.empty()) {
t.set_value(extension_);
t.set_selection(0, 0);
} else {
t.clear();
}
}
void file_dialog::refresh_fileview(window& 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);
isort_dir_entries(dir_files_);
isort_dir_entries(dir_subdirs_);
//
// Clear and refill the filelist box.
//
listbox& filelist = find_widget<listbox>(&window, "filelist", false);
button& rm_button = find_widget<button>(&window, "delete_file", 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);
rm_button.set_active(false);
} else {
rm_button.set_active(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<styled_widget>(&window, "current_dir", false).set_label(current_dir_);
set_input_text(find_widget<text_box>(&window, "filename", false), current_entry_);
on_row_selected(window);
}
void file_dialog::push_fileview_row(listbox& 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;
grid& 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<toggle_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 file_dialog::sync_bookmarks_bar(window& window)
{
listbox& bookmarks_bar = find_widget<listbox>(&window, "bookmarks", false);
// Internal state has normalized path delimiters but dot entries aren't
// resolved after callers call set_path(), so compare against a canonical
// version. The bookmark paths are already canonical, though.
const std::string& canon_current_dir = fs::normalize_path(current_dir_, true, true);
// Go backwards so we can match user-defined bookmarks first (otherwise it may
// become impossible for the user to delete them if they match any of the
// predefined paths).
auto it = std::find(bookmark_paths_.rbegin(), bookmark_paths_.rend(), canon_current_dir);
if(it == bookmark_paths_.rend()) {
if(current_bookmark_ >= 0) {
bookmarks_bar.select_row(unsigned(current_bookmark_), false);
}
current_bookmark_ = -1;
} else {
const int new_selection = int(std::distance(bookmark_paths_.begin(), it.base()) - 1);
if(new_selection != current_bookmark_) {
assert(unsigned(new_selection) < bookmarks_bar.get_item_count());
if(current_bookmark_ >= 0) {
bookmarks_bar.select_row(unsigned(current_bookmark_), false);
}
bookmarks_bar.select_row(unsigned(new_selection), true);
current_bookmark_ = new_selection;
}
}
// Update bookmark edit controls.
button& del_button = find_widget<button>(&window, "remove_bookmark", false);
if(user_bookmarks_begin_ == -1) {
del_button.set_active(false);
} else {
del_button.set_active(current_bookmark_ >= user_bookmarks_begin_);
}
}
void file_dialog::on_row_selected(window& window)
{
listbox& filelist = find_widget<listbox>(&window, "filelist", false);
text_box& file_textbox = find_widget<text_box>(&window, "filename", false);
button& rm_button = find_widget<button>(&window, "delete_file", 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) {
set_input_text(file_textbox, current_entry_);
rm_button.set_active(true);
} else {
clear_input_text(file_textbox);
rm_button.set_active(false);
}
// Need to do this every time so that input can still be sent to the
// textbox without clicking on it.
window.keyboard_capture(&file_textbox);
}
void file_dialog::on_bookmark_selected(window& window)
{
// Don't let us steal the focus from the primary widgets.
text_box& file_textbox = find_widget<text_box>(&window, "filename", false);
window.keyboard_capture(&file_textbox);
listbox& bookmarks_bar = find_widget<listbox>(&window, "bookmarks", false);
const int new_selection = bookmarks_bar.get_selected_row();
if(new_selection < 0) {
if(current_bookmark_ >= 0) {
// Don't allow the user to unselect the selected bookmark. That wouldn't
// make any sense.
bookmarks_bar.select_row(unsigned(current_bookmark_));
}
return;
}
assert(unsigned(new_selection) < bookmark_paths_.size());
current_bookmark_ = new_selection;
set_path(bookmark_paths_[new_selection]);
refresh_fileview(window);
// Update bookmark edit controls.
button& del_button = find_widget<button>(&window, "remove_bookmark", false);
del_button.set_active(user_bookmarks_begin_ >= 0
&& current_bookmark_ >= user_bookmarks_begin_);
}
void file_dialog::on_bookmark_add_cmd(window& window)
{
const std::string& default_label = fs::base_name(current_dir_);
std::string label = default_label;
const bool confirm = bookmark_create::execute(label, window.video());
if(!confirm) {
return;
}
if(label.empty()) {
label = default_label;
}
listbox& bookmarks_bar = find_widget<listbox>(&window, "bookmarks", false);
desktop::add_user_bookmark(label, current_dir_);
bookmark_paths_.push_back(current_dir_);
const unsigned top_bookmark = bookmark_paths_.size() - 1;
if(user_bookmarks_begin_ == -1) {
user_bookmarks_begin_ = top_bookmark;
}
std::map<std::string, string_map> data;
data["bookmark"]["label"] = label;
bookmarks_bar.add_row(data);
current_bookmark_ = -1;
sync_bookmarks_bar(window);
}
void file_dialog::on_bookmark_del_cmd(window& window)
{
assert(user_bookmarks_begin_ >= 0
&& current_bookmark_ >= 0
&& current_bookmark_ >= user_bookmarks_begin_
&& current_bookmark_ < int(bookmark_paths_.size()));
listbox& bookmarks_bar = find_widget<listbox>(&window, "bookmarks", false);
desktop::remove_user_bookmark(current_bookmark_ - user_bookmarks_begin_);
bookmark_paths_.erase(bookmark_paths_.begin() + current_bookmark_);
bookmarks_bar.remove_row(current_bookmark_);
current_bookmark_ = -1;
sync_bookmarks_bar(window);
}
void file_dialog::on_dir_create_cmd(window& window)
{
std::string new_dir_name;
if(folder_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 file_dialog::on_file_delete_cmd(window& 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, message::yes_no_buttons) == gui2::window::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 dialogs
} // namespace gui2