コード例 #1
0
        private static string GetFolderName(bool bForceRoot, PwExportInfo pwExportInfo,
                                            PwGroup pg)
        {
            string strBaseName = UrlUtil.FilterFileName(string.IsNullOrEmpty(
                                                            Program.Config.Defaults.WinFavsBaseFolderName) ? PwDefs.ShortProductName :
                                                        Program.Config.Defaults.WinFavsBaseFolderName);

            if (bForceRoot || (pwExportInfo == null) || (pg == null))
            {
                return(strBaseName);
            }

            string strGroup    = UrlUtil.FilterFileName(pg.Name);
            string strRootName = strBaseName;

            if (strGroup.Length > 0)
            {
                strRootName += (" - " + strGroup);
            }

            if (pwExportInfo.ContextDatabase != null)
            {
                if (pg == pwExportInfo.ContextDatabase.RootGroup)
                {
                    strRootName = strBaseName;
                }
            }

            return(strRootName);
        }
コード例 #2
0
        private static void ExportGroup(PwGroup pg, string strDir, IStatusLogger slLogger,
                                        uint uTotalEntries, ref uint uEntriesProcessed, PwExportInfo pxi)
        {
            foreach (PwEntry pe in pg.Entries)
            {
                ExportEntry(pe, strDir, pxi);

                ++uEntriesProcessed;
                if (slLogger != null)
                {
                    slLogger.SetProgress(((uEntriesProcessed * 50U) /
                                          uTotalEntries) + 50U);
                }
            }

            foreach (PwGroup pgSub in pg.Groups)
            {
                string strGroup = UrlUtil.FilterFileName(pgSub.Name);
                string strSub   = (UrlUtil.EnsureTerminatingSeparator(strDir, false) +
                                   (!string.IsNullOrEmpty(strGroup) ? strGroup : KPRes.Group));

                ExportGroup(pgSub, strSub, slLogger, uTotalEntries,
                            ref uEntriesProcessed, pxi);
            }
        }
コード例 #3
0
        private static void ExportEntry(PwEntry pe, string strDir)
        {
            string strUrl = pe.Strings.ReadSafe(PwDefs.UrlField);

            if (string.IsNullOrEmpty(strUrl))
            {
                return;
            }

            string strTitle = pe.Strings.ReadSafe(PwDefs.TitleField);

            if (string.IsNullOrEmpty(strTitle))
            {
                strTitle = KPRes.Entry;
            }
            strTitle = Program.Config.Defaults.WinFavsFileNamePrefix + strTitle;

            string strSuffix = Program.Config.Defaults.WinFavsFileNameSuffix + ".url";

            strSuffix = UrlUtil.FilterFileName(strSuffix);

            string strFileBase = (UrlUtil.EnsureTerminatingSeparator(strDir,
                                                                     false) + UrlUtil.FilterFileName(strTitle));
            string strFile = strFileBase + strSuffix;
            int    iFind   = 2;

            while (File.Exists(strFile))
            {
                strFile = strFileBase + " (" + iFind.ToString() + ")" + strSuffix;
                ++iFind;
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine(@"[InternetShortcut]");
            sb.AppendLine(@"URL=" + strUrl);             // No additional line break
            sb.AppendLine(@"[" + PwDefs.ShortProductName + @"]");
            sb.AppendLine(IniTypeKey + @"=" + IniTypeValue);
            // Terminating line break is important

            if (!Directory.Exists(strDir))
            {
                try { Directory.CreateDirectory(strDir); }
                catch (Exception exDir)
                {
                    throw new Exception(strDir + MessageService.NewParagraph + exDir.Message);
                }

                WaitForDirCommit(strDir, true);
            }

            try { File.WriteAllText(strFile, sb.ToString(), Encoding.Default); }
            catch (Exception exWrite)
            {
                throw new Exception(strFile + MessageService.NewParagraph + exWrite.Message);
            }
        }
コード例 #4
0
        private static void ExportEntry(PwEntry pe, string strDir, PwExportInfo pxi)
        {
            PwDatabase pd  = ((pxi != null) ? pxi.ContextDatabase : null);
            SprContext ctx = new SprContext(pe, pd, SprCompileFlags.NonActive, false, false);

            KeyValuePair <string, string>?okvpCmd = null;
            string strUrl = SprEngine.Compile(pe.Strings.ReadSafe(PwDefs.UrlField), ctx);

            if (WinUtil.IsCommandLineUrl(strUrl))
            {
                strUrl = WinUtil.GetCommandLineFromUrl(strUrl);

                if (!NativeLib.IsUnix())                // LNKs only supported on Windows
                {
                    string strApp, strArgs;
                    StrUtil.SplitCommandLine(strUrl, out strApp, out strArgs);

                    if (!string.IsNullOrEmpty(strApp))
                    {
                        okvpCmd = new KeyValuePair <string, string>(strApp, strArgs);
                    }
                }
            }
            if (string.IsNullOrEmpty(strUrl))
            {
                return;
            }
            bool bLnk = okvpCmd.HasValue;

            string strTitleCmp = SprEngine.Compile(pe.Strings.ReadSafe(PwDefs.TitleField), ctx);

            if (string.IsNullOrEmpty(strTitleCmp))
            {
                strTitleCmp = KPRes.Entry;
            }
            string strTitle = Program.Config.Defaults.WinFavsFileNamePrefix + strTitleCmp;

            string strSuffix = Program.Config.Defaults.WinFavsFileNameSuffix +
                               (bLnk ? ".lnk" : ".url");

            strSuffix = UrlUtil.FilterFileName(strSuffix);

            string strFileBase = (UrlUtil.EnsureTerminatingSeparator(strDir,
                                                                     false) + UrlUtil.FilterFileName(strTitle));
            string strFile = strFileBase + strSuffix;
            int    iFind   = 2;

            while (File.Exists(strFile))
            {
                strFile = strFileBase + " (" + iFind.ToString() + ")" + strSuffix;
                ++iFind;
            }

            if (!Directory.Exists(strDir))
            {
                try { Directory.CreateDirectory(strDir); }
                catch (Exception exDir)
                {
                    throw new Exception(strDir + MessageService.NewParagraph + exDir.Message);
                }

                WaitForDirCommit(strDir, true);
            }

            try
            {
                if (bLnk)
                {
                    int    ccMaxDesc = NativeMethods.INFOTIPSIZE - 1 - LnkDescSuffix.Length;
                    string strDesc   = StrUtil.CompactString3Dots(strUrl, ccMaxDesc) +
                                       LnkDescSuffix;

                    ShellLinkEx sl = new ShellLinkEx(okvpCmd.Value.Key,
                                                     okvpCmd.Value.Value, strDesc);
                    sl.Save(strFile);
                }
                else
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine(@"[InternetShortcut]");
                    sb.AppendLine(@"URL=" + strUrl);                     // No additional line break
                    sb.AppendLine(@"[" + PwDefs.ShortProductName + @"]");
                    sb.AppendLine(IniTypeKey + @"=" + IniTypeValue);
                    // Terminating line break is important

                    File.WriteAllText(strFile, sb.ToString(), Encoding.Default);
                }
            }
            catch (Exception exWrite)
            {
                throw new Exception(strFile + MessageService.NewParagraph + exWrite.Message);
            }
        }
コード例 #5
0
        private void OnBtnCustomSave(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection lvsic = m_lvCustomIcons.SelectedItems;
            if (lvsic.Count == 0)
            {
                return;
            }

            if (lvsic.Count == 1)
            {
                PwCustomIcon ci = (lvsic[0].Tag as PwCustomIcon);
                if (ci == null)
                {
                    Debug.Assert(false); return;
                }

                string strName = KPRes.Export;
                if (ci.Name.Length != 0)
                {
                    strName = ci.Name;
                }
                strName = UrlUtil.FilterFileName(strName);

                StringBuilder sbFilter = new StringBuilder();
                AddFileType(sbFilter, "*.png", "Portable Network Graphics (*.png)");
                // AddFileType(sbFilter, "*.ico", "Windows Icon (*.ico)");
                sbFilter.Append(@"|" + KPRes.AllFiles + @" (*.*)|*.*");

                SaveFileDialogEx sfd = UIUtil.CreateSaveFileDialog(KPRes.ExportFileTitle,
                                                                   strName + ".png", sbFilter.ToString(), 1, null,
                                                                   AppDefs.FileDialogContext.Export);
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    SaveImageFile(ci, sfd.FileName);
                }
            }
            else             // lvsic.Count >= 2
            {
                FolderBrowserDialog fbd = UIUtil.CreateFolderBrowserDialog(KPRes.ExportToPrompt);
                if (fbd.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                string strDir = UrlUtil.EnsureTerminatingSeparator(
                    fbd.SelectedPath, false);
                Dictionary <string, int> dStart = new Dictionary <string, int>();

                foreach (ListViewItem lvi in lvsic)
                {
                    try
                    {
                        PwCustomIcon ci = (lvi.Tag as PwCustomIcon);
                        if (ci == null)
                        {
                            Debug.Assert(false); continue;
                        }

                        string strName = KPRes.Export;
                        if (ci.Name.Length != 0)
                        {
                            strName = ci.Name;
                        }
                        strName = UrlUtil.FilterFileName(strName);

                        int iStart;
                        dStart.TryGetValue(strName, out iStart);

                        for (int i = iStart; i < int.MaxValue; ++i)
                        {
                            string strFile = strDir + strName + ((i == 0) ?
                                                                 string.Empty : (" (" + i.ToString() + ")")) +
                                             ".png";

                            if (!File.Exists(strFile))
                            {
                                SaveImageFile(ci, strFile);
                                dStart[strName] = i + 1;
                                break;
                            }
                        }
                    }
                    catch (Exception ex) { MessageService.ShowWarning(ex); }
                }
            }
        }