File: externaleditorpreferences.cc

package info (click to toggle)
rawtherapee 5.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 124,328 kB
  • sloc: cpp: 271,715; ansic: 27,904; sh: 1,109; python: 521; cs: 155; xml: 57; makefile: 15
file content (407 lines) | stat: -rw-r--r-- 13,410 bytes parent folder | download | duplicates (2)
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
/*
 *  This file is part of RawTherapee.
 *
 *  Copyright (c) 2021 Lawrence Lee <billee@ucdavis.edu>
 *
 *  RawTherapee 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.
 *
 *  RawTherapee 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 RawTherapee.  If not, see <https://www.gnu.org/licenses/>.
 */
#include <iostream>

#include <giomm/contenttype.h>
#include <glibmm/shell.h>
#include <gtkmm/filechooserdialog.h>
#include <gtkmm/stock.h>

#include "externaleditorpreferences.h"
#include "multilangmgr.h"


ExternalEditorPreferences::ExternalEditorPreferences():
    Box(Gtk::Orientation::ORIENTATION_VERTICAL),
    list_model(Gtk::ListStore::create(model_columns)),
    toolbar(Gtk::Orientation::ORIENTATION_HORIZONTAL)
{
    // List view.
    list_view = Gtk::manage(new Gtk::TreeView());
    list_view->set_model(list_model);
    list_view->append_column(*Gtk::manage(makeAppColumn()));
#ifndef __APPLE__
    list_view->append_column(*Gtk::manage(makeNativeCommandColumn()));
#endif
    list_view->append_column(*Gtk::manage(makeCommandColumn()));

    for (auto &&column : list_view->get_columns()) {
        column->set_sizing(Gtk::TreeViewColumnSizing::TREE_VIEW_COLUMN_FIXED);
    }

    list_view->set_grid_lines(Gtk::TREE_VIEW_GRID_LINES_VERTICAL);
    list_view->set_reorderable();

    // List scroll area.
    list_scroll_area.set_hexpand();
    list_scroll_area.set_vexpand();
    list_scroll_area.add(*list_view);

    // Toolbar buttons.
    button_add = Gtk::manage(new Gtk::Button());
    button_remove = Gtk::manage(new Gtk::Button());
    button_add->set_image_from_icon_name("add-small");
    button_remove->set_image_from_icon_name("remove-small");
    button_app_chooser =
#ifdef __APPLE__
        nullptr;
#else
        Gtk::manage(new Gtk::Button(M("PREFERENCES_EXTERNALEDITOR_CHANGE")));
#endif
    button_file_chooser = Gtk::manage(new Gtk::Button(M("PREFERENCES_EXTERNALEDITOR_CHANGE_FILE")));

    if (button_app_chooser) {
        button_app_chooser->signal_pressed().connect(sigc::mem_fun(
                    *this, &ExternalEditorPreferences::openAppChooserDialog));
    }
    button_add->signal_pressed().connect(sigc::mem_fun(
            *this, &ExternalEditorPreferences::addEditor));
    button_file_chooser->signal_pressed().connect(sigc::mem_fun(
        *this, &ExternalEditorPreferences::openFileChooserDialog));
    button_remove->signal_pressed().connect(sigc::mem_fun(
            *this, &ExternalEditorPreferences::removeSelectedEditors));

    list_view->get_selection()->signal_changed().connect(sigc::mem_fun(
                *this, &ExternalEditorPreferences::updateToolbarSensitivity));
    updateToolbarSensitivity();

    // Toolbar.
    toolbar.set_halign(Gtk::Align::ALIGN_END);
    if (button_app_chooser) {
        toolbar.add(*button_app_chooser);
    }
    toolbar.add(*button_file_chooser);
    toolbar.add(*button_add);
    toolbar.add(*button_remove);

    // This widget's children.
    add(list_scroll_area);
    add(toolbar);
    show_all();
}

std::vector<ExternalEditorPreferences::EditorInfo>
ExternalEditorPreferences::getEditors() const
{
    std::vector<EditorInfo> editors;

    auto children = list_model->children();

    for (auto rowIter = children.begin(); rowIter != children.end(); rowIter++) {
        const auto icon = rowIter->get_value(model_columns.icon);
        const auto &icon_serialized = !icon ? "" : icon->serialize().print();
        editors.emplace_back(
            rowIter->get_value(model_columns.name),
            rowIter->get_value(model_columns.command),
            icon_serialized,
            rowIter->get_value(model_columns.native_command),
            rowIter->get_value(model_columns.other_data));
    }

    return editors;
}

