-
-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
158 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,97 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using Text_Grab.Utilities; | ||
|
||
namespace Text_Grab.Models; | ||
|
||
public record WebSearchUrlModel | ||
{ | ||
public required string Name { get; set; } | ||
public required string Url { get; set; } | ||
public string Name { get; set; } = string.Empty; | ||
public string Url { get; set; } = string.Empty; | ||
|
||
private WebSearchUrlModel? defaultSearcher; | ||
|
||
public static List<WebSearchUrlModel> GetDefaultWebSearchUrls() | ||
public WebSearchUrlModel DefaultSearcher | ||
{ | ||
get | ||
{ | ||
defaultSearcher ??= GetDefaultSearcher(); | ||
return defaultSearcher; | ||
} | ||
set | ||
{ | ||
defaultSearcher = value; | ||
SaveDefaultSearcher(defaultSearcher); | ||
} | ||
} | ||
|
||
|
||
private List<WebSearchUrlModel> webSearchers = []; | ||
|
||
public List<WebSearchUrlModel> WebSearchers | ||
{ | ||
get | ||
{ | ||
if (webSearchers.Count == 0) | ||
webSearchers = GetWebSearchUrls(); | ||
|
||
return webSearchers; | ||
} | ||
set | ||
{ | ||
webSearchers = value; | ||
SaveWebSearchUrls(webSearchers); | ||
} | ||
} | ||
|
||
private WebSearchUrlModel GetDefaultSearcher() | ||
{ | ||
string searcherName = AppUtilities.TextGrabSettings.DefaultWebSearch; | ||
if (string.IsNullOrWhiteSpace(searcherName)) | ||
return WebSearchers[0]; | ||
|
||
WebSearchUrlModel? searcher = WebSearchers | ||
.FirstOrDefault(searcher => searcher.Name == searcherName); | ||
|
||
return searcher ?? WebSearchers[0]; | ||
} | ||
|
||
private void SaveDefaultSearcher(WebSearchUrlModel webSearchUrl) | ||
{ | ||
AppUtilities.TextGrabSettings.DefaultWebSearch = webSearchUrl.Name; | ||
AppUtilities.TextGrabSettings.Save(); | ||
} | ||
|
||
private static List<WebSearchUrlModel> GetDefaultWebSearchUrls() | ||
{ | ||
return | ||
[ | ||
new() { Name = "Google", Url = "https://www.google.com/search?q=" }, | ||
new() { Name = "Bing", Url = "https://www.bing.com/search?q=" }, | ||
new() { Name = "DuckDuckGo", Url = "https://duckduckgo.com/?q=" }, | ||
new() { Name = "Yahoo", Url = "https://search.yahoo.com/search?p=" }, | ||
new() { Name = "Yandex", Url = "https://yandex.com/search/?text=" }, | ||
new() { Name = "Baidu", Url = "https://www.baidu.com/s?wd=" }, | ||
new() { Name = "Brave", Url = "https://search.brave.com/search?q=" }, | ||
new() { Name = "GitHub Code", Url = "https://github.com/search?type=code&q=" }, | ||
new() { Name = "GitHub Repos", Url = "https://github.com/search?type=repositories&q=" }, | ||
]; | ||
} | ||
|
||
public static List<WebSearchUrlModel> GetWebSearchUrls() | ||
{ | ||
string json = AppUtilities.TextGrabSettings.WebSearchItemsJson; | ||
if (string.IsNullOrWhiteSpace(json)) | ||
return GetDefaultWebSearchUrls(); | ||
List<WebSearchUrlModel>? webSearchUrls = JsonSerializer.Deserialize<List<WebSearchUrlModel>>(json); | ||
if (webSearchUrls is null || webSearchUrls.Count == 0) | ||
return GetDefaultWebSearchUrls(); | ||
|
||
return webSearchUrls; | ||
} | ||
|
||
public static void SaveWebSearchUrls(List<WebSearchUrlModel> webSearchUrls) | ||
{ | ||
string json = JsonSerializer.Serialize(webSearchUrls); | ||
AppUtilities.TextGrabSettings.WebSearchItemsJson = json; | ||
AppUtilities.TextGrabSettings.Save(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters