summaryrefslogtreecommitdiffstats
path: root/src/helper/choose-file.cpp
blob: 3890bee211d46dc2059b750eece5186ab603a969 (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later

#include "choose-file.h"
#include <glib/gi18n.h>
#include <gtkmm/filechooser.h>
#include <gtkmm/filechooserdialog.h>
#include <glibmm/miscutils.h>
#include <string>

namespace Inkscape {

std::string choose_file_save(Glib::ustring title, Gtk::Window* parent, Glib::ustring mime_type, Glib::ustring file_name, std::string& current_folder) {
    if (!parent) return {};

    if (current_folder.empty()) {
        current_folder = Glib::get_home_dir();
    }

    Gtk::FileChooserDialog dlg(*parent, title, Gtk::FILE_CHOOSER_ACTION_SAVE);
    constexpr int save_id = Gtk::RESPONSE_OK;
    dlg.add_button(_("Cancel"), Gtk::RESPONSE_CANCEL);
    dlg.add_button(_("Save"), save_id);
    dlg.set_default_response(save_id);
    auto filter = Gtk::FileFilter::create();
    filter->add_mime_type(mime_type);
    dlg.set_filter(filter);
    dlg.set_current_folder(current_folder);
    dlg.set_current_name(file_name);
    dlg.set_do_overwrite_confirmation();
    dlg.set_modal();

    auto id = dlg.run();
    if (id != save_id) return {};

    auto fname = dlg.get_filename();
    if (fname.empty()) return {};

    current_folder = dlg.get_current_folder();

    return fname;
}

std::string choose_file_open(Glib::ustring title, Gtk::Window* parent, std::vector<Glib::ustring> mime_types, std::string& current_folder) {
    if (!parent) return {};

    if (current_folder.empty()) {
        current_folder = Glib::get_home_dir();
    }

    Gtk::FileChooserDialog dlg(*parent, title, Gtk::FILE_CHOOSER_ACTION_OPEN);
    constexpr int open_id = Gtk::RESPONSE_OK;
    dlg.add_button(_("Cancel"), Gtk::RESPONSE_CANCEL);
    dlg.add_button(_("Open"), open_id);
    dlg.set_default_response(open_id);
    auto filter = Gtk::FileFilter::create();
    for (auto&& t : mime_types) {
        filter->add_mime_type(t);
    }
    dlg.set_filter(filter);
    dlg.set_current_folder(current_folder);
    dlg.set_modal();

    auto id = dlg.run();
    if (id != open_id) return {};

    auto fname = dlg.get_filename();
    if (fname.empty()) return {};

    current_folder = dlg.get_current_folder();

    return fname;
}

} // namespace Inkscape