-
Notifications
You must be signed in to change notification settings - Fork 5
/
CommonUtils.cs
166 lines (143 loc) · 5.45 KB
/
CommonUtils.cs
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Resources;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
namespace TextForge
{
internal class CommonUtils
{
private class RememberAccessChoice
{
private Dictionary<string, bool> _rememberAccess = new Dictionary<string, bool>();
public bool IsGrantedAccess(string website)
{
bool result;
_rememberAccess.TryGetValue(website, out result);
return result;
}
public void Grant(string website)
{
_rememberAccess[website] = true;
}
public void Revoke(string website)
{
_rememberAccess[website] = false;
}
}
public static readonly HttpClient client = new HttpClient();
private static RememberAccessChoice _accessChoice = new RememberAccessChoice();
public static void ConfigureTLS()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13;
}
public static void DisplayError(Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public static void DisplayError(string messageIntro, Exception ex)
{
MessageBox.Show($"{messageIntro}: {ex.Message}", ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public static void DisplayWarning(Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
public static void DisplayInformation(Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public static bool GetInternetAccessPermission(Uri uri)
{
string baseUrl = uri.GetLeftPart(UriPartial.Authority);
if (_accessChoice.IsGrantedAccess(baseUrl))
{
return true;
}
else
{
var result = MessageBox.Show(
$"{Forge.CultureHelper.GetLocalizedString("(CommonUtils.cs) [GetInternetAccessPermission] MessageBox #1 Text")}{Environment.NewLine}{baseUrl}",
Forge.CultureHelper.GetLocalizedString("(CommonUtils.cs) [GetInternetAccessPermission] MessageBox #1 Caption"), MessageBoxButtons.YesNo, MessageBoxIcon.Warning
);
if (result == DialogResult.Yes)
{
_accessChoice.Grant(baseUrl);
return true;
}
return false;
}
}
public static Word.Application GetApplication()
{
return Globals.ThisAddIn.Application;
}
public static Document GetActiveDocument()
{
return Globals.ThisAddIn.Application.ActiveDocument;
}
public static Comments GetComments()
{
return GetActiveDocument().Comments;
}
public static Range GetSelectionRange()
{
return GetApplication().Selection.Range;
}
public static int GetWordPageCount()
{
int pageCount = GetActiveDocument().ComputeStatistics(Word.WdStatistic.wdStatisticPages, false);
return pageCount;
}
public static IEnumerable<string> SplitString(string str, int chunkSize)
{
List<string> result = new List<string>();
for (int i = 0; i < str.Length; i += chunkSize)
{
if (i + chunkSize > str.Length)
chunkSize = str.Length - i;
result.Add(str.Substring(i, chunkSize));
}
return result;
}
public static string SubstringTokens(string text, int maxTokens)
{
return SubstringWithoutBounds(text, TokensToCharCount(maxTokens));
}
private static string SubstringWithoutBounds(string text, int maxLen)
{
return (maxLen >= text.Length) ? text : text.Substring(0, maxLen);
}
public static int TokensToCharCount(int tokenCount)
{
return tokenCount * 4; // https://platform.openai.com/tokenizer
}
public static int CharToTokenCount(int charCount)
{
return charCount / 4; // https://platform.openai.com/tokenizer
}
public static void GetEnvironmentVariableIfAvailable(ref string dest, string variable)
{
var key = Environment.GetEnvironmentVariable(variable);
if (key != null)
dest = key;
}
}
public class CultureLocalizationHelper
{
private ResourceManager _resourceManager;
public CultureLocalizationHelper(string baseName, System.Reflection.Assembly assembly)
{
_resourceManager = new ResourceManager(baseName, assembly);
}
public string GetLocalizedString(string key, CultureInfo culture = null)
{
string localizedString = _resourceManager.GetString(key, culture);
return (localizedString == null) ? _resourceManager.GetString(key, CultureInfo.InvariantCulture) : localizedString;
}
}
}