[longomatch] Refactor project import addins support



commit abec10c063bb2a15fc096b3929278bd593a50afd
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Mon Jul 7 12:41:18 2014 +0200

    Refactor project import addins support

 LongoMatch.Addins/AddinsManager.cs                 |   19 ++--
 .../ExtensionPoints/IImportProject.cs              |    7 +-
 LongoMatch.Core/Common/EventsBroker.cs             |   12 ++-
 LongoMatch.Core/Common/ExtensionMethods.cs         |   10 ++
 LongoMatch.Core/Handlers/Handlers.cs               |    4 +-
 LongoMatch.Core/Interfaces/GUI/IGUIToolkit.cs      |    2 +-
 LongoMatch.Core/Interfaces/GUI/IMainController.cs  |    3 -
 LongoMatch.Core/LongoMatch.Core.mdp                |    2 +
 LongoMatch.Core/Utils.cs                           |    2 +-
 LongoMatch.GUI/Gui/GUIToolkit.cs                   |    2 +-
 LongoMatch.GUI/Gui/MainWindow.cs                   |   21 +---
 LongoMatch.GUI/Gui/Panel/WelcomePanel.cs           |    4 +-
 .../gtk-gui/LongoMatch.Gui.MainWindow.cs           |    2 +-
 LongoMatch.GUI/gtk-gui/gui.stetic                  |    2 +-
 LongoMatch.Services/LongoMatch.Services.mdp        |    2 +
 LongoMatch.Services/Services/Core.cs               |    2 +
 LongoMatch.Services/Services/ToolsManager.cs       |  121 +++++++++++++++-----
 LongoMatch/Main.cs                                 |    2 +-
 18 files changed, 145 insertions(+), 74 deletions(-)
---
diff --git a/LongoMatch.Addins/AddinsManager.cs b/LongoMatch.Addins/AddinsManager.cs
index 3129de8..20f4111 100644
--- a/LongoMatch.Addins/AddinsManager.cs
+++ b/LongoMatch.Addins/AddinsManager.cs
@@ -24,13 +24,15 @@ using LongoMatch.Addins.ExtensionPoints;
 using LongoMatch.Interfaces.GUI;
 using LongoMatch.Store;
 using LongoMatch.Interfaces.Multimedia;
+using System.Collections.Generic;
+using LongoMatch.Interfaces;
 
 [assembly:AddinRoot ("LongoMatch", "1.0")]
 
 namespace LongoMatch.Addins
 {
-       public class AddinsManager
-       {
+       public class AddinsManager {
+       
                public AddinsManager (string configPath, string searchPath)
                {
                        Log.Information("Initializing addins at path: " + searchPath);
@@ -61,15 +63,12 @@ namespace LongoMatch.Addins
                        }
                }
                
-               public void LoadImportProjectAddins(IMainController mainWindow) {
+               public void LoadImportProjectAddins(IProjectsImporter importer) {
                        foreach (IImportProject importProject in 
AddinManager.GetExtensionObjects<IImportProject> ()) {
-                               try{
-                                       mainWindow.AddImportEntry(importProject.GetMenuEntryName(), 
importProject.GetMenuEntryShortName(),
-                                                                 importProject.GetFilterName(), 
importProject.GetFilter(), importProject.ImportProject, true);
-                               } catch (Exception ex) {
-                                       Log.Error ("Error adding import entry");
-                                       Log.Exception (ex);
-                               }
+                               importer.RegisterImporter (new Func<string, Project> 
(importProject.ImportProject),
+                                                          importProject.FilterName,
+                                                          importProject.FilterExtensions,
+                                                          importProject.NeedsEdition);
                        }
                }
                
diff --git a/LongoMatch.Addins/ExtensionPoints/IImportProject.cs 
b/LongoMatch.Addins/ExtensionPoints/IImportProject.cs
index 7671140..606dbb2 100644
--- a/LongoMatch.Addins/ExtensionPoints/IImportProject.cs
+++ b/LongoMatch.Addins/ExtensionPoints/IImportProject.cs
@@ -26,10 +26,9 @@ namespace LongoMatch.Addins.ExtensionPoints
        [TypeExtensionPoint]
        public interface IImportProject
        {
-               string GetMenuEntryName();
-               string GetMenuEntryShortName();
-               string GetFilterName();
-               string GetFilter();
+               string FilterName {get;}
+               string[] FilterExtensions {get;}
+               bool NeedsEdition {get;}
                Project ImportProject(string filename);
        }
 }
diff --git a/LongoMatch.Core/Common/EventsBroker.cs b/LongoMatch.Core/Common/EventsBroker.cs
index 11ff874..6ee7bbb 100644
--- a/LongoMatch.Core/Common/EventsBroker.cs
+++ b/LongoMatch.Core/Common/EventsBroker.cs
@@ -77,6 +77,7 @@ namespace LongoMatch.Common
                public event ConvertVideoFilesHandler ConvertVideoFilesEvent;
                
                public event OpenedProjectChangedHandler OpenedProjectChanged;
+               public event CreateThumbnailsHandler CreateThumbnailsEvent;
                
                /* Player and Capturer */
                public event TickHandler Tick;
@@ -263,10 +264,9 @@ namespace LongoMatch.Common
                                ManageCategoriesEvent();
                }
                
-               public void EmitImportProject(string name, string filterName, string filter,
-                                              Func<string, Project> func, bool requiresNewFile) {
+               public void EmitImportProject () {
                        if (ImportProjectEvent != null)
-                               ImportProjectEvent(name, filterName, filter, func, requiresNewFile);
+                               ImportProjectEvent ();
                }
                
                public void EmitExportProject (Project project) {
@@ -375,6 +375,12 @@ namespace LongoMatch.Common
                                PlaybackRateChanged (val);
                        }
                }
+               
+               public void EmitCreateThumbnails (Project project) {
+                       if (CreateThumbnailsEvent != null) {
+                               CreateThumbnailsEvent (project);
+                       }
+               }
        }
 }
 
