File: AppHelp.cs

package info (click to toggle)
keepass2 2.47%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 13,992 kB
  • sloc: cs: 111,053; xml: 5,956; cpp: 308; makefile: 48; sh: 48
file content (206 lines) | stat: -rw-r--r-- 5,713 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2021 Dominik Reichl <dominik.reichl@t-online.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Diagnostics;
using System.IO;

using KeePass.Util;
using KeePass.Util.Spr;

using KeePassLib;
using KeePassLib.Native;
using KeePassLib.Utility;

namespace KeePass.App
{
	public enum AppHelpSource
	{
		Local,
		Online
	}

	/// <summary>
	/// Application help provider. Starts an external application that
	/// shows help on a specified topic.
	/// </summary>
	public static class AppHelp
	{
		private static string g_strLocalHelpFile = null;

		/// <summary>
		/// Get the path of the local help file.
		/// </summary>
		public static string LocalHelpFile
		{
			get
			{
				if(g_strLocalHelpFile == null)
					g_strLocalHelpFile = UrlUtil.StripExtension(
						WinUtil.GetExecutable()) + ".chm";

				return g_strLocalHelpFile;
			}
		}

		public static bool LocalHelpAvailable
		{
			get
			{
				try
				{
					string strFile = AppHelp.LocalHelpFile;
					if(!string.IsNullOrEmpty(strFile))
						return Directory.Exists("/usr/share/doc/keepass2/Chm/help/");
				}
				catch(Exception) { Debug.Assert(false); }

				return false;
			}
		}

		public static AppHelpSource PreferredHelpSource
		{
			get
			{
				return (Program.Config.Application.HelpUseLocal ?
					AppHelpSource.Local : AppHelpSource.Online);
			}

			set
			{
				Program.Config.Application.HelpUseLocal =
					(value == AppHelpSource.Local);
			}
		}

		/// <summary>
		/// Show a help page.
		/// </summary>
		/// <param name="strTopic">Topic name. May be <c>null</c>.</param>
		/// <param name="strSection">Section name. May be <c>null</c>. Must not start
		/// with the '#' character.</param>
		public static void ShowHelp(string strTopic, string strSection)
		{
			AppHelp.ShowHelp(strTopic, strSection, false);
		}

		/// <summary>
		/// Show a help page.
		/// </summary>
		/// <param name="strTopic">Topic name. May be <c>null</c>.</param>
		/// <param name="strSection">Section name. May be <c>null</c>.
		/// Must not start with the '#' character.</param>
		/// <param name="bPreferLocal">Specify if the local help file should be
		/// preferred. If no local help file is available, the online help
		/// system will be used, independent of the <c>bPreferLocal</c> flag.</param>
		public static void ShowHelp(string strTopic, string strSection, bool bPreferLocal)
		{
			if(AppHelp.LocalHelpAvailable)
			{
				if(bPreferLocal || (AppHelp.PreferredHelpSource == AppHelpSource.Local))
					AppHelp.ShowHelpLocal(strTopic, strSection);
				else
					AppHelp.ShowHelpOnline(strTopic, strSection);
			}
			else AppHelp.ShowHelpOnline(strTopic, strSection);
		}

		private static void ShowHelpLocal(string strTopic, string strSection)
		{
			string strFile = AppHelp.LocalHelpFile;
			if(string.IsNullOrEmpty(strFile)) { Debug.Assert(false); return; }

			string strCmd = "/usr/share/doc/keepass2/Chm/help/";

			if(!string.IsNullOrEmpty(strTopic))
			{
				strCmd += strTopic + ".html";

				if(!string.IsNullOrEmpty(strSection))
					strCmd += "#" + strSection;
			}
			else
			{
				strCmd += "../index.html";
			}

			try { Process.Start("x-www-browser", strCmd); }
			catch(Exception exStart)
			{
				MessageService.ShowWarning("x-www-browser " + strCmd, exStart);
			}
		}

		private static bool ShowHelpLocalKcv(string strQuotedMsIts)
		{
			try
			{
				if(!NativeLib.IsUnix()) return false;

				string strApp = AppLocator.FindAppUnix("kchmviewer");
				if(string.IsNullOrEmpty(strApp)) return false;

				string strFile = StrUtil.GetStringBetween(strQuotedMsIts, 0, ":", "::");
				if(string.IsNullOrEmpty(strFile))
					strFile = StrUtil.GetStringBetween(strQuotedMsIts, 0, ":", "\"");
				if(string.IsNullOrEmpty(strFile))
				{
					Debug.Assert(false);
					return false;
				}

				string strUrl = StrUtil.GetStringBetween(strQuotedMsIts, 0, "::", "\"");

				// https://www.ulduzsoft.com/linux/kchmviewer/kchmviewer-integration-reference/
				string strArgs = "\"" + SprEncoding.EncodeForCommandLine(strFile) + "\"";
				if(!string.IsNullOrEmpty(strUrl))
					strArgs = "-showPage \"" + SprEncoding.EncodeForCommandLine(
						strUrl) + "\" " + strArgs;

				NativeLib.StartProcess(strApp, strArgs);
				return true;
			}
			catch(Exception) { Debug.Assert(false); }

			return false;
		}

		private static void ShowHelpOnline(string strTopic, string strSection)
		{
			string strUrl = GetOnlineUrl(strTopic, strSection);
			WinUtil.OpenUrl(strUrl, null);
		}

		internal static string GetOnlineUrl(string strTopic, string strSection)
		{
			string str = PwDefs.HelpUrl;

			if(!string.IsNullOrEmpty(strTopic))
			{
				str += strTopic + ".html";

				if(!string.IsNullOrEmpty(strSection))
					str += "#" + strSection;
			}

			return str;
		}
	}
}