void ExternalEditorPreferences::setEditors(
    const std::vector<ExternalEditorPreferences::EditorInfo> &editors)
{
    list_model->clear();

    for (const EditorInfo & editor : editors) {
        auto row = *list_model->append();
        Glib::RefPtr<Gio::Icon> icon;

        // Get icon.
        if (editor.icon_serialized.empty()) {
            icon = Glib::RefPtr<Gio::Icon>();
        } else {
            GError *e = nullptr;
            GVariant *icon_variant = g_variant_parse(
                nullptr, editor.icon_serialized.c_str(), nullptr, nullptr, &e);
            if (e) {
                std::cerr
                    << "Error loading external editor icon from \""
                    << editor.icon_serialized << "\": " << e->message
                    << std::endl;
                icon = Glib::RefPtr<Gio::Icon>();
            } else {
                icon = Gio::Icon::deserialize(Glib::VariantBase(icon_variant));
            }
        }

        row[model_columns.name] = editor.name;
        row[model_columns.icon] = icon;
        row[model_columns.command] = editor.command;
        row[model_columns.native_command] = editor.native_command;
        row[model_columns.other_data] = editor.other_data;
    }
}

void ExternalEditorPreferences::addEditor()
{
    Gtk::TreeModel::Row row;
    auto selected = list_view->get_selection()->get_selected_rows();

    if (selected.size()) {
        row = *list_model->insert_after(list_model->get_iter(selected.back()));
    } else {
        row = *list_model->append();
    }

    row[model_columns.name] = "-";
#ifdef __APPLE__
    row[model_columns.native_command] = true;
#endif
    list_view->get_selection()->select(row);
}

Gtk::TreeViewColumn *ExternalEditorPreferences::makeAppColumn()
{
    auto name_renderer = Gtk::manage(new Gtk::CellRendererText());
    auto icon_renderer = Gtk::manage(new Gtk::CellRendererPixbuf());
    auto col = Gtk::manage(new Gtk::TreeViewColumn());

    col->set_title(M("PREFERENCES_EXTERNALEDITOR_COLUMN_NAME"));
    col->set_resizable();
    col->pack_start(*icon_renderer, false);
    col->pack_start(*name_renderer);
    col->add_attribute(icon_renderer->property_gicon(), model_columns.icon);
    col->add_attribute(name_renderer->property_text(), model_columns.name);
    col->set_min_width(20);

    name_renderer->property_editable() = true;
    name_renderer->signal_edited().connect(
        sigc::mem_fun(*this, &ExternalEditorPreferences::setAppName));

    return col;
}

Gtk::TreeViewColumn *ExternalEditorPreferences::makeCommandColumn()
{
    auto command_renderer = Gtk::manage(new Gtk::CellRendererText());
    auto col = Gtk::manage(new Gtk::TreeViewColumn());

    col->set_title(M("PREFERENCES_EXTERNALEDITOR_COLUMN_COMMAND"));
    col->pack_start(*command_renderer);
    col->add_attribute(command_renderer->property_text(), model_columns.command);

    command_renderer->property_editable() = true;
    command_renderer->signal_edited().connect(
        sigc::mem_fun(*this, &ExternalEditorPreferences::setAppCommand));

    return col;
}

Gtk::TreeViewColumn *ExternalEditorPreferences::makeNativeCommandColumn()
{
    auto toggle_renderer = Gtk::manage(new Gtk::CellRendererToggle());
    auto col = Gtk::manage(new Gtk::TreeViewColumn());

    col->set_title(M("PREFERENCES_EXTERNALEDITOR_COLUMN_NATIVE_COMMAND"));
    col->pack_start(*toggle_renderer);
    col->add_attribute(toggle_renderer->property_active(), model_columns.native_command);

    toggle_renderer->signal_toggled().connect([this](const Glib::ustring &path) {
        const auto row_iter = list_model->get_iter(path);
        bool new_value = !row_iter->get_value(model_columns.native_command);
        row_iter->set_value(model_columns.native_command, new_value);
    });

    return col;
}

void ExternalEditorPreferences::onAppChooserDialogResponse(
    int response_id, RTAppChooserDialog *dialog)
{
    switch (response_id) {
        case Gtk::RESPONSE_OK:
            dialog->close();
            setApp(dialog->get_app_info());
            break;

        case Gtk::RESPONSE_CANCEL:
        case Gtk::RESPONSE_CLOSE:
            dialog->close();
            break;

        default:
            break;
    }
}

void ExternalEditorPreferences::onFileChooserDialogResponse(
        int response_id, Gtk::FileChooserDialog *dialog)
{
    switch (response_id) {
        case Gtk::RESPONSE_OK: {
            dialog->close();

            auto selection = list_view->get_selection()->get_selected_rows();
            for (const auto &selected : selection) {
                auto row = *list_model->get_iter(selected);
                row[model_columns.icon] = Glib::RefPtr<Gio::Icon>(nullptr);
                row[model_columns.native_command] =
#ifdef __APPLE__
                    true;
#else
                    false;
#endif
                row[model_columns.command] =
#ifdef _WIN32
                    '"' + dialog->get_filename() + '"';
#else
                    Glib::shell_quote(dialog->get_filename());
#endif
            }

            break;
        }

        case Gtk::RESPONSE_CANCEL:
        case Gtk::RESPONSE_CLOSE:
            dialog->close();
            break;

        default:
            break;
    }
}

