Beispiel #1
0
 private void SetProfilePath(string path)
 {
     if (string.IsNullOrEmpty(path))
     {
         profilePathLabel.Text = string.Empty;
     }
     else
     {
         profilePathLabel.Text = StrUtil.CompactString3Dots(path, 65);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Shorten a path.
        /// </summary>
        /// <param name="strPath">Path to make shorter.</param>
        /// <param name="nMaxChars">Maximum number of characters in the returned string.</param>
        /// <returns>Shortened path.</returns>
        public static string CompactPath(string strPath, int nMaxChars)
        {
            Debug.Assert(strPath != null);
            if (strPath == null)
            {
                throw new ArgumentNullException("strPath");
            }
            Debug.Assert(nMaxChars >= 0);
            if (nMaxChars < 0)
            {
                throw new ArgumentOutOfRangeException("nMaxChars");
            }

            if (nMaxChars == 0)
            {
                return(string.Empty);
            }
            if (strPath.Length <= nMaxChars)
            {
                return(strPath);
            }

            try
            {
                StringBuilder sb = new StringBuilder(strPath.Length * 2 + 4);

                if (NativeMethods.PathCompactPathEx(sb, strPath, (uint)nMaxChars, 0) == false)
                {
                    return(StrUtil.CompactString3Dots(strPath, nMaxChars));
                }

                Debug.Assert(sb.Length <= nMaxChars);
                if ((sb.Length <= nMaxChars) && (sb.Length != 0))
                {
                    return(sb.ToString());
                }
                else
                {
                    return(StrUtil.CompactString3Dots(strPath, nMaxChars));
                }
            }
            catch (Exception) { Debug.Assert(false); }

            return(StrUtil.CompactString3Dots(strPath, nMaxChars));
        }
Beispiel #3
0
        /// <summary>
        /// Shorten a path.
        /// </summary>
        /// <param name="strPath">Path to make shorter.</param>
        /// <param name="cchMax">Maximum number of characters in the returned string.</param>
        /// <returns>Shortened path.</returns>
        public static string CompactPath(string strPath, int cchMax)
        {
            Debug.Assert(strPath != null);
            if (strPath == null)
            {
                throw new ArgumentNullException("strPath");
            }
            Debug.Assert(cchMax >= 0);
            if (cchMax < 0)
            {
                throw new ArgumentOutOfRangeException("cchMax");
            }

            if (strPath.Length <= cchMax)
            {
                return(strPath);
            }
            if (cchMax == 0)
            {
                return(string.Empty);
            }

            try
            {
                if (!NativeLib.IsUnix())
                {
                    StringBuilder sb = new StringBuilder(strPath.Length + 2);

                    if (NativeMethods.PathCompactPathEx(sb, strPath, (uint)cchMax + 1, 0))
                    {
                        if ((sb.Length <= cchMax) && (sb.Length != 0))
                        {
                            return(sb.ToString());
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                    }
                }
            }
            catch (Exception) { Debug.Assert(false); }

            return(StrUtil.CompactString3Dots(strPath, cchMax));
        }
Beispiel #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);
            }
        }
Beispiel #5
0
        public static string CreateSummaryList(PwGroup pgSubGroups, PwEntry[] vEntries)
        {
            int    nMaxEntries = 10;
            string strSummary  = string.Empty;

            if (pgSubGroups != null)
            {
                PwObjectList <PwGroup> vGroups = pgSubGroups.GetGroups(true);
                if (vGroups.UCount > 0)
                {
                    StringBuilder sbGroups = new StringBuilder();
                    sbGroups.Append("- ");
                    uint uToList = Math.Min(3U, vGroups.UCount);
                    for (uint u = 0; u < uToList; ++u)
                    {
                        if (sbGroups.Length > 2)
                        {
                            sbGroups.Append(", ");
                        }
                        sbGroups.Append(vGroups.GetAt(u).Name);
                    }
                    if (uToList < vGroups.UCount)
                    {
                        sbGroups.Append(", ...");
                    }
                    strSummary += sbGroups.ToString();                     // New line below

                    nMaxEntries -= 2;
                }
            }

            int nSummaryShow = Math.Min(nMaxEntries, vEntries.Length);

            if (nSummaryShow == (vEntries.Length - 1))
            {
                --nSummaryShow;                                                   // Plural msg
            }
            for (int iSumEnum = 0; iSumEnum < nSummaryShow; ++iSumEnum)
            {
                if (strSummary.Length > 0)
                {
                    strSummary += MessageService.NewLine;
                }

                PwEntry pe = vEntries[iSumEnum];
                strSummary += ("- " + StrUtil.CompactString3Dots(
                                   pe.Strings.ReadSafe(PwDefs.TitleField), 39));
                if (PwDefs.IsTanEntry(pe))
                {
                    string strTanIdx = pe.Strings.ReadSafe(PwDefs.UserNameField);
                    if (!string.IsNullOrEmpty(strTanIdx))
                    {
                        strSummary += (@" (#" + strTanIdx + @")");
                    }
                }
            }
            if (nSummaryShow != vEntries.Length)
            {
                strSummary += (MessageService.NewLine + "- " +
                               KPRes.MoreEntries.Replace(@"{PARAM}", (vEntries.Length -
                                                                      nSummaryShow).ToString()));
            }

            return(strSummary);
        }
Beispiel #6
0
        private static void DrawText(Graphics g, string strText, int x,
                                     int y, Font font, Color clrForeground, bool bRtl, int wImg)
        {
            if (string.IsNullOrEmpty(strText))
            {
                return;
            }

            // With ClearType on, text drawn using Graphics.DrawString
            // looks better than TextRenderer.DrawText;
            // https://sourceforge.net/p/keepass/discussion/329220/thread/06ef4466/

            /* // On Windows 2000 the DrawText method taking a Point doesn't
             * // work by design, see MSDN:
             * // https://msdn.microsoft.com/en-us/library/ms160657.aspx
             * if(WinUtil.IsWindows2000)
             *      TextRenderer.DrawText(g, strText, font, new Rectangle(pt.X, pt.Y,
             *              nWidth - pt.X - 1, nHeight - pt.Y - 1), clrForeground, tff);
             * else
             *      TextRenderer.DrawText(g, strText, font, pt, clrForeground, tff); */

            using (SolidBrush br = new SolidBrush(clrForeground))
            {
                StringFormatFlags sff = (StringFormatFlags.FitBlackBox |
                                         StringFormatFlags.NoClip);
                if (bRtl)
                {
                    sff |= StringFormatFlags.DirectionRightToLeft;
                }

                using (StringFormat sf = new StringFormat(sff))
                {
                    bool bDrawn = false;

                    try
                    {
                        if (bRtl)
                        {
                            return;                              // Default draw (in 'finally')
                        }
                        GraphicsUnit gu = g.PageUnit;            // For MeasureString
                        if ((gu != GraphicsUnit.Pixel) && (gu != GraphicsUnit.Display))
                        {
                            Debug.Assert(false);                             // The code below assumes pixels
                            return;
                        }

                        int wTextMax = wImg - x - 1;                         // 1 px free on right
                        if (wTextMax <= 0)
                        {
                            Debug.Assert(false); return;
                        }

                        for (int cch = strText.Length; cch > 0; --cch)
                        {
                            string str   = StrUtil.CompactString3Dots(strText, cch);
                            int    wText = (int)g.MeasureString(str, font).Width;

                            if (wText <= 0)
                            {
                                Debug.Assert(false);
                                break;                                 // Default draw (in 'finally')
                            }
                            if (wText <= wTextMax)
                            {
                                g.DrawString(str, font, br, x, y, sf);
                                bDrawn = true;
                                break;
                            }
                        }
                        Debug.Assert(bDrawn);                         // Even one char too wide?!
                    }
                    catch (Exception) { Debug.Assert(false); }
                    finally
                    {
                        if (!bDrawn)
                        {
                            g.DrawString(strText, font, br, x, y, sf);
                        }
                    }
                }
            }
        }
Beispiel #7
0
        private string CreateSummaryList(IEnumerable <String> attachmentNames)
        {
            var maxItemsToShow = 10;
            // Consistent behaviour with EntryUtil.CreateSummaryList (EntryUtil.cs)
            var summary = String.Join(MessageService.NewLine, (from attachmentName in attachmentNames.Take(maxItemsToShow) select "- " + StrUtil.CompactString3Dots(attachmentName, 39)).ToArray());

            var count = attachmentNames.Count();

            if (count > maxItemsToShow)
            {
                summary += MessageService.NewLine + "- " + String.Format(Properties.Resources.MoreAttachments, count - maxItemsToShow);
            }

            return(summary);
        }