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
|
// subtitleeditor -- a tool to create or edit subtitle
//
// https://subtitleeditor.github.io/subtitleeditor/
// https://github.com/subtitleeditor/subtitleeditor/
//
// Copyright @ 2005-2018, kitone
//
// 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 3 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; 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 this program. If not, see <http://www.gnu.org/licenses/>.
#include <documents.h>
#include <extension/action.h>
#include <filereader.h>
#include <gui/dialogfilechooser.h>
#include <subtitleformatsystem.h>
#include <utility.h>
class PlainTextPlugin : public Action {
public:
PlainTextPlugin() {
activate();
update_ui();
}
~PlainTextPlugin() {
deactivate();
}
void activate() {
se_dbg(SE_DBG_PLUGINS);
// actions
action_group = Gtk::ActionGroup::create("PlainTextPlugin");
action_group->add(Gtk::Action::create("plain-text-import",
_("_Import Plain Text"),
_("Create a new document from a text file (one or more consecutive blank lines separate chunks of text "
"that will be imported as separate subtitles) ")),
sigc::mem_fun(*this, &PlainTextPlugin::on_import_transcript));
action_group->add(Gtk::Action::create("plain-text-export",
_("_Export Plain Text"),
_("Export text of subtitles into a file (blank lines separating the text of individual subtitles)")),
sigc::mem_fun(*this, &PlainTextPlugin::on_export_transcript));
// ui
Glib::RefPtr<Gtk::UIManager> ui = get_ui_manager();
ui_id = ui->new_merge_id();
ui->insert_action_group(action_group);
ui->add_ui(ui_id, "/menubar/menu-file/plain-text-import", "plain-text-import", "plain-text-import");
ui->add_ui(ui_id, "/menubar/menu-file/plain-text-export", "plain-text-export", "plain-text-export");
}
void deactivate() {
se_dbg(SE_DBG_PLUGINS);
Glib::RefPtr<Gtk::UIManager> ui = get_ui_manager();
ui->remove_ui(ui_id);
ui->remove_action_group(action_group);
}
void update_ui() {
se_dbg(SE_DBG_PLUGINS);
bool visible = (get_current_document() != NULL);
action_group->get_action("plain-text-export")->set_sensitive(visible);
}
protected:
void on_import_transcript() {
se_dbg(SE_DBG_PLUGINS);
DialogImportText::unique_ptr ui = DialogImportText::create();
if (ui->run() == Gtk::RESPONSE_OK) {
Glib::ustring uri = ui->get_uri();
Glib::ustring filename = ui->get_filename();
Glib::ustring charset = ui->get_encoding();
try {
Glib::ustring format = cfg::get_string("document", "format");
Glib::ustring extension = SubtitleFormatSystem::instance().get_extension_of_format(format);
Glib::ustring untitled = se::documents::generate_untitled_name(extension);
Glib::ustring untitled_fullname = Glib::build_filename(ui->get_current_folder(), untitled);
Document* doc = new Document(true);
SubtitleFormatSystem::instance().open_from_uri(doc, uri, charset, "Plain Text Format");
doc->setName(untitled);
doc->setFilename(untitled_fullname);
// override the plain text format with the preferred format setting
doc->setFormat(format);
se::documents::append(doc);
} catch (const std::exception& ex) {
dialog_error(build_message(_("Could not import from file \"%s\"."), uri.c_str()), ex.what());
}
}
}
void on_export_transcript() {
se_dbg(SE_DBG_PLUGINS);
DialogExportText::unique_ptr ui = DialogExportText::create();
if (ui->run() == Gtk::RESPONSE_OK) {
Glib::ustring uri = ui->get_uri();
Glib::ustring charset = ui->get_encoding();
Glib::ustring newline = ui->get_newline();
try {
Document* doc = get_current_document();
SubtitleFormatSystem::instance().save_to_uri(doc, uri, "Plain Text Format", charset, newline);
} catch (const std::exception& ex) {
dialog_error(build_message(_("Could not export to the file \"%s\"."), uri.c_str()), ex.what());
}
}
}
protected:
Gtk::UIManager::ui_merge_id ui_id;
Glib::RefPtr<Gtk::ActionGroup> action_group;
};
REGISTER_EXTENSION(PlainTextPlugin)
|