diff --git a/LongoMatch.Core/Common/ExtensionMethods.cs b/LongoMatch.Core/Common/ExtensionMethods.cs
index f1f9fc4..cbbb0ac 100644
--- a/LongoMatch.Core/Common/ExtensionMethods.cs
+++ b/LongoMatch.Core/Common/ExtensionMethods.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;
 
 namespace LongoMatch.Common
@@ -32,5 +33,14 @@ namespace LongoMatch.Common
                        list[index1] = list[index2];
                        list[index2] = temp;
                }
+               
+               public static T[] Merge<T>(this List<T[]> list) {
+                       var res = new List<T>();
+                       
+                       foreach (T[] t in list) {
+                               res.AddRange (t);
+                       }
+                       return res.ToArray ();
+               }
        }
 }
diff --git a/LongoMatch.Core/Handlers/Handlers.cs b/LongoMatch.Core/Handlers/Handlers.cs
index ca8f283..282f929 100644
--- a/LongoMatch.Core/Handlers/Handlers.cs
+++ b/LongoMatch.Core/Handlers/Handlers.cs
@@ -65,10 +65,10 @@ namespace LongoMatch.Handlers
        public delegate void CloseOpenendProjectHandler();
        public delegate void NewProjectHandler();
        public delegate void OpenNewProjectHandler(Project project, ProjectType projectType, CaptureSettings 
captureSettings);
-       public delegate void ImportProjectHandler(string name, string filterName, string filter,
-                                                 Func <string, Project> importProjectFunc, bool 
requiresNewFile);
+       public delegate void ImportProjectHandler ();
        public delegate void ExportProjectHandler (Project project);
        public delegate void QuitApplicationHandler ();
+       public delegate void CreateThumbnailsHandler (Project project);
        
        /* GUI */
        public delegate void ManageJobsHandler();
diff --git a/LongoMatch.Core/Interfaces/GUI/IGUIToolkit.cs b/LongoMatch.Core/Interfaces/GUI/IGUIToolkit.cs
index 6d61c44..ff121fc 100644
--- a/LongoMatch.Core/Interfaces/GUI/IGUIToolkit.cs
+++ b/LongoMatch.Core/Interfaces/GUI/IGUIToolkit.cs
@@ -47,7 +47,7 @@ namespace LongoMatch.Interfaces.GUI
                string SaveFile(string title, string defaultName, string defaultFolder,
                        string filterName, string[] extensionFilter);
                string OpenFile(string title, string defaultName, string defaultFolder,
-                       string filterName, string[] extensionFilter);
+                       string filterName = null, string[] extensionFilter = null);
                List<string> OpenFiles(string title, string defaultName, string defaultFolder,
                        string filterName, string[] extensionFilter);
                string SelectFolder(string title, string defaultName, string defaultFolder,
diff --git a/LongoMatch.Core/Interfaces/GUI/IMainController.cs 
b/LongoMatch.Core/Interfaces/GUI/IMainController.cs
index d2cd23b..8ce8942 100644
--- a/LongoMatch.Core/Interfaces/GUI/IMainController.cs
+++ b/LongoMatch.Core/Interfaces/GUI/IMainController.cs
@@ -24,8 +24,5 @@ namespace LongoMatch.Interfaces.GUI
        public interface IMainController
        {
                void AddExportEntry (string name, string shortName, Action<Project, IGUIToolkit> 
exportAction);
-               void AddImportEntry (string name, string shortName, string filterName,
-                                    string filter, Func<string, Project> importFunc,
-                                    bool requiresNewFile);
        }
 }
\ No newline at end of file
diff --git a/LongoMatch.Core/LongoMatch.Core.mdp b/LongoMatch.Core/LongoMatch.Core.mdp
index 2454bef..f5d45f4 100644
--- a/LongoMatch.Core/LongoMatch.Core.mdp
+++ b/LongoMatch.Core/LongoMatch.Core.mdp
@@ -137,6 +137,8 @@
     <File subtype="Directory" buildaction="Compile" name="Store/Drawables" />
     <File subtype="Code" buildaction="Compile" name="Store/Drawables/Text.cs" />
     <File subtype="Code" buildaction="Compile" name="Store/Drawables/Counter.cs" />
+    <File subtype="Directory" buildaction="Compile" name="Interfaces" />
+    <File subtype="Code" buildaction="Compile" name="Interfaces/IProjectsImporter.cs" />
   </Contents>
   <References>
     <ProjectReference type="Package" localcopy="True" refto="System, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089" />
diff --git a/LongoMatch.Core/Utils.cs b/LongoMatch.Core/Utils.cs
index 836d436..11a6ef3 100644
--- a/LongoMatch.Core/Utils.cs
+++ b/LongoMatch.Core/Utils.cs
@@ -34,7 +34,7 @@ namespace LongoMatch.Utils
                        
                        
                        folder = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
-                       filename = gui.OpenFile (Catalog.GetString("Open file"), null, folder, null, null);   
                          
+                       filename = gui.OpenFile (Catalog.GetString("Open file"), null, folder);               
              
                        if (filename == null)
                                        return null;
                        
diff --git a/LongoMatch.GUI/Gui/GUIToolkit.cs b/LongoMatch.GUI/Gui/GUIToolkit.cs
index 27bfe16..6d17248 100644
--- a/LongoMatch.GUI/Gui/GUIToolkit.cs
+++ b/LongoMatch.GUI/Gui/GUIToolkit.cs
@@ -114,7 +114,7 @@ namespace LongoMatch.Gui
                }
                
                public string OpenFile(string title, string defaultName, string defaultFolder,
-                       string filterName, string[] extensionFilter)
+                       string filterName = null, string[] extensionFilter = null)
                {
                        return FileChooserHelper.OpenFile (mainWindow as Widget, title, defaultName,
                                                           defaultFolder, filterName, extensionFilter);
diff --git a/LongoMatch.GUI/Gui/MainWindow.cs b/LongoMatch.GUI/Gui/MainWindow.cs
index 49de6bd..253e908 100644
--- a/LongoMatch.GUI/Gui/MainWindow.cs
+++ b/LongoMatch.GUI/Gui/MainWindow.cs
@@ -68,12 +68,6 @@ namespace LongoMatch.Gui
                        ConnectSignals();
                        ConnectMenuSignals();
                        
-                       MenuItem parent = ImportProjectActionMenu;
-                       parent.Submenu = new Menu();
-                       AddImportEntry(Catalog.GetString("Import file project"), "ImportFileProject",
-                                      Constants.PROJECT_NAME + " (" + Constants.PROJECT_EXT + ")",
-                                      "*" + Constants.PROJECT_EXT, Project.Import,
-                                      false);
                        screen = Display.Default.DefaultScreen;
                        this.Resize(screen.Width * 80 / 100, screen.Height * 80 / 100);
                }
