public void Simplify(DelphiComponent component)
        {
            InternalSimplify(component);

            foreach (DelphiComponent subComponent in component.Components)
            {
                Simplify(subComponent);
            }
        }
        private static void GatherControlNames(DelphiComponent component, IList<string> names)
        {
            foreach (DelphiComponent childComponent in component.Components)
            {
                if (childComponent.Properties.ContainsKey("CGUID"))
                {
                    names.Add(childComponent.Name);
                }

                GatherControlNames(childComponent, names);
            }
        }
 private static bool TryGetRectangle(DelphiComponent component, out Rectangle rectangle)
 {
     int left;
     int top = 0;
     int width = 0;
     int height = 0;
     bool success = (component.TryGetPropertyValue("Left", out left) &&
                     component.TryGetPropertyValue("Top", out top) &&
                     component.TryGetPropertyValue("Width", out width) &&
                     component.TryGetPropertyValue("Height", out height));
     rectangle = (success
                      ? new Rectangle(left, top, width, height)
                      : Rectangle.Empty);
     return success;
 }
        private static void OutputComponent(DelphiComponent component, string indentation)
        {
            WriteLine("{0}object {1}: {2}", indentation, component.Name, component.Type);

            foreach (KeyValuePair<string, object> property in component.Properties)
            {
                OutputProperty(property.Key, property.Value, indentation + "  ");
            }

            foreach (DelphiComponent subComponent in component.Components)
            {
                OutputComponent(subComponent, indentation + "  ");
            }

            WriteLine("{0}end", indentation);
        }
        private static void InternalProcess(DelphiComponent component, bool parentVisible)
        {
            int count = component.Components.Count;

            for (int i = 0; i < count - 1; i++)
            {
                DelphiComponent component1 = component.Components[i];
                bool visible = (parentVisible && (!component1.TryGetPropertyValue("Visible", out visible) || visible));
                Rectangle rect1;

                if (visible && TryGetRectangle(component1, out rect1))
                {
                    int area1 = rect1.Width*rect1.Height;
                    int areaThreshold = area1 - (area1 >> 3); // 7/8ths rounded up

                    for (int j = i + 1; j < count; j++)
                    {
                        DelphiComponent component2 = component.Components[j];
                        Rectangle rect2;

                        if (TryGetRectangle(component2, out rect2))
                        {
                            Rectangle intersection = Rectangle.Intersect(rect1, rect2);

                            if (intersection != Rectangle.Empty && (intersection.Width*intersection.Height) > areaThreshold)
                            {
                                visible = false;
                                break;
                            }
                        }
                    }
                }

                if (!visible)
                {
                    component1.Properties["Visible"] = false;
                }

                InternalProcess(component1, visible);
            }
        }
        public void Simplify(DelphiComponent component)
        {
            if (component.Type == "TAXForm" || component.Type == "TSupportAXForm")
            {
                for (int i = component.Components.Count - 1; i >= 0; i--)
                {
                    DelphiComponent subComponent = component.Components[i];

                    if (subComponent.Type == "TDreamDesigner" || subComponent.Type == "TActiveXTranslator" || subComponent.Type == "TDCScripter")
                    {
                        component.Components.RemoveAt(i);
                    }
                    else
                    {
                        Simplify(subComponent);
                    }
                }
            }

            InternalSimplify(component);
        }
        private static void InternalSimplify(DelphiComponent component)
        {
            IDictionary<string, object> changes = new Dictionary<string, object>();

            foreach (KeyValuePair<string, object> property in component.Properties)
            {
                IConvertible convertible = property.Value as IConvertible;

                if (convertible != null)
                {
                    switch (convertible.GetTypeCode())
                    {
                        case TypeCode.SByte:
                        case TypeCode.Int16:
                        case TypeCode.Int64:
                        case TypeCode.Byte:
                        case TypeCode.UInt16:
                        case TypeCode.UInt32:
                        case TypeCode.UInt64:
                            changes.Add(property.Key, convertible.ToInt32(null));
                            break;
                        case TypeCode.Single:
                            changes.Add(property.Key, convertible.ToDouble(null));
                            break;
                    }
                }
                else if (property.Value is DelphiIdentity ||
                         property.Value is DelphiUTF8String ||
                         property.Value is DelphiWString)
                {
                    changes.Add(property.Key, property.Value.ToString());
                }
            }

            foreach (KeyValuePair<string, object> change in changes)
            {
                component.Properties[change.Key] = change.Value;
            }
        }
 public void Flatten(DelphiComponent component, bool isLegacy)
 {
     return;
 }
    protected void _changePassword_Click(object sender, EventArgs e)
    {
        DelphiComponent dc = new DelphiComponent();
        Sage.SalesLogix.SystemInformation si = Sage.SalesLogix.SystemInformationRules.GetSystemInfo();

        DelphiBinaryReader delphi = new DelphiBinaryReader(si.Data);
        dc = delphi.ReadComponent(true);

        string minPasswordLength = dc.Properties["MinPasswordLength"].ToString();
        bool noBlankPassword  = (bool)dc.Properties["NoBlankPassword"];
        bool alphaNumPassword = (bool)dc.Properties["AlphaNumPassword"];
        bool noNameInPassword = (bool)dc.Properties["NoNameInPassword"];

        Regex objAlphaNumericPattern = new Regex("[a-zA-Z][0-9]");
        string changingUser = string.Empty;

        // Get the user name of the person who's getting their password changed
         Sage.Entity.Interfaces.IUser us = User.LookupResultValue as IUser;

         if (us != null)
         {
             changingUser = us.UserName;
         }
         else
         {
             changingUser = slxUserService.UserName;
         }

        string newPassword = _newPassword.Text;
        if (Convert.ToInt32(minPasswordLength) != 0)
        {
            if (newPassword.Length < Convert.ToInt32(minPasswordLength))
            {
                lblMessage.Text = string.Format(GetLocalResourceObject("minPasswordLength").ToString(), minPasswordLength); //   "Password length must be {0} chars or greater!"
                return;
            }
            if (alphaNumPassword && !objAlphaNumericPattern.IsMatch(newPassword))
            {
                lblMessage.Text = GetLocalResourceObject("alphaNumPassword").ToString();// "Passwords must be alphanumeric!";
                return;
            }

        }
        else if (noBlankPassword && newPassword.Length == 0)
        {
            lblMessage.Text = GetLocalResourceObject("noBlankPassword").ToString();//Passwords can not be blank!";
            return;
        }

        if (noNameInPassword && newPassword.ToUpper().Contains(changingUser.ToUpper()))
        {
            if (curUser.ToUpper().Contains("ADMIN") && !changingUser.ToUpper().Contains("ADMIN"))
                lblMessage.Text = GetLocalResourceObject("noNameInPasswordAdmin").ToString(); // "Passwords cannot contain the user name!";
            else
                lblMessage.Text = GetLocalResourceObject("noNameInPasswordUser").ToString(); //"Passwords cannot contain your user name!";
            return;
        }

        // save values
        if (newPassword == _confirmPassword.Text)
        {
            ChangePasswordOptions options = new ChangePasswordOptions();
            options.NewPassword = newPassword;

            string curUser = slxUserService.GetUser().Id;
            if (curUser.ToString().Trim() != "ADMIN")
            {
                options.UserId = curUser;
            }
            else
            {
                options.UserId = ((IUser)this.User.LookupResultValue).Id.ToString();
            }
            options.Save();
            if (_newPassword.Text.Length == 0)
            {
                lblMessage.Text = GetLocalResourceObject("PasswordBlank").ToString();
            }
            else
            {
                lblMessage.Text = string.Empty;
            }
        }
        else
        {
            lblMessage.Text = GetLocalResourceObject("PasswordNotMatch").ToString();
        }
    }
        public static ControlBuilder CreateBuilder(string legacyType, DelphiComponent component)
        {
            Type builderType = null;

            foreach (KeyValuePair<BuilderMappingAttribute, Type> builder in _builders)
            {
                if (builderType != builder.Value && builder.Key.IsApplicable(legacyType, component.Properties))
                {
                    bool isSpecific = (builder.Key.GetType() != typeof (BuilderMappingAttribute));

                    if (builderType == null || isSpecific)
                    {
                        builderType = builder.Value;

                        if (isSpecific)
                        {
                            break;
                        }
                    }
                }
            }

            ControlBuilder colBuilder;

            if (builderType != null)
            {
                colBuilder = (ControlBuilder) Activator.CreateInstance(builderType);
                colBuilder._component = component;
                return colBuilder;
            }
            else
            {
                colBuilder = null;
            }

            return colBuilder;
        }
 public void Determine(DelphiComponent component)
 {
     InternalProcess(component, true);
 }
 public HiddenControlBuilder(DelphiComponent component)
 {
     _component = component;
 }
        private void InternalSimplify(DelphiComponent component)
        {
            string cGuid;
            byte[] data;
            IDictionary<string, DelphiComponent> tabControlOwners = null;

            if (component.TryGetPropertyValue("CGUID", out cGuid))
            {
                Type type = StandardControls.LookupType(cGuid);

                if (type != null && component.TryGetPropertyValue("ControlData", out data))
                {
                    DelphiComponent controlData = ParseControlData(data);
                    component.Type = controlData.Type;

                    foreach (KeyValuePair<string, object> property in controlData.Properties)
                    {
                        if (!component.Properties.ContainsKey(property.Key))
                        {
                            component.Properties.Add(property);
                        }
                    }

                    /*if (type == typeof (AxTabControl))
                    {
                        tabControlOwners = new Dictionary<string, DelphiComponent>(StringComparer.InvariantCultureIgnoreCase);

                        foreach (DelphiComponent subComponent in controlData.Components)
                        {
                            InternalSimplify(subComponent);
                            string controlNames;

                            if (subComponent.TryGetPropertyValue("ControlNames", out controlNames))
                            {
                                foreach (string controlName in controlNames.Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries))
                                {
                                    if (!tabControlOwners.ContainsKey(controlName))
                                    {
                                        tabControlOwners.Add(controlName, subComponent);
                                    }
                                }

                                //subComponent.Properties.Remove("ControlNames");
                            }

                            component.Components.Add(subComponent);
                        }
                    }
                    else if (type == typeof (AxDataGrid))
                    {
                        foreach (DelphiComponent subComponent in controlData.Components)
                        {
                            InternalSimplify(subComponent);
                            component.Components.Add(subComponent);
                        }
                    }
                    else if (type == typeof(AxPanel))
                    {
                        foreach (DelphiComponent subComponent in controlData.Components)
                        {
                            InternalSimplify(subComponent);
                            component.Components.Add(subComponent);
                        }
                    }
                    else
                    {
                        Debug.Assert(controlData.Components.Count == 0);
                    }
                     */

                    //component.Properties.Remove("ControlData");

                    foreach (DelphiComponent subComponent in controlData.Components)
                    {
                        InternalSimplify(subComponent);
                        component.Components.Add(subComponent);
                    }
                }
            }

            _componentSimplifier.Simplify(component);

            if (component.TryGetPropertyValue("ChildControls", out data))
            {
                ICollection<DelphiComponent> childControls = ParseChildControls(data);

                foreach (DelphiComponent childControl in childControls)
                {
                    InternalSimplify(childControl);

                    if (tabControlOwners == null)
                    {
                        component.Components.Add(childControl);
                    }
                    else
                    {
                        DelphiComponent ownerTab;

                        if (tabControlOwners.TryGetValue(childControl.Name, out ownerTab))
                        {
                            ownerTab.Components.Add(childControl);
                        }
                    }
                }

                component.Properties.Remove("ChildControls");
            }
        }
        private DelphiComponent ParseComponent(DelphiBinaryReader reader)
        {
            DelphiComponent component = new DelphiComponent();
            component.Type = reader.ReadString();
            component.Name = reader.ReadString();

            while (reader.PeekChar() != 0)
            {
                ParseProperty(reader, component.Properties);
            }

            byte b = reader.ReadByte();
            Debug.Assert(b == 0);

            while (reader.PeekChar() != 0)
            {
                component.Components.Add(ParseComponent(reader));
            }

            b = reader.ReadByte();
            Debug.Assert(b == 0);
            return component;
        }
        private ICollection<DelphiComponent> ParseChildControls(byte[] data)
        {
            using (DelphiBinaryReader binaryReader = new DelphiBinaryReader(new MemoryStream(data)))
            {
                int count = binaryReader.ReadInteger();
                ICollection<DelphiComponent> childControls = new List<DelphiComponent>();

                for (int i = 0; i < count; i++)
                {
                    DelphiComponent component = new DelphiComponent();
                    component.Name = (string) binaryReader.ReadValue();
                    //TODO: figure out what this number means
                    byte b = binaryReader.ReadByte();
                    Debug.Assert(b == 1);

                    while (binaryReader.PeekChar() != 0)
                    {
                        ParseProperty(binaryReader, component.Properties);
                    }

                    b = binaryReader.ReadByte();
                    Debug.Assert(b == 0);
                    childControls.Add(component);
                }

                Debug.Assert(binaryReader.BaseStream.Position == binaryReader.BaseStream.Length);
                return childControls;
            }
        }
        public static ControlBuilder CreateBuilder(string name, DelphiComponent component)
        {
            //invisible controls are mapped to the HiddenControl control
            bool visible = (!component.TryGetPropertyValue("Visible", out visible) || visible);
            if (!visible)
            {
                ControlBuilder builder = new HiddenControlBuilder(component);
                builder._component = component;
                return builder;

            }

            Type builderType = null;

            foreach (KeyValuePair<BuilderMappingAttribute, Type> builder in _builders)
            {
                if (builderType != builder.Value && builder.Key.IsApplicable(name, component.Properties))
                {
                    bool isSpecific = (builder.Key.GetType() != typeof (BuilderMappingAttribute));

                    if (builderType == null || isSpecific)
                    {
                        builderType = builder.Value;

                        if (isSpecific)
                        {
                            break;
                        }
                    }
                }
            }

            ControlBuilder colBuilder;

            if (builderType != null)
            {
                colBuilder = (ControlBuilder) Activator.CreateInstance(builderType);
                colBuilder._component = component;
                return colBuilder;
            }
            else
            {
                colBuilder = null;
            }

            return colBuilder;
        }