[longomatch] Add support for system templates



commit 35eda42d751734fc51eb7723eaf04fc728300e56
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Thu Oct 16 18:46:29 2014 +0200

    Add support for system templates

 LongoMatch.Addins/AddinsManager.cs                 |   12 +++
 .../ExtensionPoints/IAnalsysDashboardsProvider.cs  |   32 ++++++++
 LongoMatch.Addins/LongoMatch.Addins.csproj         |    1 +
 LongoMatch.Addins/Makefile.am                      |    1 +
 LongoMatch.Core/Interfaces/ITemplates.cs           |    1 +
 LongoMatch.Core/Store/Templates/Dashboard.cs       |    6 ++
 LongoMatch.GUI/Gui/Component/DashboardWidget.cs    |    1 +
 LongoMatch.GUI/Gui/Panel/SportsTemplatesPanel.cs   |   79 ++++++++++++++-----
 LongoMatch.Plugins/LongoMatch.Plugins.csproj       |    1 +
 LongoMatch.Plugins/Makefile.am                     |    3 +-
 LongoMatch.Plugins/SystemDashboards.cs             |   50 ++++++++++++
 LongoMatch.Services/Services/TemplatesService.cs   |   31 ++++++--
 LongoMatch/LongoMatch.csproj                       |    8 ++
 LongoMatch/Main.cs                                 |    1 +
 14 files changed, 199 insertions(+), 28 deletions(-)
---
diff --git a/LongoMatch.Addins/AddinsManager.cs b/LongoMatch.Addins/AddinsManager.cs
index 1c153a0..2c20e46 100644
--- a/LongoMatch.Addins/AddinsManager.cs
+++ b/LongoMatch.Addins/AddinsManager.cs
@@ -23,6 +23,7 @@ using LongoMatch.Core.Interfaces.GUI;
 using LongoMatch.Core.Interfaces.Multimedia;
 using LongoMatch.Core.Store;
 using Mono.Addins;
+using LongoMatch.Core.Store.Templates;
 
 [assembly:AddinRoot ("LongoMatch", "1.1")]
 namespace LongoMatch.Addins
@@ -108,6 +109,17 @@ namespace LongoMatch.Addins
                                backend.Shutdown ();
                        }
                }
+               
+               public void LoadDashboards (ICategoriesTemplatesProvider provider)
+               {
+                       foreach (IAnalsysDashboardsProvider plugin in 
AddinManager.GetExtensionObjects<IAnalsysDashboardsProvider> ()) {
+                               foreach (Dashboard dashboard in plugin.Dashboards) {
+                                       dashboard.Static = true;
+                                       provider.Register (dashboard);
+                               }
+                       }
+                       
+               }
        }
 }
 
diff --git a/LongoMatch.Addins/ExtensionPoints/IAnalsysDashboardsProvider.cs 
b/LongoMatch.Addins/ExtensionPoints/IAnalsysDashboardsProvider.cs
new file mode 100644
index 0000000..f7b14b2
--- /dev/null
+++ b/LongoMatch.Addins/ExtensionPoints/IAnalsysDashboardsProvider.cs
@@ -0,0 +1,32 @@
+//
+//  Copyright (C) 2014 Andoni Morales Alastruey
+//
+//  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 LongoMatch.Core.Store.Templates;
+using System.Collections.Generic;
+using Mono.Addins;
+
+namespace LongoMatch.Addins.ExtensionPoints
+{
+       [TypeExtensionPoint]
+       public interface IAnalsysDashboardsProvider: ILongoMatchPlugin
+       {
+               List<Dashboard> Dashboards {
+                       get;
+               }
+       }
+}
diff --git a/LongoMatch.Addins/LongoMatch.Addins.csproj b/LongoMatch.Addins/LongoMatch.Addins.csproj
index 0eebc22..5822456 100644
--- a/LongoMatch.Addins/LongoMatch.Addins.csproj
+++ b/LongoMatch.Addins/LongoMatch.Addins.csproj
@@ -34,6 +34,7 @@
     <Compile Include="ExtensionPoints\IMultimediaBackend.cs" />
     <Compile Include="ExtensionPoints\ILongoMatchPlugin.cs" />
     <Compile Include="ExtensionPoints\IGStreamerPluginsProvider.cs" />
+    <Compile Include="ExtensionPoints\IAnalsysDashboardsProvider.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="Mono.Addins, Version=0.6.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756">
diff --git a/LongoMatch.Addins/Makefile.am b/LongoMatch.Addins/Makefile.am
index cfc3824..87a7f86 100644
--- a/LongoMatch.Addins/Makefile.am
+++ b/LongoMatch.Addins/Makefile.am
@@ -4,6 +4,7 @@ TARGET = library
 LINK = $(REF_DEP_LONGOMATCH_ADDINS)
 
 SOURCES = AddinsManager.cs \
+       ExtensionPoints/IAnalsysDashboardsProvider.cs \
        ExtensionPoints/IConfigModifier.cs \
        ExtensionPoints/IExportProject.cs \
        ExtensionPoints/IGStreamerPluginsProvider.cs \
diff --git a/LongoMatch.Core/Interfaces/ITemplates.cs b/LongoMatch.Core/Interfaces/ITemplates.cs
index 50a8f81..197e6d3 100644
--- a/LongoMatch.Core/Interfaces/ITemplates.cs
+++ b/LongoMatch.Core/Interfaces/ITemplates.cs
@@ -44,6 +44,7 @@ namespace LongoMatch.Core.Interfaces
                T Load (string name);
                void Save (ITemplate template);
                void Update (ITemplate template);
+               void Register (T template);
        }
        
        public interface ICategoriesTemplatesProvider: ITemplateProvider<Dashboard> {}
diff --git a/LongoMatch.Core/Store/Templates/Dashboard.cs b/LongoMatch.Core/Store/Templates/Dashboard.cs
index 77142e7..c6619cd 100644
--- a/LongoMatch.Core/Store/Templates/Dashboard.cs
+++ b/LongoMatch.Core/Store/Templates/Dashboard.cs
@@ -64,6 +64,12 @@ namespace LongoMatch.Core.Store.Templates
                        get;
                        set;
                }
+
+               [JsonIgnore]
+               public bool Static {
+                       get;
+                       set;
+               }
                
                public List<DashboardButton> List {
                        get;
diff --git a/LongoMatch.GUI/Gui/Component/DashboardWidget.cs b/LongoMatch.GUI/Gui/Component/DashboardWidget.cs
index a15f600..2c4ebf9 100644
--- a/LongoMatch.GUI/Gui/Component/DashboardWidget.cs
+++ b/LongoMatch.GUI/Gui/Component/DashboardWidget.cs
@@ -228,6 +228,7 @@ namespace LongoMatch.Gui.Component
                                template.List.Add (button);
                        }
                        button.Position = new Point (template.CanvasWidth, 0);
+                       edited = true;
                        Refresh (button);
                }
                
diff --git a/LongoMatch.GUI/Gui/Panel/SportsTemplatesPanel.cs 
b/LongoMatch.GUI/Gui/Panel/SportsTemplatesPanel.cs
index 5856998..5623550 100644
--- a/LongoMatch.GUI/Gui/Panel/SportsTemplatesPanel.cs
+++ b/LongoMatch.GUI/Gui/Panel/SportsTemplatesPanel.cs
@@ -16,6 +16,7 @@
 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 //
 using System;
+using System.Linq;
 using System.Collections.Generic;
 using Gdk;
 using Gtk;
@@ -94,15 +95,16 @@ namespace LongoMatch.Gui.Panel
                        timerbutton.Clicked += (object sender, EventArgs e) => {
                                buttonswidget.AddButton ("Timer"); };
 
-                       templates = new ListStore (typeof(Pixbuf), typeof(string), typeof(Dashboard));
+                       templates = new ListStore (typeof(Pixbuf), typeof(string), typeof(Dashboard), 
typeof(bool));
 
                        // Connect treeview with Model and configure
                        dashboardseditortreeview.Model = templates;
                        dashboardseditortreeview.HeadersVisible = false;
                        var cell = new CellRendererText { SizePoints = 14.0 };
-                       cell.Editable = true;
+                       //cell.Editable = true;
                        cell.Edited += HandleEdited;
-                       dashboardseditortreeview.AppendColumn ("Text", cell, "text", 1); 
+                       var col = dashboardseditortreeview.AppendColumn ("Text", cell, "text", 1); 
+                       col.AddAttribute (cell, "editable", 3);
                        dashboardseditortreeview.SearchColumn = 0;
                        dashboardseditortreeview.EnableGridLines = TreeViewGridLines.None;
                        dashboardseditortreeview.CursorChanged += HandleSelectionChanged;
@@ -140,13 +142,18 @@ namespace LongoMatch.Gui.Panel
                        foreach (Dashboard template in provider.Templates) {
                                Pixbuf img;
                                TreeIter iter;
+                               string name;
                                
                                if (template.Image != null)
                                        img = template.Image.Value;
                                else
                                        img = Helpers.Misc.LoadIcon ("longomatch", 20, 
IconLookupFlags.ForceSvg);
-                                       
-                               iter = templates.AppendValues (img, template.Name, template);
+                               
+                               name = template.Name;
+                               if (template.Static) {
+                                       name += " (" + Catalog.GetString ("System") + ")";
+                               }
+                               iter = templates.AppendValues (img, name, template, !template.Static);
                                if (first || template.Name == templateName) {
                                        templateIter = iter;
                                }
@@ -158,13 +165,49 @@ namespace LongoMatch.Gui.Panel
                        }
                }
 
