| 1 | #!/usr/bin/env python |
|---|
| 2 | #coding=utf-8 |
|---|
| 3 | ############################################################################# |
|---|
| 4 | # manejador-de-proyecto.py |
|---|
| 5 | # Copyright (C) Redolfi Javier, Henze Agustin 2009 <> |
|---|
| 6 | # |
|---|
| 7 | # manejador-de-proyecto.py is free software: you can redistribute it and/or modify it |
|---|
| 8 | # under the terms of the GNU General Public License as published by the |
|---|
| 9 | # Free Software Foundation, either version 3 of the License, or |
|---|
| 10 | # (at your option) any later version. |
|---|
| 11 | # |
|---|
| 12 | # manejador-de-proyecto.py is distributed in the hope that it will be useful, but |
|---|
| 13 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 15 | # See the GNU General Public License for more details. |
|---|
| 16 | # |
|---|
| 17 | # You should have received a copy of the GNU General Public License along |
|---|
| 18 | # with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 19 | ############################################################################# |
|---|
| 20 | |
|---|
| 21 | import gedit, gtk, gtk.glade |
|---|
| 22 | import os |
|---|
| 23 | import time |
|---|
| 24 | from xml.dom import minidom |
|---|
| 25 | import subprocess |
|---|
| 26 | |
|---|
| 27 | ui_str="""<ui> |
|---|
| 28 | <menubar name="MenuBar"> |
|---|
| 29 | <menu name="ProjectMenu" action="ProjectMenuAction"> |
|---|
| 30 | <placeholder name="ProjectOps_1"> |
|---|
| 31 | <menuitem name="NewProject" action="NewProjectAction"/> |
|---|
| 32 | </placeholder> |
|---|
| 33 | <placeholder name="ProjectOps_2"> |
|---|
| 34 | <menuitem name="OpenProject" action="OpenProjectAction"/> |
|---|
| 35 | <menuitem name="OpenLastProjectMenu" action="OpenLastProjectMenuAction"/> |
|---|
| 36 | <menuitem name="CloseProject" action="CloseProjectAction"/> |
|---|
| 37 | <separator /> |
|---|
| 38 | <menuitem name="SimulateProject" action="SimulateProjectAction"/> |
|---|
| 39 | <separator /> |
|---|
| 40 | <menuitem name="OpenFileAsProject" action="OpenFileAsProjectAction"/> |
|---|
| 41 | <separator /> |
|---|
| 42 | <menuitem name="ViewProjectFiles" action="ViewProjectFilesAction"/> |
|---|
| 43 | <menuitem name="ReopenAllFiles" action="ReopenAllFilesAction"/> |
|---|
| 44 | <separator /> |
|---|
| 45 | </placeholder> |
|---|
| 46 | <placeholder name="ProjectOps_3"> |
|---|
| 47 | <menuitem name="AddProjectFile" action="AddProjectFileAction"/> |
|---|
| 48 | <menuitem name="AddCurrentFile" action="AddCurrentFileAction"/> |
|---|
| 49 | <menuitem name="DelProjectFile" action="DelProjectFileAction"/> |
|---|
| 50 | <separator /> |
|---|
| 51 | </placeholder> |
|---|
| 52 | <placeholder name="ProjectOps_4"> |
|---|
| 53 | <menuitem name="OpenCurrentFileFolder" action="OpenCurrentFileFolderAction"/> |
|---|
| 54 | <menuitem name="OpenProjectFolder" action="OpenProjectFolderAction"/> |
|---|
| 55 | <separator /> |
|---|
| 56 | </placeholder> |
|---|
| 57 | <placeholder name="ProjectOps_5"> |
|---|
| 58 | </placeholder> |
|---|
| 59 | </menu> |
|---|
| 60 | </menubar> |
|---|
| 61 | <toolbar name="ToolBar"> |
|---|
| 62 | <separator /> |
|---|
| 63 | <toolitem name="OpenLastProjectButton" action="OpenLastProjectButtonAction"/> |
|---|
| 64 | </toolbar> |
|---|
| 65 | </ui> |
|---|
| 66 | """ |
|---|
| 67 | class ProjectData: |
|---|
| 68 | |
|---|
| 69 | def __init__( self ): |
|---|
| 70 | self.filename = "" |
|---|
| 71 | self._files = list() |
|---|
| 72 | self.active = False |
|---|
| 73 | |
|---|
| 74 | def add_file( self, filename ): |
|---|
| 75 | if filename in self._files: |
|---|
| 76 | return False |
|---|
| 77 | self._files.append( filename ) |
|---|
| 78 | print "Agregando Archivo al Proyecto ... " + filename |
|---|
| 79 | return True |
|---|
| 80 | |
|---|
| 81 | def del_file( self, filename ): |
|---|
| 82 | if filename in self._files: |
|---|
| 83 | self._files.remove( filename ) |
|---|
| 84 | return True |
|---|
| 85 | return False |
|---|
| 86 | |
|---|
| 87 | def get_files( self ): |
|---|
| 88 | return self._files |
|---|
| 89 | |
|---|
| 90 | def clear( self ): |
|---|
| 91 | self.name = "" |
|---|
| 92 | self.filename = "" |
|---|
| 93 | self._files = list() |
|---|
| 94 | self.active = False |
|---|
| 95 | |
|---|
| 96 | class ProjectPluginInstance: |
|---|
| 97 | CHOOSER_OPEN = 10 |
|---|
| 98 | CHOOSER_SAVE = 11 |
|---|
| 99 | CHOOSER_CANCEL = 12 |
|---|
| 100 | CHOOSER_NEW = 13 |
|---|
| 101 | CHOOSER_OK = 14 |
|---|
| 102 | OPEN_PROJECT = 20 |
|---|
| 103 | SAVE_PROJECT = 21 |
|---|
| 104 | NEW_PROJECT = 22 |
|---|
| 105 | OPEN_FILES = 23 |
|---|
| 106 | SAVE_FILE = 24 |
|---|
| 107 | SAVE_AND_CLOSE_FILE = 25 |
|---|
| 108 | WITH_LOG = True |
|---|
| 109 | MAX_HISTORY_FILE_NO = 15 |
|---|
| 110 | STATE_PROJECT_NO_FILES_NO = 0 |
|---|
| 111 | STATE_PROJECT_YES_FILES_NO = 1 |
|---|
| 112 | STATE_PROJECT_NO_FILES_YES = 2 |
|---|
| 113 | STATE_PROJECT_YES_FILES_YES = 3 |
|---|
| 114 | |
|---|
| 115 | CAN_ADD_FILE = 1 |
|---|
| 116 | CAN_REM_FILE = 2 |
|---|
| 117 | |
|---|
| 118 | OS_WINDOW_MANAGER = "nautilus" |
|---|
| 119 | |
|---|
| 120 | def __init__( self, plugin, window ): |
|---|
| 121 | self._window = window |
|---|
| 122 | self._plugin = plugin |
|---|
| 123 | self._encoding = gedit.encoding_get_current() |
|---|
| 124 | self._project = ProjectData() |
|---|
| 125 | self._history_file = os.path.expanduser( '~' ) + "/.gnome2/gedit/plugins/gedit-project-manager-history" |
|---|
| 126 | self._history = list() |
|---|
| 127 | self._action_group = gtk.ActionGroup( "ProjectPluginActions" ) |
|---|
| 128 | self._history_action_group = gtk.ActionGroup( "ProjectPluginHistoryActions" ) |
|---|
| 129 | self._state = self.STATE_PROJECT_NO_FILES_NO |
|---|
| 130 | self._add_remove_state = None |
|---|
| 131 | self._message = list() |
|---|
| 132 | |
|---|
| 133 | self._init_history() |
|---|
| 134 | self._insert_menu() |
|---|
| 135 | self._create_choosers() |
|---|
| 136 | |
|---|
| 137 | def deactivate( self ): |
|---|
| 138 | self._remove_menu() |
|---|
| 139 | self._save_history() |
|---|
| 140 | self._action_group = None |
|---|
| 141 | self._window = None |
|---|
| 142 | self._plugin = None |
|---|
| 143 | self._project = None |
|---|
| 144 | |
|---|
| 145 | self._open_file_chooser = None |
|---|
| 146 | self._save_file_chooser = None |
|---|
| 147 | |
|---|
| 148 | def update_ui( self ): |
|---|
| 149 | manager = self._window.get_ui_manager() |
|---|
| 150 | # REMOVE OLD HISTORY |
|---|
| 151 | manager.remove_ui( self._ui_id ) |
|---|
| 152 | manager.add_ui_from_string( ui_str ) |
|---|
| 153 | manager.remove_action_group(self._history_action_group) |
|---|
| 154 | self._history_action_group = gtk.ActionGroup( "ProjectPluginHistoryActions" ) |
|---|
| 155 | manager.ensure_update() |
|---|
| 156 | # INSERT NEW HISTORY |
|---|
| 157 | if len(self._history) != 0: |
|---|
| 158 | for item in self._history: |
|---|
| 159 | if item.strip() != "": |
|---|
| 160 | self._insert_history_menu_item( manager, item, self._history.index( item ) ) |
|---|
| 161 | manager.insert_action_group( self._history_action_group, 1 ) |
|---|
| 162 | manager.ensure_update() |
|---|
| 163 | # SET BUTTONS SENSITIVITY |
|---|
| 164 | self._add_remove_state = None |
|---|
| 165 | if (self._window.get_active_document() and self._window.get_active_document().get_uri() != None): |
|---|
| 166 | if (self._project.active): |
|---|
| 167 | self._state = self.STATE_PROJECT_YES_FILES_YES |
|---|
| 168 | if (self._window.get_active_document().get_data( "BelongsToProject" ) == self._project.filename): #GYLL... |
|---|
| 169 | self._add_remove_state = self.CAN_REM_FILE |
|---|
| 170 | else: |
|---|
| 171 | self._add_remove_state = self.CAN_ADD_FILE |
|---|
| 172 | else: |
|---|
| 173 | self._state = self.STATE_PROJECT_NO_FILES_YES |
|---|
| 174 | else: |
|---|
| 175 | if (self._project.active): |
|---|
| 176 | self._state = self.STATE_PROJECT_YES_FILES_NO |
|---|
| 177 | else: |
|---|
| 178 | self._state = self.STATE_PROJECT_NO_FILES_NO |
|---|
| 179 | self._set_menu_state( manager ) |
|---|
| 180 | #print self._window.gedit_notebook |
|---|
| 181 | #print self._state |
|---|
| 182 | return |
|---|
| 183 | |
|---|
| 184 | def _set_menu_state( self, manager ): |
|---|
| 185 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_1/NewProject").set_sensitive( not self._project.active ) |
|---|
| 186 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/OpenProject").set_sensitive( not self._project.active ) |
|---|
| 187 | |
|---|
| 188 | #GYLL |
|---|
| 189 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/OpenFileAsProject").set_sensitive( False ) |
|---|
| 190 | if (not self._project.active): |
|---|
| 191 | if self._window.get_active_document() and self._window.get_active_document().get_uri(): |
|---|
| 192 | if self._has_gedit_project_extension(self._window.get_active_document().get_uri().strip()): |
|---|
| 193 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/OpenFileAsProject").set_sensitive( True ) |
|---|
| 194 | |
|---|
| 195 | #GYLL |
|---|
| 196 | manager.get_action("/ToolBar/OpenLastProjectButton").set_sensitive( len(self._history) != 0 and not self._project.active) |
|---|
| 197 | #GYLL |
|---|
| 198 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/OpenLastProjectMenu").set_sensitive( len(self._history) != 0 and not self._project.active) |
|---|
| 199 | |
|---|
| 200 | if (self._state == self.STATE_PROJECT_NO_FILES_NO): |
|---|
| 201 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/CloseProject").set_sensitive(False) |
|---|
| 202 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/ViewProjectFiles").set_sensitive(False) |
|---|
| 203 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/AddProjectFile").set_sensitive(False) |
|---|
| 204 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/AddCurrentFile").set_sensitive(False) |
|---|
| 205 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/ReopenAllFiles").set_sensitive(False) |
|---|
| 206 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/DelProjectFile").set_sensitive(False) |
|---|
| 207 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_4/OpenProjectFolder").set_sensitive(False) |
|---|
| 208 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_4/OpenCurrentFileFolder").set_sensitive(False) |
|---|
| 209 | |
|---|
| 210 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/SimulateProject").set_sensitive(False) |
|---|
| 211 | elif (self._state == self.STATE_PROJECT_YES_FILES_NO): |
|---|
| 212 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/CloseProject").set_sensitive(True) |
|---|
| 213 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/ViewProjectFiles").set_sensitive(True) |
|---|
| 214 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/AddProjectFile").set_sensitive(True) |
|---|
| 215 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/AddCurrentFile").set_sensitive(False) |
|---|
| 216 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/ReopenAllFiles").set_sensitive(True) |
|---|
| 217 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/DelProjectFile").set_sensitive(False) |
|---|
| 218 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_4/OpenProjectFolder").set_sensitive(True) |
|---|
| 219 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_4/OpenCurrentFileFolder").set_sensitive(False) |
|---|
| 220 | |
|---|
| 221 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/SimulateProject").set_sensitive(False) |
|---|
| 222 | elif (self._state == self.STATE_PROJECT_NO_FILES_YES): |
|---|
| 223 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/CloseProject").set_sensitive(False) |
|---|
| 224 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/ViewProjectFiles").set_sensitive(False) |
|---|
| 225 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/AddProjectFile").set_sensitive(False) |
|---|
| 226 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/AddCurrentFile").set_sensitive(False) |
|---|
| 227 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/ReopenAllFiles").set_sensitive(False) |
|---|
| 228 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/DelProjectFile").set_sensitive(False) |
|---|
| 229 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_4/OpenProjectFolder").set_sensitive(False) |
|---|
| 230 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_4/OpenCurrentFileFolder").set_sensitive(True) |
|---|
| 231 | |
|---|
| 232 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/SimulateProject").set_sensitive(False) |
|---|
| 233 | elif (self._state == self.STATE_PROJECT_YES_FILES_YES): |
|---|
| 234 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/CloseProject").set_sensitive(True) |
|---|
| 235 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/ViewProjectFiles").set_sensitive(True) |
|---|
| 236 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/AddProjectFile").set_sensitive(True) |
|---|
| 237 | if (self._add_remove_state == self.CAN_ADD_FILE): |
|---|
| 238 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/AddCurrentFile").set_sensitive(True) |
|---|
| 239 | else: |
|---|
| 240 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/AddCurrentFile").set_sensitive(False) |
|---|
| 241 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/ReopenAllFiles").set_sensitive(True) |
|---|
| 242 | if (self._add_remove_state == self.CAN_REM_FILE): |
|---|
| 243 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/DelProjectFile").set_sensitive(True) |
|---|
| 244 | else: |
|---|
| 245 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_3/DelProjectFile").set_sensitive(False) |
|---|
| 246 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_4/OpenProjectFolder").set_sensitive(True) |
|---|
| 247 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_4/OpenCurrentFileFolder").set_sensitive(True) |
|---|
| 248 | |
|---|
| 249 | doc = self._window.get_active_document() |
|---|
| 250 | fileName = os.path.basename( doc.get_uri_for_display() ) |
|---|
| 251 | if fileName[fileName.find('.'):]=='.tbw': |
|---|
| 252 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/SimulateProject").set_sensitive(True) |
|---|
| 253 | else: |
|---|
| 254 | manager.get_action("/MenuBar/ProjectMenu/ProjectOps_2/SimulateProject").set_sensitive(False) |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | def _init_history( self ): |
|---|
| 258 | if os.path.exists( self._history_file ) == True: |
|---|
| 259 | hist_fp = open( self._history_file, "r" ) |
|---|
| 260 | try: |
|---|
| 261 | for line in hist_fp: |
|---|
| 262 | self._history.insert( len( self._history ) , line.strip() ) |
|---|
| 263 | finally: |
|---|
| 264 | hist_fp.close() |
|---|
| 265 | else: |
|---|
| 266 | hist_fp = open( self._history_file, "w" ) |
|---|
| 267 | hist_fp.close() |
|---|
| 268 | |
|---|
| 269 | def _add_to_history( self, filename ): |
|---|
| 270 | if filename.strip() != "": |
|---|
| 271 | for item in self._history: |
|---|
| 272 | if item == filename: |
|---|
| 273 | self._history.remove( item ) |
|---|
| 274 | if os.path.exists(filename): |
|---|
| 275 | if len( self._history ) == self.MAX_HISTORY_FILE_NO: |
|---|
| 276 | self._history.pop( len( self._history )-1 ) |
|---|
| 277 | self._history.insert( 0, filename ) |
|---|
| 278 | self._save_history() |
|---|
| 279 | self.update_ui() # GYLL |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | def _save_history( self ): |
|---|
| 283 | print "Saving the history ... " |
|---|
| 284 | hist_fp = open( self._history_file, "w" ) |
|---|
| 285 | for line in self._history: |
|---|
| 286 | hist_fp.write( line + "\n" ) |
|---|
| 287 | hist_fp.close() |
|---|
| 288 | |
|---|
| 289 | def _insert_history_menu_item ( self, manager, filename, position ): |
|---|
| 290 | file_name, file_ext = os.path.splitext( os.path.basename( filename ) ) |
|---|
| 291 | action_label = str( position + 1 ) + ". " + file_name |
|---|
| 292 | manager.add_ui( merge_id = self._ui_id, |
|---|
| 293 | path = "/MenuBar/ProjectMenu/ProjectOps_5", |
|---|
| 294 | name = filename, |
|---|
| 295 | action = filename, |
|---|
| 296 | type = gtk.UI_MANAGER_AUTO, |
|---|
| 297 | top = False) |
|---|
| 298 | history_action = gtk.Action(name = filename, |
|---|
| 299 | label = action_label, |
|---|
| 300 | tooltip=filename, |
|---|
| 301 | stock_id=None ) |
|---|
| 302 | history_action.connect("activate", lambda a: self.on_history_action(filename) ) |
|---|
| 303 | history_action.set_sensitive( not self._project.active ) |
|---|
| 304 | history_action.set_visible(True) |
|---|
| 305 | self._history_action_group.add_action( history_action ) |
|---|
| 306 | |
|---|
| 307 | def _insert_menu( self ): |
|---|
| 308 | "create and add the Project menu to the menubar" |
|---|
| 309 | manager = self._window.get_ui_manager() |
|---|
| 310 | |
|---|
| 311 | project_menu_action = gtk.Action( name="ProjectMenuAction", |
|---|
| 312 | label="Proyecto", |
|---|
| 313 | tooltip="Menú Proyecto", |
|---|
| 314 | stock_id=None ) |
|---|
| 315 | project_menu_action.connect( "activate", lambda a: self.update_ui() ) |
|---|
| 316 | self._action_group.add_action( project_menu_action ) |
|---|
| 317 | |
|---|
| 318 | #GYLL : reopen last project button |
|---|
| 319 | open_last_project_button_action = gtk.Action( name="OpenLastProjectButtonAction", |
|---|
| 320 | label="Ãltimo Proyecto", |
|---|
| 321 | tooltip="Reabriendo el Ãltimo Proyecto", |
|---|
| 322 | stock_id=gtk.STOCK_GO_BACK ) |
|---|
| 323 | open_last_project_button_action.connect( "activate", lambda a: self.on_open_last_project_action() ) |
|---|
| 324 | self._action_group.add_action( open_last_project_button_action ) |
|---|
| 325 | |
|---|
| 326 | #GYLL : reopen last project menu |
|---|
| 327 | open_last_project_menu_action = gtk.Action( name="OpenLastProjectMenuAction", |
|---|
| 328 | label="Abriendo el Ãltimo Proyecto", |
|---|
| 329 | tooltip="Reabriendo el Ãltimo Proyecto", |
|---|
| 330 | stock_id=gtk.STOCK_GO_BACK ) |
|---|
| 331 | open_last_project_menu_action.connect( "activate", lambda a: self.on_open_last_project_action() ) |
|---|
| 332 | self._action_group.add_action( open_last_project_menu_action ) |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | ## First Placeholder ## |
|---|
| 337 | new_project_action = gtk.Action( name="NewProjectAction", |
|---|
| 338 | label="Proyecto Nuevo...\t", |
|---|
| 339 | tooltip="Crear un Proyecto Nuevo", |
|---|
| 340 | stock_id=gtk.STOCK_NEW ) |
|---|
| 341 | new_project_action.connect( "activate", lambda a: self.on_new_project_action() ) |
|---|
| 342 | self._action_group.add_action( new_project_action ) |
|---|
| 343 | |
|---|
| 344 | ## Second Placeholder ## |
|---|
| 345 | open_project_action = gtk.Action( name="OpenProjectAction", |
|---|
| 346 | label="Abrir Proyecto...\t", |
|---|
| 347 | tooltip="Abriendo un Archivo de Proyecto", |
|---|
| 348 | stock_id=gtk.STOCK_OPEN ) |
|---|
| 349 | open_file_as_project_action = gtk.Action( name="OpenFileAsProjectAction", #GYLL |
|---|
| 350 | label="Abriendo el Archivo Actual como Proyecto\t", |
|---|
| 351 | tooltip="Abriendo el Proyecto asociado con el Archivo '.gedit-project' Actual", |
|---|
| 352 | stock_id=None ) |
|---|
| 353 | close_project_action = gtk.Action( name="CloseProjectAction", |
|---|
| 354 | label="Cerrar Proyecto\t", |
|---|
| 355 | tooltip="Cerrar el Proyecto Actual", |
|---|
| 356 | stock_id=gtk.STOCK_QUIT ) |
|---|
| 357 | view_project_files_action = gtk.Action( name="ViewProjectFilesAction", |
|---|
| 358 | label="Ver la lista de Archivos del Proyecto\t", |
|---|
| 359 | tooltip="Muestra una lista con los Archivos del Proyecto.", |
|---|
| 360 | stock_id=gtk.STOCK_INFO) |
|---|
| 361 | open_all_files_action = gtk.Action( name="ReopenAllFilesAction", |
|---|
| 362 | label="Abrir todos los Archivos del Proyecto\t", |
|---|
| 363 | tooltip="Abre todos los Archivos contenidos en el Proyecto Actual.", |
|---|
| 364 | stock_id=gtk.STOCK_REFRESH ) |
|---|
| 365 | |
|---|
| 366 | simulate_project_action = gtk.Action( name="SimulateProjectAction", |
|---|
| 367 | label="Simular Proyecto\t", |
|---|
| 368 | tooltip="Simular el Proyecto Actual", |
|---|
| 369 | stock_id=gtk.STOCK_MEDIA_PLAY ) |
|---|
| 370 | |
|---|
| 371 | open_project_action.connect( "activate", lambda a: self.on_open_project_action() ) |
|---|
| 372 | open_file_as_project_action.connect( "activate", lambda a: self.on_open_file_as_project_action() ) |
|---|
| 373 | close_project_action.connect( "activate", lambda a: self.on_close_project_action() ) |
|---|
| 374 | view_project_files_action.connect( "activate", lambda a: self.on_view_project_files_action() ) |
|---|
| 375 | open_all_files_action.connect( "activate", lambda a: self.on_open_all_files_action() ) |
|---|
| 376 | |
|---|
| 377 | simulate_project_action.connect( "activate", lambda a: self.on_simulate_project_action() ) |
|---|
| 378 | |
|---|
| 379 | self._action_group.add_action( open_project_action ) |
|---|
| 380 | self._action_group.add_action( open_file_as_project_action ) |
|---|
| 381 | self._action_group.add_action( close_project_action ) |
|---|
| 382 | self._action_group.add_action_with_accel( view_project_files_action, "<Ctrl>3" ) |
|---|
| 383 | self._action_group.add_action_with_accel( open_all_files_action, "<Ctrl>2" ) |
|---|
| 384 | self._action_group.add_action_with_accel( simulate_project_action, "<Ctrl>7" ) |
|---|
| 385 | |
|---|
| 386 | # Third Placeholder ## |
|---|
| 387 | add_file_action = gtk.Action( name="AddProjectFileAction", |
|---|
| 388 | label="Agregar Archivos ...\t", |
|---|
| 389 | tooltip="Agrega un Archivo existente al Proyecto Abierto.", |
|---|
| 390 | stock_id=gtk.STOCK_DND_MULTIPLE ) |
|---|
| 391 | add_current_file_action = gtk.Action( name="AddCurrentFileAction", |
|---|
| 392 | label="Agregar Archivo Actual\t", |
|---|
| 393 | tooltip="Agrega el Archivo Actual al Proyecto Abierto.", |
|---|
| 394 | stock_id=gtk.STOCK_DND ) |
|---|
| 395 | del_file_action = gtk.Action( name="DelProjectFileAction", |
|---|
| 396 | label="Borrar el Archivo Actual\t", |
|---|
| 397 | tooltip="Borra el Archivo Actual del Proyecto.", |
|---|
| 398 | stock_id=gtk.STOCK_DELETE ) |
|---|
| 399 | add_file_action.connect( "activate", lambda a: self.on_add_file_action() ) |
|---|
| 400 | add_current_file_action.connect( "activate", lambda a: self.on_add_current_file_action() ) |
|---|
| 401 | del_file_action.connect( "activate", lambda a: self.on_del_file_action() ) |
|---|
| 402 | self._action_group.add_action( add_file_action ) |
|---|
| 403 | self._action_group.add_action_with_accel( add_current_file_action, "<Ctrl>1" ) |
|---|
| 404 | self._action_group.add_action_with_accel( del_file_action, "<Ctrl>0" ) |
|---|
| 405 | |
|---|
| 406 | ## Fourth Placeholder ## |
|---|
| 407 | open_project_folder_action = gtk.Action( name="OpenProjectFolderAction", |
|---|
| 408 | label="Abrir la Carpeta del Proyecto...\t", |
|---|
| 409 | tooltip="Abre la Carpeta que contiene al Proyecto.", |
|---|
| 410 | stock_id=None ) |
|---|
| 411 | open_file_folder_action = gtk.Action( name="OpenCurrentFileFolderAction", |
|---|
| 412 | label="Abrir la Carpeta del Archivo Actual...\t", |
|---|
| 413 | tooltip="Abre la Carpeta del Proyecto Actual.", |
|---|
| 414 | stock_id=None ) |
|---|
| 415 | |
|---|
| 416 | open_project_folder_action.connect( "activate", lambda a: self.on_open_project_folder_action() ) |
|---|
| 417 | open_file_folder_action.connect( "activate", lambda a: self.on_open_files_folder_action() ) |
|---|
| 418 | self._action_group.add_action( open_project_folder_action ) |
|---|
| 419 | self._action_group.add_action( open_file_folder_action ) |
|---|
| 420 | |
|---|
| 421 | manager.insert_action_group( self._action_group, 0 ) |
|---|
| 422 | manager.insert_action_group( self._history_action_group, 1 ) |
|---|
| 423 | |
|---|
| 424 | self._ui_id = manager.new_merge_id() |
|---|
| 425 | manager.add_ui_from_string( ui_str ) |
|---|
| 426 | manager.ensure_update() |
|---|
| 427 | |
|---|
| 428 | def _remove_menu( self ): |
|---|
| 429 | "removes the project menu items from the menu" |
|---|
| 430 | manager = self._window.get_ui_manager() |
|---|
| 431 | manager.remove_ui( self._ui_id ) |
|---|
| 432 | manager.remove_action_group( self._action_group ) |
|---|
| 433 | manager.ensure_update() |
|---|
| 434 | |
|---|
| 435 | def _create_choosers( self ): |
|---|
| 436 | "Creates open/save file choosers for opening and saving projects" |
|---|
| 437 | self.project_file_filter = gtk.FileFilter() |
|---|
| 438 | self.project_file_filter.add_pattern("*.gedit-project") |
|---|
| 439 | self.all_files_filter = gtk.FileFilter() |
|---|
| 440 | self.all_files_filter.add_pattern("*") |
|---|
| 441 | |
|---|
| 442 | self._open_file_chooser = gtk.FileChooserDialog( "Abrir Archivo de Proyecto", |
|---|
| 443 | self._window, |
|---|
| 444 | gtk.FILE_CHOOSER_ACTION_OPEN ) |
|---|
| 445 | self._open_file_chooser.set_select_multiple( False ) |
|---|
| 446 | self._open_file_chooser.add_buttons( gtk.STOCK_OPEN, self.CHOOSER_OPEN, |
|---|
| 447 | gtk.STOCK_CANCEL, self.CHOOSER_CANCEL ) |
|---|
| 448 | |
|---|
| 449 | self._save_file_chooser = gtk.FileChooserDialog( "Guardar Archivo de Proyecto", |
|---|
| 450 | self._window, |
|---|
| 451 | gtk.FILE_CHOOSER_ACTION_SAVE ) |
|---|
| 452 | self._save_file_chooser.set_select_multiple( False ) |
|---|
| 453 | self._save_file_chooser.add_buttons( gtk.STOCK_SAVE, self.CHOOSER_SAVE, |
|---|
| 454 | gtk.STOCK_CANCEL, self.CHOOSER_CANCEL ) |
|---|
| 455 | |
|---|
| 456 | self._open_fa_id = -1 |
|---|
| 457 | self._open_resp_id = -1 |
|---|
| 458 | self._save_fa_id = -1 |
|---|
| 459 | self._save_resp_id = -1 |
|---|
| 460 | |
|---|
| 461 | #### MENU ITEM ACTIONS #### |
|---|
| 462 | |
|---|
| 463 | ## First Placeholder ## |
|---|
| 464 | def on_new_project_action( self ): |
|---|
| 465 | "Displays the new project dialog box" |
|---|
| 466 | self._show_chooser( self.NEW_PROJECT ) |
|---|
| 467 | |
|---|
| 468 | ## Second Placeholder ## |
|---|
| 469 | def on_open_project_action( self ): |
|---|
| 470 | "Activated when the 'open project' menu item is selected" |
|---|
| 471 | self._show_chooser( self.OPEN_PROJECT ) |
|---|
| 472 | |
|---|
| 473 | def on_close_project_action( self ): |
|---|
| 474 | "Close the active project" |
|---|
| 475 | project_filename = os.path.basename(self._project.filename) #GYLL |
|---|
| 476 | self._save_project( self._project.filename ) |
|---|
| 477 | #GYLL self._show_alert( "Project closed: '" + self._project.filename + "'", self.WITH_LOG ) |
|---|
| 478 | self._close_project() |
|---|
| 479 | if self._message: self._show_alert( "Proyecto Cerrado: '" + project_filename + "'", self.WITH_LOG ) #GYLL |
|---|
| 480 | |
|---|
| 481 | def on_view_project_files_action( self ): |
|---|
| 482 | file_list = list() |
|---|
| 483 | xml = minidom.parse( self._project.filename ) |
|---|
| 484 | for subfile in xml.getElementsByTagName( 'file' ): |
|---|
| 485 | file_list.append( subfile.childNodes[0].data.strip() ) |
|---|
| 486 | #GYLL message = "Current project: '/" + os.path.basename(self._project.filename) + "'\nFiles in the project:" |
|---|
| 487 | message = "Proyecto Actual:\n" + self._project.filename + "\n\nArchivos en el Proyecto:" #GYLL |
|---|
| 488 | while file_list: |
|---|
| 489 | message = message + "\n - /" + file_list.pop().strip("file:/").replace("%20", " ") |
|---|
| 490 | self._show_alert( message , False, gtk.DIALOG_DESTROY_WITH_PARENT ) |
|---|
| 491 | |
|---|
| 492 | def on_open_project_folder_action( self ): |
|---|
| 493 | "Open the current project folder." |
|---|
| 494 | if os.path.dirname(self._project.filename): |
|---|
| 495 | #GYLL os.system(self.OS_WINDOW_MANAGER + " " + os.path.dirname(self._project.filename)) |
|---|
| 496 | os.system(self.OS_WINDOW_MANAGER + " file://" + os.path.dirname(self._project.filename).replace(" ","%20")) #GYLL |
|---|
| 497 | else: |
|---|
| 498 | self._show_alert( "No se puede Abrir la Carpeta del Proyecto.\nRazón: Ruta no Encontrada." ) |
|---|
| 499 | |
|---|
| 500 | def on_open_files_folder_action( self ): |
|---|
| 501 | "Open the current file folder." |
|---|
| 502 | if (self._window.get_active_document()): |
|---|
| 503 | if(self._window.get_active_document().get_uri()): |
|---|
| 504 | current_file = self._window.get_active_document().get_uri().strip() |
|---|
| 505 | if os.path.dirname(current_file): |
|---|
| 506 | os.system( self.OS_WINDOW_MANAGER + " " + os.path.dirname(current_file)) |
|---|
| 507 | else: |
|---|
| 508 | self._show_alert( "No se puede Abrir la Carpeta del Proyecto.\nRazón: Ruta no Encontrada." ) |
|---|
| 509 | else: |
|---|
| 510 | self._show_alert( "No hay ningún Archivo Abierto o este No tiene TÃtulo." ) |
|---|
| 511 | |
|---|
| 512 | ## Third Placeholder ## |
|---|
| 513 | def on_add_file_action( self ): |
|---|
| 514 | "Adds an existing file to the active project." |
|---|
| 515 | self._show_chooser( self.OPEN_FILES ) |
|---|
| 516 | |
|---|
| 517 | def on_add_current_file_action( self ): |
|---|
| 518 | "Adds the active file to the active project." |
|---|
| 519 | if self._window.get_active_document(): |
|---|
| 520 | doc = self._window.get_active_document() |
|---|
| 521 | print "Agregando Archivo al Proyecto ... " + doc.get_uri() |
|---|
| 522 | #GYLL if doc.get_uri() in self._project._files: |
|---|
| 523 | if doc.get_data( "BelongsToProject" ) == self._project.filename: |
|---|
| 524 | self._show_alert( "El Archivo Actual ya ESTA en el Proyecto: '/" + |
|---|
| 525 | doc.get_uri().lstrip("file:/").replace("%20"," ") + "'") |
|---|
| 526 | else: |
|---|
| 527 | doc.set_data( "BelongsToProject", self._project.filename ) #GYLL |
|---|
| 528 | self._show_alert( "El Archivo Actual ha sido AGREGADO al Proyecto: '/" + |
|---|
| 529 | doc.get_uri().lstrip("file:/").replace("%20"," ") + "'") |
|---|
| 530 | #GYLL if not doc.get_data( "BelongsToProject" ): doc.set_data( "BelongsToProject", self._project.filename ) |
|---|
| 531 | self._project.add_file( doc.get_uri() ) |
|---|
| 532 | self._save_project( self._project.filename ) |
|---|
| 533 | |
|---|
| 534 | def on_open_all_files_action( self ): |
|---|
| 535 | self._open_project( self._project.filename ) |
|---|
| 536 | self._save_project( self._project.filename ) |
|---|
| 537 | if self._message: self._show_alert( "Proyecto Reabierto: '" + os.path.basename( self._project.filename ) + "'", self.WITH_LOG ) |
|---|
| 538 | |
|---|
| 539 | def on_del_file_action( self ): |
|---|
| 540 | "Removes the active file from the active project." |
|---|
| 541 | if self._window.get_active_document(): |
|---|
| 542 | doc = self._window.get_active_document() |
|---|
| 543 | if doc.get_data( "BelongsToProject" ) == self._project.filename: |
|---|
| 544 | print "Borrando Archivo del Proyecto ... " + doc.get_uri() |
|---|
| 545 | self._project.del_file( doc.get_uri().strip() ) |
|---|
| 546 | #self._window.close_tab( gedit.gedit_tab_get_from_document( doc ) ) |
|---|
| 547 | self._save_project( self._project.filename ) |
|---|
| 548 | doc.set_data( "BelongsToProject", None ) #GYLL |
|---|
| 549 | self._show_alert( "El Archivo Actual ha sido BORRADO del Proyecto: '/" + |
|---|
| 550 | doc.get_uri().lstrip("file:/").replace("%20"," ") + "'") |
|---|
| 551 | else: |
|---|
| 552 | self._show_alert( "El Archivo Actual no Pertenece al Proyecto: '/" + |
|---|
| 553 | doc.get_uri().lstrip("file:/").replace("%20"," ") + "'") |
|---|
| 554 | |
|---|
| 555 | ## CHOOSER ACTIONS ## |
|---|
| 556 | def _show_chooser( self, chooser_type ): |
|---|
| 557 | "A unified interface to show both open/save file choosers" |
|---|
| 558 | if self._open_fa_id > -1: self._open_file_chooser.disconnect( self._open_fa_id ) |
|---|
| 559 | if self._open_resp_id > -1: self._open_file_chooser.disconnect( self._open_resp_id ) |
|---|
| 560 | if self._save_fa_id > -1: self._save_file_chooser.disconnect( self._save_fa_id ) |
|---|
| 561 | if self._save_resp_id > -1: self._save_file_chooser.disconnect( self._save_resp_id ) |
|---|
| 562 | |
|---|
| 563 | if chooser_type == self.OPEN_PROJECT: |
|---|
| 564 | self._open_file_chooser.set_title( "Abrir Archivo de Proyecto" ) |
|---|
| 565 | self._open_file_chooser.set_filter(self.project_file_filter) |
|---|
| 566 | self._open_file_chooser.set_select_multiple( False ) |
|---|
| 567 | self._open_fa_id = self._open_file_chooser.connect( "file-activated", |
|---|
| 568 | self.on_open_project, |
|---|
| 569 | self.CHOOSER_OPEN ) |
|---|
| 570 | self._open_resp_id = self._open_file_chooser.connect( "response", self.on_open_project ) |
|---|
| 571 | self._open_file_chooser.show() |
|---|
| 572 | |
|---|
| 573 | elif chooser_type == self.OPEN_FILES: |
|---|
| 574 | self._open_file_chooser.set_title( "Agregar Archivo al Proyecto" ) |
|---|
| 575 | self._open_file_chooser.set_filter(self.all_files_filter) |
|---|
| 576 | self._open_file_chooser.set_select_multiple( True ) |
|---|
| 577 | self._open_fa_id = self._open_file_chooser.connect( "file-activated", |
|---|
| 578 | self.on_open_files, |
|---|
| 579 | self.CHOOSER_OPEN ) |
|---|
| 580 | self._open_resp_id = self._open_file_chooser.connect( "response", self.on_open_files ) |
|---|
| 581 | self._open_file_chooser.show() |
|---|
| 582 | |
|---|
| 583 | elif chooser_type == self.NEW_PROJECT: |
|---|
| 584 | self._save_file_chooser.set_title( "Guardar Archivo de Proyecto Nuevo como ..." ) |
|---|
| 585 | self._save_file_chooser.set_filter(self.project_file_filter) |
|---|
| 586 | self._save_file_chooser.set_current_name("Pongale_Un_Nombre_A_Su_Proyecto" + str(time.clock())) |
|---|
| 587 | self._save_fa_id = self._save_file_chooser.connect( "file-activated", |
|---|
| 588 | self.on_save_project, |
|---|
| 589 | self.CHOOSER_SAVE ) |
|---|
| 590 | self._save_resp_id = self._save_file_chooser.connect( "response", |
|---|
| 591 | self.on_save_project ) |
|---|
| 592 | self._save_file_chooser.show() |
|---|
| 593 | |
|---|
| 594 | else: |
|---|
| 595 | return False |
|---|
| 596 | |
|---|
| 597 | return True |
|---|
| 598 | |
|---|
| 599 | |
|---|
| 600 | def _has_gedit_project_extension( self, filename ): #GYLL |
|---|
| 601 | RANDOM_MARKER = "/45n687q2qVcsAHsfDord8326bfaW8e7c" |
|---|
| 602 | filename = filename + RANDOM_MARKER |
|---|
| 603 | if filename.find( ".gedit-project" + RANDOM_MARKER ) != -1: |
|---|
| 604 | return True |
|---|
| 605 | else: |
|---|
| 606 | return False |
|---|
| 607 | |
|---|
| 608 | |
|---|
| 609 | def _show_alert( self, text=None, with_log=False, my_flags=gtk.DIALOG_MODAL ): |
|---|
| 610 | alert_box_text = text + "\n" |
|---|
| 611 | if with_log and self._message: |
|---|
| 612 | self._message.sort() |
|---|
| 613 | while self._message: |
|---|
| 614 | alert_box_text = alert_box_text + self._message.pop() + "\n" |
|---|
| 615 | alert_box = gtk.MessageDialog( parent = self._window, |
|---|
| 616 | flags = my_flags, |
|---|
| 617 | type = gtk.MESSAGE_INFO, |
|---|
| 618 | buttons = gtk.BUTTONS_OK, |
|---|
| 619 | message_format = alert_box_text ) |
|---|
| 620 | alert_box.connect( "response", self.on_alert_response ) |
|---|
| 621 | alert_box.show() |
|---|
| 622 | |
|---|
| 623 | def on_alert_response( self, widget, data=None ): widget.hide() |
|---|
| 624 | |
|---|
| 625 | def on_open_files( self, widget, data=None ): |
|---|
| 626 | "Activated when the user selects a file to open" |
|---|
| 627 | if data == self.CHOOSER_OPEN: |
|---|
| 628 | file_list = list() |
|---|
| 629 | for filename in self._open_file_chooser.get_uris(): |
|---|
| 630 | file_list.append( filename ) |
|---|
| 631 | self._open_files( file_list ) |
|---|
| 632 | self._save_project( self._project.filename ) |
|---|
| 633 | if self._message: self._show_alert( "Archivos Abiertos.\n", self.WITH_LOG ) |
|---|
| 634 | self._open_file_chooser.hide() |
|---|
| 635 | |
|---|
| 636 | def on_history_action( self, filename=None ): |
|---|
| 637 | "Opens a recently used project file" |
|---|
| 638 | if os.path.exists( filename ): |
|---|
| 639 | self._open_project( filename ) |
|---|
| 640 | self._save_project( self._project.filename ) |
|---|
| 641 | if self._message: self._show_alert( "Proyecto Abierto: '" + os.path.basename( filename ) + "'", self.WITH_LOG ) |
|---|
| 642 | else: |
|---|
| 643 | self._show_alert( "Proyecto No Abierto: '"+ os.path.basename( filename ) + "'\n\nArchivo de Proyecto no Encontrado: \n" + filename ) |
|---|
| 644 | self._add_to_history( filename ) |
|---|
| 645 | |
|---|
| 646 | #GYLL |
|---|
| 647 | def on_open_last_project_action( self ): |
|---|
| 648 | if self._project.active == True: |
|---|
| 649 | self.update_ui() |
|---|
| 650 | return |
|---|
| 651 | for filename in self._history: |
|---|
| 652 | if os.path.exists( filename ): |
|---|
| 653 | self._open_project( filename ) |
|---|
| 654 | self._save_project( self._project.filename ) |
|---|
| 655 | if self._message: self._show_alert( "Proyecto Abierto: '" + os.path.basename( self._project.filename ) + "'", self.WITH_LOG ) |
|---|
| 656 | else: |
|---|
| 657 | self._show_alert( "Proyecto No Abierto: '"+ os.path.basename( filename ) + "'\n\nArchivo de Proyecto No Encontrada: \n" + filename ) |
|---|
| 658 | self._add_to_history( filename ) |
|---|
| 659 | return |
|---|
| 660 | self._show_alert("Lista de Historia de Proyecto VacÃa") |
|---|
| 661 | |
|---|
| 662 | #GYLL |
|---|
| 663 | def on_open_file_as_project_action( self): |
|---|
| 664 | doc = self._window.get_active_document() |
|---|
| 665 | if (doc and doc.get_uri() != None): |
|---|
| 666 | filename = "/" + doc.get_uri().strip().lstrip("file:/").replace("%20", " ") |
|---|
| 667 | print "Opening file as project: " + filename |
|---|
| 668 | if self._project.active: |
|---|
| 669 | self._show_alert("No se Puede Abrir el Proyecto: " + filename + "\nRazón: Un Proyecto ya está Abierto") |
|---|
| 670 | return |
|---|
| 671 | if not self._has_gedit_project_extension( filename ): |
|---|
| 672 | self._show_alert("No se Puede Abrir el Proyecto.\nRazón: El Archivo Actual no es un Archivo '.gedit-project'") |
|---|
| 673 | return |
|---|
| 674 | if os.path.exists( filename ): |
|---|
| 675 | if doc in self._window.get_unsaved_documents(): |
|---|
| 676 | self._show_alert( "No se Puede Abrir el Archivo Actual como un Proyecto.\nRazón: El Archivo de Proyecto fue modificado pero NO GUARDADO." ) |
|---|
| 677 | return |
|---|
| 678 | self._window.close_tab( gedit.gedit_tab_get_from_document( doc )) |
|---|
| 679 | self._open_project( filename ) |
|---|
| 680 | self._save_project( filename ) |
|---|
| 681 | self._add_to_history( filename ) |
|---|
| 682 | if self._message: self._show_alert( "Proyecto Abierto: '" + os.path.basename( self._project.filename ) + "'", self.WITH_LOG ) |
|---|
| 683 | else: |
|---|
| 684 | self._show_alert( "Proyecto No Abierto: '"+ os.path.basename( filename ) + "'\n\nArchivo de Proyecto No Encontrado: \n" + filename ) |
|---|
| 685 | |
|---|
| 686 | def on_open_project( self, widget, data=None ): |
|---|
| 687 | "Activated when the user opens a project from the chooser" |
|---|
| 688 | if data == self.CHOOSER_OPEN: |
|---|
| 689 | if self._open_file_chooser.get_filename(): |
|---|
| 690 | self._open_project( self._open_file_chooser.get_filename().strip() ) |
|---|
| 691 | self._save_project( self._project.filename ) |
|---|
| 692 | self._add_to_history( self._project.filename ) |
|---|
| 693 | if self._message: self._show_alert( "Proyecto Abierto: '" + os.path.basename( self._project.filename ) + "'", self.WITH_LOG ) |
|---|
| 694 | self._open_file_chooser.hide() |
|---|
| 695 | |
|---|
| 696 | |
|---|
| 697 | def on_save_project( self, widget, data=None ): |
|---|
| 698 | "Activated when the user saves a project with the chooser" |
|---|
| 699 | if data == self.CHOOSER_SAVE: |
|---|
| 700 | if self._save_file_chooser.get_filename(): |
|---|
| 701 | project_filename = self._save_file_chooser.get_filename().strip() #GYLL |
|---|
| 702 | if not self._has_gedit_project_extension( project_filename ): |
|---|
| 703 | project_filename = project_filename + ".gedit-project" #GYLL |
|---|
| 704 | if os.path.exists( project_filename.replace("%20", " ") ): #GYLL |
|---|
| 705 | self._show_alert("Archivo de Proyecto ya EXISTE; este no ha sido reemplazado: " + project_filename.replace("%20", " ") ) #GYLL |
|---|
| 706 | else: |
|---|
| 707 | self._save_project( project_filename ) #GYLL |
|---|
| 708 | self._add_to_history( project_filename ) #GYLL |
|---|
| 709 | self._project.active = True |
|---|
| 710 | self.update_ui() #GYLL |
|---|
| 711 | self._show_alert( "Nuevo Proyecto Abierto: '" + os.path.basename( self._project.filename ) ) |
|---|
| 712 | self._save_file_chooser.hide() |
|---|
| 713 | |
|---|
| 714 | def _open_files( self, file_list ): |
|---|
| 715 | for file_uri in file_list: |
|---|
| 716 | if os.path.exists( file_uri.lstrip( "file:" ).replace("%20", " ") ): |
|---|
| 717 | file_is_not_open = True |
|---|
| 718 | for item in self._window.get_documents(): |
|---|
| 719 | if item.get_uri() == file_uri: |
|---|
| 720 | item.set_data( "BelongsToProject", self._project.filename ) #GYLL |
|---|
| 721 | file_is_not_open = False |
|---|
| 722 | break |
|---|
| 723 | if file_is_not_open: |
|---|
| 724 | tab = self._window.create_tab_from_uri( file_uri, self._encoding, 0, False, False ) |
|---|
| 725 | tab.get_document().set_data( "BelongsToProject", self._project.filename ) |
|---|
| 726 | self._window.set_active_tab( tab ) |
|---|
| 727 | else: |
|---|
| 728 | self._message.append( " - El siguiente Archivo YA ESTA ABIERTO en gedit; este no ha sido cargado desde el disco: '/" + file_uri.lstrip("file:/").replace("%20"," ") + "'" ) |
|---|
| 729 | self._project.add_file( file_uri ) |
|---|
| 730 | else: |
|---|
| 731 | self._message.append( " - El siguiente Archivo no HA SIDO ENCONTRADO; este ha sido BORRADO del Proyecto: '/" + file_uri.lstrip( "file:/" ).replace("%20"," ") + "'") |
|---|
| 732 | print file_uri.lstrip( "file:/" ).replace("%20"," ") |
|---|
| 733 | |
|---|
| 734 | def _close_project( self ): |
|---|
| 735 | print "Closing project ... " + self._project.filename |
|---|
| 736 | for item in self._window.get_documents(): #GYLL |
|---|
| 737 | if item.get_uri() == None and not (item in self._window.get_unsaved_documents()): #GYLL |
|---|
| 738 | self._window.close_tab( gedit.gedit_tab_get_from_document( item ) ) #GYLL |
|---|
| 739 | |
|---|
| 740 | there_are_unsaved_files = False |
|---|
| 741 | for item in self._window.get_documents(): |
|---|
| 742 | if item.get_data("BelongsToProject") == self._project.filename: |
|---|
| 743 | item.set_data( "BelongsToProject", None ) #GYLL |
|---|
| 744 | if not (item in self._window.get_unsaved_documents()): |
|---|
| 745 | self._window.close_tab( gedit.gedit_tab_get_from_document( item ) ) |
|---|
| 746 | else: |
|---|
| 747 | self._message.append( " - El siguiente Archivo no ha sido GUARDADO; este No ha sido cerrado: '/" + |
|---|
| 748 | item.get_uri().lstrip("file:/").replace("%20", " ") + "'" ) |
|---|
| 749 | self._project.clear() |
|---|
| 750 | self.update_ui() |
|---|
| 751 | |
|---|
| 752 | def _open_project( self, filename ): |
|---|
| 753 | "Process the project file and open all the child files" |
|---|
| 754 | for item in self._window.get_documents(): #GYLL |
|---|
| 755 | if item.get_uri() == None and not (item in self._window.get_unsaved_documents()): #GYLL |
|---|
| 756 | self._window.close_tab( gedit.gedit_tab_get_from_document( item ) ) #GYLL |
|---|
| 757 | |
|---|
| 758 | self._project.filename = filename |
|---|
| 759 | file_list = list() |
|---|
| 760 | |
|---|
| 761 | xml = minidom.parse( filename ) |
|---|
| 762 | for subfile in xml.getElementsByTagName( 'file' ): |
|---|
| 763 | file_list.append( subfile.childNodes[0].data.strip() ) |
|---|
| 764 | self._open_files( file_list ) |
|---|
| 765 | self._project.active = True #GYLL -- this line has been inserted into this function; it used to be placed throught the program after each call to _open_project |
|---|
| 766 | print "Opening project ... " + filename |
|---|
| 767 | |
|---|
| 768 | def _save_project( self, filename ): |
|---|
| 769 | "Output the project XML to a file" |
|---|
| 770 | self._project.filename = filename |
|---|
| 771 | self._saving_project = True |
|---|
| 772 | |
|---|
| 773 | # TODO: Compile an XML document out of self._project and save it |
|---|
| 774 | out_xml = minidom.Document() |
|---|
| 775 | out_xml.version = 1.0 |
|---|
| 776 | |
|---|
| 777 | gedit_project_element = minidom.Element( 'gedit-project' ) |
|---|
| 778 | |
|---|
| 779 | for subfilename in self._project.get_files(): |
|---|
| 780 | file_element = minidom.Element( 'file' ) |
|---|
| 781 | text_node = minidom.Text() |
|---|
| 782 | text_node.data = subfilename |
|---|
| 783 | file_element.childNodes.append( text_node ) |
|---|
| 784 | gedit_project_element.childNodes.append( file_element ) |
|---|
| 785 | |
|---|
| 786 | out_xml.childNodes.append( gedit_project_element ) |
|---|
| 787 | |
|---|
| 788 | outfile = file( self._project.filename, "w" ) |
|---|
| 789 | outfile.writelines( out_xml.toprettyxml() ) |
|---|
| 790 | outfile.close() |
|---|
| 791 | |
|---|
| 792 | self._saving_project = False |
|---|
| 793 | print "Guardando Proyecto ... " + self._project.filename |
|---|
| 794 | |
|---|
| 795 | def on_simulate_project_action( self ): |
|---|
| 796 | "Activada cuando el usuario esta sobre un archivo del tipo *.tbw" |
|---|
| 797 | doc = self._window.get_active_document() |
|---|
| 798 | # preguntamos si no existe el doc |
|---|
| 799 | if not doc: |
|---|
| 800 | return |
|---|
| 801 | fileName = os.path.basename( doc.get_uri_for_display() ) |
|---|
| 802 | #comprobamos que el documento este guardado |
|---|
| 803 | for doc_unsaved in self._window.get_unsaved_documents(): |
|---|
| 804 | if doc_unsaved == doc: |
|---|
| 805 | print "doc_unsaved" |
|---|
| 806 | return |
|---|
| 807 | #comprobamos que el documento sea del tipo *.tbw |
|---|
| 808 | fileName = os.path.basename( doc.get_uri_for_display() ) |
|---|
| 809 | if fileName[fileName.find('.'):]!='.tbw': |
|---|
| 810 | print "el documento no es un tbw" |
|---|
| 811 | return |
|---|
| 812 | file_list = list() |
|---|
| 813 | xml = minidom.parse( self._project.filename ) |
|---|
| 814 | for subfile in xml.getElementsByTagName('file'): |
|---|
| 815 | file_list.append(subfile.childNodes[0].data.strip()) |
|---|
| 816 | archivos_del_proyecto = "" |
|---|
| 817 | while file_list: |
|---|
| 818 | ghdl_process = subprocess.Popen(["ghdl", "-i", "--ieee=synopsys", "-fexplicit", file_list.pop()[7:]], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
|---|
| 819 | arreglo_salida = "" |
|---|
| 820 | for salida_ghdl in ghdl_process.stdout: |
|---|
| 821 | #aca guardamos los errores que pueden haber |
|---|
| 822 | arreglo_salida = salida_ghdl.split("\n") |
|---|
| 823 | |
|---|
| 824 | ghdl_process = subprocess.Popen(["ghdl", "-m", "--ieee=synopsys", "-fexplicit", fileName[:fileName.find('.')]], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
|---|
| 825 | arreglo_salida = "" |
|---|
| 826 | for salida_ghdl in ghdl_process.stdout: |
|---|
| 827 | arreglo_salida = salida_ghdl.split("\n") |
|---|
| 828 | print arreglo_salida |
|---|
| 829 | print "despues de -m" |
|---|
| 830 | |
|---|
| 831 | print fileName |
|---|
| 832 | # preguntamos si se creo el archivo vcd para el gtkwave |
|---|
| 833 | if os.path.exists('/home/javier/.gnome2/gedit/plugins/proyecto_1/nombre_entidad_testbench'): |
|---|
| 834 | print "existe el archivo para el gtkwave" |
|---|
| 835 | |
|---|
| 836 | #ejecutamos el ejecutable para generar el vcd |
|---|
| 837 | ejecutable = fileName[:fileName.find('.')] |
|---|
| 838 | print ejecutable |
|---|
| 839 | ghdl_process = subprocess.Popen([fileName[:fileName.find('.')],"--vcd=" + fileName[:fileName.find('.')] + ".vcd" ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
|---|
| 840 | arreglo_salida = "" |
|---|
| 841 | for salida_ghdl in ghdl_process.stdout: |
|---|
| 842 | arreglo_salida = salida_ghdl.split("\n") |
|---|
| 843 | print arreglo_salida |
|---|
| 844 | print "despues de el ejecutable" |
|---|
| 845 | |
|---|
| 846 | ghdl_process = subprocess.Popen(["gtkwave", fileName[:fileName.find('.')] + ".vcd"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
|---|
| 847 | arreglo_salida = "" |
|---|
| 848 | for salida_ghdl in ghdl_process.stdout: |
|---|
| 849 | arreglo_salida = salida_ghdl.split("\n") |
|---|
| 850 | print arreglo_salida |
|---|
| 851 | print "despues de el gtkwave" |
|---|
| 852 | |
|---|
| 853 | class ProjectPlugin( gedit.Plugin ): |
|---|
| 854 | DATA_TAG = "ProjectPluginInstance" |
|---|
| 855 | |
|---|
| 856 | def __init__( self ): |
|---|
| 857 | gedit.Plugin.__init__( self ) |
|---|
| 858 | |
|---|
| 859 | def _get_instance( self, window ): |
|---|
| 860 | return window.get_data( self.DATA_TAG ) |
|---|
| 861 | |
|---|
| 862 | def _set_instance( self, window, instance ): |
|---|
| 863 | window.set_data( self.DATA_TAG, instance ) |
|---|
| 864 | |
|---|
| 865 | def activate( self, window ): |
|---|
| 866 | self._set_instance( window, ProjectPluginInstance( self, window ) ) |
|---|
| 867 | |
|---|
| 868 | def deactivate( self, window ): |
|---|
| 869 | self._get_instance( window ).deactivate() |
|---|
| 870 | self._set_instance( window, None ) |
|---|
| 871 | |
|---|
| 872 | def update_ui( self, window ): |
|---|
| 873 | self._get_instance( window ).update_ui() |
|---|