[pitivi] preset: Allow faking removal of saved EncodingTarget/Profiles



commit 67a352c59a87789d355b0bb3b94f4b6ac0acd0a6
Author: Thibault Saunier <thibault saunier osg samsung com>
Date:   Thu Feb 2 10:21:50 2017 -0300

    preset: Allow faking removal of saved EncodingTarget/Profiles
    
    There is no actual removing of the serialized files but we add a json
    file with a list of names of EncodingProfiles that have been 'removed'
    by the user.
    
    Differential Revision: https://phabricator.freedesktop.org/D1638

 pitivi/preset.py     |   50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 tests/test_render.py |   49 ++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 88 insertions(+), 11 deletions(-)
---
diff --git a/pitivi/preset.py b/pitivi/preset.py
index 953574a..2f7a185 100644
--- a/pitivi/preset.py
+++ b/pitivi/preset.py
@@ -29,6 +29,7 @@ from gi.repository import Gtk
 from pitivi.configure import get_audiopresets_dir
 from pitivi.configure import get_renderpresets_dir
 from pitivi.configure import get_videopresets_dir
+from pitivi.settings import xdg_config_home
 from pitivi.settings import xdg_data_home
 from pitivi.utils.loggable import Loggable
 from pitivi.utils.ui import alter_style_class
@@ -345,6 +346,7 @@ class PresetManager(GObject.Object, Loggable):
                 self._markRemoved(name)
             else:
                 os.remove(filepath)
+
         self.cur_preset = None
         self._forgetPreset(name)
 
@@ -499,6 +501,13 @@ class EncodingTargetManager(PresetManager):
     def __init__(self, project):
         PresetManager.__init__(self)
         self._project = project
+        self._removed_file_list = os.path.join(xdg_data_home(),
+                                               'hidden_encoding_profiles.json')
+        try:
+            with open(self._removed_file_list) as f:
+                self._removed_profiles = json.loads(f.read())
+        except FileNotFoundError:
+            self._removed_profiles = []
 
     def _add_target(self, target):
         profiles = target.get_profiles()
@@ -507,6 +516,10 @@ class EncodingTargetManager(PresetManager):
             if len(profiles) != 1 and profile.get_name().lower() != 'default':
                 name += '_' + profile.get_name()
 
+            if name in self._removed_profiles:
+                continue
+
+            self.presets[name] = profile
             self._addPreset(name, profile)
 
     def loadAll(self):
@@ -518,7 +531,21 @@ class EncodingTargetManager(PresetManager):
             if target.get_category() != GstPbutils.ENCODING_CATEGORY_FILE_EXTENSION:
                 self._add_target(target)
 
-    def saveCurrentPreset(self, new_name):
+    def createPreset(self, name, values=None):
+        self.saveCurrentPreset(name, validate_name=False)
+
+    def getNewPresetName(self):
+        """Gets a unique name for a new preset."""
+        # Translators: This must contain exclusively low case alphanum and '-'
+        name = _("new-profile")
+        i = 1
+        while self.hasPreset(name):
+            # Translators: This must contain exclusively low case alphanum and '-'
+            name = _("new-profile-%d") % i
+            i += 1
+        return name
+
+    def saveCurrentPreset(self, new_name, validate_name=True):
         """PresetManager override, saves currently selected profile on disk.
 
         Override from PresetManager
@@ -526,16 +553,21 @@ class EncodingTargetManager(PresetManager):
         Args:
             new_name (str): The name to save current Gst.EncodingProfile as.
         """
-        if not self.combo.get_parent().valid:
+        if validate_name and not self.combo.get_parent().valid:
             self.error("Current encoding target name is not valid")
             return
 
+        if new_name in self._removed_profiles:
+            self._removed_profiles.remove(new_name)
+            self._save_removed_profiles()
+
         target = GstPbutils.EncodingTarget.new(new_name, "user-defined",
                                                new_name,
                                                [self._project.container_profile])
         target.save()
 
         self._add_target(target)
+        self.combo.set_active(len(self.combo.props.model) - 1)
 
     def select_preset(self, combo):
         """Selects preset from currently active row in @combo.
@@ -546,10 +578,24 @@ class EncodingTargetManager(PresetManager):
             combo (str): The Gtk.ComboBox to retrieve selected GstEncodingProfile from.
         """
         active_iter = combo.get_active_iter()
+        name = None
         if active_iter:
             # The user selected a preset.
+            name = combo.props.model.get_value(active_iter, 0)
             profile = combo.props.model.get_value(active_iter, 1)
             self.emit("profile-selected", profile)
+        self.cur_preset = name
+
+    def _save_removed_profiles(self):
+        with open(self._removed_file_list, 'w') as f:
+            json.dump(self._removed_profiles, f)
+
+    def removeCurrentPreset(self):
+        self._removed_profiles.append(self.cur_preset)
+        self._save_removed_profiles()
+        self._forgetPreset(self.cur_preset)
+        self.combo.get_parent().setWidgetValue(self.getNewPresetName())
+        self.cur_preset = None
 
     def restorePreset(self, values):
         """Raises NotImplemented as it does not make sense for that class.
diff --git a/tests/test_render.py b/tests/test_render.py
index 07005f0..1575636 100644
--- a/tests/test_render.py
+++ b/tests/test_render.py
@@ -18,6 +18,7 @@
 # Boston, MA 02110-1301, USA.
 """Tests for the render module."""
 # pylint: disable=protected-access,no-self-use
+# pylint: disable=too-many-locals
 from unittest import mock
 from unittest import skipUnless
 
@@ -43,6 +44,15 @@ def factory_exists(*factories):
     return True, ""
 
 
+def find_preset_row_index(combo, name):
+    """Finds @name in @combo."""
+    for i, row in enumerate(combo.get_model()):
+        if row[0] == name:
+            return i
+
+    return None
+
+
 class TestRender(common.TestCase):
     """Tests for functions."""
 
@@ -142,19 +152,10 @@ class TestRender(common.TestCase):
             self.assertEqual(project.video_profile.get_restriction()[0]["format"],
                              "Y444")
 
-    # pylint: disable=too-many-locals
     @skipUnless(*factory_exists("vorbisenc", "theoraenc", "oggmux",
                                 "opusenc", "vp8enc"))
     def test_loading_preset(self):
         """Checks preset values are properly exposed in the UI."""
-        def find_preset_row_index(combo, name):
-            """Finds @name in @combo."""
-            for i, row in enumerate(combo.get_model()):
-                if row[0] == name:
-                    return i
-
-            return None
-
         def preset_changed_cb(combo, changed):
             """Callback for the 'combo::changed' signal."""
             changed.append(1)
@@ -218,3 +219,33 @@ class TestRender(common.TestCase):
                     self.assertEqual(combo_value, val, preset_name)
 
                 self.assertEqual(getattr(project, attr), val)
+
+    @skipUnless(*factory_exists("vorbisenc", "theoraenc", "oggmux",
+                                "opusenc", "vp8enc"))
+    def test_remove_profile(self):
+        """Tests removing EncodingProfile and re-saving it."""
+        project = self.create_simple_project()
+        dialog = self.create_rendering_dialog(project)
+        preset_combo = dialog.render_presets.combo
+        i = find_preset_row_index(preset_combo, 'test')
+        self.assertIsNotNone(i)
+        preset_combo.set_active(i)
+
+        # Check the 'test' profile is selected
+        active_iter = preset_combo.get_active_iter()
+        self.assertEqual(preset_combo.props.model.get_value(active_iter, 0), 'test')
+
+        # Remove current profile and verify it has been removed
+        dialog.render_presets.action_remove.activate()
+        profile_names = [i[0] for i in preset_combo.props.model]
+        active_iter = preset_combo.get_active_iter()
+        self.assertEqual(active_iter, None)
+
+        # Re save the current EncodingProfile calling it the same as before.
+        preset_combo.get_child().set_text("test")
+        self.assertTrue(dialog.render_presets.action_save.get_enabled())
+        dialog.render_presets.action_save.activate(None)
+        self.assertEqual([i[0] for i in preset_combo.props.model],
+                         profile_names + ['test'])
+        active_iter = preset_combo.get_active_iter()
+        self.assertEqual(preset_combo.props.model.get_value(active_iter, 0), 'test')


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