Esempio n. 1
0
        internal static void Install(string path)
        {
            if (File.Exists(path))
            {
                string tempFolder = Path.Combine(Path.GetTempPath(), "wox\\plugins");
                if (Directory.Exists(tempFolder))
                {
                    Directory.Delete(tempFolder, true);
                }

                UnZip(path, tempFolder, true);

                string iniPath = Path.Combine(tempFolder, "plugin.json");
                if (!File.Exists(iniPath))
                {
                    MessageBox.Show("Install failed: plugin config is missing");
                    return;
                }

                PluginMetadata plugin = GetMetadataFromJson(tempFolder);
                if (plugin == null || plugin.Name == null)
                {
                    MessageBox.Show("Install failed: plugin config is invalid");
                    return;
                }

                string pluginFolderPath = Infrastructure.Constant.PluginsDirectory;

                string newPluginName = plugin.Name
                                       .Replace("/", "_")
                                       .Replace("\\", "_")
                                       .Replace(":", "_")
                                       .Replace("<", "_")
                                       .Replace(">", "_")
                                       .Replace("?", "_")
                                       .Replace("*", "_")
                                       .Replace("|", "_")
                                       + "-" + Guid.NewGuid();
                string newPluginPath = Path.Combine(pluginFolderPath, newPluginName);
                string content       = $"Do you want to install following plugin?{Environment.NewLine}{Environment.NewLine}" +
                                       $"Name: {plugin.Name}{Environment.NewLine}" +
                                       $"Version: {plugin.Version}{Environment.NewLine}" +
                                       $"Author: {plugin.Author}";
                PluginPair existingPlugin = PluginManager.GetPluginForId(plugin.ID);

                if (existingPlugin != null)
                {
                    content = $"Do you want to update following plugin?{Environment.NewLine}{Environment.NewLine}" +
                              $"Name: {plugin.Name}{Environment.NewLine}" +
                              $"Old Version: {existingPlugin.Metadata.Version}" +
                              $"{Environment.NewLine}New Version: {plugin.Version}" +
                              $"{Environment.NewLine}Author: {plugin.Author}";
                }

                var result = MessageBox.Show(content, "Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    if (existingPlugin != null && Directory.Exists(existingPlugin.Metadata.PluginDirectory))
                    {
                        // when plugin is in use, we can't delete them. That's why we need to make plugin folder a random name
                        File.Create(Path.Combine(existingPlugin.Metadata.PluginDirectory, "NeedDelete.txt")).Close();
                    }

                    UnZip(path, newPluginPath, true);
                    Directory.Delete(tempFolder, true);

                    // existing plugins could be loaded by the application,
                    // if we try to delete those kind of plugins, we will get a  error that indicate the
                    // file is been used now.
                    // current solution is to restart wox. Ugly.
                    // if (MainWindow.Initialized)
                    // {
                    //    Plugins.Initialize();
                    // }
                    if (MessageBox.Show($"You have installed plugin {plugin.Name} successfully.{Environment.NewLine}" +
                                        "Restart Wox to take effect?",
                                        "Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                    {
                        PluginManager.API.RestartApp();
                    }
                }
            }
        }