コード例 #1
0
        private static void AddTags(List <string> lTags, string strNewTags)
        {
            if (string.IsNullOrEmpty(strNewTags))
            {
                return;
            }

            StrUtil.AddTags(lTags, StrUtil.StringToTags(
                                strNewTags.Replace(' ', ';')));
        }
コード例 #2
0
ファイル: CsvImportForm.cs プロジェクト: gh-andre/KeePassFont
        private void PerformImport(PwGroup pgStorage, bool bCreatePreview)
        {
            List <CsvFieldInfo> lFields = GetCsvFieldInfos();

            if (bCreatePreview)
            {
                int dx = m_lvImportPreview.ClientRectangle.Width;                 // Before clearing

                m_lvImportPreview.Items.Clear();
                m_lvImportPreview.Columns.Clear();

                foreach (CsvFieldInfo cfi in lFields)
                {
                    string strCol = CsvFieldToString(cfi.Type);
                    if (cfi.Type == CsvFieldType.CustomString)
                    {
                        strCol = (cfi.Name ?? string.Empty);
                    }
                    m_lvImportPreview.Columns.Add(strCol, dx / lFields.Count);
                }
            }

            CsvOptions opt = GetCsvOptions();

            if (opt == null)
            {
                Debug.Assert(bCreatePreview); return;
            }

            string            strData = GetDecodedText();
            CsvStreamReaderEx csr     = new CsvStreamReaderEx(strData, opt);

            Dictionary <string, PwGroup> dGroups = new Dictionary <string, PwGroup>();

            dGroups[string.Empty] = pgStorage;

            if (bCreatePreview)
            {
                m_lvImportPreview.BeginUpdate();
            }

            DateTime dtNow           = DateTime.UtcNow;
            DateTime dtNoExpire      = KdbTime.NeverExpireTime.ToDateTime();
            bool     bIgnoreFirstRow = m_cbIgnoreFirst.Checked;
            bool     bIsFirstRow     = true;
            bool     bMergeGroups    = m_cbMergeGroups.Checked;

            while (true)
            {
                string[] v = csr.ReadLine();
                if (v == null)
                {
                    break;
                }
                if (v.Length == 0)
                {
                    continue;
                }
                if ((v.Length == 1) && (v[0].Length == 0))
                {
                    continue;
                }

                if (bIsFirstRow && bIgnoreFirstRow)
                {
                    bIsFirstRow = false;
                    continue;
                }
                bIsFirstRow = false;

                PwGroup pg = pgStorage;
                PwEntry pe = new PwEntry(true, true);

                ListViewItem lvi = null;
                for (int i = 0; i < Math.Min(v.Length, lFields.Count); ++i)
                {
                    string       strField = v[i];
                    CsvFieldInfo cfi      = lFields[i];

                    if (cfi.Type == CsvFieldType.Ignore)
                    {
                    }
                    else if (cfi.Type == CsvFieldType.Group)
                    {
                        pg = FindCreateGroup(strField, pgStorage, dGroups,
                                             cfi.Format, opt, bMergeGroups);
                    }
                    else if (cfi.Type == CsvFieldType.Title)
                    {
                        ImportUtil.AppendToField(pe, PwDefs.TitleField,
                                                 strField, m_pwDatabase);
                    }
                    else if (cfi.Type == CsvFieldType.UserName)
                    {
                        ImportUtil.AppendToField(pe, PwDefs.UserNameField,
                                                 strField, m_pwDatabase);
                    }
                    else if (cfi.Type == CsvFieldType.Password)
                    {
                        ImportUtil.AppendToField(pe, PwDefs.PasswordField,
                                                 strField, m_pwDatabase);
                    }
                    else if (cfi.Type == CsvFieldType.Url)
                    {
                        ImportUtil.AppendToField(pe, PwDefs.UrlField,
                                                 strField, m_pwDatabase);
                    }
                    else if (cfi.Type == CsvFieldType.Notes)
                    {
                        ImportUtil.AppendToField(pe, PwDefs.NotesField,
                                                 strField, m_pwDatabase);
                    }
                    else if (cfi.Type == CsvFieldType.CustomString)
                    {
                        ImportUtil.AppendToField(pe, (string.IsNullOrEmpty(cfi.Name) ?
                                                      PwDefs.NotesField : cfi.Name), strField, m_pwDatabase);
                    }
                    else if (cfi.Type == CsvFieldType.CreationTime)
                    {
                        pe.CreationTime = ParseDateTime(ref strField, cfi, dtNow);
                    }
                    // else if(cfi.Type == CsvFieldType.LastAccessTime)
                    //	pe.LastAccessTime = ParseDateTime(ref strField, cfi, dtNow);
                    else if (cfi.Type == CsvFieldType.LastModTime)
                    {
                        pe.LastModificationTime = ParseDateTime(ref strField, cfi, dtNow);
                    }
                    else if (cfi.Type == CsvFieldType.ExpiryTime)
                    {
                        bool bParseSuccess;
                        pe.ExpiryTime = ParseDateTime(ref strField, cfi, dtNow,
                                                      out bParseSuccess);
                        pe.Expires = (bParseSuccess && (pe.ExpiryTime != dtNoExpire));
                    }
                    else if (cfi.Type == CsvFieldType.Tags)
                    {
                        StrUtil.AddTags(pe.Tags, StrUtil.StringToTags(strField));
                    }
                    else
                    {
                        Debug.Assert(false);
                    }

                    if (bCreatePreview)
                    {
                        strField = StrUtil.MultiToSingleLine(strField);

                        if (lvi != null)
                        {
                            lvi.SubItems.Add(strField);
                        }
                        else
                        {
                            lvi = m_lvImportPreview.Items.Add(strField);
                        }
                    }
                }

                if (bCreatePreview)
                {
                    // Create remaining subitems
                    for (int r = v.Length; r < lFields.Count; ++r)
                    {
                        if (lvi != null)
                        {
                            lvi.SubItems.Add(string.Empty);
                        }
                        else
                        {
                            lvi = m_lvImportPreview.Items.Add(string.Empty);
                        }
                    }
                }

                pg.AddEntry(pe, true);
            }

            if (bCreatePreview)
            {
                m_lvImportPreview.EndUpdate();
                ProcessResize();
            }
        }
