public string[] GetUnsavedItems(Plugin plugin)
 {
     try
     {
         var service = plugin.GetService<IUnsavedData>();
         if (service == null) return null;
         return service.GetNamesOfUnsavedItems();
     }
     catch (Exception ex)
     {
         _errorHandlingService.LogError("Error getting unsaved data from " + plugin.Title, ex);
         return null;
     }
 }
 public PluginErrorEventArgs(Plugin plugin, string message, Exception exception)
 {
     Plugin = plugin;
     Message = message;
     Exception = exception;
 }
        private void DisposePlugin(Plugin plugin)
        {
            if (plugin == null) return;
            plugin.Error -= OnPluginError;

            try
            {
                plugin.Dispose();
            }
            catch (Exception ex)
            {
                _errorHandlingService.LogError("Error disposing plugin " + plugin.Title, ex);
            }
        }
 public void RemovePlugin(Plugin plugin)
 {
     LoadedPlugins.Remove(plugin);
     DisposePlugin(plugin);
 }
        private bool CanClose(Plugin plugin)
        {
            var unsavedItems = _pluginController.GetUnsavedItems(plugin);
            if (unsavedItems == null || unsavedItems.Length == 0) return true;

            var message = "The following items are not saved:\r\n" +
                String.Join("\r\n", unsavedItems) + "\r\n\r\n" +
                "Are you sure you want to close " + plugin.Title + "?";

            return _errorHandlingService.Confirm(message);
        }
        private void CloseTab(Plugin plugin)
        {
            if (!CanClose(plugin)) return;

            bool changeSelection = (plugin == SelectedPlugin);
            int selectedIndex = LoadedPlugins.IndexOf(plugin);
                
            _pluginController.RemovePlugin(plugin);

            if (changeSelection)
            {
                int count = LoadedPlugins.Count;

                if (count == 0)
                {
                    SelectedPlugin = null;
                }
                else
                {
                    if (selectedIndex >= count) selectedIndex = count - 1;
                    SelectedPlugin = LoadedPlugins[selectedIndex];
                }
            }
        }