+               void SaveStatic ()
+               {
+                       string msg = Catalog.GetString ("System templates can't be edited, do you want to 
create a copy?");
+                       if (Config.GUIToolkit.QuestionMessage (msg, null, this)) {
+                               string newName;
+                               while (true) {
+                                       newName = Config.GUIToolkit.QueryMessage (Catalog.GetString 
("Name:"), null,
+                                                                                 loadedTemplate.Name + " 
_copy", this);
+                                       if (newName == null)
+                                               break;
+                                       if (provider.TemplatesNames.Contains (newName)) {
+                                               msg = Catalog.GetString ("A template with the same name 
already exists"); 
+                                               Config.GUIToolkit.ErrorMessage (msg, this);
+                                       }
+                                       else {
+                                               break;
+                                       }
+                               }
+                               if (newName == null) {
+                                       return;
+                               }
+                               Dashboard newtemplate = loadedTemplate.Clone ();
+                               newtemplate.Name = loadedTemplate.Name + "_copy";
+                               newtemplate.Static = false;
+                               provider.Save (newtemplate);
+                               Load (newtemplate.Name);
+                       }
+               }
+
                void Save (bool prompt)
                {
                        if (loadedTemplate != null && buttonswidget.Edited) {
-                               string msg = Catalog.GetString ("Do you want to save the current template");
-                               if (!prompt || Config.GUIToolkit.QuestionMessage (msg, null, this)) {
-                                       provider.Update (loadedTemplate);
-                                       buttonswidget.Edited = false;
+                               if (loadedTemplate.Static) {
+                                       /* prompt=false when we click the save button */
+                                       if (!prompt) {
+                                               SaveStatic ();
+                                       }
+                               } else {
+                                       string msg = Catalog.GetString ("Do you want to save the current 
template");
+                                       if (!prompt || Config.GUIToolkit.QuestionMessage (msg, null, this)) {
+                                               provider.Update (loadedTemplate);
+                                               buttonswidget.Edited = false;
+                                       }
                                }
                        }
                }
@@ -214,26 +257,23 @@ namespace LongoMatch.Gui.Panel
                        selected = templates.GetValue (iter, 2) as Dashboard;
                        deletetemplatebutton.Visible = selected != null;
                        buttonswidget.Sensitive = selected != null;
-                       loadedTemplate = selected;
                        if (selected != null) {
                                Save (true);
+                               deletetemplatebutton.Sensitive = !selected.Static;
                                buttonswidget.Template = selected;
                        }
+                       loadedTemplate = selected;
                }
 
                void HandleDeleteTeamClicked (object sender, EventArgs e)
                {
                        if (loadedTemplate != null) {
-                               if (loadedTemplate.Name == "default") {
-                                       MessagesHelpers.ErrorMessage (this, Catalog.GetString ("The default 
template can't be deleted"));
-                               } else {
-                                       string msg = Catalog.GetString ("Do you really want to delete the 
template: ") + loadedTemplate.Name;
-                                       if (MessagesHelpers.QuestionMessage (this, msg, null)) {
-                                               provider.Delete (loadedTemplate.Name);
-                                       }
+                               string msg = Catalog.GetString ("Do you really want to delete the template: 
") + loadedTemplate.Name;
+                               if (MessagesHelpers.QuestionMessage (this, msg, null)) {
+                                       provider.Delete (loadedTemplate.Name);
                                }
                        }
-                       Load ("default");
+                       Load (provider.TemplatesNames.FirstOrDefault ());
                }
 
                void HandleNewTeamClicked (object sender, EventArgs e)
@@ -252,9 +292,6 @@ namespace LongoMatch.Gui.Panel
                                if (dialog.Text == "") {
                                        MessagesHelpers.ErrorMessage (dialog, Catalog.GetString ("The 
template name is empty."));
                                        continue;
-                               } else if (dialog.Text == "default") {
-                                       MessagesHelpers.ErrorMessage (dialog, Catalog.GetString ("The 
template can't be named 'default'."));
-                                       continue;
                                } else if (provider.Exists (dialog.Text)) {
                                        var msg = Catalog.GetString ("The template already exists. " +
                                                "Do you want to overwrite it ?");
diff --git a/LongoMatch.Plugins/LongoMatch.Plugins.csproj b/LongoMatch.Plugins/LongoMatch.Plugins.csproj
index fedb1dc..d943478 100644
--- a/LongoMatch.Plugins/LongoMatch.Plugins.csproj
+++ b/LongoMatch.Plugins/LongoMatch.Plugins.csproj
@@ -29,6 +29,7 @@
   <ItemGroup>
     <Compile Include="Assembly.cs" />
     <Compile Include="CSVExporter.cs" />
+    <Compile Include="SystemDashboards.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="System" />
diff --git a/LongoMatch.Plugins/Makefile.am b/LongoMatch.Plugins/Makefile.am
index b6c712c..506ff14 100644
--- a/LongoMatch.Plugins/Makefile.am
+++ b/LongoMatch.Plugins/Makefile.am
@@ -5,7 +5,8 @@ INSTALL_DIR = $(pkglibdir)/plugins
 LINK = $(REF_DEP_LONGOMATCH_PLUGINS)
 
 SOURCES = Assembly.cs \
-       CSVExporter.cs
+       CSVExporter.cs \
+       SystemDashboards.cs
 
 RESOURCES = 
 
diff --git a/LongoMatch.Plugins/SystemDashboards.cs b/LongoMatch.Plugins/SystemDashboards.cs
new file mode 100644
index 0000000..d4a684c
--- /dev/null
+++ b/LongoMatch.Plugins/SystemDashboards.cs
@@ -0,0 +1,50 @@
+//
+//  Copyright (C) 2014 Andoni Morales Alastruey
+//
+//  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 LongoMatch.Addins.ExtensionPoints;
+using LongoMatch.Core.Store.Templates;
+using System.Collections.Generic;
+using Mono.Addins;
+using Mono.Unix;
+
+namespace LongoMatch.Plugins
+{
+       [Extension]
+       public class SystemDashboards: IAnalsysDashboardsProvider
+       {
+               public string Name {
+                       get {
+                               return Catalog.GetString ("LongoMatch default dashboard");
+                       }
+               }
+
+               public string Description {
+                       get {
+                               return Catalog.GetString ("LongoMatch default dashboard");
+                       }
+               }
+
+               public List<Dashboard> Dashboards {
+                       get {
+                               Dashboard d = Dashboard.DefaultTemplate (14);
+                               d.Name = "Default";
+                               return new List<Dashboard> {d}; 
+                       }
+               }
+       }
+}
diff --git a/LongoMatch.Services/Services/TemplatesService.cs 
b/LongoMatch.Services/Services/TemplatesService.cs
index cc8de5e..91863fe 100644
--- a/LongoMatch.Services/Services/TemplatesService.cs
+++ b/LongoMatch.Services/Services/TemplatesService.cs
@@ -18,6 +18,7 @@
 using System;
 using System.Collections.Generic;
 using System.IO;
+using System.Linq;
 using System.Reflection;
 using LongoMatch.Core.Common;
 using LongoMatch.Core.Interfaces;
@@ -72,6 +73,7 @@ namespace LongoMatch.Services
                readonly string extension;
                readonly MethodInfo methodLoad;
                readonly MethodInfo methodDefaultTemplate;
+               List<T> systemTemplates;
 
                public TemplatesProvider (string basePath, string extension)
                {
@@ -79,6 +81,7 @@ namespace LongoMatch.Services
                        this.extension = extension;
                        methodLoad = typeof(T).GetMethod ("Load");
                        methodDefaultTemplate = typeof(T).GetMethod ("DefaultTemplate");
+                       systemTemplates = new List<T>();
                }
 
                private string GetPath (string templateName)
@@ -86,7 +89,7 @@ namespace LongoMatch.Services
                        return System.IO.Path.Combine (basePath, templateName) + extension;
                }
 
-               public void CheckDefaultTemplate ()
+               public virtual void CheckDefaultTemplate ()
                {
                        string path;
                        
@@ -122,16 +125,23 @@ namespace LongoMatch.Services
                                foreach (string path in Directory.GetFiles (basePath, "*" + extension)) {
                                        l.Add (Path.GetFileNameWithoutExtension (path));
                                }
-                               return l;
+                               return l.Concat (systemTemplates.Select (t => t.Name)).ToList ();
                        }
                }
 
                public T Load (string name)
                {
-                       Log.Information ("Loading template " + name);
-                       var template = (T)methodLoad.Invoke (null, new object[] { GetPath(name) });
-                       template.Name = name;
-                       return template;
+                       T template;
+                       
+                       template = systemTemplates.FirstOrDefault (t => t.Name == name);
+                       if (template != null) {
+                               return template;
+                       } else {
+                               Log.Information ("Loading template " + name);
+                               template = (T)methodLoad.Invoke (null, new object[] { GetPath(name) });
+                               template.Name = name;
+                               return template;
+                       }
                }
 
                public void Save (ITemplate template)
@@ -162,6 +172,10 @@ namespace LongoMatch.Services
                        template.Save (filename);
                }
 
+               public void Register (T template) {
+                       systemTemplates.Add (template);
+               }
+
                public void Copy (string orig, string copy)
                {
                        if (File.Exists (copy)) {
@@ -208,5 +222,10 @@ namespace LongoMatch.Services
                public CategoriesTemplatesProvider (string basePath): base (basePath, 
Constants.CAT_TEMPLATE_EXT)
                {
                }
+
+               public override void CheckDefaultTemplate ()
+               {
+                       /* Do nothing now that we have a plugin that creates the default template */
+               }
        }
 }
diff --git a/LongoMatch/LongoMatch.csproj b/LongoMatch/LongoMatch.csproj
index ded7c98..10367f1 100644
--- a/LongoMatch/LongoMatch.csproj
+++ b/LongoMatch/LongoMatch.csproj
@@ -79,6 +79,14 @@
       <Project>{AE98609B-353C-4CE4-A5B7-606BB4EE3576}</Project>
       <Name>LongoMatch.Drawing.Cairo</Name>
     </ProjectReference>
+    <ProjectReference Include="..\LongoMatch.Plugins\LongoMatch.Plugins.csproj">
+      <Project>{1F3FE3B0-5EF5-4420-BE2D-139B69AF0211}</Project>
+      <Name>LongoMatch.Plugins</Name>
+    </ProjectReference>
+    <ProjectReference Include="..\LongoMatch.Plugins.GStreamer\LongoMatch.Plugins.GStreamer.csproj">
+      <Project>{92BF45E5-4F84-48FB-B3F0-BB8878B6137B}</Project>
+      <Name>LongoMatch.Plugins.GStreamer</Name>
+    </ProjectReference>
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
 </Project>
\ No newline at end of file
diff --git a/LongoMatch/Main.cs b/LongoMatch/Main.cs
index 3c0e7cb..351d0f9 100644
--- a/LongoMatch/Main.cs
+++ b/LongoMatch/Main.cs
@@ -71,6 +71,7 @@ namespace LongoMatch
                                        Log.Exception (locked);
                                        return;
                                }
+                               manager.LoadDashboards (Config.CategoriesTemplatesProvider);
                                manager.LoadImportProjectAddins (CoreServices.ProjectsImporter);
                                Application.Run ();
                        } catch (Exception ex) {


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