@@ -111,18 +105,6 @@ namespace LongoMatch.Gui
                        (parent.Submenu as Menu).Append(item);
                }
                
-               public void AddImportEntry (string name, string shortName, string filterName,
-                                           string filter, Func<string, Project> importFunc,
-                                           bool requiresNewFile) {
-                       MenuItem parent = ImportProjectActionMenu;
-                       MenuItem item = new MenuItem(name);
-                       item.Activated += (sender, e) => (
-                               Config.EventsBroker.EmitImportProject (name, filterName, filter,
-                                                              importFunc, requiresNewFile));
-                       item.Show();
-                       (parent.Submenu as Menu).Append(item);
-               }
-               
                public IAnalysisWindow SetProject(Project project, ProjectType projectType, CaptureSettings 
props, PlaysFilter filter)
                {
                        ExportProjectAction1.Sensitive = true;
@@ -206,6 +188,9 @@ namespace LongoMatch.Gui
                        TagSubcategoriesAction.Activated += (sender, e) => {
                                Config.EventsBroker.EmitTagSubcategories (TagSubcategoriesAction.Active);
                        };
+                       ImportProjectAction.Activated += (sender, e) => {
+                               Config.EventsBroker.EmitImportProject ();
+                       };
                }
                
                void RemovePanel () {
diff --git a/LongoMatch.GUI/Gui/Panel/WelcomePanel.cs b/LongoMatch.GUI/Gui/Panel/WelcomePanel.cs
index 375501f..55db4e4 100644
--- a/LongoMatch.GUI/Gui/Panel/WelcomePanel.cs
+++ b/LongoMatch.GUI/Gui/Panel/WelcomePanel.cs
@@ -36,7 +36,7 @@ namespace LongoMatch.Gui.Panel
                        openbutton.Clicked += (sender, e) => {
                                Config.EventsBroker.EmitOpenProject ();};
                        newbutton.Clicked += (sender, e) => {
-                               Config.EventsBroker.EmitNewProject ();};
+                               Config.EventsBroker.EmitNewProject (null);};
                        teamsbutton.Clicked += (sender, e) => {
                                Config.EventsBroker.EmitManageTeams ();};
                        sportsbutton.Clicked += (sender, e) => {
@@ -45,6 +45,8 @@ namespace LongoMatch.Gui.Panel
                                Config.EventsBroker.EmitEditPreferences ();};
                    projectsbutton.Clicked += (sender, e) =>  {
                                Config.EventsBroker.EmitManageProjects ();};
+                       importbutton.Clicked += (sender, e) =>  {
+                               Config.EventsBroker.EmitImportProject ();};
                }
        }
 }
diff --git a/LongoMatch.GUI/gtk-gui/LongoMatch.Gui.MainWindow.cs 
b/LongoMatch.GUI/gtk-gui/LongoMatch.Gui.MainWindow.cs
index cb9ba3f..66be6cd 100644
--- a/LongoMatch.GUI/gtk-gui/LongoMatch.Gui.MainWindow.cs
+++ b/LongoMatch.GUI/gtk-gui/LongoMatch.Gui.MainWindow.cs
@@ -153,7 +153,7 @@ namespace LongoMatch.Gui
                        this.menubox.Name = "menubox";
                        this.menubox.Spacing = 6;
                        // Container child menubox.Gtk.Box+BoxChild
