Beispiel #1
0
        /// <summary>
        /// IRibbonExtensibility method, returns XML describing the Ribbon customizations.
        /// Called directly by OneNote when initializing the addin.
        /// </summary>
        /// <param name="RibbonID">The ID of the ribbon</param>
        /// <returns>XML starting at the customUI root element</returns>
        public string GetCustomUI(string RibbonID)
        {
            var root = XElement.Parse(Resx.Ribbon);

            ns = root.GetDefaultNamespace();

            var provider = new SettingsProvider();

            var ribbonbar = provider.GetCollection("RibbonBarSheet");

            if (ribbonbar.Count > 0)
            {
                AddRibbonBarCommands(ribbonbar, root);
            }

            var ccommands = provider.GetCollection("ContextMenuSheet");
            var searchers = provider.GetCollection("SearchEngineSheet");

            if (ccommands.Count == 0 && searchers.Count == 0)
            {
                return(root.ToString(SaveOptions.DisableFormatting));
            }

            try
            {
                // construct context menu UI
                var menu = new XElement(ns + "contextMenu",
                                        new XAttribute("idMso", "ContextMenuText"));

                if (ccommands.Count > 0)
                {
                    AddContextMenuCommands(ccommands, root, menu);
                }

                if (searchers.Count > 0)
                {
                    AddContextMenuSearchers(searchers, menu);
                }

                // add separator before Cut
                menu.Add(new XElement(ns + "menuSeparator",
                                      new XAttribute("id", "omContextMenuSeparator"),
                                      new XAttribute("insertBeforeMso", "Cut")
                                      ));

                //logger.WriteLine(menu.ToString());

                root.Add(new XElement(ns + "contextMenus", menu));
                return(root.ToString(SaveOptions.DisableFormatting));
            }
            catch (Exception exc)
            {
                logger.WriteLine("Error extending context menu", exc);
                return(root.ToString(SaveOptions.DisableFormatting));
            }
        }
Beispiel #2
0
        private void SetGeneralOptions()
        {
            var provider = new SettingsProvider();
            var settings = provider.GetCollection("GeneralSheet");

            EnablersEnabled = settings.Get <bool>("enablers", true);
        }
Beispiel #3
0
        private void RecordLastAction(Command command, params object[] args)
        {
            // ignore commands that pass the ribbon as an argument
            if (args.Any(a => a != null && a.GetType().Name.Contains("ComObject")))
            {
                return;
            }

            try
            {
                // SettingsProvider will only return an XElement if it has child elements so
                // wrap the argument list in an <arguments> element, which itself may be empty
                var arguments = new XElement("arguments");

                foreach (var arg in args)
                {
                    if (arg != null)
                    {
                        arguments.Add(new XElement("arg",
                                                   new XAttribute("type", arg.GetType().FullName),
                                                   new XAttribute("value", arg.ToString())
                                                   ));
                    }
                }

                var setting = new XElement("command",
                                           new XAttribute("type", command.GetType().FullName),
                                           arguments
                                           );

                var provider = new SettingsProvider();
                var settings = provider.GetCollection("lastAction");
                settings.Clear();

                // setting name should equate to the XML root element name here
                // the XML is not wrapped with an extra parent, so no worries
                settings.Add("command", setting);

                var replay = command.GetReplayArguments();
                if (replay != null)
                {
                    settings.Add("context", new XElement("context", replay));
                }

                provider.SetCollection(settings);
                provider.Save();
            }
            catch (Exception exc)
            {
                logger.WriteLine("error recording last action", exc);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Instantiates and executes the most recently executed command.
        /// </summary>
        /// <returns>Task</returns>
        public async Task ReplayLastAction()
        {
            var provider = new SettingsProvider();
            var settings = provider.GetCollection("lastAction");
            var action   = settings.Get <XElement>("command");

            if (action != null)
            {
                try
                {
                    var command = (Command)Activator.CreateInstance(
                        Type.GetType(action.Attribute("type").Value)
                        );

                    var args = new List <object>();
                    foreach (var arg in action.Element("arguments").Elements("arg"))
                    {
                        var type = Type.GetType(arg.Attribute("type").Value);
                        if (type.IsEnum)
                        {
                            args.Add(Enum.Parse(type, arg.Attribute("value").Value));
                        }
                        else
                        {
                            args.Add(Convert.ChangeType(
                                         arg.Attribute("value").Value,
                                         Type.GetType(arg.Attribute("type").Value)
                                         ));
                        }
                    }

                    var context = settings.Get <XElement>("context");
                    if (context != null && context.HasElements)
                    {
                        args.Add(context.Elements().First());
                    }

                    await Run("Replaying", command, args.ToArray());
                }
                catch (Exception exc)
                {
                    provider.RemoveCollection("lastAction");
                    provider.Save();

                    logger.WriteLine("error parsing last action; history cleared", exc);
                }
            }
            else
            {
                await Task.Yield();
            }
        }
Beispiel #5
0
        private async Task SetGeneralOptions()
        {
            var provider = new SettingsProvider();
            var settings = provider.GetCollection("GeneralSheet");

            EnablersEnabled = settings.Get("enablers", true);

            if (settings.Get("checkUpdates", false))
            {
                try
                {
                    await factory.Run <Commands.UpdateCommand>();
                }
                catch (Exception exc)
                {
                    logger.WriteLine("error checking for updates", exc);
                }
            }
        }