void ExternalEditorPreferences::openAppChooserDialog()
{
    if (app_chooser_dialog.get()) {
        app_chooser_dialog->refresh();
        app_chooser_dialog->show();
        return;
    }

    app_chooser_dialog.reset(new RTAppChooserDialog("image/tiff"));
    app_chooser_dialog->signal_response().connect(sigc::bind(
                sigc::mem_fun(*this, &ExternalEditorPreferences::onAppChooserDialogResponse),
                app_chooser_dialog.get()
            ));
    app_chooser_dialog->set_modal();
    app_chooser_dialog->show();
}

void ExternalEditorPreferences::openFileChooserDialog()
{
    if (file_chooser_dialog.get()) {
        file_chooser_dialog->show();
        return;
    }

    file_chooser_dialog.reset(new Gtk::FileChooserDialog(M("PREFERENCES_EXTERNALEDITOR_CHANGE_FILE")));

    const auto exe_filter = Gtk::FileFilter::create();
    exe_filter->set_name(M("FILECHOOSER_FILTER_EXECUTABLE"));
    exe_filter->add_custom(Gtk::FILE_FILTER_MIME_TYPE, [](const Gtk::FileFilter::Info &info) {
#ifdef _WIN32
        return info.mime_type == "application/x-msdownload";
#else
        return Gio::content_type_can_be_executable(info.mime_type);
#endif
    });
    const auto all_filter = Gtk::FileFilter::create();
    all_filter->set_name(M("FILECHOOSER_FILTER_ANY"));
    all_filter->add_pattern("*");
    file_chooser_dialog->add_filter(exe_filter);
    file_chooser_dialog->add_filter(all_filter);

    file_chooser_dialog->signal_response().connect(sigc::bind(
        sigc::mem_fun(*this, &ExternalEditorPreferences::onFileChooserDialogResponse),
        file_chooser_dialog.get()));
    file_chooser_dialog->set_modal();
    file_chooser_dialog->add_button(M("GENERAL_CANCEL"), Gtk::RESPONSE_CANCEL);
    file_chooser_dialog->add_button(M("GENERAL_OPEN"), Gtk::RESPONSE_OK);
    file_chooser_dialog->show();
}

void ExternalEditorPreferences::removeSelectedEditors()
{
    auto selection = list_view->get_selection()->get_selected_rows();

    for (const auto &selected : selection) {
        list_model->erase(list_model->get_iter(selected));
    }
}

void ExternalEditorPreferences::setApp(const Glib::RefPtr<Gio::AppInfo> app_info)
{
    auto selection = list_view->get_selection()->get_selected_rows();

    for (const auto &selected : selection) {
        auto row = *list_model->get_iter(selected);
        row[model_columns.icon] = app_info->get_icon();
        row[model_columns.name] = app_info->get_name();
        row[model_columns.command] = app_info->get_commandline();
        row[model_columns.native_command] = false;
    }
}

void ExternalEditorPreferences::setAppCommand(
    const Glib::ustring & path, const Glib::ustring & new_text)
{
    auto row_iter = list_model->get_iter(path);

    if (!row_iter->get_value(model_columns.command).compare(new_text)) {
        return;
    }

    row_iter->set_value(model_columns.command, new_text);
    row_iter->set_value(model_columns.icon, Glib::RefPtr<Gio::Icon>(nullptr));
}

void ExternalEditorPreferences::setAppName(
    const Glib::ustring & path, const Glib::ustring & new_text)
{
    list_model->get_iter(path)->set_value(model_columns.name, new_text);
}

void ExternalEditorPreferences::updateToolbarSensitivity()
{
    bool selected = list_view->get_selection()->count_selected_rows();
    if (button_app_chooser) {
        button_app_chooser->set_sensitive(selected);
    }
    button_file_chooser->set_sensitive(selected);
    button_remove->set_sensitive(selected);
}

ExternalEditorPreferences::EditorInfo::EditorInfo(
    const Glib::ustring &name,
    const Glib::ustring &command,
    const Glib::ustring &icon_serialized,
    bool native_command,
    EditorTag other_data) :
    name(name),
    icon_serialized(icon_serialized),
    command(command),
    native_command(native_command),
    other_data(other_data)
{
}

ExternalEditorPreferences::ModelColumns::ModelColumns()
{
    add(name);
    add(icon);
    add(command);
    add(native_command);
    add(other_data);
}