Skip to content

Commit 06f387a

Browse files
committed
feat: Shorten item display in launcher and make it configurable.
refactor: Make db module stronger.
1 parent f5b581c commit 06f387a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+389
-368
lines changed

Ui/AppInit.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private void WritePermissionCheck(string path, bool isFile)
2828
var flag = isFile == false ? IoPermissionHelper.HasWritePermissionOnDir(path) : IoPermissionHelper.HasWritePermissionOnFile(path);
2929
if (flag == false)
3030
{
31-
MessageBox.Show(LanguageService.Translate("write permissions alert", path), LanguageService.Translate("Warning"), MessageBoxButton.OK);
31+
MessageBoxHelper.ErrorAlert(LanguageService?.Translate("write permissions alert", path) ?? "write permissions error:" + path);
3232
Environment.Exit(1);
3333
}
3434
}
@@ -73,7 +73,7 @@ public void InitOnStart()
7373
{
7474
Debug.Assert(App.ResourceDictionary != null);
7575

76-
LanguageService = new LanguageService(App.ResourceDictionary);
76+
LanguageService = new LanguageService(App.ResourceDictionary!);
7777
LanguageService.SetLanguage(CultureInfo.CurrentCulture.Name.ToLower());
7878
#region Portable mode or not
7979
{
@@ -259,7 +259,7 @@ public void InitOnStart()
259259
}
260260

261261
ConfigurationService.SetSelfStart();
262-
ThemeService = new ThemeService(App.ResourceDictionary, ConfigurationService.Theme);
262+
ThemeService = new ThemeService(App.ResourceDictionary!, ConfigurationService.Theme);
263263
GlobalData = new GlobalData(ConfigurationService);
264264
}
265265

Ui/Controls/AutoCompleteComboBox.xaml.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
3737
e.Handled = true;
3838
this.SelectedItem = null;
3939
var cmbTextBox = (TextBox)this.Template.FindName("PART_EditableTextBox", this);
40-
cmbTextBox.Text = value;
40+
cmbTextBox.Text = value!;
4141
cmbTextBox.CaretIndex = cmbTextBox.Text.Length;
4242
IsDropDownOpen = false;
43-
OnSelectionConfirmByMouse?.Invoke(value);
43+
OnSelectionConfirmByMouse?.Invoke(cmbTextBox.Text);
4444
}
4545
else
4646
{
@@ -115,12 +115,10 @@ public string Text
115115

116116
private static void TextChanged(AutoCompleteComboBox o, string newValue)
117117
{
118-
if ((o.Selections?.Count() ?? 0) == 0)
119-
return;
120-
if (o._textChangedEnabled == false)
121-
return;
118+
if(o.Selections == null) return;
119+
if (!o.Selections.Any()) return;
120+
if (o._textChangedEnabled == false) return;
122121
o._textChangedEnabled = false;
123-
Debug.Assert(o?.Selections != null);
124122
if (string.IsNullOrWhiteSpace(newValue))
125123
{
126124
o.CbContent.IsDropDownOpen = false;

Ui/Model/DAO/Dapper/DapperDataBase.cs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,16 @@ public override void OpenConnection()
6666
throw new NotImplementedException(_databaseType.ToString() + " not supported!");
6767
}
6868
}
69-
_dbConnection.Open();
69+
70+
try
71+
{
72+
_dbConnection.Open();
73+
}
74+
catch (Exception e)
75+
{
76+
MsAppCenterHelper.Error(e, new Dictionary<string, string>() { { "_databaseType", _databaseType.ToString() } });
77+
MessageBoxHelper.ErrorAlert(EnumDatabaseStatus.AccessDenied.GetErrorInfo() + " ----> ", e.Message);
78+
}
7079
}
7180
}
7281

@@ -102,6 +111,8 @@ public override bool IsConnected()
102111

