[longomatch] Add a new registry for UI components



commit f21689e2608fba4e24f024ee176334a096a2a1ee
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Fri Mar 20 20:13:13 2015 +0100

    Add a new registry for UI components

 LongoMatch.Addins/ExtensionPoints/IGUIBackend.cs |   30 ++++++++++
 LongoMatch.Addins/LongoMatch.Addins.csproj       |    1 +
 LongoMatch.Core/Common/Registry.cs               |   67 ++++++++++++++++++++++
 LongoMatch.Core/Interfaces/GUI/IGUIToolkit.cs    |    2 +
 LongoMatch.Core/LongoMatch.Core.csproj           |    1 +
 LongoMatch.GUI/Gui/GUIToolkit.cs                 |    7 ++
 LongoMatch.Multimedia/Utils/MultimediaFactory.cs |   52 ++++-------------
 7 files changed, 121 insertions(+), 39 deletions(-)
---
diff --git a/LongoMatch.Addins/ExtensionPoints/IGUIBackend.cs 
b/LongoMatch.Addins/ExtensionPoints/IGUIBackend.cs
new file mode 100644
index 0000000..c75781d
--- /dev/null
+++ b/LongoMatch.Addins/ExtensionPoints/IGUIBackend.cs
@@ -0,0 +1,30 @@
+//
+//  Copyright (C) 2015 Fluendo S.A.
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+using System;
+using Mono.Addins;
+using LongoMatch.Core.Interfaces.GUI;
+
+namespace LongoMatch.Addins.ExtensionPoints
+{
+       [TypeExtensionPoint]
+       public interface IGUIBackend
+       {
+               void RegisterElements (IGUIToolkit gtoolkit);
+       }
+}
+
diff --git a/LongoMatch.Addins/LongoMatch.Addins.csproj b/LongoMatch.Addins/LongoMatch.Addins.csproj
index 5e8ebec..86da607 100644
--- a/LongoMatch.Addins/LongoMatch.Addins.csproj
+++ b/LongoMatch.Addins/LongoMatch.Addins.csproj
@@ -37,6 +37,7 @@
     <Compile Include="PreferencesAttribute.cs" />
     <Compile Include="ConfigurablePlugin.cs" />
     <Compile Include="ExtensionPoints\IStatsUI.cs" />