コード例 #3
0
        private static void AddObject(PwGroup pgStorage, JsonObject jo,
                                      PwDatabase pwContext, bool bCreateSubGroups,
                                      Dictionary <string, List <string> > dTags, List <PwEntry> lCreatedEntries)
        {
            string strRoot = jo.GetValue <string>("root");

            if (string.Equals(strRoot, "tagsFolder", StrUtil.CaseIgnoreCmp))
            {
                ImportTags(jo, dTags);
                return;
            }

            JsonObject[] v = jo.GetValueArray <JsonObject>("children");
            if (v != null)
            {
                PwGroup pgNew;
                if (bCreateSubGroups)
                {
                    pgNew      = new PwGroup(true, true);
                    pgNew.Name = (jo.GetValue <string>("title") ?? string.Empty);

                    pgStorage.AddGroup(pgNew, true);
                }
                else
                {
                    pgNew = pgStorage;
                }

                foreach (JsonObject joSub in v)
                {
                    if (joSub == null)
                    {
                        Debug.Assert(false); continue;
                    }

                    AddObject(pgNew, joSub, pwContext, true, dTags, lCreatedEntries);
                }

                return;
            }

            PwEntry pe = new PwEntry(true, true);

            // SetString(pe, "Index", false, jo, "index");
            SetString(pe, PwDefs.TitleField, pwContext.MemoryProtection.ProtectTitle,
                      jo, "title");
            // SetString(pe, "ID", false, jo, "id");
            SetString(pe, PwDefs.UrlField, pwContext.MemoryProtection.ProtectUrl,
                      jo, "uri");
            // SetString(pe, "CharSet", false, jo, "charset");

            v = jo.GetValueArray <JsonObject>("annos");
            if (v != null)
            {
                foreach (JsonObject joAnno in v)
                {
                    if (joAnno == null)
                    {
                        Debug.Assert(false); continue;
                    }

                    string strName  = joAnno.GetValue <string>("name");
                    string strValue = joAnno.GetValue <string>("value");

                    if ((strName == "bookmarkProperties/description") &&
                        !string.IsNullOrEmpty(strValue))
                    {
                        pe.Strings.Set(PwDefs.NotesField, new ProtectedString(
                                           pwContext.MemoryProtection.ProtectNotes, strValue));
                    }
                }
            }

            // Tags support (new versions)
            string strTags = jo.GetValue <string>("tags");

            if (!string.IsNullOrEmpty(strTags))
            {
                StrUtil.AddTags(pe.Tags, strTags.Split(','));
            }

            string strKeyword = jo.GetValue <string>("keyword");

            if (!string.IsNullOrEmpty(strKeyword))
            {
                ImportUtil.AppendToField(pe, "Keyword", strKeyword, pwContext);
            }

            if ((pe.Strings.ReadSafe(PwDefs.TitleField).Length != 0) ||
                (pe.Strings.ReadSafe(PwDefs.UrlField).Length != 0))
            {
                pgStorage.AddEntry(pe, true);
                lCreatedEntries.Add(pe);
            }
        }
コード例 #4
0
        private static void ProcessCsvLine(string[] vLine, PwDatabase pwStorage,
                                           SortedDictionary <string, PwGroup> dictGroups)
        {
            string strType = ParseCsvWord(vLine[0]);

            string strGroupName = ParseCsvWord(vLine[12]);             // + " - " + strType;

            if (strGroupName.Length == 0)
            {
                strGroupName = strType;
            }

            SplashIdMapping mp = null;

            foreach (SplashIdMapping mpFind in SplashIdCsv402.SplashIdMappings)
            {
                if (mpFind.TypeName == strType)
                {
                    mp = mpFind;
                    break;
                }
            }

            PwIcon pwIcon = ((mp != null) ? mp.Icon : PwIcon.Key);

            PwGroup pg = null;

            if (dictGroups.ContainsKey(strGroupName))
            {
                pg = dictGroups[strGroupName];
            }
            else
            {
                // PwIcon pwGroupIcon = ((pwIcon == PwIcon.Key) ?
                //	PwIcon.FolderOpen : pwIcon);
                // pg = new PwGroup(true, true, strGroupName, pwGroupIcon);

                pg      = new PwGroup(true, true);
                pg.Name = strGroupName;

                pwStorage.RootGroup.AddGroup(pg, true);
                dictGroups[strGroupName] = pg;
            }

            PwEntry pe = new PwEntry(true, true);

            pg.AddEntry(pe, true);

            pe.IconId = pwIcon;

            StrUtil.AddTags(pe.Tags, StrUtil.StringToTags(strType));

            for (int iField = 0; iField < 9; ++iField)
            {
                string strData = ParseCsvWord(vLine[iField + 1]);
                if (strData.Length == 0)
                {
                    continue;
                }

                string strLookup = ((mp != null) ? mp.FieldNames[iField] :
                                    null);
                string strField = (strLookup ?? ("Field " + (iField + 1).ToString()));

                ImportUtil.AppendToField(pe, strField, strData, pwStorage);
            }

            ImportUtil.AppendToField(pe, PwDefs.NotesField, ParseCsvWord(vLine[11]),
                                     pwStorage);

            DateTime?odt = TimeUtil.ParseUSTextDate(ParseCsvWord(vLine[10]),
                                                    DateTimeKind.Local);

            if (odt.HasValue)
            {
                DateTime dt = TimeUtil.ToUtc(odt.Value, false);
                pe.LastAccessTime       = dt;
                pe.LastModificationTime = dt;
            }
        }
