/// <summary> /// Loads the language files of a plugin. /// </summary> /// <param name="plugin">The plugin onto which to put the loaded languages</param> /// <param name="folder">The folder in which to search for language files</param> /// <returns>Whether or not the operation was successful</returns> private static bool LoadLanguages(Plugin plugin, string folder) { try { string p = Path.Combine(folder, "Languages"); if (Directory.Exists(p)) { DirectoryInfo d = new DirectoryInfo(p); foreach (FileInfo f in d.GetFiles("*.xml", SearchOption.AllDirectories)) { string ietf = Path.GetFileNameWithoutExtension(f.Name); try { XmlSerializer ser = new XmlSerializer(typeof(List<Translation>)); FileStream fs = new FileStream(f.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); List<Translation> t = (List<Translation>)ser.Deserialize(fs); fs.Close(); plugin.Languages.Add(new Language { Culture = ietf, Translations = t }); } catch (Exception e) { U.L(LogLevel.Warning, "PLUGIN", String.Format("Problem loading language file {0} for plugin '{1}': {2}", f.Name, plugin.ID, e.Message)); } } } return true; } catch (Exception e) { U.L(LogLevel.Warning, "PLUGIN", String.Format("Problem loading languages for plugin '{0}': {1}", plugin.ID, e.Message)); return false; } }
/// <summary> /// Dispatches the Stopped event. /// </summary> /// <param name="plugin">The plugin that was stopped</param> private static void DispatchStopped(Plugin plugin) { if (Stopped != null) Stopped(null, new PluginEventArgs(plugin)); }
/// <summary> /// Dispatches the Uninstalled event. /// </summary> /// <param name="plugin">The plugin that was uninstalled</param> private static void DispatchUninstalled(Plugin plugin) { if (Uninstalled != null) Uninstalled(null, new PluginEventArgs(plugin)); }
/// <summary> /// Uninstalls a plugin. /// </summary> /// <param name="plugin">The plugin to uninstall</param> public static void Uninstall(Plugin plugin) { if (PluginManager.IsActive(plugin)) Stop(plugin); if (!plugin.OnUninstall()) U.L(LogLevel.Warning, "PLUGIN", String.Format("Plugin '{0}' was not uninstalled correctly", plugin.ID)); if (pluginPaths.ContainsValue(plugin.ID)) { string path = pluginPaths.Keys.ElementAt<string>(pluginPaths.Values.IndexOf(plugin.ID)); string name = Path.GetFileNameWithoutExtension(path); pluginPaths.Remove(path); Remove(name); } // forget settings for (int i=0; i < SettingsManager.PluginSettings.Count; i++) { if (SettingsManager.PluginSettings[i].PluginID == plugin.ID) { SettingsManager.PluginSettings.RemoveAt(i); break; } } DispatchUninstalled(plugin); plugins.Remove(plugin); }
/// <summary> /// Dispatches the Refresh event. /// </summary> /// <param name="plugin">The plugin that needs to be refreshed</param> private static void DispatchRefresh(Plugin plugin) { if (Refresh != null) Refresh(null, new PluginEventArgs(plugin)); }
/// <summary> /// Creates an instance of plugin event arguments. /// </summary> /// <param name="plugin">The plugin</param> public PluginEventArgs(Plugin plugin) { Plugin = plugin; }
/// <summary> /// Stops a plugin. /// </summary> /// <param name="plugin">The plugin to stop</param> public static void Stop(Plugin plugin) { if (activePlugins.Contains(plugin)) { if (plugin.OnStart()) { DispatchStopped(plugin); activePlugins.Remove(plugin); PluginSettings set = GetSettings(plugin.ID); if (set != null) set.Enabled = false; PluginItem p = GetListItem(plugin.ID); if (p != null) p.Disabled = true; if (activePlugins.Count == 0) { ticker.Dispose(); ticker = null; } if (plugin.Type == PluginType.Filter) { Plugins.Filter filter = plugin as Plugins.Filter; filter.VolumeChanged -= Plugin_VolumeChanged; } U.L(LogLevel.Information, "PLUGIN", "Stopped plugin '" + plugin.ID + "'"); } else U.L(LogLevel.Warning, "PLUGIN", "Could not stop plugin '" + plugin.ID + "'"); } else U.L(LogLevel.Warning, "PLUGIN", "Won't stop not running plugin '" + plugin.ID + "'"); }
/// <summary> /// Starts a plugin. /// </summary> /// <param name="plugin">The plugin to start</param> public static void Start(Plugin plugin) { if (!activePlugins.Contains(plugin)) { if (plugin.OnStart()) { DispatchStarted(plugin); activePlugins.Add(plugin); PluginSettings set = GetSettings(plugin.ID); if (set != null) set.Enabled = true; PluginItem p = GetListItem(plugin.ID); if (p != null) p.Disabled = false; if (ticker == null) ticker = new Timer(Tick, null, 0, 30); if (plugin.Type == PluginType.Filter) { Plugins.Filter filter = plugin as Plugins.Filter; filter.VolumeChanged += Plugin_VolumeChanged; } U.L(LogLevel.Information, "PLUGIN", "Started plugin '" + plugin.ID + "'"); } else U.L(LogLevel.Warning, "PLUGIN", "Could not start plugin '" + plugin.ID + "'"); } else U.L(LogLevel.Warning, "PLUGIN", "Won't start running plugin '" + plugin.ID + "'"); }
/// <summary> /// Gets whether or not a given plugin is running. /// </summary> /// <param name="plugin">The plugin to check</param> /// <returns>True if the plugin is running, otherwise false</returns> public static bool IsActive(Plugin plugin) { return activePlugins.Contains<Plugin>(plugin); }
/// <summary> /// Installs a single plugin. /// </summary> /// <param name="plugin">The plugin to install</param> public static void Install(Plugin plugin) { if (plugin.OnInstall()) { plugins.Add(plugin); // load remembered settings PluginSettings set = GetSettings(plugin.ID); if (set != null) { foreach (Setting savedSetting in set.Settings) { foreach (Setting pluginSetting in plugin.Settings) { if (pluginSetting.ID == savedSetting.ID && pluginSetting.Type == savedSetting.Type) { try { pluginSetting.Value = savedSetting.Value; } catch (Exception e) { U.L(LogLevel.Warning, "PLUGINS", String.Format("Could not load saved setting {0}: {1}", savedSetting.ID, e.Message)); } break; } } } set.Settings = plugin.Settings; } if (set == null) { SettingsManager.PluginSettings.Add(new PluginSettings { PluginID = plugin.ID, Settings = plugin.Settings, Installed = DateTime.Now, Enabled = false }); } DispatchInstalled(plugin); if (set != null && set.Enabled && plugin.Type != PluginType.Visualizer) Start(plugin); } else U.L(LogLevel.Warning, "PLUGIN", "Could not install plugin '" + plugin.ID + "'"); }
/// <summary> /// Populates the settings panel with the settings of a given plugin. /// </summary> /// <param name="plugin">The plugin whose settings to display</param> private void PopulateSettings(Plugin plugin) { SettingsGrid.RowDefinitions.Clear(); SettingsGrid.Children.Clear(); for (int i = 0; i < plugin.Settings.Count; i++) { Setting s = plugin.Settings[i]; // add row SettingsGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); // create label TextBlock tb = new TextBlock() { Text = plugin.T(s.ID), Margin = new Thickness(5, 5, 10, 5), VerticalAlignment = VerticalAlignment.Center }; tb.SetBinding(TextBlock.VisibilityProperty, new Binding("IsVisible") { Source = s, Mode = BindingMode.OneWay, Converter = new BooleanToVisibilityConverter() }); Grid.SetRow(tb, i); Grid.SetColumn(tb, 0); SettingsGrid.Children.Add(tb); FrameworkElement control = null; // create control if (s.Type == typeof(Boolean)) { // checkbox control = new CheckBox() { Height = 15 }; control.SetBinding(CheckBox.IsCheckedProperty, new Binding("Value") { Source = s, Mode = BindingMode.TwoWay }); } else if (s.Type == typeof(Color)) { // color selector control = new ColorPicker() { ShowAvailableColors = false, ShowStandardColors = true, Width = 50, }; if (s.PossibleValues != null) { ColorConverter converter = new ColorConverter(); ((ColorPicker)control).AvailableColors.Clear(); foreach (Color c in s.PossibleValues) { System.Windows.Media.Color color = (System.Windows.Media.Color)converter.Convert(c, null, null, null); ((ColorPicker)control).AvailableColors.Add(new ColorItem(color, c.Name)); } } control.SetBinding(ColorPicker.SelectedColorProperty, new Binding("Value") { Source = s, Mode = BindingMode.TwoWay, Converter = new ColorConverter() }); } else if (s.PossibleValues != null) { // dropdown control = new ComboBox(); foreach (Object val in s.PossibleValues) { try { String content = val.ToString(); if (s.Type == typeof(String)) content = plugin.T(val.ToString()); ((ComboBox)control).Items.Add(new ComboBoxItem { Content = content, Name = val.ToString() }); } catch (Exception exc) { U.L(LogLevel.Warning, "PLUGIN", "Could not add combobox item in plugin settings: " + exc.Message); } } ((ComboBox)control).SelectedValuePath = "Name"; control.SetBinding(ComboBox.SelectedValueProperty, new Binding("Value") { Source = s, Mode = BindingMode.TwoWay }); } else if (s.Type == typeof(String)) { // text input control = new TextBox() { MaxWidth = 400, MinWidth = 250 }; control.SetBinding(TextBox.TextProperty, new Binding("Value") { Source = s, Mode = BindingMode.TwoWay }); } else if (s.Type == typeof(Int32)) { if (s.Maximum != null) { // slider control = new Slider() { Maximum = (Int32)s.Maximum, AutoToolTipPlacement = AutoToolTipPlacement.TopLeft, Width = 200, }; if (s.Minimum != null) ((Slider)control).Minimum = (Int32)s.Minimum; control.SetBinding(Slider.ValueProperty, new Binding("Value") { Source = s, Mode = BindingMode.TwoWay }); } else { // spinner control = new IntegerUpDown(); control.SetBinding(IntegerUpDown.ValueProperty, new Binding("Value") { Source = s, Mode = BindingMode.TwoWay }); } } else if (s.Type == typeof(Double)) { if (s.Maximum != null) { // slider control = new Slider() { Maximum = (Double)s.Maximum, AutoToolTipPlacement = AutoToolTipPlacement.TopLeft, Width = 200, }; if (s.Minimum != null) ((Slider)control).Minimum = (Double)s.Minimum; control.SetBinding(Slider.ValueProperty, new Binding("Value") { Source = s, Mode = BindingMode.TwoWay }); } else { // spinner control = new DoubleUpDown(); control.SetBinding(DoubleUpDown.ValueProperty, new Binding("Value") { Source = s, Mode = BindingMode.TwoWay }); } } if (control != null) { control.Margin = new Thickness(0, 5, 0, 5); control.VerticalAlignment = VerticalAlignment.Center; control.HorizontalAlignment = HorizontalAlignment.Left; control.SetBinding(FrameworkElement.VisibilityProperty, new Binding("IsVisible") { Source = s, Mode = BindingMode.OneWay, Converter = new BooleanToVisibilityConverter() }); Grid.SetRow(control, i); Grid.SetColumn(control, 1); SettingsGrid.Children.Add(control); } } }
/// <summary> /// Populates the status label panel with the status labels of a given plugin. /// </summary> /// <param name="plugin">The plugin whose status labels to display</param> private void PopulateLabels(Plugin plugin) { StatusLabelGrid.RowDefinitions.Clear(); for (int i=0; i < 5; i++) StatusLabelGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); while (StatusLabelGrid.Children.Count > 10) StatusLabelGrid.Children.RemoveAt(10); PluginName.Text = plugin.T("Name"); PluginDescription.Text = plugin.T("Description"); PluginAuthor.Text = plugin.Author; PluginVersion.Text = plugin.Version.ToString(); PluginType.Text = U.T(plugin.Type); PluginEnabled.Click -= PluginEnabled_Click; PluginEnabled.IsChecked = PluginManager.IsActive(plugin); PluginEnabled.Click += PluginEnabled_Click; if (plugin.StatusLabels != null) for (int i = 0; i < plugin.StatusLabels.Count; i++) { StatusLabel s = plugin.StatusLabels[i]; // add row StatusLabelGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); // create label TextBlock label = new TextBlock() { Text = plugin.T(s.Label), Margin = new Thickness(5, 5, 10, 5), VerticalAlignment = VerticalAlignment.Center }; Grid.SetRow(label, i + 5); Grid.SetColumn(label, 0); StatusLabelGrid.Children.Add(label); // create status TextBlock status = new TextBlock() { Text = plugin.T(s.Status), Margin = new Thickness(0, 5, 0, 5), VerticalAlignment = VerticalAlignment.Center }; Grid.SetRow(status, i + 5); Grid.SetColumn(status, 1); StatusLabelGrid.Children.Add(status); currentStatusLabels.Add(s, status); s.PropertyChanged += PluginStatus_PropertyChanged; } }