1
# -*- coding: utf-8 -*-
2
# Copyright © 2007 Lateef Alabi-Oki
4
# This file is part of Scribes.
6
# Scribes is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# Scribes is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with Scribes; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22
This module documents a class that defines the behavior of the
23
import dialog for the template editor.
25
@author: Lateef Alabi-Oki
26
@organization: The Scribes Project
27
@copyright: Copyright © 2007 Lateef Alabi-Oki
28
@license: GNU GPLv2 or Later
29
@contact: mystilleef@gmail.com
32
class ImportDialog(object):
34
This class defines the behavior of the import dialog.
37
def __init__(self, manager, editor):
41
@param self: Reference to the ImportDialog instance.
42
@type self: A ImportDialog object.
44
@param manager: Reference to the TemplateManager instance.
45
@type manager: A TemplateManager object.
47
@param editor: Reference to the text editor.
48
@type editor: An Editor object.
50
self.__init_attributes(manager, editor)
51
self.__set_properties()
52
self.__signal_id_1 = manager.connect("destroy", self.__destroy_cb)
53
self.__signal_id_2 = self.__dialog.connect("delete-event", self.__delete_event_cb)
54
self.__signal_id_3 = self.__dialog.connect("response", self.__response_cb)
55
self.__signal_id_4 = self.__dialog.connect("selection-changed", self.__selection_changed_cb)
56
self.__signal_id_5 = self.__dialog.connect("file-activated", self.__file_activated_cb)
57
self.__signal_id_6 = self.__button.connect("clicked", self.__clicked_cb)
59
def __init_attributes(self, manager, editor):
61
Initialize data attributes.
63
@param self: Reference to the ImportDialog instance.
64
@type self: A ImportDialog object.
66
@param manager: Reference to the TemplateManager instance.
67
@type manager: A TemplateManager object.
69
@param editor: Reference to the text editor.
70
@type editor: An Editor object.
72
from os.path import join, split
73
current_folder = split(globals()["__file__"])[0]
74
glade_file = join(current_folder, "Template.glade")
75
from gtk.glade import XML
76
glade = XML(glade_file, "ImportDialog", domain="scribes")
77
self.__dialog = glade.get_widget("ImportDialog")
78
self.__button = glade.get_widget("ImportDialogImportButton")
79
self.__manager = manager
80
self.__editor = editor
81
self.__signal_id_1 = self.__signal_id_2 = self.__signal_id_3 = None
82
self.__signal_id_4 = self.__signal_id_5 = self.__signal_id_6 = None
85
def __set_properties(self):
87
Set default properties.
89
@param self: Reference to the ImportDialog instance.
90
@type self: An ImportDialog object.
92
from i18n import msg0004
93
self.__dialog.set_property("title", msg0004)
94
from SCRIBES.utils import calculate_resolution_independence
95
width, height = calculate_resolution_independence(self.__editor.window,
97
self.__dialog.set_default_size(width, height)
98
self.__dialog.set_property("icon-name", "stock_open")
99
self.__dialog.set_transient_for(self.__manager.glade.get_widget("TemplateEditorWindow"))
100
from SCRIBES.dialogfilter import create_filter
101
from i18n import msg0005
102
xml_filter = create_filter(msg0005, "text/xml")
103
self.__dialog.add_filter(xml_filter)
104
from SCRIBES.info import home_folder, desktop_folder
106
if path.exists(desktop_folder):
107
self.__dialog.set_current_folder(desktop_folder)
109
self.__dialog.set_current_folder(home_folder)
114
Show the import dialog.
116
@param self: Reference to the ImportDialog instance.
117
@type self: An ImportDialog object.
126
@param self: Reference to the ImportDialog instance.
127
@type self: An ImportDialog object.
129
self.__editor.response()
131
self.__editor.response()
134
def __import_template(self):
136
Import templates from an XML file.
138
@param self: Reference to the ImportDialog instance.
139
@type self: An ImportDialog object.
142
from Exceptions import FileNotFoundError, InvalidFileError
143
from Exceptions import ValidationError, NoDataError
144
from operator import is_, not_
145
self.__manager.emit("importing")
146
xml_template_file = self.__dialog.get_filename()
147
if is_(xml_template_file, None): raise FileNotFoundError
148
from ImportTemplate import import_template_from_file
149
templates = import_template_from_file(xml_template_file)
150
if not_(templates): raise NoDataError
151
language = templates[-1][-1]
152
self.__manager.emit("imported-language", language)
153
except FileNotFoundError:
154
from i18n import msg0013
155
self.__editor.error_dialog.show_message(msg0013, parent_window=self.__dialog)
156
self.__manager.emit("import-error")
157
except InvalidFileError:
158
from i18n import msg0014
159
self.__editor.error_dialog.show_message(msg0014, parent_window=self.__dialog)
160
self.__manager.emit("import-error")
161
except ValidationError:
162
from i18n import msg0014
163
self.__editor.error_dialog.show_message(msg0014, parent_window=self.__dialog)
164
self.__manager.emit("import-error")
166
from i18n import msg0015
167
self.__editor.error_dialog.show_message(msg0015, parent_window=self.__dialog)
168
self.__manager.emit("import-error")
171
def __destroy_cb(self, manager):
173
Handles callback when the "destroy" signal is emitted.
175
@param self: Reference to the ImportDialog instance.
176
@type self: An ImportDialog object.
178
@param manager: Reference to the TemplateManager instance.
179
@type manager: A TemplateManager object.
181
from SCRIBES.utils import delete_attributes, disconnect_signal
182
disconnect_signal(self.__signal_id_1, manager)
183
disconnect_signal(self.__signal_id_2, self.__dialog)
184
disconnect_signal(self.__signal_id_3, self.__dialog)
185
disconnect_signal(self.__signal_id_4, self.__dialog)
186
disconnect_signal(self.__signal_id_5, self.__dialog)
187
disconnect_signal(self.__signal_id_6, self.__button)
188
self.__button.destroy()
189
self.__dialog.destroy()
190
delete_attributes(self)
195
def __delete_event_cb(self, *args):
197
Handles callback when the "delete-event" signal is emitted.
199
@param self: Reference to the ImportDialog instance.
200
@type self: An ImportDialog object.
205
def __response_cb(self, *args):
207
Handles callback when the "response" signal is emitted.
209
@param self: Reference to the ImportDialog instance.
210
@type self: An ImportDialog object.
212
@param dialog: Reference to the ImportDialog instance.
213
@type dialog: An ImportDialog object.
215
@param response_id: A response identification.
216
@type response_id: A Integer object.
218
@return: True to propagate signals to parent widgets.
219
@type: A Boolean Object.
224
def __selection_changed_cb(self, filechooser):
226
Handles callback when the "selection-changed" signal is emitted.
228
@param self: Reference to the ImportDialog instance.
229
@type self: A ImportDialog object.
231
@return: True to propagate signals to parent widgets.
232
@type: A Boolean Object.
234
filename = filechooser.get_filename()
235
if filename is None: return
237
if path.isdir(filename):
238
self.__button.set_property("sensitive", False)
240
self.__button.set_property("sensitive", True)
243
def __file_activated_cb(self, *args):
245
Handles callback when the "file-activated" signal is emitted.
247
@param self: Reference to the ImportDialog instance.
248
@type self: A ImportDialog object.
250
@return: True to propagate signals to parent widgets.
251
@type: A Boolean Object.
253
self.__button.activate()
256
def __clicked_cb(self, *args):
258
Handles callback when the "clicked" signal is emitted.
260
@param self: Reference to the ImportDialog instance.
261
@type self: A ImportDialog object.
263
self.__import_template()