diff --git a/KindleMate2/FrmAboutBox.cs b/KindleMate2/FrmAboutBox.cs index 213fcf4..0e3cfd8 100644 --- a/KindleMate2/FrmAboutBox.cs +++ b/KindleMate2/FrmAboutBox.cs @@ -1,5 +1,4 @@ -using System; -using System.ComponentModel; +using System.ComponentModel; using System.Diagnostics; using System.Reflection; using DarkModeForms; @@ -76,7 +75,7 @@ private void FrmAboutBox_Load(object sender, EventArgs e) { okButton.Text = Strings.Confirm_Button; var bw = new BackgroundWorker(); - bw.DoWork += (_, workEventArgs) => { workEventArgs.Result = GetRepoInfo(); }; + bw.DoWork += (_, workEventArgs) => { workEventArgs.Result = StaticData.GetRepoInfo(); }; bw.RunWorkerAsync(); bw.RunWorkerCompleted += (_, workerCompletedEventArgs) => { if (workerCompletedEventArgs.Result == null) { @@ -89,46 +88,10 @@ private void FrmAboutBox_Load(object sender, EventArgs e) { var tagName = string.IsNullOrWhiteSpace(release.tag_name) ? string.Empty : release.tag_name; var toolTip = new ToolTip(); toolTip.SetToolTip(pictureBox1, Strings.New_Version + tagName); - pictureBox1.Visible = IsUpdate(assemblyVersion, tagName); + pictureBox1.Visible = _staticData.IsUpdate(assemblyVersion, tagName); }; } - private static GitHubRelease GetRepoInfo() { - const string url = "https://api.github.com/repos/lzcapp/KindleMate2/releases"; - var httpClient = new HttpClient(); - httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd("request"); - var response = httpClient.GetStringAsync(url).Result; - return JsonConvert.DeserializeObject(response)?[0] ?? new GitHubRelease(); - } - - private bool IsUpdate(string current, string tagname) { - DateTime normalizeVersion = NormalizeVersion(current); - DateTime normalizeTagName = NormalizeVersion(tagname); - if (normalizeVersion != DateTime.MinValue || normalizeTagName != DateTime.MinValue) { - return normalizeVersion < normalizeTagName; - } - var splitVersion = current.Split('.'); - var splitTagname = current.Split('.'); - for (var i = 0; i < 3; i++) { - if (!int.TryParse(splitVersion[i], out var intVersion) || !int.TryParse(splitTagname[i], out var intTagname)) { - continue; - } - if (intVersion < intTagname) { - return true; - } - } - return false; - } - - private static DateTime NormalizeVersion(string version) { - var date = DateTime.MinValue; - var parts = version.Split('.'); - if (int.TryParse(parts[0], out var year) && int.TryParse(parts[1], out var month) && int.TryParse(parts[2], out var day)) { - date = new DateTime(year, month, day); - } - return date; - } - private void pictureBox1_Click(object sender, EventArgs e) { const string repoUrl = "https://github.com/lzcapp/KindleMate2/releases/latest"; try { diff --git a/KindleMate2/FrmMain.cs b/KindleMate2/FrmMain.cs index cb91cf5..48de560 100644 --- a/KindleMate2/FrmMain.cs +++ b/KindleMate2/FrmMain.cs @@ -3,8 +3,10 @@ using System.Data.SQLite; using System.Diagnostics; using System.Globalization; +using System.Reflection; using System.Text; using System.Text.RegularExpressions; +using System.Windows.Forms; using DarkModeForms; using KindleMate2.Entities; using Markdig; @@ -73,6 +75,29 @@ public FrmMain() { _staticData.DisposeConnection(); }; + var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? string.Empty; + var bw = new BackgroundWorker(); + bw.DoWork += (_, workEventArgs) => { workEventArgs.Result = StaticData.GetRepoInfo(); }; + bw.RunWorkerAsync(); + bw.RunWorkerCompleted += (_, workerCompletedEventArgs) => { + if (workerCompletedEventArgs.Result == null) { + return; + } + var release = (GitHubRelease)workerCompletedEventArgs.Result; + if (string.IsNullOrWhiteSpace(assemblyVersion)) { + return; + } + var tagName = string.IsNullOrWhiteSpace(release.tag_name) ? string.Empty : release.tag_name; + var toolTip = new ToolTip(); + var isUpdate = _staticData.IsUpdate(assemblyVersion, tagName); + if (isUpdate) { + DialogResult resultUpdate = Messenger.MessageBox(Strings.New_Version + tagName, Strings.New_Version, MessageBoxButtons.OKCancel); + if (resultUpdate == DialogResult.OK) { + OpenUrl("https://github.com/lzcapp/KindleMate2/releases/latest"); + } + } + }; + _programsDirectory = Environment.CurrentDirectory; _filePath = Path.Combine(_programsDirectory, "KM2.dat"); _kindleClippingsPath = Path.Combine("documents", "My Clippings.txt"); @@ -1464,13 +1489,17 @@ private void MenuImportKindleMate_Click(object sender, EventArgs e) { private void MenuRepo_Click(object sender, EventArgs e) { const string repoUrl = "https://github.com/lzcapp/KindleMate2"; + OpenUrl(repoUrl); + } + + private void OpenUrl(string url) { try { Process.Start(new ProcessStartInfo { - FileName = repoUrl, + FileName = url, UseShellExecute = true }); } catch (Exception) { - Clipboard.SetText(repoUrl); + Clipboard.SetText(url); MessageBox(Strings.Repo_URL_Copied, Strings.Prompt, MessageBoxButtons.OK, MessageBoxIcon.Information); } } diff --git a/KindleMate2/StaticData.cs b/KindleMate2/StaticData.cs index fbec9fe..a2b2b6f 100644 --- a/KindleMate2/StaticData.cs +++ b/KindleMate2/StaticData.cs @@ -3,16 +3,17 @@ using System.Reflection; using DarkModeForms; using KindleMate2.Entities; +using Newtonsoft.Json; namespace KindleMate2 { - public class StaticData { + internal class StaticData { private const string ConnectionString = "Data Source=KM2.dat;Version=3;"; private readonly SQLiteConnection _connection = new(ConnectionString); private SQLiteTransaction? _trans; - public StaticData() { + internal StaticData() { _connection.Open(); using var command = new SQLiteCommand("PRAGMA synchronous=OFF", _connection); @@ -23,28 +24,28 @@ private void OpenConnection() { _connection.Open(); } - public void CloseConnection() { + internal void CloseConnection() { _connection.Close(); } - public void DisposeConnection() { + internal void DisposeConnection() { _connection.Dispose(); } - public void BeginTransaction() { + internal void BeginTransaction() { _trans = _connection.BeginTransaction(); } - public void CommitTransaction() { + internal void CommitTransaction() { _trans?.Commit(); } - public void RollbackTransaction() { + internal void RollbackTransaction() { _trans?.Rollback(); } // ReSharper disable once IdentifierTypo - public DataTable GetClipingsDataTable() { + internal DataTable GetClipingsDataTable() { var dataTable = new DataTable(); const string queryClippings = "SELECT * FROM clippings;"; @@ -56,7 +57,7 @@ public DataTable GetClipingsDataTable() { return dataTable; } - public DataTable GetClipingsDataTableFuzzySearch(string strSearch, string type) { + internal DataTable GetClipingsDataTableFuzzySearch(string strSearch, string type) { var dataTable = new DataTable(); var sql = string.Empty; @@ -79,7 +80,7 @@ public DataTable GetClipingsDataTableFuzzySearch(string strSearch, string type) return dataTable; } - public List GetClippingsBookTitleList() { + internal List GetClippingsBookTitleList() { var list = new List(); DataTable dt = GetClipingsDataTable(); if (dt.Rows.Count <= 0) { @@ -94,7 +95,7 @@ public List GetClippingsBookTitleList() { return list; } - public List GetClippingsAuthorList() { + internal List GetClippingsAuthorList() { var list = new List(); DataTable dt = GetClipingsDataTable(); if (dt.Rows.Count <= 0) { @@ -109,7 +110,7 @@ public List GetClippingsAuthorList() { return list; } - public bool IsExistOriginalClippings(string? key) { + internal bool IsExistOriginalClippings(string? key) { switch (key) { case null: case "": @@ -125,7 +126,7 @@ public bool IsExistOriginalClippings(string? key) { return result > 0; } - public bool IsExistClippings(string? key) { + internal bool IsExistClippings(string? key) { switch (key) { case null: case "": @@ -141,7 +142,7 @@ public bool IsExistClippings(string? key) { return result > 0; } - public bool IsExistClippingsOfContent(string? content) { + internal bool IsExistClippingsOfContent(string? content) { switch (content) { case null: case "": @@ -157,7 +158,7 @@ public bool IsExistClippingsOfContent(string? content) { return result > 0; } - public bool IsExistClippingsContainingContent(string? content) { + internal bool IsExistClippingsContainingContent(string? content) { if (content == null) { return false; } @@ -176,7 +177,7 @@ public bool IsExistClippingsContainingContent(string? content) { } /* - public int GetOriginClippingsCount() { + internal int GetOriginClippingsCount() { const string queryCount = "SELECT COUNT(1) FROM original_clipping_lines"; using var commandCount = new SQLiteCommand(queryCount, _connection); var count = Convert.ToInt32(commandCount.ExecuteScalar()); @@ -185,7 +186,7 @@ public int GetOriginClippingsCount() { } */ - public DataTable GetOriginClippingsDataTableFuzzySearch(string strSearch, string type) { + internal DataTable GetOriginClippingsDataTableFuzzySearch(string strSearch, string type) { var dataTable = new DataTable(); var sql = string.Empty; @@ -206,7 +207,7 @@ public DataTable GetOriginClippingsDataTableFuzzySearch(string strSearch, string return dataTable; } - public DataTable GetOriginClippingsDataTable() { + internal DataTable GetOriginClippingsDataTable() { var dataTable = new DataTable(); const string queryClippings = "SELECT * FROM original_clipping_lines;"; @@ -218,7 +219,7 @@ public DataTable GetOriginClippingsDataTable() { return dataTable; } - public bool InsertOriginClippings(string key, string line1, string line2, string line3, string line4, string line5) { + internal bool InsertOriginClippings(string key, string line1, string line2, string line3, string line4, string line5) { if (key == string.Empty || line4 == string.Empty) { return false; } @@ -244,7 +245,7 @@ public bool InsertOriginClippings(string key, string line1, string line2, string return result > 0; } - public bool DeleteClippingsByKey(string key) { + internal bool DeleteClippingsByKey(string key) { if (string.IsNullOrWhiteSpace(key)) { return false; } @@ -258,7 +259,7 @@ public bool DeleteClippingsByKey(string key) { return result > 0; } - public bool DeleteClippingsByBook(string bookname) { + internal bool DeleteClippingsByBook(string bookname) { if (bookname == string.Empty) { return false; } @@ -272,12 +273,12 @@ public bool DeleteClippingsByBook(string bookname) { return result > 0; } - public bool InsertClippings(Clipping entityClipping) { + internal bool InsertClippings(Clipping entityClipping) { var result = Insert(entityClipping, "clippings", "key", true); return result > 0; } - public bool RenameBook(string originBookname, string bookname, string authorname) { + internal bool RenameBook(string originBookname, string bookname, string authorname) { if (string.IsNullOrWhiteSpace(originBookname) || string.IsNullOrWhiteSpace(bookname)) { return false; } @@ -305,7 +306,7 @@ public bool RenameBook(string originBookname, string bookname, string authorname return result > 0; } - public bool UpdateClippings(string key, string content, string bookname) { + internal bool UpdateClippings(string key, string content, string bookname) { if (string.IsNullOrWhiteSpace(key)) { return false; } @@ -338,7 +339,7 @@ public bool UpdateClippings(string key, string content, string bookname) { return result > 0; } - public int InsertLookups(string word_key, string usage, string title, string authors, string timestamp) { + internal int InsertLookups(string word_key, string usage, string title, string authors, string timestamp) { if (string.IsNullOrWhiteSpace(word_key) || string.IsNullOrWhiteSpace(timestamp)) { return 0; } @@ -362,7 +363,7 @@ public int InsertLookups(string word_key, string usage, string title, string aut return result; } - public void UpdateLookups(string origintitle, string title, string authors) { + internal void UpdateLookups(string origintitle, string title, string authors) { if (string.IsNullOrWhiteSpace(origintitle) || string.IsNullOrWhiteSpace(title)) { return; } @@ -388,7 +389,7 @@ public void UpdateLookups(string origintitle, string title, string authors) { } /* - public bool InsertVocab(string id, string word_key, string word, string stem, int category, string translation, string timestamp, int frequency, int sync, int colorRGB) { + internal bool InsertVocab(string id, string word_key, string word, string stem, int category, string translation, string timestamp, int frequency, int sync, int colorRGB) { if (id == string.Empty || word == string.Empty) { return false; } @@ -425,7 +426,7 @@ public bool InsertVocab(string id, string word_key, string word, string stem, in } */ - public int InsertVocab(string id, string word_key, string word, string stem, int category, string timestamp, int frequency) { + internal int InsertVocab(string id, string word_key, string word, string stem, int category, string timestamp, int frequency) { if (id == string.Empty || word == string.Empty) { return 0; } @@ -454,7 +455,7 @@ public int InsertVocab(string id, string word_key, string word, string stem, int } /* - public bool UpdateVocab(string word_key, string word, string stem, int category, string timestamp, int frequency) { + internal bool UpdateVocab(string word_key, string word, string stem, int category, string timestamp, int frequency) { if (word == string.Empty) { return false; } @@ -485,7 +486,7 @@ public bool UpdateVocab(string word_key, string word, string stem, int category, } */ - public void UpdateVocab(string word_key, int frequency) { + internal void UpdateVocab(string word_key, int frequency) { if (word_key == string.Empty) { return; } @@ -501,7 +502,7 @@ public void UpdateVocab(string word_key, int frequency) { command.ExecuteNonQuery(); } - public DataTable GetVocabDataTable() { + internal DataTable GetVocabDataTable() { var dataTable = new DataTable(); const string query = "SELECT * FROM vocab;"; @@ -513,7 +514,7 @@ public DataTable GetVocabDataTable() { return dataTable; } - public DataTable GetVocabDataTableFuzzySearch(string strSearch, string type) { + internal DataTable GetVocabDataTableFuzzySearch(string strSearch, string type) { var dataTable = new DataTable(); var sql = string.Empty; @@ -536,7 +537,7 @@ public DataTable GetVocabDataTableFuzzySearch(string strSearch, string type) { - public List GetVocabWordList() { + internal List GetVocabWordList() { var list = new List(); DataTable dt = GetVocabDataTable(); if (dt.Rows.Count <= 0) { @@ -551,7 +552,7 @@ public List GetVocabWordList() { return list; } - public List GetVocabStemList() { + internal List GetVocabStemList() { var list = new List(); DataTable dt = GetVocabDataTable(); if (dt.Rows.Count <= 0) { @@ -566,7 +567,7 @@ public List GetVocabStemList() { return list; } - public DataTable GetLookupsDataTable() { + internal DataTable GetLookupsDataTable() { var dataTable = new DataTable(); const string query = "SELECT DISTINCT * FROM lookups"; @@ -578,7 +579,7 @@ public DataTable GetLookupsDataTable() { return dataTable; } - public DataTable GetLookupsDataTableFuzzySearch(string strSearch, string type) { + internal DataTable GetLookupsDataTableFuzzySearch(string strSearch, string type) { var dataTable = new DataTable(); var sql = string.Empty; @@ -603,7 +604,7 @@ public DataTable GetLookupsDataTableFuzzySearch(string strSearch, string type) { return dataTable; } - public bool IsExistVocab(string word_key) { + internal bool IsExistVocab(string word_key) { if (word_key == string.Empty) { return false; } @@ -617,7 +618,7 @@ public bool IsExistVocab(string word_key) { return result > 0; } - public bool IsExistVocabById(string id) { + internal bool IsExistVocabById(string id) { if (id == string.Empty) { return false; } @@ -631,7 +632,7 @@ public bool IsExistVocabById(string id) { return result > 0; } - public bool IsExistLookups(string timestamp) { + internal bool IsExistLookups(string timestamp) { if (timestamp == string.Empty) { return true; } @@ -645,7 +646,7 @@ public bool IsExistLookups(string timestamp) { return result > 0; } - public bool DeleteVocab(string word) { + internal bool DeleteVocab(string word) { if (word == string.Empty) { return false; } @@ -659,7 +660,7 @@ public bool DeleteVocab(string word) { return result > 0; } - public bool DeleteLookupsByTimeStamp(string timestamp) { + internal bool DeleteLookupsByTimeStamp(string timestamp) { if (timestamp == string.Empty) { return false; } @@ -673,7 +674,7 @@ public bool DeleteLookupsByTimeStamp(string timestamp) { return result > 0; } - public bool DeleteLookupsByWordKey(string word_key) { + internal bool DeleteLookupsByWordKey(string word_key) { if (word_key == string.Empty) { return false; } @@ -723,7 +724,7 @@ private string GetTheme() { return GetSettings("theme"); } - public bool IsDarkTheme() { + internal bool IsDarkTheme() { var theme = GetTheme(); bool isWindowsDarkTheme; if (string.IsNullOrWhiteSpace(theme)) { @@ -746,7 +747,7 @@ private bool IsWindowsDarkTheme() { return isWindowsDarkTheme; } - public void SetTheme(string value) { + internal void SetTheme(string value) { SetSettings("theme", value); } @@ -754,15 +755,15 @@ private void SetTheme(bool isDarkTheme) { SetTheme(isDarkTheme ? "dark" : "light"); } - public string GetLanguage() { + internal string GetLanguage() { return GetSettings("lang"); } - public void SetLanguage(string value) { + internal void SetLanguage(string value) { SetSettings("lang", value); } - public void VacuumDatabase() { + internal void VacuumDatabase() { if (IsConnectionOpen()) { CloseConnection(); @@ -792,7 +793,7 @@ private bool IsConnectionOpen() { return _connection.State == ConnectionState.Open; } - public bool EmptyTables() { + internal bool EmptyTables() { var result = 0; var tableNames = new List() { @@ -807,7 +808,7 @@ public bool EmptyTables() { return result > 0; } - public bool IsDatabaseEmpty() { + internal bool IsDatabaseEmpty() { var result = 0; var tableNames = new List() { @@ -848,7 +849,7 @@ internal static string FormatFileSize(long fileSize) { { 'M', 1000 } }; - public int RomanToInteger(string roman) { + internal int RomanToInteger(string roman) { var result = 0; var prevValue = 0; @@ -918,5 +919,41 @@ private int Insert(T entity, string tableName, string primaryKey, bool isAddP } return 0; } + + internal bool IsUpdate(string current, string tagname) { + DateTime normalizeVersion = NormalizeVersion(current); + DateTime normalizeTagName = NormalizeVersion(tagname); + if (normalizeVersion != DateTime.MinValue || normalizeTagName != DateTime.MinValue) { + return normalizeVersion < normalizeTagName; + } + var splitVersion = current.Split('.'); + var splitTagname = current.Split('.'); + for (var i = 0; i < 3; i++) { + if (!int.TryParse(splitVersion[i], out var intVersion) || !int.TryParse(splitTagname[i], out var intTagname)) { + continue; + } + if (intVersion < intTagname) { + return true; + } + } + return false; + } + + private static DateTime NormalizeVersion(string version) { + var date = DateTime.MinValue; + var parts = version.Split('.'); + if (int.TryParse(parts[0], out var year) && int.TryParse(parts[1], out var month) && int.TryParse(parts[2], out var day)) { + date = new DateTime(year, month, day); + } + return date; + } + + internal static GitHubRelease GetRepoInfo() { + const string url = "https://api.github.com/repos/lzcapp/KindleMate2/releases"; + var httpClient = new HttpClient(); + httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd("request"); + var response = httpClient.GetStringAsync(url).Result; + return JsonConvert.DeserializeObject(response)?[0] ?? new GitHubRelease(); + } } } \ No newline at end of file diff --git a/KindleMate2/Strings.en.resx b/KindleMate2/Strings.en.resx index acba723..ef11c55 100644 --- a/KindleMate2/Strings.en.resx +++ b/KindleMate2/Strings.en.resx @@ -421,7 +421,7 @@ A backup has been detected. Do you want to restore from the backup? - Repo URL copied to clipboard + URL copied to clipboard Cleaned diff --git a/KindleMate2/Strings.resx b/KindleMate2/Strings.resx index 63a43af..aa533ff 100644 --- a/KindleMate2/Strings.resx +++ b/KindleMate2/Strings.resx @@ -434,8 +434,8 @@ 检测到备份,需要从备份恢复吗? - 仓库URL已经复制到剪贴板 - + URL已经复制到剪贴板 + 清理 diff --git a/KindleMate2/Strings.zh-cn.resx b/KindleMate2/Strings.zh-cn.resx index a8b5488..ef99216 100644 --- a/KindleMate2/Strings.zh-cn.resx +++ b/KindleMate2/Strings.zh-cn.resx @@ -427,7 +427,7 @@ 检测到备份,需要从备份恢复吗? - 仓库URL已经复制到剪贴板 + URL已经复制到剪贴板 清理 diff --git a/KindleMate2/Strings.zh-hans.resx b/KindleMate2/Strings.zh-hans.resx index 91ded04..07cc036 100644 --- a/KindleMate2/Strings.zh-hans.resx +++ b/KindleMate2/Strings.zh-hans.resx @@ -427,7 +427,7 @@ 检测到备份,需要从备份恢复吗? - 仓库URL已经复制到剪贴板 + URL已经复制到剪贴板 清理 diff --git a/KindleMate2/Strings.zh-hant.resx b/KindleMate2/Strings.zh-hant.resx index 3c853c6..07a8b0a 100644 --- a/KindleMate2/Strings.zh-hant.resx +++ b/KindleMate2/Strings.zh-hant.resx @@ -427,7 +427,7 @@ 檢測到備份,需要從備份恢復嗎? - 倉庫URL已經復製到剪貼板 + URL已經復製到剪貼板 清理