예제 #1
0
        private void setMultilineAddressField(PwEntry pwEntry, string sectionTitle, AddressSectionField addressSectionField)
        {
            string addressLocale = addressSectionField.v.country;

            if (string.IsNullOrEmpty(addressLocale))
            {
                addressLocale = "us";
            }

            // Find the address format used in the country where this address is located
            // Locale IDs can contain dashes (-) which are illegal characters in resource files, so they're replaced with underscores (_)
            string addressFormatString = Properties.MultilineAddressFormat.ResourceManager.GetString(string.Join("_", new string[] { "Country", addressLocale.Replace('-', '_') }));

            // If we couldn't find a specific format for the country, use a generic one
            if (string.IsNullOrEmpty(addressFormatString))
            {
                addressFormatString = Properties.MultilineAddressFormat.Country_us;
            }

            List <string> components = this.getTokenizedAddressComponents(addressFormatString, addressSectionField);

            if (components.Count > 0)
            {
                string fieldValue = string.Join(Environment.NewLine, components.ToArray());
                string fieldLabel = addressSectionField.t;

                // If the field is in a named section, prefix its name to avoid collisions
                if (!string.IsNullOrEmpty(sectionTitle))
                {
                    fieldLabel = string.Concat(sectionTitle, " - ", addressSectionField.t);
                }

                pwEntry.Strings.Set(fieldLabel, new ProtectedString(false, fieldValue));
            }
        }