103112
public override void InitTables()
104113
{
114+
OpenConnection();
115+
if (!IsConnected()) return;
105116
_dbConnection?.Execute(@$"
106117
CREATE TABLE IF NOT EXISTS `{Config.TABLE_NAME}` (
107118
`{nameof(Config.Key)}` VARCHAR (64) PRIMARY KEY
@@ -129,6 +140,8 @@ NOT NULL
129140
{
130141
lock (this)
131142
{
143+
OpenConnection();
144+
if (!IsConnected()) return null;
132145
Debug.Assert(id > 0);
133146
var dbServer =
134147
_dbConnection?.QueryFirstOrDefault<Server>(
@@ -142,6 +155,8 @@ NOT NULL
142155
{
143156
lock (this)
144157
{
158+
OpenConnection();
159+
if (!IsConnected()) return null;
145160
#pragma warning disable CS8619
146161
return _dbConnection?.Query<Server>($"SELECT * FROM `{Server.TABLE_NAME}`").Select(x => x?.ToProtocolServerBase()).Where(x => x != null).ToList();
147162
#pragma warning restore CS8619
@@ -152,6 +167,8 @@ public override int GetServerCount()
152167
{
153168
lock (this)
154169
{
170+
OpenConnection();
171+
if (!IsConnected()) return 0;
155172
return _dbConnection?.ExecuteScalar<int>($"SELECT COUNT(*) FROM `{Server.TABLE_NAME}`") ?? 0;
156173
}
157174
}
@@ -162,10 +179,17 @@ public override int GetServerCount()
162179
VALUES
163180
(@{nameof(Server.Id)}, @{nameof(Server.Protocol)}, @{nameof(Server.ClassVersion)}, @{nameof(Server.Json)});";
164181

182+
/// <summary>
183+
/// return new id
184+
/// </summary>
185+
/// <param name="protocolBase"></param>
186+
/// <returns></returns>
165187
public override string AddServer(ProtocolBase protocolBase)
166188
{
167189
lock (this)
168190
{
191+
OpenConnection();
192+
if (!IsConnected()) return string.Empty;
169193
if (protocolBase.IsTmpSession())
170194
protocolBase.Id = Ulid.NewUlid().ToString();
171195
var server = protocolBase.ToDbServer();
@@ -180,6 +204,8 @@ public override int AddServer(IEnumerable<ProtocolBase> protocolBases)
180204
{
181205
lock (this)
182206
{
207+
OpenConnection();
208+
if (!IsConnected()) return 0;
183209
var rng = new NUlid.Rng.MonotonicUlidRng();
184210
foreach (var protocolBase in protocolBases)
185211
{
@@ -204,6 +230,7 @@ public override bool UpdateServer(ProtocolBase server)
204230
lock (this)
205231
{
206232
OpenConnection();
233+
if (!IsConnected()) return false;
207234
var ret = _dbConnection?.Execute(SqlUpdate, server.ToDbServer()) > 0;
208235
if (ret)
209236
SetDataUpdateTimestamp();
@@ -216,6 +243,7 @@ public override bool UpdateServer(IEnumerable<ProtocolBase> servers)
216243
lock (this)
217244
{
218245
OpenConnection();
246+
if (!IsConnected()) return false;
219247
var dbss = servers.Select(x => x.ToDbServer());
220248
var ret = _dbConnection?.Execute(SqlUpdate, dbss) > 0;
221249
if (ret)
@@ -229,6 +257,7 @@ public override bool DeleteServer(string id)
229257
lock (this)
230258
{
231259
OpenConnection();
260+
if (!IsConnected()) return false;
232261
var ret = _dbConnection?.Execute($@"DELETE FROM `{Server.TABLE_NAME}` WHERE `{nameof(Server.Id)}` = @{nameof(Server.Id)};", new { Id = id }) > 0;
233262
if (ret)
234263
SetDataUpdateTimestamp();
@@ -241,6 +270,7 @@ public override bool DeleteServer(IEnumerable<string> ids)
241270
lock (this)
242271
{
243272
OpenConnection();
273+
if (!IsConnected()) return false;
244274
var ret = _dbConnection?.Execute($@"DELETE FROM `{Server.TABLE_NAME}` WHERE `{nameof(Server.Id)}` IN @{nameof(Server.Id)};", new { Id = ids }) > 0;
245275
if (ret)
246276
SetDataUpdateTimestamp();
@@ -258,6 +288,7 @@ public override bool DeleteServer(IEnumerable<string> ids)
258288
lock (this)
259289
{
260290
OpenConnection();
291+
if (!IsConnected()) return null;
261292
var config = _dbConnection?.QueryFirstOrDefault<Config>($"SELECT * FROM `{Config.TABLE_NAME}` WHERE `{nameof(Config.Key)}` = @{nameof(Config.Key)}",
262293
new { Key = key, });
263294
return config?.Value;
@@ -277,44 +308,12 @@ protected bool SetConfigPrivate(string key, string value)
277308
lock (this)
278309
{
279310
OpenConnection();
311+
if (!IsConnected()) return false;
280312
var existed = GetConfigPrivate(key) != null;
281313
return _dbConnection?.Execute(existed ? SqlUpdateConfig : SqlInsertConfig, new { Key = key, Value = value, }) > 0;
282314
}
283315
}
284316

285-
public override bool SetConfigRsa(string privateKeyPath, string publicKey, IEnumerable<ProtocolBase> servers)
286-
{
287-
lock (this)
288-
{
289-
if (_dbConnection == null)
290-
return false;
291-
OpenConnection();
292-
var data = servers.Select(x => x.ToDbServer());
293-
using var tran = _dbConnection.BeginTransaction();
294-
try
295-
{
296-
var existedPrivate = GetConfigPrivate("RSA_PrivateKeyPath") != null;
297-
var existedPublic = GetConfigPrivate("RSA_PublicKey") != null;
298-
299-
_dbConnection.Execute(existedPrivate ? SqlUpdateConfig : SqlInsertConfig, new { Key = "RSA_PrivateKeyPath", Value = privateKeyPath, }, tran);
300-
_dbConnection.Execute(existedPublic ? SqlUpdateConfig : SqlInsertConfig, new { Key = "RSA_PublicKey", Value = publicKey, }, tran);
301-
if (data.Any())
302-
_dbConnection?.Execute(SqlUpdate, data, tran);
303-
tran.Commit();
304-
}
305-
catch (Exception e)
306-
{
307-
SimpleLogHelper.Fatal(e);
308-
// Not needed any rollback, if you don't call Complete
309-
// a rollback is automatic exiting from the using block
310-
//tran.Rollback();
311-
return false;
312-
}
313-
314-
return true;
315-
}
316-
}
317-
318317
public override void SetDataUpdateTimestamp(long time = -1)
319318
{
320319
lock (this)

Ui/Model/DAO/Dapper/DapperDataBaseFree.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,6 @@ public override bool SetConfig(string key, string value)
153153
}
154154
}
155155

156-
/// <inheritdoc />
157-
public override bool SetConfigRsa(string privateKeyPath, string publicKey, IEnumerable<ProtocolBase> servers)
158-
{
159-
OpenConnection();
160-
var ret = base.SetConfigRsa(privateKeyPath, publicKey, servers);
161-
CloseConnection();
162-
return ret;
163-
}
164-
165156

166157
public override void SetDataUpdateTimestamp(long time = -1)
167158
{

Ui/Model/DAO/EnumDatabaseStatus.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public static class EnumConnectResultErrorInfo
1919
public static string GetErrorInfo(this EnumDatabaseStatus result)
2020
{
2121
var lang = IoC.Get<LanguageService>();
22-
Debug.Assert(lang != null);
2322
switch (result)
2423
{
2524
case EnumDatabaseStatus.AccessDenied:

Ui/Model/DAO/IDataBase.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ public abstract class IDatabase
6868

6969
public abstract bool SetConfig(string key, string value);
7070

71-
/// <summary>
72-
/// set rsa encryption and encrypt or decrypt the data.
73-
/// </summary>
74-
/// <param name="privateKeyPath"></param>
75-
/// <param name="publicKey"></param>
76-
/// <param name="servers">已加密或已解密的数据</param>
77-
public abstract bool SetConfigRsa(string privateKeyPath, string publicKey, IEnumerable<ProtocolBase> servers);
78-
7971
public abstract void SetDataUpdateTimestamp(long time = -1);
8072
public abstract long GetDataUpdateTimestamp();
8173

Ui/Model/Protocol/Base/ProtocolBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,9 @@ public ProtocolBase Clone()
280280
//}
281281

282282

283-
var clone = this.MemberwiseClone() as ProtocolBase;
283+
var clone = (ProtocolBase)this.MemberwiseClone();
284284
Debug.Assert(clone != null);
285-
clone.Tags = new List<string>(this.Tags);
285+
clone!.Tags = new List<string>(this.Tags);
286286
if (this is ProtocolBaseWithAddressPortUserPwd p
287287
&& clone is ProtocolBaseWithAddressPortUserPwd c)
288288
{

Ui/Model/Protocol/FileTransmit/Transmitters/TransmissionController/TransmitTask.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,14 @@ public TransmitTask(ILanguageService languageService, ITransmitter trans, string
7777
TransmitItemNames += di.Name + ", ";
7878
}
7979
}
80-
Debug.Assert(TransmitItemNames != null);
8180
TransmitItemNames = TransmitItemNames.Trim(',', ' ');
8281
RaisePropertyChanged(nameof(TransmitItemDstDirectoryPath));
8382
RaisePropertyChanged(nameof(TransmitItemSrcDirectoryPath));
8483
RaisePropertyChanged(nameof(TransmitItemNames));
8584
}
8685

87-
public TransmitTask(ILanguageService languageService, ITransmitter trans, string connectionId, string destinationDirectoryPath, RemoteItem[]? ris)
86+
public TransmitTask(ILanguageService languageService, ITransmitter trans, string connectionId, string destinationDirectoryPath, RemoteItem[] ris)
8887
{
89-
Debug.Assert(ris != null);
9088
TransmitTaskStatus = ETransmitTaskStatus.WaitTransmitStart;
9189
TransmissionType = ETransmissionType.ServerToHost;
9290
this._transOrg = trans;
@@ -107,7 +105,7 @@ public TransmitTask(ILanguageService languageService, ITransmitter trans, string
107105
TransmitItemNames += item.Name + ", ";
108106
}
109107
Debug.Assert(TransmitItemNames != null);
110-
TransmitItemNames = TransmitItemNames.Trim(',', ' ');
108+
TransmitItemNames = TransmitItemNames!.Trim(',', ' ');
111109
RaisePropertyChanged(nameof(TransmitItemDstDirectoryPath));
112110
RaisePropertyChanged(nameof(TransmitItemSrcDirectoryPath));
113111
RaisePropertyChanged(nameof(TransmitItemNames));
@@ -156,7 +154,8 @@ public string? TransmitDstDirectoryPath
156154
var dst = Items?.FirstOrDefault()?.DstPath;
157155
if (TransmissionType == ETransmissionType.HostToServer)
158156
{
159-
if (!string.IsNullOrWhiteSpace(dst)
157+
if (dst != null
158+
&& !string.IsNullOrWhiteSpace(dst)
160159
&& dst.LastIndexOf("/", StringComparison.Ordinal) > 0)
161160
{
162161
return dst.Substring(0, dst.LastIndexOf("/", StringComparison.Ordinal));
@@ -292,8 +291,7 @@ private void AddLocalDirectory(DirectoryInfo topDirectory)
292291
if (!topDirectory.Exists)
293292
return;
294293

295-
Debug.Assert(topDirectory?.Parent?.FullName != null);
296-
var srcParentDirPath = topDirectory.Parent.FullName.TrimEnd('/', '\\');
294+
var srcParentDirPath = topDirectory.Parent!.FullName.TrimEnd('/', '\\');
297295

298296
var dis = new Queue<DirectoryInfo>();
299297
var allItems = new Queue<TransmitItem>();
@@ -338,7 +336,7 @@ private void AddServerDirectory(RemoteItem topItem)
338336

339337
try
340338
{
341-
if (!_trans.Exists(topItem.FullName))
339+
if (_trans?.Exists(topItem.FullName) != true)
342340
return;
343341

344342
var srcParentDirPath = topItem.FullName.TrimEnd('/', '\\');

Ui/Resources/Icons/ServerIcons.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static ServerIcons Instance
2626
{
2727
get
2828
{
29-
Debug.Assert(_uniqueInstance != null);
29+
_uniqueInstance ??= new ServerIcons();
3030
return _uniqueInstance;
3131
}
3232
}

Ui/Resources/Languages/cs-cz.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,5 @@
260260
<s:String x:Key="Detecting your alternate host...">Detecting your alternate host...</s:String>
261261
<s:String x:Key="XXX is already existed!">`{0}` is already existed!</s:String>
262262
<s:String x:Key="XXX is not existed!">`{0}` is not existed!</s:String>
263+
<s:String x:Key="Show credentials info">Zobrazit informace o přihlašovacích údajích</s:String>
263264
</ResourceDictionary>

0 commit comments

Comments
 (0)