File: ask_subtitles.py

package info (click to toggle)
devede 4.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 3,864 kB
  • sloc: python: 5,416; makefile: 17
file content (120 lines) | stat: -rw-r--r-- 4,299 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
# Copyright 2014 (C) Raster Software Vigo (Sergio Costas)
#
# This file is part of DeVeDe-NG
#
# DeVeDe-NG 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.
#
# DeVeDe-NG 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/>

from gi.repository import Gtk
import os
import devedeng.configuration_data
import devedeng.add_files

class ask_subtitles:

    def __init__(self):

        self.config = devedeng.configuration_data.configuration.get_config()

    def run(self):

        builder = Gtk.Builder()
        builder.set_translation_domain(self.config.gettext_domain)

        builder.add_from_file(os.path.join(self.config.glade,"wask_subtitles.ui"))
        builder.connect_signals(self)
        wask_window = builder.get_object("ask_subtitles")
        self.wfilename = builder.get_object("subtitle_file")
        self.waccept = builder.get_object("accept")
        wlist_encodings = builder.get_object("list_encodings")
        wlist_languages = builder.get_object("list_languages")
        wencoding = builder.get_object("encoding_l")
        wlanguage = builder.get_object("language_l")

        if (devedeng.add_files.add_files.last_path is not None):
            self.wfilename.set_current_folder(devedeng.add_files.add_files.last_path)

        lang_selection = 0
        enc_selection = 0

        self.language = None
        self.encoding = None
        self.put_upper = False
        self.filename = None
        
        counter = 0
        encodings = open(os.path.join(self.config.other_path,"codepages.lst"))
        for element in encodings:
            element = element.strip()
            if (element == self.config.sub_codepage):
                enc_selection = counter
            wlist_encodings.append([element])
            counter += 1
        encodings.close()
        
        counter = 0
        languages = open(os.path.join(self.config.other_path,"languages.lst"))
        for element in languages:
            element = element.strip()
            if (element == self.config.sub_language):
                lang_selection = counter
            wlist_languages.append([element])
            counter += 1
        languages.close()
        
        wencoding.set_active(enc_selection)
        wlanguage.set_active(lang_selection)

        file_filter_subt=Gtk.FileFilter()
        file_filter_subt.set_name(_("Subtitle files"))

        file_filter_subt.add_pattern("*.sub")
        file_filter_subt.add_pattern("*.srt")
        file_filter_subt.add_pattern("*.ssa")
        file_filter_subt.add_pattern("*.smi")
        file_filter_subt.add_pattern("*.rt")
        file_filter_subt.add_pattern("*.txt")
        file_filter_subt.add_pattern("*.aqt")

        file_filter_all=Gtk.FileFilter()
        file_filter_all.set_name(_("All files"))
        file_filter_all.add_pattern("*")

        self.wfilename.add_filter(file_filter_subt)
        self.wfilename.add_filter(file_filter_all)

        wask_window.show_all()
        self.on_subtitle_file_set(None)
        retval = wask_window.run()
        if (retval == 2): # accept
            self.put_upper = builder.get_object("put_subtitles_upper").get_active()
            self.config.sub_codepage = wencoding.get_active_id()
            self.config.sub_language = wlanguage.get_active_id()
            self.encoding = wencoding.get_active_id()
            self.language = wlanguage.get_active_id()[:2]
            self.filename = self.wfilename.get_filename()

        wask_window.destroy()
        if (retval == 2):
            return True
        else:
            return False

    
    def on_subtitle_file_set(self,b):
        
        f = self.wfilename.get_filename()
        if (f is None) or (f == ""):
            self.waccept.set_sensitive(False)
        else:
            self.waccept.set_sensitive(True)