예제 #1
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);
            }
        }
예제 #2
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();
            }
        }