Esempio n. 1
0
 protected static extern void qt_widget_attribute_set(IntPtr raw, WidgetAttribute attribute);
Esempio n. 2
0
        /// <summary>
        /// Detect the Widgets in assemblies then create the widget template to widget catalog.
        /// </summary>
        public void Detect(string targetPath, string defaultCategory = "utilities")
        {
            if (string.IsNullOrEmpty(targetPath))
            {
                throw new ArgumentNullException("targetPath");
            }

            //Auto detect package info
            string[] files = Directory.GetFiles(targetPath, "*.dll");

            foreach (string file in files)
            {
                try
                {
                    //When using LoadFile will cause could not get CustomAttributes!
                    Assembly assembly = Assembly.LoadFrom(file);
                    var      attrs    = assembly.GetCustomAttributes(true);
                    var      fileInfo = new FileInfo(file);
                    var      asmname  = assembly.GetName();

                    Type[] types = assembly.GetTypes();

                    var controllers = types.Where(c => (c.BaseType == typeof(Controller) || c.BaseType == typeof(AsyncController)));

                    foreach (Type controller in controllers)
                    {
                        var           methods             = controller.GetMethods(BindingFlags.Public | BindingFlags.Instance);
                        var           actions             = methods.Where(m => (m.GetCustomAttributes(typeof(WidgetAttribute), true).Length > 0));
                        string        controllerShortName = controller.Name.Replace("Controller", "");
                        List <string> actionNames         = new List <string>();

                        foreach (MethodInfo action in actions)
                        {
                            WidgetAttribute widgetAttr = (WidgetAttribute)action.GetCustomAttributes(typeof(WidgetAttribute), true)[0];
                            string          widgetCat  = !string.IsNullOrEmpty(widgetAttr.Category) ? widgetAttr.Category : defaultCategory;
                            var             actionName = action.Name;

                            //Supports the AsyncController
                            if (action.Name.EndsWith("async", StringComparison.OrdinalIgnoreCase))
                            {
                                actionName = action.Name.Replace("Async", "");
                            }

                            var tmplID = widgetCat + "\\" + actionName;

                            //When the Widget had already registered then continue.
                            var registeredDescriptor = DataContext.WidgetDescriptors.Find(tmplID);

                            if (registeredDescriptor != null)
                            {
                                continue;
                            }

                            actionNames.Add("\"" + actionName + "\"");

                            var properties = action.GetCustomAttributes(typeof(PropertyAttribute), true);
                            var defaults   = new Dictionary <string, object>();
                            foreach (var pro in properties)
                            {
                                var _pro = ((PropertyAttribute)pro);
                                defaults.Add(_pro.Name, _pro.DefaultValue);
                            }

                            var tmpl = new WidgetElement()
                            {
                                ID   = tmplID,
                                Name = new NameElement()
                                {
                                    ShortName = actionName,
                                    FullName  = widgetAttr.Title
                                },
                                Description = widgetAttr.Description,
                                Version     = assembly.GetName().Version.ToString(),
                                //Author = new AuthorElement()
                                //{
                                //    //Name = webOwner.UserName,
                                //    //Uri = appPath,
                                //    //Email = webOwner.Email
                                //},
                                //Icons = new List<IconElement>(){
                                //  new IconElement(){ Source= widgetAttr.IconUrl}
                                //},
                                Features = new List <FeatureElement>()
                                {
                                    new FeatureElement()
                                    {
                                        Name       = "mvc",
                                        IsRequried = true,
                                        Params     = new List <ParamElement>()
                                        {
                                            new ParamElement()
                                            {
                                                Name = "area", Value = string.IsNullOrEmpty(widgetAttr.Area) ? GetArea(controller) : widgetAttr.Area
                                            },
                                            new ParamElement()
                                            {
                                                Name = "controller", Value = controllerShortName
                                            },
                                            new ParamElement()
                                            {
                                                Name = "action", Value = actionName
                                            }
                                        }
                                    }
                                }
                            };

                            if (!string.IsNullOrEmpty(widgetAttr.IconUrl))
                            {
                                tmpl.Icons = new List <IconElement>()
                                {
                                    new IconElement()
                                    {
                                        Source = widgetAttr.IconUrl
                                    }
                                }
                            }
                            ;

                            if (defaults.Count > 0)
                            {
                                tmpl.Preferences = new List <PreferenceElement>();

                                foreach (var key in defaults.Keys)
                                {
                                    tmpl.Preferences.Add(new PreferenceElement()
                                    {
                                        IsReadonly = false,
                                        Name       = key,
                                        //Type = defaults[key] != null ? defaults[key].GetType().ToString() : (typeof(string)).ToString(),
                                        Value = defaults[key] != null ? defaults[key].ToString() : ""
                                    });
                                }
                            }

                            var widgetPath = InstalledPath + widgetCat + "\\" + actionName;
                            if (!Directory.Exists(widgetPath))
                            {
                                Directory.CreateDirectory(widgetPath);
                            }

                            var fileName = widgetPath + "\\config.xml";
                            XmlSerializerUtility.SerializeToXmlFile(fileName, tmpl);
                        }
                    }
                }
                catch (Exception e)
                {
                    string msg = e.Message;
                    continue;
                }
            }
        }