-                       this.UIManager.AddUiFromString ("<ui><menubar name='menubar1'><menu name='FileAction' 
action='FileAction'><menuitem name='NewPojectAction' action='NewPojectAction'/><menuitem name='openAction' 
action='openAction'/><menuitem name='SaveProjectAction' action='SaveProjectAction'/><menuitem 
name='CloseProjectAction' action='CloseProjectAction'/><separator/><menu name='ImportProjectAction' 
action='ImportProjectAction'/><separator/><menuitem name='PreferencesAction' 
action='PreferencesAction'/><separator/><menuitem name='QuitAction' action='QuitAction'/></menu><menu 
name='ToolsAction' action='ToolsAction'><menuitem name='ProjectsManagerAction' 
action='ProjectsManagerAction'/><menuitem name='CategoriesTemplatesManagerAction' 
action='CategoriesTemplatesManagerAction'/><menuitem name='TeamsTemplatesManagerAction' 
action='TeamsTemplatesManagerAction'/><menuitem name='DatabasesManagerAction' 
action='DatabasesManagerAction'/><separator/><menuitem name='ShowProjectStatsAction' action='
 ShowProjectStatsAction'/><menu name='ExportProjectAction1' action='ExportProjectAction1'><menuitem 
name='ExportToProjectFileAction' action='ExportToProjectFileAction'/></menu><separator/><menuitem 
name='VideoConverterToolAction' action='VideoConverterToolAction'/></menu><menu name='ViewAction' 
action='ViewAction'><menuitem name='FullScreenAction' action='FullScreenAction'/><separator/><menuitem 
name='TagSubcategoriesAction' action='TagSubcategoriesAction'/></menu><menu name='HelpAction' 
action='HelpAction'><menuitem name='AboutAction' action='AboutAction'/><menuitem name='HelpAction1' 
action='HelpAction1'/><menuitem name='dialogInfoAction' action='dialogInfoAction'/></menu></menubar></ui>");
+                       this.UIManager.AddUiFromString ("<ui><menubar name='menubar1'><menu name='FileAction' 
action='FileAction'><menuitem name='NewPojectAction' action='NewPojectAction'/><menuitem name='openAction' 
action='openAction'/><menuitem name='SaveProjectAction' action='SaveProjectAction'/><menuitem 
name='CloseProjectAction' action='CloseProjectAction'/><separator/><menuitem name='ImportProjectAction' 
action='ImportProjectAction'/><separator/><menuitem name='PreferencesAction' 
action='PreferencesAction'/><separator/><menuitem name='QuitAction' action='QuitAction'/></menu><menu 
name='ToolsAction' action='ToolsAction'><menuitem name='ProjectsManagerAction' 
action='ProjectsManagerAction'/><menuitem name='CategoriesTemplatesManagerAction' 
action='CategoriesTemplatesManagerAction'/><menuitem name='TeamsTemplatesManagerAction' 
action='TeamsTemplatesManagerAction'/><menuitem name='DatabasesManagerAction' 
action='DatabasesManagerAction'/><separator/><menuitem name='ShowProjectStatsAction' acti
 on='ShowProjectStatsAction'/><menu name='ExportProjectAction1' action='ExportProjectAction1'><menuitem 
name='ExportToProjectFileAction' action='ExportToProjectFileAction'/></menu><separator/><menuitem 
name='VideoConverterToolAction' action='VideoConverterToolAction'/></menu><menu name='ViewAction' 
action='ViewAction'><menuitem name='FullScreenAction' action='FullScreenAction'/><separator/><menuitem 
name='TagSubcategoriesAction' action='TagSubcategoriesAction'/></menu><menu name='HelpAction' 
action='HelpAction'><menuitem name='AboutAction' action='AboutAction'/><menuitem name='HelpAction1' 
action='HelpAction1'/><menuitem name='dialogInfoAction' action='dialogInfoAction'/></menu></menubar></ui>");
                        this.menubar1 = ((global::Gtk.MenuBar)(this.UIManager.GetWidget ("/menubar1")));
                        this.menubar1.Name = "menubar1";
                        this.menubox.Add (this.menubar1);
diff --git a/LongoMatch.GUI/gtk-gui/gui.stetic b/LongoMatch.GUI/gtk-gui/gui.stetic
index 7edd81f..fe04e85 100644
--- a/LongoMatch.GUI/gtk-gui/gui.stetic
+++ b/LongoMatch.GUI/gtk-gui/gui.stetic
@@ -370,7 +370,7 @@
                     <node type="Menuitem" action="SaveProjectAction" />
                     <node type="Menuitem" action="CloseProjectAction" />
                     <node type="Separator" />
-                    <node type="Menu" action="ImportProjectAction" />
+                    <node type="Menuitem" action="ImportProjectAction" />
                     <node type="Separator" />
                     <node type="Menuitem" action="PreferencesAction" />
                     <node type="Separator" />
diff --git a/LongoMatch.Services/LongoMatch.Services.mdp b/LongoMatch.Services/LongoMatch.Services.mdp
index 10fae6b..5b42483 100644
--- a/LongoMatch.Services/LongoMatch.Services.mdp
+++ b/LongoMatch.Services/LongoMatch.Services.mdp
@@ -29,6 +29,7 @@
     <File subtype="Code" buildaction="Compile" name="Services/DataBaseManager.cs" />
     <File subtype="Code" buildaction="Compile" name="Services/ToolsManager.cs" />
     <File subtype="Code" buildaction="Compile" name="Services/FileDB.cs" />
+    <File subtype="Directory" buildaction="Compile" name="Services" />
   </Contents>
   <References>
     <ProjectReference type="Package" localcopy="True" refto="System, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089" />
@@ -38,5 +39,6 @@
     <ProjectReference type="Package" specificVersion="False" localcopy="False" refto="System.Core, 
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
     <ProjectReference type="Package" localcopy="False" refto="gdk-sharp, Version=2.12.0.0, Culture=neutral, 
PublicKeyToken=35e10195dab3c99f" />
     <ProjectReference type="Project" localcopy="True" refto="LongoMatch.Drawing" />
+    <ProjectReference type="Project" localcopy="True" refto="LongoMatch.Addins" />
   </References>
 </Project>
\ No newline at end of file
diff --git a/LongoMatch.Services/Services/Core.cs b/LongoMatch.Services/Services/Core.cs
index 9e2c245..1f1983f 100644
--- a/LongoMatch.Services/Services/Core.cs
+++ b/LongoMatch.Services/Services/Core.cs
@@ -39,6 +39,7 @@ namespace LongoMatch.Services
                static EventsManager eManager;
                static HotKeysManager hkManager;
                static RenderingJobsManager videoRenderer;
+               public static IProjectsImporter ProjectsImporter;
 #if OSTYPE_WINDOWS
                [DllImport("libglib-2.0-0.dll") /* willfully unmapped */ ]
                static extern void g_setenv (String env, String val);
@@ -99,6 +100,7 @@ namespace LongoMatch.Services
                        
                        /* State the tools manager */
                        toolsManager = new ToolsManager (guiToolkit, multimediaToolkit, ts);
+                       ProjectsImporter = toolsManager;
                        
                        /* Start the events manager */
                        eManager = new EventsManager (guiToolkit, videoRenderer);
diff --git a/LongoMatch.Services/Services/ToolsManager.cs b/LongoMatch.Services/Services/ToolsManager.cs
index 4ad07ce..35921ff 100644
--- a/LongoMatch.Services/Services/ToolsManager.cs
+++ b/LongoMatch.Services/Services/ToolsManager.cs
@@ -16,6 +16,7 @@
 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 //
 using System;
+using System.Linq;
 using Mono.Unix;
 
 using LongoMatch.Interfaces.GUI;
@@ -23,16 +24,20 @@ using LongoMatch.Interfaces.Multimedia;
 using LongoMatch.Store;
 using LongoMatch.Common;
 using LongoMatch.Interfaces;
+using LongoMatch.Addins;
+using System.Collections.Generic;
+using System.IO;
 
 namespace LongoMatch.Services {
 
-       public class ToolsManager
+       public class ToolsManager: IProjectsImporter 
        {
                
                TemplatesService templatesService;
                Project openedProject;
                IGUIToolkit guiToolkit;
                IMultimediaToolkit multimediaToolkit;
+               AddinsManager addinsManager;
                
                public ToolsManager (IGUIToolkit guiToolkit, IMultimediaToolkit multimediaToolkit,
                                     TemplatesService templatesService)
@@ -40,6 +45,11 @@ namespace LongoMatch.Services {
                        this.guiToolkit = guiToolkit;
                        this.multimediaToolkit = multimediaToolkit;
                        this.templatesService = templatesService;
+                       ProjectImporters = new List<ProjectImporter> ();
+                       
+                       RegisterImporter (Project.Import, Constants.PROJECT_NAME,
+                                         new string[] {Constants.PROJECT_EXT}, false);
+
                        Config.EventsBroker.OpenedProjectChanged += (pr, pt, f, a) => {
                                this.openedProject = pr;
                        };
@@ -60,10 +70,22 @@ namespace LongoMatch.Services {
                                guiToolkit.OpenProjectsManager(this.openedProject);
                        };
                        
-                       Config.EventsBroker.ImportProjectEvent += ImportProject;
                        Config.EventsBroker.ExportProjectEvent += ExportProject;
+                       Config.EventsBroker.ImportProjectEvent += ImportProject;
+                       Config.EventsBroker.CreateThumbnailsEvent += CreateThumbnails;
                }
 
+               public void RegisterImporter (Func<string, Project> importFunction,
+                                             string filterName, string[] extensions, 
+                                             bool needsEdition) {
+                       ProjectImporter importer = new ProjectImporter {
+                               ImportFunction=importFunction,
+                               FilterName=filterName,
+                               Extensions=extensions,
+                               NeedsEdition=needsEdition};
+                       ProjectImporters.Add (importer);
+               }
+               
                public static void AddVideoFile (Project project)
                {
                        string videofile;
@@ -73,7 +95,7 @@ namespace LongoMatch.Services {
                        guiToolkit.InfoMessage (Catalog.GetString ("This project doesn't have any file 
associated.\n" +
                                                                   "Select one in the next window"));
                        videofile = guiToolkit.OpenFile (Catalog.GetString ("Select a video file"), null,
-                                                        Config.HomeDir, null, null);
+                                                        Config.HomeDir);
                        if (videofile == null) {
                                guiToolkit.ErrorMessage (Catalog.GetString ("Could not import project, you 
need a video file"));
                                return;
@@ -86,18 +108,20 @@ namespace LongoMatch.Services {
                                        guiToolkit.ErrorMessage (ex.Message);
                                        return;
                                }
-                               CreateThumbnails (project, guiToolkit, multimediaToolkit.GetFramesCapturer 
());
+                               CreateThumbnails (project);
                        }
                }
                
-               public static void CreateThumbnails(Project project, IGUIToolkit guiToolkit, IFramesCapturer 
capturer) {
+               public static void CreateThumbnails(Project project) {
                        IBusyDialog dialog;
+                       IFramesCapturer capturer;
 
-                       dialog = guiToolkit.BusyDialog(Catalog.GetString("Creating video thumbnails. This can 
take a while."));
+                       dialog = Config.GUIToolkit.BusyDialog(Catalog.GetString("Creating video thumbnails. 
This can take a while."));
                        dialog.Show();
                        dialog.Pulse();
 
                        /* Create all the thumbnails */
+                       capturer = Config.MultimediaToolkit.GetFramesCapturer ();
                        capturer.Open(project.Description.File.FilePath);
                        foreach(Play play in project.Timeline) {
                                try {
@@ -116,6 +140,11 @@ namespace LongoMatch.Services {
                        dialog.Destroy();
                }
 
+               List<ProjectImporter> ProjectImporters {
+                       get;
+                       set;
+               } 
+
                void ExportProject (Project project) {
                        if (project == null) {
                                Log.Warning("Opened project is null and can't be exported");
@@ -138,16 +167,20 @@ namespace LongoMatch.Services {
                        }
                }
                
-               private void ImportProject(string name, string filterName, string filter,
-                                          Func<string, Project> importProject, bool requiresNewFile) {
+               void ImportProject () {
                        Project project;
-                       string fileName;
+                       ProjectImporter importer;
+                       string fileName, filterName;
+                       string[] extensions;
                        IDatabase DB = Config.DatabaseManager.ActiveDB;
-
+                       
+                       
                        Log.Debug("Importing project");
+                       filterName = String.Join ("\n", ProjectImporters.Select (p => p.FilterName));
+                       extensions =ExtensionMethods.Merge (ProjectImporters.Select (p => 
p.Extensions).ToList()); 
                        /* Show a file chooser dialog to select the file to import */
-                       fileName = guiToolkit.OpenFile(name, null, Config.HomeDir, filterName,
-                                                      new string[] {filter});
+                       fileName = guiToolkit.OpenFile (Catalog.GetString ("Import project"), null, 
Config.HomeDir,
+                                                       filterName, extensions);
                                
                        if(fileName == null)
                                return;
@@ -155,7 +188,13 @@ namespace LongoMatch.Services {
                        /* try to import the project and show a message error is the file
                         * is not a valid project */
                        try {
-                               project = importProject (fileName);
+                               string extension = "*" + Path.GetExtension (fileName);
+                               importer = ProjectImporters.Where (p => p.Extensions.Contains 
(extension)).FirstOrDefault ();
+                               if (importer != null) {
+                                       project = importer.ImportFunction (fileName);
+                               } else {
+                                       throw new Exception (Catalog.GetString ("Plugin not found"));
+                               }
                        }
                        catch(Exception ex) {
                                guiToolkit.ErrorMessage (Catalog.GetString ("Error importing project:") +
@@ -164,24 +203,52 @@ namespace LongoMatch.Services {
                                return;
                        }
 
-                       if (requiresNewFile) {
-                               AddVideoFile (project);
-                       }
-                       
-                       /* If the project exists ask if we want to overwrite it */
-                       if (DB.Exists (project)) {
-                               var res = guiToolkit.QuestionMessage (Catalog.GetString ("A project already 
exists for the file:") +
-                                                                     project.Description.File.FilePath+ "\n" 
+
-                                                                     Catalog.GetString ("Do you want to 
overwrite it?"), null);
-                               if(!res)
-                                       return;
-                               DB.UpdateProject(project);
+                       if (importer.NeedsEdition) {
+                               Config.EventsBroker.EmitNewProject (project);
                        } else {
-                               DB.AddProject(project);
+                               if (project.Description.File == null ||
+                                   !File.Exists (project.Description.File.FilePath)) {
+                                       AddVideoFile (project);
+                               }
+                               /* If the project exists ask if we want to overwrite it */
+                               if (DB.Exists (project)) {
+                                       var res = guiToolkit.QuestionMessage (Catalog.GetString ("A project 
already exists for the file:") +
+                                                                             
project.Description.File.FilePath+ "\n" +
+                                                                             Catalog.GetString ("Do you want 
to overwrite it?"), null);
+                                       if(!res)
+                                               return;
+                                       DB.UpdateProject(project);
+                               } else {
+                                       DB.AddProject (project);
+                               }
+                               Config.EventsBroker.EmitOpenProjectID (project.ID);
                        }
-                       guiToolkit.InfoMessage(Catalog.GetString("Project successfully imported."));
                }
                
        }
+       
+       public class ProjectImporter
+       {
+               public Func<string, Project> ImportFunction {
+                       get;
+                       set;
+               }
+               
+               public string [] Extensions {
+                       get;
+                       set;
+               }
+               
+               public string FilterName {
+                       get;
+                       set;
+               }
+               
+               public bool NeedsEdition {
+                       get;
+                       set;
+               }
+       
+       }
 }
 
diff --git a/LongoMatch/Main.cs b/LongoMatch/Main.cs
index 0203d9e..f598894 100644
--- a/LongoMatch/Main.cs
+++ b/LongoMatch/Main.cs
@@ -68,7 +68,6 @@ namespace LongoMatch
                            Config.MultimediaToolkit = multimediaToolkit;
                            GUIToolkit guiToolkit = new GUIToolkit(version);
                            manager.LoadExportProjectAddins(guiToolkit.MainController);
-                           manager.LoadImportProjectAddins(guiToolkit.MainController);
                            manager.LoadMultimediaBackendsAddins(multimediaToolkit);
                            try {
                                        Core.Start(guiToolkit, multimediaToolkit);
@@ -79,6 +78,7 @@ namespace LongoMatch
                                        Log.Exception (locked);
                                        return;
                            }
+                           manager.LoadImportProjectAddins(Core.ProjectsImporter);
                                Application.Run();
                        } catch(Exception ex) {
                                ProcessExecutionError(ex);


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