Example #1
0
        /// <summary>
        /// Convert Enpass Item to HandySafe Card
        /// </summary>
        private static HandySafeModel.Card ConvertItemToCard(EnpassModel.Item enpassItem)
        {
            List <HandySafeModel.Field> hsFields = new List <HandySafeModel.Field>();

            if (enpassItem.Fields != null)
            {
                foreach (EnpassModel.Field enpassField in enpassItem.Fields)
                {
                    if (String.IsNullOrEmpty(enpassField.Value))
                    {
                        continue;
                    }

                    HandySafeModel.Field hsField = new HandySafeModel.Field()
                    {
                        Name  = enpassField.Label,
                        Value = enpassField.Value
                    };

                    int?fieldType = MapFieldType(enpassField);
                    if (fieldType.HasValue)
                    {
                        hsField.Type = fieldType.ToString();
                    }

                    hsFields.Add(hsField);
                }
            }

            int hsIcon = MapIcon(enpassItem);

            HandySafeModel.Card hsCard = new HandySafeModel.Card()
            {
                Name   = enpassItem.Title,
                Icon   = hsIcon.ToString(),
                Fields = hsFields
            };

            // Add Note in enpassItem do hsCard and replace newline
            if (!String.IsNullOrEmpty(enpassItem.Note))
            {
                hsCard.Note = enpassItem.Note;//.Replace("|", System.Environment.NewLine);
                hsCard.Note = enpassItem.Note.Replace(System.Environment.NewLine, " | ");
            }

            return(hsCard);
        }
Example #2
0
        static void Main(string[] args)
        {
            try
            {
                if (!File.Exists(inputFile))
                {
                    throw new Exception($"Input file '{inputFile}' not found.");
                }

                string jsonData = File.ReadAllText(inputFile);
                JsonSerializerSettings settings = new JsonSerializerSettings();
                Enpass enpass = JsonConvert.DeserializeObject <Enpass>(jsonData);

                Dictionary <string, string> enpassFolders = new Dictionary <string, string>();
                foreach (EnpassModel.Folder enpassFolder in enpass.Folders)
                {
                    if (!enpassIgnoredFolders.Contains(enpassFolder.Uuid))
                    {
                        enpassFolders.Add(enpassFolder.Uuid, enpassFolder.Title);
                    }
                }

                HandySafe handySafe = new HandySafe()
                {
                    Folders = new List <HandySafeModel.Folder>()
                };

                // Items without Tag (uncategorized)
                IEnumerable <EnpassModel.Item> enpassUncategorizedItems = enpass.Items
                                                                          .Where(i => i.Folders == null || i.Folders.Length == 0);

                if (enpassUncategorizedItems.Count() > 0)
                {
                    HandySafeModel.Folder hsFolder = new HandySafeModel.Folder()
                    {
                        Name  = "Nezaƙazeno",
                        Cards = new List <Card>()
                    };
                    handySafe.Folders.Add(hsFolder);

                    foreach (EnpassModel.Item enpassUncategorizedItem in enpassUncategorizedItems)
                    {
                        Console.WriteLine(hsFolder.Name + ": " + enpassUncategorizedItem.Title);
                        HandySafeModel.Card hsCard = ConvertItemToCard(enpassUncategorizedItem);
                        hsFolder.Cards.Add(hsCard);
                    }
                }

                // Items with Tag (categorized)
                foreach (KeyValuePair <string, string> enpassFolder in enpassFolders)
                {
                    HandySafeModel.Folder hsFolder = new HandySafeModel.Folder()
                    {
                        Name  = enpassFolder.Value,
                        Cards = new List <Card>()
                    };

                    IEnumerable <EnpassModel.Item> enpassFolderItems = enpass.Items
                                                                       .Where(i => i.Folders != null &&
                                                                              i.Folders.ToList().Contains(enpassFolder.Key));

                    foreach (EnpassModel.Item enpassFolderItem in enpassFolderItems)
                    {
                        Console.WriteLine(hsFolder.Name + ": " + enpassFolderItem.Title);
                        HandySafeModel.Card hsCard = ConvertItemToCard(enpassFolderItem);
                        hsFolder.Cards.Add(hsCard);
                    }

                    handySafe.Folders.Add(hsFolder);
                }

                XmlSerializer xmlSerializer = new XmlSerializer(typeof(HandySafeModel.HandySafe));

                XmlSerializerNamespaces emptyNamespaces   = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
                XmlWriterSettings       xmlWriterSettings = new XmlWriterSettings();
                xmlWriterSettings.Indent             = true;
                xmlWriterSettings.OmitXmlDeclaration = true;

                using (XmlWriter xw = XmlWriter.Create(outputFile, xmlWriterSettings))
                {
                    xmlSerializer.Serialize(xw, handySafe, emptyNamespaces);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }