[pitivi] Move saveProject to ProjectManager, implement the project-saved signal and hook that up with the act



commit c97969fb65bef2b2034dd1fc82c037d3d71c9d3a
Author: Alessandro Decina <alessandro d gmail com>
Date:   Fri Jun 12 14:00:01 2009 +0200

    Move saveProject to ProjectManager, implement the project-saved signal and hook that up with the action_log.

 pitivi/formatters/base.py    |    8 +++++-
 pitivi/formatters/format.py  |   28 --------------------
 pitivi/projectmanager.py     |   58 ++++++++++++++++++++++++++++++++++++++++-
 pitivi/ui/mainwindow.py      |   21 ++++++++++++---
 tests/test_projectmanager.py |   13 ++++++---
 5 files changed, 89 insertions(+), 39 deletions(-)
---
diff --git a/pitivi/formatters/base.py b/pitivi/formatters/base.py
index ad9a900..a17131b 100644
--- a/pitivi/formatters/base.py
+++ b/pitivi/formatters/base.py
@@ -66,6 +66,8 @@ class Formatter(Signallable, Loggable):
         "new-project-created": ["project"],
         "new-project-loaded": ["project"],
         "new-project-failed": ["uri", "exception"],
+        "save-project-failed": ["project", "uri", "exception"],
+        "project-saved": ["project", "uri"],
         "missing-uri" : ["uri"]
         }
 
@@ -185,7 +187,11 @@ class Formatter(Signallable, Loggable):
             raise FormatterURIError()
         if overwrite == False and uri_is_reachable(location):
             raise FormatterOverwriteError()
-        self._saveProject(project, location)
+        if self._saveProject(project, location):
+            self.emit("project-saved", project, location)
+            return True
+
+        return False
 
     #}
 
diff --git a/pitivi/formatters/format.py b/pitivi/formatters/format.py
index 3be2233..ba25f3d 100644
--- a/pitivi/formatters/format.py
+++ b/pitivi/formatters/format.py
@@ -29,34 +29,6 @@ from gettext import gettext as _
 
 _formatters = []
 
