~sir-rainbow/+junk/scribes-on-win

« back to all changes in this revision

Viewing changes to plugins/TemplateEditor/ImportDialog.py

  • Committer: goldenmyst
  • Date: 2007-09-25 17:15:52 UTC
  • Revision ID: goldenmyst@goldenmyst-desktop-20070925171552-mvrhxdd39iibs0sr
New branch. New Trigger Management System. New Trigger API. New Plugin Management System. Fix for bug triggered by PyGTK+ version 2.11 or better.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright © 2007 Lateef Alabi-Oki
 
3
#
 
4
# This file is part of Scribes.
 
5
#
 
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.
 
10
#
 
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.
 
15
#
 
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
 
19
# USA
 
20
 
 
21
"""
 
22
This module documents a class that defines the behavior of the
 
23
import dialog for the template editor.
 
24
 
 
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
 
30
"""
 
31
 
 
32
class ImportDialog(object):
 
33
        """
 
34
        This class defines the behavior of the import dialog.
 
35
        """
 
36
 
 
37
        def __init__(self, manager, editor):
 
38
                """
 
39
                Initialize object.
 
40
 
 
41
                @param self: Reference to the ImportDialog instance.
 
42
                @type self: A ImportDialog object.
 
43
 
 
44
                @param manager: Reference to the TemplateManager instance.
 
45
                @type manager: A TemplateManager object.
 
46
 
 
47
                @param editor: Reference to the text editor.
 
48
                @type editor: An Editor object.
 
49
                """
 
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)
 
58
 
 
59
        def __init_attributes(self, manager, editor):
 
60
                """
 
61
                Initialize data attributes.
 
62
 
 
63
                @param self: Reference to the ImportDialog instance.
 
64
                @type self: A ImportDialog object.
 
65
 
 
66
                @param manager: Reference to the TemplateManager instance.
 
67
                @type manager: A TemplateManager object.
 
68
 
 
69
                @param editor: Reference to the text editor.
 
70
                @type editor: An Editor object.
 
71
                """
 
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
 
83
                return
 
84
 
 
85
        def __set_properties(self):
 
86
                """
 
87
                Set default properties.
 
88
 
 
89
                @param self: Reference to the ImportDialog instance.
 
90
                @type self: An ImportDialog object.
 
91
                """
 
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,
 
96
                                                                                                                1.6, 1.929648241)
 
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
 
105
                from os import path
 
106
                if path.exists(desktop_folder):
 
107
                        self.__dialog.set_current_folder(desktop_folder)
 
108
                else:
 
109
                        self.__dialog.set_current_folder(home_folder)
 
110
                return
 
111
 
 
112
        def show(self):
 
113
                """
 
114
                Show the import dialog.
 
115
 
 
116
                @param self: Reference to the ImportDialog instance.
 
117
                @type self: An ImportDialog object.
 
118
                """
 
119
                self.__dialog.run()
 
120
                return
 
121
 
 
122
        def __hide(self):
 
123
                """
 
124
                Hide the dialog.
 
125
 
 
126
                @param self: Reference to the ImportDialog instance.
 
127
                @type self: An ImportDialog object.
 
128
                """
 
129
                self.__editor.response()
 
130
                self.__dialog.hide()
 
131
                self.__editor.response()
 
132
                return
 
133
 
 
134
        def __import_template(self):
 
135
                """
 
136
                Import templates from an XML file.
 
137
 
 
138
                @param self: Reference to the ImportDialog instance.
 
139
                @type self: An ImportDialog object.
 
140
                """
 
141
                try:
 
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")
 
165
                except NoDataError:
 
166
                        from i18n import msg0015
 
167
                        self.__editor.error_dialog.show_message(msg0015, parent_window=self.__dialog)
 
168
                        self.__manager.emit("import-error")
 
169
                return
 
170
 
 
171
        def __destroy_cb(self, manager):
 
172
                """
 
173
                Handles callback when the "destroy" signal is emitted.
 
174
 
 
175
                @param self: Reference to the ImportDialog instance.
 
176
                @type self: An ImportDialog object.
 
177
 
 
178
                @param manager: Reference to the TemplateManager instance.
 
179
                @type manager: A TemplateManager object.
 
180
                """
 
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)
 
191
                self = None
 
192
                del self
 
193
                return
 
194
 
 
195
        def __delete_event_cb(self, *args):
 
196
                """
 
197
                Handles callback when the "delete-event" signal is emitted.
 
198
 
 
199
                @param self: Reference to the ImportDialog instance.
 
200
                @type self: An ImportDialog object.
 
201
                """
 
202
                self.__hide()
 
203
                return True
 
204
 
 
205
        def __response_cb(self, *args):
 
206
                """
 
207
                Handles callback when the "response" signal is emitted.
 
208
 
 
209
                @param self: Reference to the ImportDialog instance.
 
210
                @type self: An ImportDialog object.
 
211
 
 
212
                @param dialog: Reference to the ImportDialog instance.
 
213
                @type dialog: An ImportDialog object.
 
214
 
 
215
                @param response_id: A response identification.
 
216
                @type response_id: A Integer object.
 
217
 
 
218
                @return: True to propagate signals to parent widgets.
 
219
                @type: A Boolean Object.
 
220
                """
 
221
                self.__hide()
 
222
                return True
 
223
 
 
224
        def __selection_changed_cb(self, filechooser):
 
225
                """
 
226
                Handles callback when the "selection-changed" signal is emitted.
 
227
 
 
228
                @param self: Reference to the ImportDialog instance.
 
229
                @type self: A ImportDialog object.
 
230
 
 
231
                @return: True to propagate signals to parent widgets.
 
232
                @type: A Boolean Object.
 
233
                """
 
234
                filename = filechooser.get_filename()
 
235
                if filename is None: return
 
236
                from os import path
 
237
                if path.isdir(filename):
 
238
                        self.__button.set_property("sensitive", False)
 
239
                else:
 
240
                        self.__button.set_property("sensitive", True)
 
241
                return True
 
242
 
 
243
        def __file_activated_cb(self, *args):
 
244
                """
 
245
                Handles callback when the "file-activated" signal is emitted.
 
246
 
 
247
                @param self: Reference to the ImportDialog instance.
 
248
                @type self: A ImportDialog object.
 
249
 
 
250
                @return: True to propagate signals to parent widgets.
 
251
                @type: A Boolean Object.
 
252
                """
 
253
                self.__button.activate()
 
254
                return True
 
255
 
 
256
        def __clicked_cb(self, *args):
 
257
                """
 
258
                Handles callback when the "clicked" signal is emitted.
 
259
 
 
260
                @param self: Reference to the ImportDialog instance.
 
261
                @type self: A ImportDialog object.
 
262
                """
 
263
                self.__import_template()
 
264
                return True