コード例 #5
0
        public override void Import(PwDatabase pwStorage, Stream sInput,
                                    IStatusLogger slLogger)
        {
            StreamReader sr      = new StreamReader(sInput, StrUtil.Utf8, true);
            string       strData = sr.ReadToEnd();

            sr.Close();

            CsvOptions opt = new CsvOptions();

            opt.BackslashIsEscape = false;
            opt.TextQualifier     = char.MinValue;
            opt.TrimFields        = true;

            CsvStreamReaderEx csv = new CsvStreamReaderEx(strData, opt);

            string strMapIgnore  = Guid.NewGuid().ToString();
            string strMapGroup   = Guid.NewGuid().ToString();
            string strMapTags    = Guid.NewGuid().ToString();
            string strMapLastMod = Guid.NewGuid().ToString();
            string strMapEMail   = Guid.NewGuid().ToString();

            Dictionary <string, string> dMaps = new Dictionary <string, string>(
                StrUtil.CaseIgnoreComparer);

            dMaps["title"]                  = PwDefs.TitleField;
            dMaps["type"]                   = strMapIgnore;
            dMaps["username_field"]         = strMapIgnore;
            dMaps["username"]               = PwDefs.UserNameField;
            dMaps["password_field"]         = strMapIgnore;
            dMaps["password"]               = PwDefs.PasswordField;
            dMaps["url"]                    = PwDefs.UrlField;
            dMaps["category"]               = strMapGroup;
            dMaps["note"]                   = PwDefs.NotesField;
            dMaps["autofill"]               = strMapIgnore;
            dMaps["autofillenabled"]        = strMapIgnore;
            dMaps["last_password_change"]   = strMapIgnore;
            dMaps["lastmodified"]           = strMapLastMod;
            dMaps["iban"]                   = PwDefs.UserNameField;
            dMaps["bic"]                    = "BIC";
            dMaps["banking_pin"]            = PwDefs.PasswordField;
            dMaps["card_number"]            = PwDefs.UserNameField;
            dMaps["card_holder"]            = "Card Holder";
            dMaps["card_pin"]               = PwDefs.PasswordField;
            dMaps["card_verification_code"] = "Verification Code";
            dMaps["valid_from"]             = "Valid From";
            dMaps["valid_thru"]             = "Valid To";
            dMaps["name"]                   = PwDefs.UserNameField;
            dMaps["firstname"]              = PwDefs.UserNameField;
            dMaps["street"]                 = PwDefs.NotesField;
            dMaps["houseno"]                = PwDefs.NotesField;
            dMaps["zip"]                    = PwDefs.NotesField;
            dMaps["city"]                   = PwDefs.NotesField;
            dMaps["mobile_phone"]           = PwDefs.NotesField;
            dMaps["phone"]                  = PwDefs.NotesField;
            dMaps["email"]                  = strMapEMail;
            dMaps["birthday"]               = "Birthday";
            dMaps["tags"]                   = strMapTags;
            dMaps["keyword"]                = strMapTags;

            string[] vNames = csv.ReadLine();
            if ((vNames == null) || (vNames.Length == 0))
            {
                Debug.Assert(false); return;
            }

            for (int i = 0; i < vNames.Length; ++i)
            {
                string str = vNames[i];

                if (string.IsNullOrEmpty(str))
                {
                    Debug.Assert(false); str = strMapIgnore;
                }
                else
                {
                    string strMapped = null;
                    dMaps.TryGetValue(str, out strMapped);

                    if (string.IsNullOrEmpty(strMapped))
                    {
                        Debug.Assert(false);
                        strMapped = ImportUtil.MapNameToStandardField(str, true);
                        if (string.IsNullOrEmpty(strMapped))
                        {
                            strMapped = PwDefs.NotesField;
                        }
                    }

                    str = strMapped;
                }

                vNames[i] = str;
            }

            Dictionary <string, PwGroup> dGroups = new Dictionary <string, PwGroup>();

            while (true)
            {
                string[] v = csv.ReadLine();
                if (v == null)
                {
                    break;
                }
                if (v.Length == 0)
                {
                    continue;
                }

                PwEntry pe = new PwEntry(true, true);
                PwGroup pg = pwStorage.RootGroup;

                for (int i = 0; i < v.Length; ++i)
                {
                    string strValue = v[i];
                    if (string.IsNullOrEmpty(strValue))
                    {
                        continue;
                    }

                    strValue = strValue.Replace(@"<COMMA>", ",");
                    strValue = strValue.Replace(@"<-N/L-/>", "\n");

                    strValue = StrUtil.NormalizeNewLines(strValue, true);

                    string strName = ((i < vNames.Length) ? vNames[i] : PwDefs.NotesField);

                    if (strName == strMapIgnore)
                    {
                    }
                    else if (strName == strMapGroup)
                    {
                        dGroups.TryGetValue(strValue, out pg);
                        if (pg == null)
                        {
                            pg      = new PwGroup(true, true);
                            pg.Name = strValue;

                            pwStorage.RootGroup.AddGroup(pg, true);
                            dGroups[strValue] = pg;
                        }
                    }
                    else if (strName == strMapTags)
                    {
                        StrUtil.AddTags(pe.Tags, StrUtil.StringToTags(strValue));
                    }
                    else if (strName == strMapLastMod)
                    {
                        double dUnix;
                        if (double.TryParse(strValue, out dUnix))
                        {
                            pe.LastModificationTime = TimeUtil.ConvertUnixTime(dUnix);
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                    }
                    else if (strName == strMapEMail)
                    {
                        ImportUtil.AppendToField(pe, PwDefs.UrlField,
                                                 "mailto:" + strValue, pwStorage);
                    }
                    else
                    {
                        ImportUtil.AppendToField(pe, strName, strValue, pwStorage);
                    }
                }

                pg.AddEntry(pe, true);
            }
        }
コード例 #6
0
        private static void ImportGroup(XmlNode xmlNode, PwDatabase pwStorage,
                                        PwGroup pg)
        {
            PwGroup pgSub = pg;
            PwEntry pe    = null;

            foreach (XmlNode xmlChild in xmlNode)
            {
                if (xmlChild.Name == "A")
                {
                    pe = new PwEntry(true, true);
                    pg.AddEntry(pe, true);

                    pe.Strings.Set(PwDefs.TitleField, new ProtectedString(
                                       pwStorage.MemoryProtection.ProtectTitle,
                                       XmlUtil.SafeInnerText(xmlChild)));

                    XmlNode xnUrl = xmlChild.Attributes.GetNamedItem("HREF");
                    if ((xnUrl != null) && (xnUrl.Value != null))
                    {
                        pe.Strings.Set(PwDefs.UrlField, new ProtectedString(
                                           pwStorage.MemoryProtection.ProtectUrl, xnUrl.Value));
                    }
                    else
                    {
                        Debug.Assert(false);
                    }

                    // pe.Strings.Set("RDF_ID", new ProtectedString(
                    //	false, xmlChild.Attributes.GetNamedItem("ID").Value));

                    ImportIcon(xmlChild, pe, pwStorage);

                    XmlNode xnTags = xmlChild.Attributes.GetNamedItem("TAGS");
                    if ((xnTags != null) && (xnTags.Value != null))
                    {
                        StrUtil.AddTags(pe.Tags, xnTags.Value.Split(','));
                    }
                }
                else if (xmlChild.Name == "DD")
                {
                    if (pe != null)
                    {
                        ImportUtil.AppendToField(pe, PwDefs.NotesField,
                                                 XmlUtil.SafeInnerText(xmlChild).Trim(), pwStorage);
                    }
                    else
                    {
                        Debug.Assert(false);
                    }
                }
                else if (xmlChild.Name == "H3")
                {
                    string strGroup = XmlUtil.SafeInnerText(xmlChild);
                    if (strGroup.Length == 0)
                    {
                        Debug.Assert(false); pgSub = pg;
                    }
                    else
                    {
                        pgSub = new PwGroup(true, true, strGroup, PwIcon.Folder);
                        pg.AddGroup(pgSub, true);
                    }
                }
                else if (xmlChild.Name == "DL")
                {
                    ImportGroup(xmlChild, pwStorage, pgSub);
                }
                else
                {
                    Debug.Assert(false);
                }
            }
        }