-def save_project(project, uri, formatter=None, overwrite=False):
-    """
-    Save the L{Project} to the given location.
-
-    If specified, use the given formatter.
-
-    @type project: L{Project}
-    @param project: The L{Project} to save.
-    @type uri: L{str}
-    @param uri: The location to store the project to. Needs to
-    be an absolute URI.
-    @type formatter: L{Formatter}
-    @param formatter: The L{Formatter} to use to store the project if specified.
-    If it is not specified, then it will be saved at its original format.
-    @param overwrite: Whether to overwrite existing location.
-    @type overwrite: C{bool}
-    @raise FormatterSaveError: If the file couldn't be properly stored.
-
-    @see: L{Formatter.saveProject}
-    """
-    if formatter == None:
-        if project.format:
-            formatter == project.format
-        else:
-            from pitivi.formatters.etree import ElementTreeFormatter
-            formatter = ElementTreeFormatter()
-    formatter.saveProject(project, uri, overwrite)
-
 def can_handle_location(uri):
     """
     Detects whether the project at the given location can be loaded.
diff --git a/pitivi/projectmanager.py b/pitivi/projectmanager.py
index 66d8d23..af568ae 100644
--- a/pitivi/projectmanager.py
+++ b/pitivi/projectmanager.py
@@ -26,7 +26,7 @@ import gst
 
 from pitivi.project import Project
 from pitivi.formatters.format import get_formatter_for_uri
-from pitivi.formatters.base import FormatterLoadError
+from pitivi.formatters.base import FormatterLoadError, FormatterSaveError
 
 from pitivi.signalinterface import Signallable
 from pitivi.log.loggable import Loggable
@@ -39,6 +39,8 @@ class ProjectManager(Signallable, Loggable):
         "new-project-created": ["project"],
         "new-project-failed": ["uri", "exception"],
         "new-project-loaded": ["project"],
+        "save-project-failed": ["project", "uri", "exception"],
+        "project-saved": ["project", "uri"],
         "closing-project": ["project"],
         "project-closed": ["project"],
         "missing-uri": ["formatter", "uri"],
@@ -69,6 +71,44 @@ class ProjectManager(Signallable, Loggable):
         # start loading the project, from now on everything is async
         formatter.loadProject(uri)
 
+    def saveProject(self, project, uri=None, overwrite=False, formatter=None):
+        """
+        Save the L{Project} to the given location.
+
+        If specified, use the given formatter.
+
+        @type project: L{Project}
+        @param project: The L{Project} to save.
+        @type uri: L{str}
+        @param uri: The location to store the project to. Needs to
+        be an absolute URI.
+        @type formatter: L{Formatter}
+        @param formatter: The L{Formatter} to use to store the project if specified.
+        If it is not specified, then it will be saved at its original format.
+        @param overwrite: Whether to overwrite existing location.
+        @type overwrite: C{bool}
+        @raise FormatterSaveError: If the file couldn't be properly stored.
+
+        @see: L{Formatter.saveProject}
+        """
+        if formatter is None:
+            if project.format:
+                formatter == project.format
+            else:
+                from pitivi.formatters.etree import ElementTreeFormatter
+                formatter = ElementTreeFormatter()
+
+        if uri is None:
+            if project.uri is None:
+                self.emit("save-project-failed", project, uri,
+                        FormatterSaveError(_("No URI specified.")))
+                return
+
+            uri = project.uri
+
+        self._connectToFormatter(formatter)
+        return formatter.saveProject(project, uri, overwrite)
+
     def closeRunningProject(self):
         """ close the current project """
         self.info("closing running project")
@@ -77,7 +117,7 @@ class ProjectManager(Signallable, Loggable):
             return True
 
         if self.current.hasUnsavedModifications():
-            if not self.current.save():
+            if not self.saveProject(self.current):
                 return False
 
         if self.emit("closing-project", self.current) == False:
@@ -126,12 +166,18 @@ class ProjectManager(Signallable, Loggable):
                 self._formatterNewProjectLoaded)
         formatter.connect("new-project-failed",
                 self._formatterNewProjectFailed)
+        formatter.connect("save-project-failed",
+                self._formatterSaveProjectFailed)
+        formatter.connect("project-saved",
+                self._formatterProjectSaved)
 
     def _disconnectFromFormatter(self, formatter):
         formatter.disconnect_by_function(self._formatterMissingURICb)
         formatter.disconnect_by_function(self._formatterNewProjectCreated)
         formatter.disconnect_by_function(self._formatterNewProjectLoaded)
         formatter.disconnect_by_function(self._formatterNewProjectFailed)
+        formatter.disconnect_by_function(self._formatterSaveProjectFailed)
+        formatter.disconnect_by_function(self._formatterProjectSaved)
 
     def _formatterNewProjectCreated(self, formatter, project):
         self.emit("new-project-created", project)
@@ -149,3 +195,11 @@ class ProjectManager(Signallable, Loggable):
 
     def _formatterMissingURICb(self, formatter, uri):
         return self.emit("missing-uri", formatter, uri)
+
+    def _formatterSaveProjectFailed(self, formatter, project, uri, exception):
+        self._disconnectFromFormatter(formatter)
+        self.emit("save-project-failed", project, uri, exception)
+
+    def _formatterProjectSaved(self, formatter, project, uri):
+        self._disconnectFromFormatter(formatter)
+        self.emit("project-saved", project, uri)
diff --git a/pitivi/ui/mainwindow.py b/pitivi/ui/mainwindow.py
index 5781203..d3b0d56 100644
--- a/pitivi/ui/mainwindow.py
+++ b/pitivi/ui/mainwindow.py
@@ -173,6 +173,10 @@ class PitiviMainWindow(gtk.Window, Loggable):
                 self._projectManagerNewProjectLoadedCb)
         self.app.projectManager.connect("new-project-failed",
                 self._projectManagerNewProjectFailedCb)
+        self.app.projectManager.connect("save-project-failed",
+                self._projectManagerSaveProjectFailedCb)
+        self.app.projectManager.connect("project-saved",
+                self._projectManagerProjectSavedCb)
         self.app.projectManager.connect("closing-project",
                 self._projectManagerClosingProjectCb)
         self.app.projectManager.connect("project-closed",
@@ -535,13 +539,12 @@ class PitiviMainWindow(gtk.Window, Loggable):
         if not self.project.uri:
             self._saveProjectAsCb(unused_action)
         else:
-            self.project.save(overwrite=True)
+            self.app.projectManager.saveProject(self.project, overwrite=True)
 
     def _saveProjectAsCb(self, unused_action):
         uri = self._showSaveAsDialog(self.app.current)
         if uri:
-            self.project.save(uri, overwrite=True)
-            self.project.uri = uri
+            self.app.projectManager.saveProject(self.project, uri, overwrite=True)
 
     def _projectSettingsCb(self, unused_action):
         from projectsettings import ProjectSettingsDialog
@@ -668,6 +671,15 @@ class PitiviMainWindow(gtk.Window, Loggable):
     def _projectManagerNewProjectLoadingCb(self, projectManager, uri):
         self.log("A NEW project is being loaded, deactivate UI")
 
+    def _projectManagerSaveProjectFailedCb(self, projectManager,
+            project, uri, exception):
+        # FIXME: do something here
+        self.error("failed to save project")
+
+    def _projectManagerProjectSavedCb(self, projectManager, project, uri):
+        self.app.action_log.checkpoint()
+        self._syncDoUndo(self.app.action_log)
+
     def _projectManagerClosingProjectCb(self, projectManager, project):
         if not project.hasUnsavedModifications():
             return True
@@ -764,8 +776,9 @@ class PitiviMainWindow(gtk.Window, Loggable):
         else:
             undo_action.props.label = _("Undo")
 
+        dirty = action_log.dirty()
         save_action = self.actiongroup.get_action("SaveProject")
-        save_action.set_sensitive(can_undo)
+        save_action.set_sensitive(dirty)
 
         redo_action = self.actiongroup.get_action("Redo")
         can_redo = bool(action_log.redo_stacks)
diff --git a/tests/test_projectmanager.py b/tests/test_projectmanager.py
index 4219359..865180c 100644
--- a/tests/test_projectmanager.py
+++ b/tests/test_projectmanager.py
@@ -26,11 +26,13 @@ from pitivi.formatters.base import Formatter, \
         FormatterError, FormatterLoadError
 
 class MockProject(object):
-    def hasUnsavedModifications(self):
-        return True
+    settings = None
+    format = None
+    uri = None
+    has_mods = True
 
-    def save(self):
-        return True
+    def hasUnsavedModifications(self):
+        return self.has_mods
 
     def release(self):
         pass
@@ -228,6 +230,8 @@ class TestProjectManager(TestCase):
             return False
 
         self.manager.current = MockProject()
+        self.manager.current.has_mods = False
+        self.manager.current.uri = "file:///ciao"
         self.manager.connect("closing-project", closing)
 
         self.failIf(self.manager.closeRunningProject())
@@ -239,6 +243,7 @@ class TestProjectManager(TestCase):
 
     def testCloseRunningProject(self):
         current = self.manager.current = MockProject()
+        self.manager.current.has_mods = False
         self.failUnless(self.manager.closeRunningProject())
         self.failUnlessEqual(len(self.signals), 2)
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]