Skip to content

Commit 58ee3fb

Browse files
committed
feat: Allow you customize Reconnect button and Close button show or hide on tab window header #353
improve database alert
1 parent 8847063 commit 58ee3fb

File tree

8 files changed

+65
-42
lines changed

8 files changed

+65
-42
lines changed

Ui/AppInit.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,23 @@ public void InitOnConfigure()
282282

283283
public void InitOnLaunch()
284284
{
285+
if (_isNewUser == false && ConfigurationService != null)
286+
{
287+
MsAppCenterHelper.TraceSpecial($"App start with - ListPageIsCardView", $"{ConfigurationService.General.ListPageIsCardView}");
288+
MsAppCenterHelper.TraceSpecial($"App start with - ConfirmBeforeClosingSession", $"{ConfigurationService.General.ConfirmBeforeClosingSession}");
289+
MsAppCenterHelper.TraceSpecial($"App start with - LauncherEnabled", $"{ConfigurationService.Launcher.LauncherEnabled}");
290+
MsAppCenterHelper.TraceSpecial($"App start with - Theme", $"{ConfigurationService.Theme.ThemeName}");
291+
}
292+
285293
KittyConfig.CleanUpOldConfig();
286294

287295
if (_localDataConnectionStatus != EnumDatabaseStatus.OK)
288296
{
289297
string error = _localDataConnectionStatus.GetErrorInfo();
290-
MessageBox.Show(error, IoC.Get<LanguageService>().Translate("Error"), MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.None, MessageBoxOptions.DefaultDesktopOnly);
291298
IoC.Get<MainWindowViewModel>().OnMainWindowViewLoaded += () =>
292299
{
293300
IoC.Get<MainWindowViewModel>().ShowMe(goPage: EnumMainWindowPage.SettingsData);
301+
MessageBoxHelper.ErrorAlert(error);
294302
};
295303
IoC.Get<MainWindowViewModel>().ShowMe();
296304
return;
@@ -310,14 +318,6 @@ public void InitOnLaunch()
310318
};
311319
IoC.Get<MainWindowViewModel>().ShowMe();
312320
}
313-
314-
if (_isNewUser == false && ConfigurationService != null)
315-
{
316-
MsAppCenterHelper.TraceSpecial($"App start with - ListPageIsCardView", $"{ConfigurationService.General.ListPageIsCardView}");
317-
MsAppCenterHelper.TraceSpecial($"App start with - ConfirmBeforeClosingSession", $"{ConfigurationService.General.ConfirmBeforeClosingSession}");
318-
MsAppCenterHelper.TraceSpecial($"App start with - LauncherEnabled", $"{ConfigurationService.Launcher.LauncherEnabled}");
319-
MsAppCenterHelper.TraceSpecial($"App start with - Theme", $"{ConfigurationService.Theme.ThemeName}");
320-
}
321321
}
322322
}
323323
}

Ui/Model/DAO/Dapper/DapperDataBase.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
using MySql.Data.MySqlClient;
1212
using NUlid;
1313
using Shawn.Utils;
14+
using _1RM.Utils;
1415

1516
namespace _1RM.Model.DAO.Dapper
1617
{
17-
public class DapperDataBase : IDataBase
18+
public class DapperDatabase : IDatabase
1819
{
1920
protected IDbConnection? _dbConnection;
2021
protected string _connectionString = "";
@@ -121,6 +122,7 @@ NOT NULL
121122
`{nameof(Server.Json)}` TEXT NOT NULL
122123
);
123124
");
125+
base.SetEncryptionTest();
124126
}
125127