예제 #2
0
        private List <SecureContentsSection> createSectionsFromPlainFields(RecordType recordType)
        {
            Dictionary <string, SecureContentsSection> sectionsByName = new Dictionary <string, SecureContentsSection>();

            foreach (PropertyInfo propertyInfo in this.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance))
            {
                string fieldValue = Convert.ToString(propertyInfo.GetValue(this, null));
                if (!Attribute.IsDefined(propertyInfo, typeof(ItemFieldAttribute)) || string.IsNullOrEmpty(fieldValue))
                {
                    continue;
                }

                ItemFieldAttribute itemFieldAttribute = Attribute.GetCustomAttribute(propertyInfo, typeof(ItemFieldAttribute)) as ItemFieldAttribute;

                string sectionName = itemFieldAttribute.sectionName ?? string.Empty;

                SecureContentsSection section;
                if (!sectionsByName.TryGetValue(sectionName, out section))
                {
                    section = new SecureContentsSection()
                    {
                        name   = sectionName,
                        title  = Properties.Strings.ResourceManager.GetString(string.Join("_", new string[] { "TemplateSection", Enum.GetName(typeof(RecordType), recordType), sectionName.Replace(" ", "_") })),
                        fields = new List <SectionField>()
                    };
                    sectionsByName.Add(sectionName, section);
                }

                string fieldName = itemFieldAttribute.fieldName ?? propertyInfo.Name;

                if (itemFieldAttribute.type == SectionFieldType.address && Attribute.IsDefined(propertyInfo, typeof(AddressComponentAttribute)))
                {
                    AddressSectionField addressSectionField = section.fields.Find(field => { return(fieldName.Equals(field.n)); }) as AddressSectionField;

                    if (addressSectionField == null)
                    {
                        addressSectionField = new AddressSectionField()
                        {
                            n = fieldName,
                            t = Properties.Strings.ResourceManager.GetString(string.Join("_", new string[] { "TemplateField", Enum.GetName(typeof(RecordType), recordType), fieldName.Replace(" ", "_") })),
                            v = new AddressValue(),
                            k = itemFieldAttribute.type
                        };
                        section.fields.Add(addressSectionField);
                    }

                    AddressComponentAttribute addressAttribute = Attribute.GetCustomAttribute(propertyInfo, typeof(AddressComponentAttribute)) as AddressComponentAttribute;

                    switch (addressAttribute.addressPart)
                    {
                    case AddressComponentAttribute.AddressPart.Address1:
                        addressSectionField.v.street = string.IsNullOrEmpty(addressSectionField.v.street) ? fieldValue : string.Join(Environment.NewLine, new string[] { fieldValue, addressSectionField.v.street });
                        break;

                    case AddressComponentAttribute.AddressPart.Address2:
                        addressSectionField.v.street = string.IsNullOrEmpty(addressSectionField.v.street) ? fieldValue : string.Join(Environment.NewLine, new string[] { addressSectionField.v.street, fieldValue });
                        break;

                    case AddressComponentAttribute.AddressPart.ZIP:
                        addressSectionField.v.zip = fieldValue;
                        break;

                    case AddressComponentAttribute.AddressPart.City:
                        addressSectionField.v.city = fieldValue;
                        break;

                    case AddressComponentAttribute.AddressPart.State:
                        addressSectionField.v.state = fieldValue;
                        break;

                    case AddressComponentAttribute.AddressPart.Region:
                        addressSectionField.v.region = fieldValue;
                        break;

                    case AddressComponentAttribute.AddressPart.Country:
                        addressSectionField.v.country = fieldValue;
                        break;

                    default:
                        break;
                    }
                }
                else if (itemFieldAttribute.type == SectionFieldType.date && Attribute.IsDefined(propertyInfo, typeof(DateComponentAttribute)))
                {
                    DateSectionField dateSectionField = section.fields.Find(field => { return(fieldName.Equals(field.n)); }) as DateSectionField;

                    if (dateSectionField == null)
                    {
                        dateSectionField = new DateSectionField()
                        {
                            n = fieldName,
                            t = Properties.Strings.ResourceManager.GetString(string.Join("_", new string[] { "TemplateField", Enum.GetName(typeof(RecordType), recordType), fieldName.Replace(" ", "_") })),
                            // 2000 is a leap year and January has 31 days. This guarantees no matter the order in which we add days,
                            // months or years to this base date, we won't get an invalid intermediate date (assuming a valid target date).
                            v = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc),
                            k = itemFieldAttribute.type
                        };
                        section.fields.Add(dateSectionField);
                    }

                    DateComponentAttribute dateAttribute = Attribute.GetCustomAttribute(propertyInfo, typeof(DateComponentAttribute)) as DateComponentAttribute;

                    int intFieldValue;

                    if (int.TryParse(fieldValue, out intFieldValue))
                    {
                        switch (dateAttribute.datePart)
                        {
                        case DateComponentAttribute.DatePart.Day:
                            dateSectionField.v = dateSectionField.v.AddDays(intFieldValue - dateSectionField.v.Day);
                            break;

                        case DateComponentAttribute.DatePart.Month:
                            dateSectionField.v = dateSectionField.v.AddMonths(intFieldValue - dateSectionField.v.Month);
                            break;

                        case DateComponentAttribute.DatePart.Year:
                            dateSectionField.v = dateSectionField.v.AddYears(intFieldValue - dateSectionField.v.Year);
                            break;

                        default:
                            break;
                        }
                    }
                }
                else if (itemFieldAttribute.type == SectionFieldType.monthYear && Attribute.IsDefined(propertyInfo, typeof(MonthYearComponentAttribute)))
                {
                    MonthYearSectionField monthYearSectionField = section.fields.Find(field => { return(fieldName.Equals(field.n)); }) as MonthYearSectionField;

                    if (monthYearSectionField == null)
                    {
                        monthYearSectionField = new MonthYearSectionField()
                        {
                            n = fieldName,
                            t = Properties.Strings.ResourceManager.GetString(string.Join("_", new string[] { "TemplateField", Enum.GetName(typeof(RecordType), recordType), fieldName.Replace(" ", "_") })),
                            // 2000 is a leap year and January has 31 days. This guarantees no matter the order in which we add days,
                            // months or years to this base date, we won't get an invalid intermediate date (assuming a valid target date).
                            v = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc),
                            k = itemFieldAttribute.type
                        };
                        section.fields.Add(monthYearSectionField);
                    }

                    MonthYearComponentAttribute monthYearAttribute = Attribute.GetCustomAttribute(propertyInfo, typeof(MonthYearComponentAttribute)) as MonthYearComponentAttribute;

                    int intFieldValue;

                    if (int.TryParse(fieldValue, out intFieldValue))
                    {
                        switch (monthYearAttribute.monthYearPart)
                        {
                        case MonthYearComponentAttribute.MonthYearPart.Month:
                            monthYearSectionField.v = monthYearSectionField.v.AddMonths(intFieldValue - monthYearSectionField.v.Month);
                            break;

                        case MonthYearComponentAttribute.MonthYearPart.Year:
                            monthYearSectionField.v = monthYearSectionField.v.AddYears(intFieldValue - monthYearSectionField.v.Year);
                            break;

                        default:
                            break;
                        }
                    }
                }
                else // the remaining field types are all GeneralSectionFields
                {
                    section.fields.Add(new GeneralSectionField()
                    {
                        n = fieldName,
                        t = Properties.Strings.ResourceManager.GetString(string.Join("_", new string[] { "TemplateField", Enum.GetName(typeof(RecordType), recordType), fieldName.Replace(" ", "_") })),
                        v = fieldValue,
                        k = itemFieldAttribute.type,
                        a = new SectionFieldAttributes()
                        {
                            multiline = itemFieldAttribute.multiline
                        }
                    });
                }
            }

            return(new List <SecureContentsSection>(sectionsByName.Values));
        }
예제 #3
0
        private void setExpandedAddressField(PwEntry pwEntry, string sectionTitle, AddressSectionField addressSectionField)
        {
            string addressLocale = addressSectionField.v.country;

            if (string.IsNullOrEmpty(addressLocale))
            {
                addressLocale = "us";
            }

            foreach (PropertyInfo propertyInfo in addressSectionField.v.GetType().GetProperties())
            {
                List <string> fieldLabelParts = new List <string>();
                string        fieldLabel      = null;
                string        fieldValue      = propertyInfo.GetValue(addressSectionField.v, null) as string;

                // Skip the field if it's empty
                if (string.IsNullOrEmpty(fieldValue))
                {
                    continue;
                }

                // Find how this address part is called in the country where this address is located
                string addressPartName = Properties.ExpandedAddressParts.ResourceManager.GetString(string.Join("_", new string[] { "Address", addressLocale.Replace('-', '_'), propertyInfo.Name }));

                // Find the localized address part name
                if (!string.IsNullOrEmpty(addressPartName))
                {
                    fieldLabel = Properties.Strings.ResourceManager.GetString(string.Join("_", new string[] { "Address", addressPartName }));
                }

                // If no localized version of the address part name is found, use the generic field name
                if (string.IsNullOrEmpty(fieldLabel))
                {
                    fieldLabel = CultureInfo.CurrentUICulture.TextInfo.ToTitleCase(propertyInfo.Name.ToLower());
                }

                // If the field is in a named section, prefix its name to avoid collisions
                if (!string.IsNullOrEmpty(sectionTitle))
                {
                    fieldLabelParts.Add(sectionTitle);
                }

                // Then add the field title, if it has one
                if (!string.IsNullOrEmpty(addressSectionField.t))
                {
                    fieldLabelParts.Add(addressSectionField.t);
                }

                // And finally the address part name
                fieldLabelParts.Add(fieldLabel);

                // Join all parts together
                fieldLabel = string.Join(" - ", fieldLabelParts.ToArray());

                // Find the localized country name
                if (propertyInfo.Name.Equals("country"))
                {
                    // Locale IDs can contain dashes (-) which are illegal characters in resource files, so they're replaced with underscores (_)
                    fieldValue = Properties.Strings.ResourceManager.GetString(string.Join("_", new string[] { "Menu", "country", fieldValue.Replace('-', '_') }));

                    // If no localized country name is found, use the ISO country code
                    if (string.IsNullOrEmpty(fieldValue))
                    {
                        fieldValue = fieldValue.ToUpper();
                    }
                }
                else if (propertyInfo.Name.Equals("street"))
                {
                    // Older format had two address lines, which may be joined with a new line
                    fieldValue = StringExt.ReplaceNewLines(fieldValue, " ");
                }

                pwEntry.Strings.Set(fieldLabel, new ProtectedString(false, fieldValue));
            }
        }
예제 #4
0
        private List <string> getTokenizedAddressComponents(string addressFormatString, AddressSectionField addressSectionField)
        {
            string[]      tokens     = addressFormatString.Split(new char[] { '|' });
            List <string> components = new List <string>();

            foreach (string token in tokens)
            {
                string[]      subtokens     = token.Split(new char[] { ' ' });
                List <string> subcomponents = new List <string>();

                foreach (string subtoken in subtokens)
                {
                    PropertyInfo propertyInfo = addressSectionField.v.GetType().GetProperty(subtoken);
                    if (propertyInfo != null)
                    {
                        string tokenValue = propertyInfo.GetValue(addressSectionField.v, null) as string;

                        if (string.IsNullOrEmpty(tokenValue))
                        {
                            continue;
                        }

                        // Find the localized country name
                        if (subtoken.Equals("country"))
                        {
                            // Locale IDs can contain dashes (-) which are illegal characters in resource files, so they're replaced with underscores (_)
                            tokenValue = Properties.Strings.ResourceManager.GetString(string.Join("_", new string[] { "Menu", "country", tokenValue.Replace('-', '_') }));

                            // If no localized country name is found, use the ISO country code
                            if (string.IsNullOrEmpty(tokenValue))
                            {
                                tokenValue = tokenValue.ToUpper().Replace('_', ' ');
                            }
                        }
                        else if (subtoken.Equals("street"))
                        {
                            // Older format had two address lines, which may be joined with a new line
                            tokenValue = StringExt.ReplaceNewLines(tokenValue, " ");
                        }

                        subcomponents.Add(tokenValue);
                    }
                }

                if (subcomponents.Count > 0)
                {
                    components.Add(string.Join(" ", subcomponents.ToArray()));
                }
            }

            return(components);
        }