File: joindocument.cc

package info (click to toggle)
subtitleeditor 0.56.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,880 kB
  • sloc: cpp: 26,446; makefile: 1,713; perl: 434; sh: 259; xml: 149
file content (249 lines) | stat: -rw-r--r-- 8,698 bytes parent folder | download
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
// 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 <debug.h>
#include <extension/action.h>
#include <gtkmm_utility.h>
#include <gui/dialogfilechooser.h>
#include <gui/spinbuttontime.h>
#include <i18n.h>
#include <player.h>
#include <subtitleeditorwindow.h>
#include <utility.h>
#include <widget_config_utility.h>

#include <string>

class DialogJoinOffset : public Gtk::Dialog {
  public:
   DialogJoinOffset(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder) : Gtk::Dialog(cobject) {
      utility::set_transient_parent(*this);
      builder->get_widget_derived("spin-offset", m_spinOffset);
   }

   void init(Document* doc, const Subtitle& last_subtitle) {
      TIMING_MODE edit_mode = doc->get_edit_timing_mode();
      m_spinOffset->set_timing_mode(edit_mode);

      long default_offset = 0;

      // Try to prefill with video end time
      Player* player = SubtitleEditorWindow::get_instance()->get_player();
      if (player && player->get_state() != Player::NONE)
         default_offset = std::max<long>(0, player->get_duration());
      else
         default_offset = last_subtitle.get_end().totalmsecs;

      m_spinOffset->set_value(static_cast<double>(default_offset));
      m_spinOffset->grab_focus();
   }

   long get_offset_value() const {
      return static_cast<long>(m_spinOffset->get_value());
   }

  protected:
   SpinButtonTime* m_spinOffset;
};

class JoinDocumentPlugin : public Action {
  public:
   JoinDocumentPlugin() {
      activate();
      update_ui();
   }

   ~JoinDocumentPlugin() {
      deactivate();
   }

   void activate() {
      se_dbg(SE_DBG_PLUGINS);

      // actions
      action_group = Gtk::ActionGroup::create("JoinDocumentPlugin");

      action_group->add(Gtk::Action::create("join-document",
                                            Gtk::Stock::CONNECT,
                                            _("_Join Document"),
                                            _("Add subtitles from a file to the current document, adjusting "
                                              "timecodes by given offset. If a video is open, its duration is "
                                              "offered as the offset. If no video is open, the end time of the "
                                              "last subtitle is offered as the offset.")),
                        sigc::mem_fun(*this, &JoinDocumentPlugin::on_execute_join));

      action_group->add(
         Gtk::Action::create("append-document", Gtk::Stock::ADD, _("_Append Document"), _("Append subtitles from file without changing timecodes")),
         sigc::mem_fun(*this, &JoinDocumentPlugin::on_execute_append));

      // 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-tools/join-document", "join-document", "join-document");
      ui->add_ui(ui_id, "/menubar/menu-tools/append-document", "append-document", "append-document");
   }

   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("join-document")->set_sensitive(visible);
      action_group->get_action("append-document")->set_sensitive(visible);
   }

  protected:
   bool applying_offset;
   void on_execute_join() {
      se_dbg(SE_DBG_PLUGINS);
      applying_offset = true;
      execute(applying_offset);
   }

   void on_execute_append() {
      se_dbg(SE_DBG_PLUGINS);
      applying_offset = false;
      execute(applying_offset);
   }

   bool execute(bool applying_offset) {
      se_dbg(SE_DBG_PLUGINS);

      Document* doc = get_current_document();

      g_return_val_if_fail(doc, false);

      // Get the original document info before opening the file dialog
      unsigned int subtitle_size = doc->subtitles().size();

      DialogOpenDocument::unique_ptr ui = DialogOpenDocument::create();

      ui->show_video(false);
      ui->set_select_multiple(false);

      if (ui->run() != Gtk::RESPONSE_OK)
         return false;

      Glib::ustring uri = ui->get_uri();

      // tmp document to try to open the file
      Document* tmp = Document::create_from_file(uri);
      if (tmp == NULL)
         return false;

      ui->hide();  // hides (closes?) the dialog so the next one can display

      Glib::ustring encoding = tmp->getCharset();
      delete tmp;

      // Declare offset variable outside the if block so it's available later
      SubtitleTime offset = 0;

      // If we are joining, we ask for offset first, otherwise the subtitles
      // get joined without offset in the background, which looks ugly
      if (applying_offset) {
         // Get the last subtitle of the original document
         Subtitle last_orig_sub = doc->subtitles().get(subtitle_size);

         // Show offset dialog
         std::unique_ptr<DialogJoinOffset> offset_dialog(gtkmm_utility::get_widget_derived<DialogJoinOffset>(
            SE_DEV_VALUE(SE_PLUGIN_PATH_UI, SE_PLUGIN_PATH_DEV), "dialog-join-offset.ui", "dialog-join-offset"));

         offset_dialog->init(doc, last_orig_sub);

         if (offset_dialog->run() == Gtk::RESPONSE_OK) {
            offset = offset_dialog->get_offset_value();
         } else {
            doc->flash_message(_("Join cancelled."));
            return false;
         }
      }

      Glib::ustring ofile = doc->getFilename();
      Glib::ustring oformat = doc->getFormat();
      Glib::ustring ocharset = doc->getCharset();

      try {  // needs with Document::open
         doc->start_command(applying_offset ? _("Join document") : _("Append document"));
         doc->setCharset(encoding);
         doc->open(uri);

         // Get The first subtitle added to the original document
         Subtitle last_orig_sub = doc->subtitles().get(subtitle_size);
         Subtitle first_new_subs = doc->subtitles().get_next(last_orig_sub);

         // Now we apply offset
         if (applying_offset) {
            SubtitleTime min_gap = cfg::get_int("timing", "min-gap-between-subtitles");
            SubtitleTime gap = offset - last_orig_sub.get_end();
            se_dbg_msg(SE_DBG_PLUGINS, "First new %s", first_new_subs.get_start().str().c_str());
            se_dbg_msg(SE_DBG_PLUGINS, "Offset %s", offset.str().c_str());
            se_dbg_msg(SE_DBG_PLUGINS, "Last_orig %s", last_orig_sub.get_end().str().c_str());
            se_dbg_msg(SE_DBG_PLUGINS, "Min_gap %s", min_gap.str().c_str());
            se_dbg_msg(SE_DBG_PLUGINS, "Gap %s", gap.str().c_str());
            if (gap < min_gap)
               offset = offset + min_gap - gap;

            for (Subtitle sub = first_new_subs; sub; ++sub) {
               sub.set_start_and_end(sub.get_start() + offset, sub.get_end() + offset);
            }
         }

         // Make the user life easy by selecting the first new subtitle
         if (first_new_subs) {
            doc->subtitles().select(first_new_subs);
         }

         doc->setFilename(ofile);
         doc->setFormat(oformat);
         doc->setCharset(ocharset);
         doc->finish_command();

         unsigned int subtitles_added = doc->subtitles().size() - subtitle_size;

         doc->flash_message(
            ngettext("One subtitle has been added to this document.", "%d subtitles have been added to this document.", subtitles_added),
            subtitles_added);
      } catch (...) {
         se_dbg_msg(SE_DBG_PLUGINS, "Failed to %s document: %s", applying_offset ? "join" : "append", uri.c_str());
      }

      return true;
   }

  protected:
   Gtk::UIManager::ui_merge_id ui_id;
   Glib::RefPtr<Gtk::ActionGroup> action_group;
};

REGISTER_EXTENSION(JoinDocumentPlugin)