gaupol/agents/save.py¶
Source code for module gaupol.agents.save from file gaupol/agents/save.py.
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 | # -*- coding: utf-8 -*-
# Copyright (C) 2005 Osmo Salomaa
#
# 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/>.
"""Saving documents."""
import aeidon
import gaupol
import os
_ = aeidon.i18n._
from gi.repository import Gtk
class SaveAgent(aeidon.Delegate):
"""Saving documents."""
@aeidon.deco.export
def _on_save_all_documents_activate(self, *args):
"""Save all open documents."""
for page in self.pages:
with aeidon.util.silent(gaupol.Default):
self.save_main(page)
if page.project.tran_changed is not None:
with aeidon.util.silent(gaupol.Default):
self.save_translation(page)
self.update_gui()
@aeidon.deco.export
def _on_save_all_documents_as_activate(self, *args):
"""Save all open documents with different properties."""
modes = []
for page in self.pages:
if page.project.main_file is not None:
modes.append(page.project.main_file.mode)
dialog = gaupol.MultiSaveDialog(self.window, self, modes)
gaupol.util.flash_dialog(dialog)
self.update_gui()
@aeidon.deco.export
@aeidon.deco.silent(gaupol.Default)
def _on_save_main_document_activate(self, *args):
"""Save the current main document."""
page = self.get_current_page()
self.save_main(page)
self.update_gui()
@aeidon.deco.export
@aeidon.deco.silent(gaupol.Default)
def _on_save_main_document_as_activate(self, *args):
"""Save the current main document with a different name."""
page = self.get_current_page()
self.save_main_as(page)
self.update_gui()
@aeidon.deco.export
@aeidon.deco.silent(gaupol.Default)
def _on_save_translation_document_activate(self, *args):
"""Save the current translation document."""
page = self.get_current_page()
self.save_translation(page)
self.update_gui()
@aeidon.deco.export
@aeidon.deco.silent(gaupol.Default)
def _on_save_translation_document_as_activate(self, *args):
"""Save the current translation document with a different name."""
page = self.get_current_page()
self.save_translation_as(page)
self.update_gui()
def _save_document(self, page, doc, file=None):
"""Save document to `file` or raise :exc:`gaupol.Default`."""
file = file or page.project.get_file(doc)
try:
gaupol.util.set_cursor_busy(self.window)
return page.project.save(doc, file)
except IOError as error:
gaupol.util.set_cursor_normal(self.window)
basename = os.path.basename(file.path)
self._show_io_error_dialog(basename, str(error))
except UnicodeError:
gaupol.util.set_cursor_normal(self.window)
basename = os.path.basename(file.path)
self._show_encoding_error_dialog(basename, file.encoding)
finally:
gaupol.util.set_cursor_normal(self.window)
raise gaupol.Default
@aeidon.deco.export
def save_main(self, page):
"""
Save the main document of `page` to file.
Raise :exc:`gaupol.Default` if cancelled or saving failed.
"""
if (page.project.main_file is None or
page.project.main_file.path is None or
page.project.main_file.encoding is None):
return self.save_main_as(page)
self._save_document(page, aeidon.documents.MAIN)
@aeidon.deco.export
def save_main_as(self, page, file=None):
"""
Save the main document of `page` to a selected file.
If `file` is ``None`` show a filechooser dialog.
Raise :exc:`gaupol.Default` if cancelled or saving failed.
"""
if file is None:
file = page.project.main_file
file = self._select_file(_("Save As"), page, file)
self._save_document(page, aeidon.documents.MAIN, file)
self.add_to_recent_files(page.project.main_file.path,
page.project.main_file.format,
aeidon.documents.MAIN)
basename = os.path.basename(page.project.main_file.path)
self.flash_message(_('Saved main document as "{}"')
.format(basename))
@aeidon.deco.export
def save_translation(self, page):
"""
Save the translation document of `page` to file.
Raise :exc:`gaupol.Default` if cancelled or saving failed.
"""
if (page.project.tran_file is None or
page.project.tran_file.path is None or
page.project.tran_file.encoding is None):
return self.save_translation_as(page)
self._save_document(page, aeidon.documents.TRAN)
@aeidon.deco.export
def save_translation_as(self, page, file=None):
"""
Save the translation document of `page` to a selected file.
If `file` is ``None`` show a filechooser dialog.
Raise :exc:`gaupol.Default` if cancelled or saving failed.
"""
if file is None:
file = page.project.tran_file
file = self._select_file(_("Save Translation As"), page, file)
self._save_document(page, aeidon.documents.TRAN, file)
self.add_to_recent_files(page.project.tran_file.path,
page.project.tran_file.format,
aeidon.documents.TRAN)
basename = os.path.basename(page.project.tran_file.path)
self.flash_message(_('Saved translation document as "{}"')
.format(basename))
def _select_file(self, title, page, file=None):
"""Select a file to save or raise :exc:`gaupol.Default`."""
gaupol.util.set_cursor_busy(self.window)
mode = page.project.get_mode()
dialog = gaupol.SaveDialog(self.window, title, mode)
if file is not None:
dialog.set_name(file.path)
dialog.set_format(file.format)
dialog.set_encoding(file.encoding)
dialog.set_newline(file.newline)
dialog.set_framerate(page.project.framerate)
gaupol.util.set_cursor_normal(self.window)
response = gaupol.util.run_dialog(dialog)
format = dialog.get_format()
path = dialog.get_filename()
encoding = dialog.get_encoding()
newline = dialog.get_newline()
framerate = dialog.get_framerate()
dialog.destroy()
if response != Gtk.ResponseType.OK:
raise gaupol.Default
if format.mode != mode:
# Set framerate to the selected one.
self.set_current_page(page)
action = self.get_framerate_action(framerate)
action.set_active(True)
gaupol.util.iterate_main()
return aeidon.files.new(format, path, encoding, newline)
def _show_encoding_error_dialog(self, basename, codec):
"""Show an error dialog after failing to encode file."""
codec = aeidon.encodings.code_to_name(codec)
title = _('Failed to encode file "{basename}" with codec "{codec}"').format(**locals())
message = _("Please try to save the file with a different character encoding.")
dialog = gaupol.ErrorDialog(self.window, title, message)
dialog.add_button(_("_OK"), Gtk.ResponseType.OK)
dialog.set_default_response(Gtk.ResponseType.OK)
gaupol.util.flash_dialog(dialog)
def _show_io_error_dialog(self, basename, message):
"""Show an error dialog after failing to write file."""
title = _('Failed to save file "{}"').format(basename)
dialog = gaupol.ErrorDialog(self.window, title, message)
dialog.add_button(_("_OK"), Gtk.ResponseType.OK)
dialog.set_default_response(Gtk.ResponseType.OK)
gaupol.util.flash_dialog(dialog)
|