126128
public override ProtocolBase? GetServer(int id)
@@ -164,7 +166,7 @@ public override string AddServer(ProtocolBase protocolBase)
164166
{
165167
lock (this)
166168
{
167-
if(protocolBase.IsTmpSession())
169+
if (protocolBase.IsTmpSession())
168170
protocolBase.Id = Ulid.NewUlid().ToString();
169171
var server = protocolBase.ToDbServer();
170172
var ret = _dbConnection?.Execute(SqlInsert, server);
@@ -201,6 +203,7 @@ public override bool UpdateServer(ProtocolBase server)
201203
{
202204
lock (this)
203205
{
206+
OpenConnection();
204207
var ret = _dbConnection?.Execute(SqlUpdate, server.ToDbServer()) > 0;
205208
if (ret)
206209
SetDataUpdateTimestamp();
@@ -212,6 +215,7 @@ public override bool UpdateServer(IEnumerable<ProtocolBase> servers)
212215
{
213216
lock (this)
214217
{
218+
OpenConnection();
215219
var dbss = servers.Select(x => x.ToDbServer());
216220
var ret = _dbConnection?.Execute(SqlUpdate, dbss) > 0;
217221
if (ret)
@@ -224,8 +228,7 @@ public override bool DeleteServer(string id)
224228
{
225229
lock (this)
226230
{
227-
if (_dbConnection == null)
228-
return false;
231+
OpenConnection();
229232
var ret = _dbConnection?.Execute($@"DELETE FROM `{Server.TABLE_NAME}` WHERE `{nameof(Server.Id)}` = @{nameof(Server.Id)};", new { Id = id }) > 0;
230233
if (ret)
231234
SetDataUpdateTimestamp();
@@ -237,8 +240,7 @@ public override bool DeleteServer(IEnumerable<string> ids)
237240
{
238241
lock (this)
239242
{
240-
if (_dbConnection == null)
241-
return false;
243+
OpenConnection();
242244
var ret = _dbConnection?.Execute($@"DELETE FROM `{Server.TABLE_NAME}` WHERE `{nameof(Server.Id)}` IN @{nameof(Server.Id)};", new { Id = ids }) > 0;
243245
if (ret)
244246
SetDataUpdateTimestamp();
@@ -255,6 +257,7 @@ public override bool DeleteServer(IEnumerable<string> ids)
255257
{
256258
lock (this)
257259
{
260+
OpenConnection();
258261
var config = _dbConnection?.QueryFirstOrDefault<Config>($"SELECT * FROM `{Config.TABLE_NAME}` WHERE `{nameof(Config.Key)}` = @{nameof(Config.Key)}",
259262
new { Key = key, });
260263
return config?.Value;
@@ -273,6 +276,7 @@ protected bool SetConfigPrivate(string key, string value)
273276
{
274277
lock (this)
275278
{
279+
OpenConnection();
276280
var existed = GetConfigPrivate(key) != null;
277281
return _dbConnection?.Execute(existed ? SqlUpdateConfig : SqlInsertConfig, new { Key = key, Value = value, }) > 0;
278282
}
@@ -284,6 +288,7 @@ public override bool SetConfigRsa(string privateKeyPath, string publicKey, IEnum
284288
{
285289
if (_dbConnection == null)
286290
return false;
291+
OpenConnection();
287292
var data = servers.Select(x => x.ToDbServer());
288293
using var tran = _dbConnection.BeginTransaction();
289294
try

Ui/Model/DAO/Dapper/DapperDataBaseFree.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace _1RM.Model.DAO.Dapper
88
/// <summary>
99
/// DapperDb no occupation version
1010
/// </summary>
11-
public sealed class DapperDataBaseFree : DapperDataBase
11+
public sealed class DapperDatabaseFree : DapperDatabase
1212
{
1313
/// <inheritdoc />
1414
public override void InitTables()

Ui/Model/DAO/EnumDatabaseStatus.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ public static string GetErrorInfo(this EnumDatabaseStatus result)
2929
break;
3030

3131
case EnumDatabaseStatus.NotConnectedYet:
32-
return "database: NotConnected!";
32+
return "database: Primary database is notConnected!";
3333

3434
case EnumDatabaseStatus.LostConnection:
35-
return "database: Lost Connection!";
35+
return "database: Primary database lost connection!";
3636

3737
case EnumDatabaseStatus.EncryptKeyError:
38-
return $"database: your database is encrypted by a third-part build {Assert.APP_NAME}, this app can not read your data correctly!";
38+
return $"database: your primary database is encrypted by a third-part build {Assert.APP_NAME}, this app can not read your data correctly!";
3939

4040
default:
4141
throw new ArgumentOutOfRangeException(nameof(result), result, null);

Ui/Model/DAO/IDataBase.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
using System.Collections.Generic;
33
using _1RM.Model.Protocol;
44
using _1RM.Model.Protocol.Base;
5+
using _1RM.Utils;
56
using Shawn.Utils;
7+
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
68

79
namespace _1RM.Model.DAO
810
{
@@ -18,7 +20,7 @@ public enum DatabaseType
1820
Sqlite,
1921
}
2022

21-
public abstract class IDataBase
23+
public abstract class IDatabase
2224
{
2325
public abstract void CloseConnection();
2426
public abstract void OpenConnection();
@@ -76,6 +78,24 @@ public abstract class IDataBase
7678

7779
public abstract void SetDataUpdateTimestamp(long time = -1);
7880
public abstract long GetDataUpdateTimestamp();
81+
82+
protected void SetEncryptionTest()
83+
{
84+
if (string.IsNullOrEmpty(GetConfig("EncryptionTest")))
85+
{
86+
SetConfig("EncryptionTest", UnSafeStringEncipher.SimpleEncrypt("EncryptionTest"));
87+
}
88+
}
89+
90+
public bool CheckEncryptionTest()
91+
{
92+
var et = GetConfig("EncryptionTest");
93+
if (UnSafeStringEncipher.SimpleDecrypt(et ?? "") != "EncryptionTest")
94+
{
95+
return false;
96+
}
97+
return true;
98+
}
7999
}
80100

81101

@@ -92,11 +112,11 @@ public static string GetMysqlConnectionString(string host, int port, string dbNa
92112

93113

94114

95-
private static string? TryGetConfig(this IDataBase iDataBase, string key)
115+
private static string? TryGetConfig(this IDatabase iDatabase, string key)
96116
{
97117
try
98118
{
99-
var val = iDataBase.GetConfig(key) ?? "";
119+
var val = iDatabase.GetConfig(key) ?? "";
100120
return val;
101121
}
102122
catch (Exception e)
@@ -106,11 +126,11 @@ public static string GetMysqlConnectionString(string host, int port, string dbNa
106126
}
107127
}
108128

109-
private static bool TrySetConfig(this IDataBase iDataBase, string key, string value)
129+
private static bool TrySetConfig(this IDatabase iDatabase, string key, string value)
110130
{
111131
try
112132
{
113-
iDataBase.SetConfig(key, value ?? "");
133+
iDatabase.SetConfig(key, value ?? "");
114134
return true;
115135
}
116136
catch (Exception e)
@@ -120,12 +140,12 @@ private static bool TrySetConfig(this IDataBase iDataBase, string key, string va
120140
}
121141
}
122142

123-
public static bool CheckWritable(this IDataBase iDataBase)
143+
public static bool CheckWritable(this IDatabase iDatabase)
124144
{
125145
try
126146
{
127-
iDataBase.SetConfig("permission_check", "true"); // insert
128-
iDataBase.SetConfig("permission_check", "true"); // update
147+
iDatabase.SetConfig("permission_check", "true"); // insert
148+
iDatabase.SetConfig("permission_check", "true"); // update
129149
return true;
130150
}
131151
catch (Exception e)
@@ -135,11 +155,11 @@ public static bool CheckWritable(this IDataBase iDataBase)
135155
}
136156
}
137157

138-
public static bool CheckReadable(this IDataBase iDataBase)
158+
public static bool CheckReadable(this IDatabase iDatabase)
139159
{
140160
try
141161
{
142-
iDataBase.SetConfig("permission_check", "true"); // update
162+
iDatabase.SetConfig("permission_check", "true"); // update
143163
}
144164
catch
145165
{
@@ -148,7 +168,7 @@ public static bool CheckReadable(this IDataBase iDataBase)
148168

149169
try
150170
{
151-
var val = iDataBase.GetConfig("permission_check");
171+
var val = iDatabase.GetConfig("permission_check");
152172
return true;
153173
}
154174
catch

Ui/Service/DataSource/Model/MysqlSource.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace _1RM.Service.DataSource.Model
1212
{
13-
public class MysqlSource : DataSourceBase
13+
public sealed class MysqlSource : DataSourceBase
1414
{
1515

1616
private string _host = "127.0.0.1";
@@ -81,10 +81,10 @@ public override string GetConnectionString(int connectTimeOutSeconds = 5)
8181

8282

8383

84-
private readonly IDataBase _dataBase = new DapperDataBase();
85-
public override IDataBase GetDataBase()
84+
private readonly IDatabase _database = new DapperDatabase();
85+
public override IDatabase GetDataBase()
8686
{
87-
return _dataBase;
87+
return _database;
8888
}
8989

9090
public static bool TestConnection(MysqlSource config)
@@ -94,11 +94,9 @@ public static bool TestConnection(MysqlSource config)
9494
public static bool TestConnection(string host, int port, string dbName, string userName, string password)
9595
{
9696
var str = DbExtensions.GetMysqlConnectionString(host, port, dbName, userName, password, 2);
97-
var db = new DapperDataBase();
97+
var db = new DapperDatabase();
9898
try
9999
{
100-
//var dbConnection = new MySqlConnection(str);
101-
//dbConnection.Open();
102100
db.OpenNewConnection(DatabaseType.MySql, str);
103101
return db.IsConnected();
104102
}

Ui/Service/DataSource/Model/SqliteSource.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace _1RM.Service.DataSource.Model
1414
{
15-
public partial class SqliteSource : DataSourceBase
15+
public sealed partial class SqliteSource : DataSourceBase
1616
{
1717
private string _path = "";
1818
public string Path
@@ -49,10 +49,10 @@ public override string GetConnectionString(int connectTimeOutSeconds = 5)
4949

5050

5151

52-
private readonly IDataBase _dataBase = new DapperDataBaseFree();
53-
public override IDataBase GetDataBase()
52+
private readonly IDatabase _database = new DapperDatabaseFree();
53+
public override IDatabase GetDataBase()
5454
{
55-
return _dataBase;
55+
return _database;
5656
}
5757
}
5858
}

Ui/Utils/PRemoteM/PRemoteMTransferHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private static string DecryptOrReturnOriginalString(RSA? ras, string originalStr
153153

154154
private static void StartTask()
155155
{
156-
var dataBase = new DapperDataBaseFree();
156+
var dataBase = new DapperDatabaseFree();
157157
try
158158
{
159159
if (_dbPathList?.Count > 0 == false)

0 commit comments

Comments
 (0)