def __init__(self, parent, text): self._text = text self.encoding = None self._builder = Gtk.Builder() glade_file = Resources.find(path.join('data', 'gui', 'glade', 'encoding_dialog.glade')) self._builder.add_from_file(glade_file) self._dialog = self._builder.get_object('encoding_dialog') self._dialog.set_transient_for(parent) self._builder.connect_signals(self) self._text_content = self._builder.get_object('text_content') self._store_encoding = self._builder.get_object('store_encoding') encodings = sorted(TextFile.get_available_encodings_with_title(), key=itemgetter(0)) # Test which encoding can be used to decode the text-file and # add those encodings to the combo box. for encoding in encodings: try: self._text.decode(encoding[1]) # decode successfull --> add to list self._store_encoding.append(encoding) except UnicodeDecodeError: continue
def __init__(self, parent, title, enable_encoding_selection=False): self._enable_encoding_selection = enable_encoding_selection self.encoding = None self._builder = Gtk.Builder() glade_file = Resources.find(path.join('data', 'gui', 'glade', 'ext_file_chooser_dialog.glade')) self._builder.add_from_file(glade_file) self._dialog = self._builder.get_object('filechooserdialog') self._dialog.set_transient_for(parent) self._dialog.set_title(title) self._builder.connect_signals(self) self._box_encoding = self._builder.get_object('box_encoding') self._combo_encoding = self._builder.get_object('combo_encoding') self._store_encoding = self._builder.get_object('store_encoding') if not enable_encoding_selection: self._box_encoding.hide() self._combo_encoding.set_row_separator_func(self._is_separator, None) encodings = sorted(TextFile.get_available_encodings_with_title(), key=itemgetter(0)) self._store_encoding.append([_('Detect automatically'), None]) self._store_encoding.append(['-', None]) for encoding in encodings: self._store_encoding.append(encoding) self._combo_encoding.set_active(0)