+    <Compile Include="ExtensionPoints\IGUIBackend.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="Mono.Addins, Version=0.6.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756">
diff --git a/LongoMatch.Core/Common/Registry.cs b/LongoMatch.Core/Common/Registry.cs
new file mode 100644
index 0000000..9223340
--- /dev/null
+++ b/LongoMatch.Core/Common/Registry.cs
@@ -0,0 +1,67 @@
+//
+//  Copyright (C) 2015 Fluendo S.A.
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+using System;
+using System.Linq;
+using System.Collections.Generic;
+
+namespace LongoMatch.Core.Common
+{
+       public class Registry
+       {
+               Dictionary<Type, List<RegistryElement>> elements;
+               string name;
+
+               internal class RegistryElement
+               {
+                       public Type type;
+                       public int priority;
+
+                       public RegistryElement (Type type, int priority)
+                       {
+                               this.type = type;
+                               this.priority = priority;
+                       }
+               }
+
+               public Registry (string name)
+               {
+                       this.name = name;
+                       elements = new Dictionary<Type, List<RegistryElement>> ();
+               }
+
+               public void Register (int priority, Type interfac, Type elementType)
+               {
+                       if (!elements.ContainsKey (interfac)) {
+                               elements [interfac] = new List<RegistryElement> ();
+                       }
+                       elements [interfac].Add (new RegistryElement (elementType, priority));
+               }
+
+               public T GetDefault<T> (Type interfac, params object[] args)
+               {
+                       Type elementType;
+
+                       if (!elements.ContainsKey (interfac)) {
+                               throw new Exception (String.Format ("No {0} available in the {0} registry",
+                                       interfac, name));
+                       }
+                       elementType = elements [interfac].OrderByDescending (e => e.priority).First ().type;
+                       return (T)Activator.CreateInstance (elementType, args);
+               }
+       }
+}
diff --git a/LongoMatch.Core/Interfaces/GUI/IGUIToolkit.cs b/LongoMatch.Core/Interfaces/GUI/IGUIToolkit.cs
index 6b3d65e..cc7f25b 100644
--- a/LongoMatch.Core/Interfaces/GUI/IGUIToolkit.cs
+++ b/LongoMatch.Core/Interfaces/GUI/IGUIToolkit.cs
@@ -31,6 +31,8 @@ namespace LongoMatch.Core.Interfaces.GUI
 {
        public interface IGUIToolkit
        {
+               void Register (int priority, Type interfac, Type elementType);
+
                IMainController MainController { get; }
 
                IRenderingStateBar RenderingStateBar { get; }
diff --git a/LongoMatch.Core/LongoMatch.Core.csproj b/LongoMatch.Core/LongoMatch.Core.csproj
index 397ee1a..b7f56ae 100644
--- a/LongoMatch.Core/LongoMatch.Core.csproj
+++ b/LongoMatch.Core/LongoMatch.Core.csproj
@@ -138,6 +138,7 @@
     <Compile Include="Interfaces\IStorable.cs" />
     <Compile Include="Store\Templates\Team.cs" />
     <Compile Include="Interfaces\IPlayerController.cs" />
+    <Compile Include="Common\Registry.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Common\" />
diff --git a/LongoMatch.GUI/Gui/GUIToolkit.cs b/LongoMatch.GUI/Gui/GUIToolkit.cs
index 5dff5da..aa64eb4 100644
--- a/LongoMatch.GUI/Gui/GUIToolkit.cs
+++ b/LongoMatch.GUI/Gui/GUIToolkit.cs
@@ -38,6 +38,7 @@ namespace LongoMatch.Gui
        {
                static GUIToolkit instance;
                MainWindow mainWindow;
+               Registry registry;
 
                public GUIToolkit (Version version)
                {
@@ -45,6 +46,7 @@ namespace LongoMatch.Gui
                        mainWindow = new MainWindow (this);
                        (mainWindow as MainWindow).Show ();
                        instance = this;
+                       registry = new Registry ("GUI backend");
                }
 
                public static GUIToolkit Instance {
@@ -81,6 +83,11 @@ namespace LongoMatch.Gui
                        set;
                }
 
+               public void Register (int priority, Type interfac, Type elementType)
+               {
+                       registry.Register (priority, interfac, elementType);
+               }
+
                public void InfoMessage (string message, object parent = null)
                {
                        if (parent == null)
diff --git a/LongoMatch.Multimedia/Utils/MultimediaFactory.cs 
b/LongoMatch.Multimedia/Utils/MultimediaFactory.cs
index 1b73b2a..1cc5144 100644
--- a/LongoMatch.Multimedia/Utils/MultimediaFactory.cs
+++ b/LongoMatch.Multimedia/Utils/MultimediaFactory.cs
@@ -36,11 +36,11 @@ namespace LongoMatch.Video
 {
        public class MultimediaFactory
        {
-               Dictionary<Type, List<BackendElement>> elements;
+               Registry registry;
 
                public MultimediaFactory ()
                {
-                       elements = new Dictionary<Type, List<BackendElement>> ();
+                       registry = new Registry ("Multimedia Factory");
                        /* Register default elements */
                        Register (0, typeof(IPlayer), typeof(GstPlayer));
                        Register (0, typeof(IFramesCapturer), typeof(GstFramesCapturer));
@@ -53,47 +53,44 @@ namespace LongoMatch.Video
 
                public void Register (int priority, Type interfac, Type elementType)
                {
-                       if (!elements.ContainsKey (interfac)) {
-                               elements [interfac] = new List<BackendElement> ();
-                       }
-                       elements [interfac].Add (new BackendElement (elementType, priority));
+                       registry.Register (priority, interfac, elementType);
                }
 
                public IPlayer GetPlayer ()
                {
-                       return GetDefaultElement<IPlayer> (typeof(IPlayer));
+                       return registry.GetDefault<IPlayer> (typeof(IPlayer));
                }
 
                public IFramesCapturer GetFramesCapturer ()
                {
-                       return GetDefaultElement<IFramesCapturer> (typeof(IFramesCapturer));
+                       return registry.GetDefault<IFramesCapturer> (typeof(IFramesCapturer));
                }
 
                public IVideoEditor GetVideoEditor ()
                {
-                       return GetDefaultElement<IVideoEditor> (typeof(IVideoEditor));
+                       return registry.GetDefault<IVideoEditor> (typeof(IVideoEditor));
                }
 
                public IVideoConverter GetVideoConverter (string filename)
                {
-                       return GetDefaultElement<IVideoConverter> (typeof(IVideoConverter), filename);
+                       return registry.GetDefault<IVideoConverter> (typeof(IVideoConverter), filename);
                }
 
                public IDiscoverer GetDiscoverer ()
                {
-                       return GetDefaultElement<IDiscoverer> (typeof(IDiscoverer));
+                       return registry.GetDefault<IDiscoverer> (typeof(IDiscoverer));
                }
 
                public ICapturer GetCapturer ()
                {
-                       return GetDefaultElement<ICapturer> (typeof(ICapturer), "test.avi");
+                       return registry.GetDefault<ICapturer> (typeof(ICapturer), "test.avi");
                }
 
                public IRemuxer GetRemuxer (MediaFile inputFile, string outputFile, VideoMuxerType muxer)
                {
-                       return GetDefaultElement<IRemuxer> (typeof(IRemuxer),
-                                                           inputFile.FilePath,
-                                                           outputFile, muxer);
+                       return registry.GetDefault<IRemuxer> (typeof(IRemuxer),
+                               inputFile.FilePath,
+                               outputFile, muxer);
                }
 
                public MediaFile DiscoverFile (string file, bool takeScreenshot = true)
@@ -112,35 +109,12 @@ namespace LongoMatch.Video
                        return GStreamer.FileNeedsRemux (file);
                }
 
-               [DllImport("libgstreamer-0.10.dll")]
+               [DllImport ("libgstreamer-0.10.dll")]
                static extern void gst_init (int argc, string argv);
 
                public static void InitBackend ()
                {
                        gst_init (0, "");
                }
-
-               T GetDefaultElement<T> (Type interfac, params object[] args)
-               {
-                       Type elementType;
-                       
-                       if (!elements.ContainsKey (interfac)) {
-                               throw new Exception (String.Format ("No {0} available in the multimedia 
backend", interfac));
-                       }
-                       elementType = elements [interfac].OrderByDescending (e => e.priority).First ().type;
-                       return (T)Activator.CreateInstance (elementType, args);
-               }
-
-               internal class BackendElement
-               {
-                       public Type type;
-                       public int priority;
-
-                       public BackendElement (Type type, int priority)
-                       {
-                               this.type = type;
-                               this.priority = priority;
-                       }
-               }
        }
 }


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