Пример #1
0
        private static void DeletePreviousExport(string strDir, IStatusLogger slLogger)
        {
            List <string> vDirsToDelete = new List <string>();

            try
            {
                List <string> lUrlFiles = UrlUtil.GetFilePaths(strDir, "*.url",
                                                               SearchOption.AllDirectories);
                List <string> lLnkFiles = UrlUtil.GetFilePaths(strDir, "*.lnk",
                                                               SearchOption.AllDirectories);

                List <string> lFiles = new List <string>();
                lFiles.AddRange(lUrlFiles);
                lFiles.AddRange(lLnkFiles);

                for (int iFile = 0; iFile < lFiles.Count; ++iFile)
                {
                    string strFile = lFiles[iFile];
                    try
                    {
                        bool bDelete = false;

                        if (strFile.EndsWith(".url", StrUtil.CaseIgnoreCmp))
                        {
                            IniFile ini     = IniFile.Read(strFile, Encoding.Default);
                            string  strType = ini.Get(PwDefs.ShortProductName, IniTypeKey);
                            bDelete = ((strType != null) && (strType == IniTypeValue));
                        }
                        else if (strFile.EndsWith(".lnk", StrUtil.CaseIgnoreCmp))
                        {
                            ShellLinkEx sl = ShellLinkEx.Load(strFile);
                            if (sl != null)
                            {
                                bDelete = ((sl.Description != null) &&
                                           sl.Description.EndsWith(LnkDescSuffix));
                            }
                        }
                        else
                        {
                            Debug.Assert(false);
                        }

                        if (bDelete)
                        {
                            File.Delete(strFile);

                            string strCont = UrlUtil.GetFileDirectory(strFile, false, true);
                            if (vDirsToDelete.IndexOf(strCont) < 0)
                            {
                                vDirsToDelete.Add(strCont);
                            }
                        }
                    }
                    catch (Exception) { Debug.Assert(false); }

                    if (slLogger != null)
                    {
                        slLogger.SetProgress(((uint)iFile * 50U) / (uint)lFiles.Count);
                    }
                }

                bool bDeleted = true;
                while (bDeleted)
                {
                    bDeleted = false;

                    for (int i = (vDirsToDelete.Count - 1); i >= 0; --i)
                    {
                        try
                        {
                            Directory.Delete(vDirsToDelete[i], false);
                            WaitForDirCommit(vDirsToDelete[i], false);

                            vDirsToDelete.RemoveAt(i);
                            bDeleted = true;
                        }
                        catch (Exception) { }                        // E.g. not empty
                    }
                }
            }
            catch (Exception) { Debug.Assert(false); }
        }
Пример #2
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);
            }
        }