From 7ff9b688c91af89a49b94ea1b67c09f9462241cd Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Tue, 21 Sep 2021 13:50:51 -0500 Subject: [PATCH 1/6] Refactor Bookmark plugin --- .../Bookmark.cs | 47 ++-------- .../ChromeBookmarkLoader.cs | 26 ++++++ .../ChromeBookmarks.cs | 87 ------------------- .../ChromiumBookmarkLoader.cs | 62 +++++++++++++ .../Commands/Bookmarks.cs | 6 +- .../EdgeBookmarkLoader.cs | 30 +++++++ .../EdgeBookmarks.cs | 87 ------------------- ...xBookmarks.cs => FirefoxBookmarkLoader.cs} | 33 +++---- .../IBookmarkLoader.cs | 9 ++ 9 files changed, 156 insertions(+), 231 deletions(-) create mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarkLoader.cs delete mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarks.cs create mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs create mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarkLoader.cs delete mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarks.cs rename Plugins/Flow.Launcher.Plugin.BrowserBookmark/{FirefoxBookmarks.cs => FirefoxBookmarkLoader.cs} (85%) create mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/IBookmarkLoader.cs diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Bookmark.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Bookmark.cs index 790c03686..185cf1620 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Bookmark.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Bookmark.cs @@ -7,50 +7,19 @@ using System.Text; namespace Flow.Launcher.Plugin.BrowserBookmark { - public class Bookmark : IEquatable, IEqualityComparer + // Source may be important in the future + public record Bookmark(string Name, string Url, string Source = "") { - private string m_Name; - public string Name + public override int GetHashCode() { - get - { - return m_Name; - } - set - { - m_Name = value; - } - } - public string Url { get; set; } - public string Source { get; set; } - public int Score { get; set; } - - /* TODO: since Source maybe unimportant, we just need to compare Name and Url */ - public bool Equals(Bookmark other) - { - return Equals(this, other); - } - - public bool Equals(Bookmark x, Bookmark y) - { - if (Object.ReferenceEquals(x, y)) return true; - if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) - return false; - - return x.Name == y.Name && x.Url == y.Url; - } - - public int GetHashCode(Bookmark bookmark) - { - if (Object.ReferenceEquals(bookmark, null)) return 0; - int hashName = bookmark.Name == null ? 0 : bookmark.Name.GetHashCode(); - int hashUrl = bookmark.Url == null ? 0 : bookmark.Url.GetHashCode(); + var hashName = Name?.GetHashCode() ?? 0; + var hashUrl = Url?.GetHashCode() ?? 0; return hashName ^ hashUrl; } - public override int GetHashCode() + public virtual bool Equals(Bookmark other) { - return GetHashCode(this); + return other != null && Name == other.Name && Url == other.Url; } } -} +} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarkLoader.cs new file mode 100644 index 000000000..7fdb86f31 --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarkLoader.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; + +namespace Flow.Launcher.Plugin.BrowserBookmark +{ + public class ChromeBookmarkLoader : ChromiumBookmarkLoader + { + public override List GetBookmarks() + { + return LoadChromeBookmarks(); + } + + private List LoadChromeBookmarks() + { + var bookmarks = new List(); + String platformPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + bookmarks.AddRange(LoadBookmarks(Path.Combine(platformPath, @"Google\Chrome\User Data"), "Google Chrome")); + bookmarks.AddRange(LoadBookmarks(Path.Combine(platformPath, @"Google\Chrome SxS\User Data"), "Google Chrome Canary")); + bookmarks.AddRange(LoadBookmarks(Path.Combine(platformPath, @"Chromium\User Data"), "Chromium")); + return bookmarks; + } + } +} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarks.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarks.cs deleted file mode 100644 index def7f3abe..000000000 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarks.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text.RegularExpressions; - -namespace Flow.Launcher.Plugin.BrowserBookmark -{ - public class ChromeBookmarks - { - private List bookmarks = new List(); - - public List GetBookmarks() - { - bookmarks.Clear(); - LoadChromeBookmarks(); - - return bookmarks; - } - - private void ParseChromeBookmarks(String path, string source) - { - if (!File.Exists(path)) return; - - string all = File.ReadAllText(path); - Regex nameRegex = new Regex("\"name\": \"(?.*?)\""); - MatchCollection nameCollection = nameRegex.Matches(all); - Regex typeRegex = new Regex("\"type\": \"(?.*?)\""); - MatchCollection typeCollection = typeRegex.Matches(all); - Regex urlRegex = new Regex("\"url\": \"(?.*?)\""); - MatchCollection urlCollection = urlRegex.Matches(all); - - List names = (from Match match in nameCollection select match.Groups["name"].Value).ToList(); - List types = (from Match match in typeCollection select match.Groups["type"].Value).ToList(); - List urls = (from Match match in urlCollection select match.Groups["url"].Value).ToList(); - - int urlIndex = 0; - for (int i = 0; i < names.Count; i++) - { - string name = DecodeUnicode(names[i]); - string type = types[i]; - if (type == "url") - { - string url = urls[urlIndex]; - urlIndex++; - - if (url == null) continue; - if (url.StartsWith("javascript:", StringComparison.OrdinalIgnoreCase)) continue; - if (url.StartsWith("vbscript:", StringComparison.OrdinalIgnoreCase)) continue; - - bookmarks.Add(new Bookmark() - { - Name = name, - Url = url, - Source = source - }); - } - } - } - - private void LoadChromeBookmarks(string path, string name) - { - if (!Directory.Exists(path)) return; - var paths = Directory.GetDirectories(path); - - foreach (var profile in paths) - { - if (File.Exists(Path.Combine(profile, "Bookmarks"))) - ParseChromeBookmarks(Path.Combine(profile, "Bookmarks"), name + (Path.GetFileName(profile) == "Default" ? "" : (" (" + Path.GetFileName(profile) + ")"))); - } - } - - private void LoadChromeBookmarks() - { - String platformPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); - LoadChromeBookmarks(Path.Combine(platformPath, @"Google\Chrome\User Data"), "Google Chrome"); - LoadChromeBookmarks(Path.Combine(platformPath, @"Google\Chrome SxS\User Data"), "Google Chrome Canary"); - LoadChromeBookmarks(Path.Combine(platformPath, @"Chromium\User Data"), "Chromium"); - } - - private String DecodeUnicode(String dataStr) - { - Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})"); - return reg.Replace(dataStr, m => ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString()); - } - } -} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs new file mode 100644 index 000000000..7be66eea5 --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs @@ -0,0 +1,62 @@ +using Microsoft.AspNetCore.Authentication; +using System.Collections.Generic; +using System.IO; +using System.Text.Json; + +namespace Flow.Launcher.Plugin.BrowserBookmark +{ + public abstract class ChromiumBookmarkLoader : IBookmarkLoader + { + public abstract List GetBookmarks(); + protected List LoadBookmarks(string browserDataPath, string name) + { + var bookmarks = new List(); + if (!Directory.Exists(browserDataPath)) return bookmarks; + var paths = Directory.GetDirectories(browserDataPath); + + foreach (var profile in paths) + { + var bookmarkPath = Path.Combine(profile, "Bookmarks"); + if (!File.Exists(bookmarkPath)) + continue; + + var source = name + (Path.GetFileName(profile) == "Default" ? "" : $" ({Path.GetFileName(profile)})"); + bookmarks.AddRange(LoadBookmarksFromFile(bookmarkPath, source)); + } + return bookmarks; + } + + protected List LoadBookmarksFromFile(string path, string source) + { + var bookmarks = new List(); + using var jsonDocument = JsonDocument.Parse(File.ReadAllText(path)); + if (!jsonDocument.RootElement.TryGetProperty("roots", out var rootElement)) + return new(); + foreach (var folder in rootElement.EnumerateObject()) + { + EnumerateFolderBookmark(folder.Value, bookmarks, source); + } + return bookmarks; + } + + private void EnumerateFolderBookmark(JsonElement folderElement, List bookmarks, string source) + { + foreach (var subElement in folderElement.GetProperty("children").EnumerateArray()) + { + switch (subElement.GetProperty("type").GetString()) + { + case "folder": + EnumerateFolderBookmark(subElement, bookmarks, source); + break; + default: + bookmarks.Add(new Bookmark( + subElement.GetProperty("name").GetString(), + subElement.GetProperty("url").GetString(), + source)); + break; + } + } + + } + } +} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/Bookmarks.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/Bookmarks.cs index 60c4a0ee6..c7cf4867d 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/Bookmarks.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/Bookmarks.cs @@ -20,9 +20,9 @@ namespace Flow.Launcher.Plugin.BrowserBookmark.Commands { var allbookmarks = new List(); - var chromeBookmarks = new ChromeBookmarks(); - var mozBookmarks = new FirefoxBookmarks(); - var edgeBookmarks = new EdgeBookmarks(); + var chromeBookmarks = new ChromeBookmarkLoader(); + var mozBookmarks = new FirefoxBookmarkLoader(); + var edgeBookmarks = new EdgeBookmarkLoader(); //TODO: Let the user select which browser's bookmarks are displayed // Add Firefox bookmarks diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarkLoader.cs new file mode 100644 index 000000000..d3f109445 --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarkLoader.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text.Json; +using System.Text.RegularExpressions; + +namespace Flow.Launcher.Plugin.BrowserBookmark +{ + public class EdgeBookmarkLoader : ChromiumBookmarkLoader + { + + private readonly List _bookmarks = new(); + + private void LoadEdgeBookmarks() + { + var platformPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + LoadBookmarks(Path.Combine(platformPath, @"Microsoft\Edge\User Data"), "Microsoft Edge"); + LoadBookmarks(Path.Combine(platformPath, @"Microsoft\Edge Dev\User Data"), "Microsoft Edge Dev"); + LoadBookmarks(Path.Combine(platformPath, @"Microsoft\Edge SxS\User Data"), "Microsoft Edge Canary"); + } + + public override List GetBookmarks() + { + _bookmarks.Clear(); + LoadEdgeBookmarks(); + return _bookmarks; + } + } +} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarks.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarks.cs deleted file mode 100644 index 376808549..000000000 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarks.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text.RegularExpressions; - -namespace Flow.Launcher.Plugin.BrowserBookmark -{ - public class EdgeBookmarks - { - private List bookmarks = new List(); - - public List GetBookmarks() - { - bookmarks.Clear(); - LoadEdgeBookmarks(); - - return bookmarks; - } - - private void ParseEdgeBookmarks(string path, string source) - { - if (!File.Exists(path)) return; - - string all = File.ReadAllText(path); - Regex nameRegex = new Regex("\"name\": \"(?.*?)\""); - MatchCollection nameCollection = nameRegex.Matches(all); - Regex typeRegex = new Regex("\"type\": \"(?.*?)\""); - MatchCollection typeCollection = typeRegex.Matches(all); - Regex urlRegex = new Regex("\"url\": \"(?.*?)\""); - MatchCollection urlCollection = urlRegex.Matches(all); - - List names = (from Match match in nameCollection select match.Groups["name"].Value).ToList(); - List types = (from Match match in typeCollection select match.Groups["type"].Value).ToList(); - List urls = (from Match match in urlCollection select match.Groups["url"].Value).ToList(); - - int urlIndex = 0; - for (int i = 0; i < names.Count; i++) - { - string name = DecodeUnicode(names[i]); - string type = types[i]; - if (type == "url") - { - string url = urls[urlIndex]; - urlIndex++; - - if (url == null) continue; - if (url.StartsWith("javascript:", StringComparison.OrdinalIgnoreCase)) continue; - if (url.StartsWith("vbscript:", StringComparison.OrdinalIgnoreCase)) continue; - - bookmarks.Add(new Bookmark() - { - Name = name, - Url = url, - Source = source - }); - } - } - } - - private void LoadEdgeBookmarks(string path, string name) - { - if (!Directory.Exists(path)) return; - var paths = Directory.GetDirectories(path); - - foreach (var profile in paths) - { - if (File.Exists(Path.Combine(profile, "Bookmarks"))) - ParseEdgeBookmarks(Path.Combine(profile, "Bookmarks"), name + (Path.GetFileName(profile) == "Default" ? "" : (" (" + Path.GetFileName(profile) + ")"))); - } - } - - private void LoadEdgeBookmarks() - { - string platformPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); - LoadEdgeBookmarks(Path.Combine(platformPath, @"Microsoft\Edge\User Data"), "Microsoft Edge"); - LoadEdgeBookmarks(Path.Combine(platformPath, @"Microsoft\Edge Dev\User Data"), "Microsoft Edge Dev"); - LoadEdgeBookmarks(Path.Combine(platformPath, @"Microsoft\Edge SxS\User Data"), "Microsoft Edge Canary"); - } - - private string DecodeUnicode(string dataStr) - { - Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})"); - return reg.Replace(dataStr, m => ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString()); - } - } -} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarks.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs similarity index 85% rename from Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarks.cs rename to Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs index b718da3cf..5d47c80e3 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarks.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs @@ -6,7 +6,7 @@ using System.Linq; namespace Flow.Launcher.Plugin.BrowserBookmark { - public class FirefoxBookmarks + public class FirefoxBookmarkLoader : IBookmarkLoader { private const string queryAllBookmarks = @"SELECT moz_places.url, moz_bookmarks.title FROM moz_places @@ -27,10 +27,10 @@ namespace Flow.Launcher.Plugin.BrowserBookmark if (string.IsNullOrEmpty(PlacesPath) || !File.Exists(PlacesPath)) return new List(); - var bookmarList = new List(); + var bookmarkList = new List(); // create the connection string and init the connection - string dbPath = string.Format(dbPathFormat, PlacesPath); + string dbPath = string.Format(dbPathFormat, PlacesPath); using (var dbConnection = new SQLiteConnection(dbPath)) { // Open connection to the database file and execute the query @@ -38,14 +38,13 @@ namespace Flow.Launcher.Plugin.BrowserBookmark var reader = new SQLiteCommand(queryAllBookmarks, dbConnection).ExecuteReader(); // return results in List format - bookmarList = reader.Select(x => new Bookmark() - { - Name = (x["title"] is DBNull) ? string.Empty : x["title"].ToString(), - Url = x["url"].ToString() - }).ToList(); + bookmarkList = reader.Select( + x => new Bookmark(x["title"] is DBNull ? string.Empty : x["title"].ToString(), + x["url"].ToString()) + ).ToList(); } - return bookmarList; + return bookmarkList; } /// @@ -63,7 +62,8 @@ namespace Flow.Launcher.Plugin.BrowserBookmark // get firefox default profile directory from profiles.ini string ini; - using (var sReader = new StreamReader(profileIni)) { + using (var sReader = new StreamReader(profileIni)) + { ini = sReader.ReadToEnd(); } @@ -95,7 +95,10 @@ namespace Flow.Launcher.Plugin.BrowserBookmark Version=2 */ - var lines = ini.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList(); + var lines = ini.Split(new string[] + { + "\r\n" + }, StringSplitOptions.None).ToList(); var defaultProfileFolderNameRaw = lines.Where(x => x.Contains("Default=") && x != "Default=1").FirstOrDefault() ?? string.Empty; @@ -104,14 +107,14 @@ namespace Flow.Launcher.Plugin.BrowserBookmark var defaultProfileFolderName = defaultProfileFolderNameRaw.Split('=').Last(); - var indexOfDefaultProfileAtttributePath = lines.IndexOf("Path="+ defaultProfileFolderName); + var indexOfDefaultProfileAtttributePath = lines.IndexOf("Path=" + defaultProfileFolderName); // Seen in the example above, the IsRelative attribute is always above the Path attribute var relativeAttribute = lines[indexOfDefaultProfileAtttributePath - 1]; return relativeAttribute == "0" // See above, the profile is located in a custom location, path is not relative, so IsRelative=0 - ? defaultProfileFolderName + @"\places.sqlite" - : Path.Combine(profileFolderPath, defaultProfileFolderName) + @"\places.sqlite"; + ? defaultProfileFolderName + @"\places.sqlite" + : Path.Combine(profileFolderPath, defaultProfileFolderName) + @"\places.sqlite"; } } } @@ -126,4 +129,4 @@ namespace Flow.Launcher.Plugin.BrowserBookmark } } } -} +} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/IBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/IBookmarkLoader.cs new file mode 100644 index 000000000..1dbd8b6bf --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/IBookmarkLoader.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Flow.Launcher.Plugin.BrowserBookmark +{ + public interface IBookmarkLoader + { + public List GetBookmarks(); + } +} \ No newline at end of file From d51579967bad180e9bc3da557674cfe1d40fa0c1 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Wed, 22 Sep 2021 18:09:30 -0500 Subject: [PATCH 2/6] Further Refactor & add CustomBrowsers UI --- .../ChromeBookmarkLoader.cs | 3 +- .../ChromiumBookmarkLoader.cs | 5 +- .../{Bookmarks.cs => BookmarkLoader.cs} | 16 ++++--- .../CustomChromiumBookmarkLoader.cs | 14 ++++++ .../EdgeBookmarkLoader.cs | 1 + .../FirefoxBookmarkLoader.cs | 1 + ...low.Launcher.Plugin.BrowserBookmark.csproj | 4 ++ .../IBookmarkLoader.cs | 3 +- .../Main.cs | 6 +-- .../{ => Models}/Bookmark.cs | 11 ++--- .../Models/CustomBrowser.cs | 8 ++++ .../Models/Settings.cs | 20 +++++++- .../Views/CustomBrowserSetting.xaml | 36 +++++++++++++++ .../Views/CustomBrowserSetting.xaml.cs | 45 ++++++++++++++++++ .../Views/SettingsControl.xaml | 46 +++++++++++++++---- .../Views/SettingsControl.xaml.cs | 40 +++++++++++----- 16 files changed, 218 insertions(+), 41 deletions(-) rename Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/{Bookmarks.cs => BookmarkLoader.cs} (71%) create mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/CustomChromiumBookmarkLoader.cs rename Plugins/Flow.Launcher.Plugin.BrowserBookmark/{ => Models}/Bookmark.cs (73%) create mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/CustomBrowser.cs create mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/CustomBrowserSetting.xaml create mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/CustomBrowserSetting.xaml.cs diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarkLoader.cs index 7fdb86f31..d7cfcaf6a 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarkLoader.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarkLoader.cs @@ -1,4 +1,5 @@ -using System; +using Flow.Launcher.Plugin.BrowserBookmark.Models; +using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs index 7be66eea5..a6edf3302 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Authentication; +using Flow.Launcher.Plugin.BrowserBookmark.Models; +using Microsoft.AspNetCore.Authentication; using System.Collections.Generic; using System.IO; using System.Text.Json; @@ -28,6 +29,8 @@ namespace Flow.Launcher.Plugin.BrowserBookmark protected List LoadBookmarksFromFile(string path, string source) { + if (!File.Exists(path)) + return new(); var bookmarks = new List(); using var jsonDocument = JsonDocument.Parse(File.ReadAllText(path)); if (!jsonDocument.RootElement.TryGetProperty("roots", out var rootElement)) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/Bookmarks.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/BookmarkLoader.cs similarity index 71% rename from Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/Bookmarks.cs rename to Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/BookmarkLoader.cs index c7cf4867d..b4c6235e0 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/Bookmarks.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/BookmarkLoader.cs @@ -1,11 +1,12 @@ using System.Collections.Generic; using System.Linq; using Flow.Launcher.Infrastructure; +using Flow.Launcher.Plugin.BrowserBookmark.Models; using Flow.Launcher.Plugin.SharedModels; namespace Flow.Launcher.Plugin.BrowserBookmark.Commands { - internal static class Bookmarks + internal static class BookmarkLoader { internal static MatchResult MatchProgram(Bookmark bookmark, string queryString) { @@ -18,23 +19,24 @@ namespace Flow.Launcher.Plugin.BrowserBookmark.Commands internal static List LoadAllBookmarks() { - var allbookmarks = new List(); var chromeBookmarks = new ChromeBookmarkLoader(); var mozBookmarks = new FirefoxBookmarkLoader(); var edgeBookmarks = new EdgeBookmarkLoader(); + var allBookmarks = new List(); + //TODO: Let the user select which browser's bookmarks are displayed // Add Firefox bookmarks - mozBookmarks.GetBookmarks().ForEach(x => allbookmarks.Add(x)); + allBookmarks.AddRange(mozBookmarks.GetBookmarks()); // Add Chrome bookmarks - chromeBookmarks.GetBookmarks().ForEach(x => allbookmarks.Add(x)); + allBookmarks.AddRange(chromeBookmarks.GetBookmarks()); // Add Edge (Chromium) bookmarks - edgeBookmarks.GetBookmarks().ForEach(x => allbookmarks.Add(x)); + allBookmarks.AddRange(edgeBookmarks.GetBookmarks()); - return allbookmarks.Distinct().ToList(); + return allBookmarks.Distinct().ToList(); } } -} +} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/CustomChromiumBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/CustomChromiumBookmarkLoader.cs new file mode 100644 index 000000000..91189ace7 --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/CustomChromiumBookmarkLoader.cs @@ -0,0 +1,14 @@ +using Flow.Launcher.Plugin.BrowserBookmark.Models; +using System.Collections.Generic; + +namespace Flow.Launcher.Plugin.BrowserBookmark +{ + public class CustomChromiumBookmarkLoader : ChromiumBookmarkLoader + { + public string BrowserDataPath { get; init; } + public string BookmarkFilePath { get; init; } + public string BrowserName { get; init; } + + public override List GetBookmarks() => BrowserDataPath != null ? LoadBookmarks(BrowserDataPath, BrowserName) : LoadBookmarksFromFile(BookmarkFilePath, BrowserName); + } +} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarkLoader.cs index d3f109445..d982ee99e 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarkLoader.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarkLoader.cs @@ -1,3 +1,4 @@ +using Flow.Launcher.Plugin.BrowserBookmark.Models; using System; using System.Collections.Generic; using System.IO; diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs index 5d47c80e3..3df034781 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs @@ -1,3 +1,4 @@ +using Flow.Launcher.Plugin.BrowserBookmark.Models; using System; using System.Collections.Generic; using System.Data.SQLite; diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Flow.Launcher.Plugin.BrowserBookmark.csproj b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Flow.Launcher.Plugin.BrowserBookmark.csproj index acfc79d7a..46313d38f 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Flow.Launcher.Plugin.BrowserBookmark.csproj +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Flow.Launcher.Plugin.BrowserBookmark.csproj @@ -69,4 +69,8 @@ + + + + \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/IBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/IBookmarkLoader.cs index 1dbd8b6bf..2c48cfd55 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/IBookmarkLoader.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/IBookmarkLoader.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using Flow.Launcher.Plugin.BrowserBookmark.Models; +using System.Collections.Generic; namespace Flow.Launcher.Plugin.BrowserBookmark { diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index a0b443e75..eaf14d5c2 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -26,7 +26,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark _settings = context.API.LoadSettingJsonStorage(); - cachedBookmarks = Bookmarks.LoadAllBookmarks(); + cachedBookmarks = BookmarkLoader.LoadAllBookmarks(); } public List Query(Query query) @@ -45,7 +45,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark Title = c.Name, SubTitle = c.Url, IcoPath = @"Images\bookmark.png", - Score = Bookmarks.MatchProgram(c, param).Score, + Score = BookmarkLoader.MatchProgram(c, param).Score, Action = _ => { if (_settings.OpenInNewBrowserWindow) @@ -93,7 +93,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark { cachedBookmarks.Clear(); - cachedBookmarks = Bookmarks.LoadAllBookmarks(); + cachedBookmarks = BookmarkLoader.LoadAllBookmarks(); } public string GetTranslatedPluginTitle() diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Bookmark.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Bookmark.cs similarity index 73% rename from Plugins/Flow.Launcher.Plugin.BrowserBookmark/Bookmark.cs rename to Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Bookmark.cs index 185cf1620..c2fa9d977 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Bookmark.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Bookmark.cs @@ -1,11 +1,6 @@ -using BinaryAnalysis.UnidecodeSharp; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; - -namespace Flow.Launcher.Plugin.BrowserBookmark +namespace Flow.Launcher.Plugin.BrowserBookmark.Models { // Source may be important in the future public record Bookmark(string Name, string Url, string Source = "") @@ -21,5 +16,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark { return other != null && Name == other.Name && Url == other.Url; } + + public List CustomBrowsers { get; set; }= new(); } } \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/CustomBrowser.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/CustomBrowser.cs new file mode 100644 index 000000000..1ddeab5df --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/CustomBrowser.cs @@ -0,0 +1,8 @@ +namespace Flow.Launcher.Plugin.BrowserBookmark.Models +{ + public class CustomBrowser : BaseModel + { + public string Name { get; set; } + public string Path { get; set; } + } +} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Settings.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Settings.cs index dc444fd73..e63b90dbc 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Settings.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Settings.cs @@ -1,9 +1,27 @@ -namespace Flow.Launcher.Plugin.BrowserBookmark.Models +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Text.Json.Serialization; + +namespace Flow.Launcher.Plugin.BrowserBookmark.Models { public class Settings : BaseModel { public bool OpenInNewBrowserWindow { get; set; } = true; public string BrowserPath { get; set; } + + public ObservableCollection CustomBrowsers { get; set; } = new() + { + new CustomBrowser + { + Name="awefawef", + Path="awefawef" + }, + new CustomBrowser + { + Name = "awefawefawefawef", + Path = "aweawefawfawef" + } + }; } } \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/CustomBrowserSetting.xaml b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/CustomBrowserSetting.xaml new file mode 100644 index 000000000..2370789f1 --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/CustomBrowserSetting.xaml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + +