Skip to content

Commit 7ff9b68

Browse files
committed
Refactor Bookmark plugin
1 parent df9cb4b commit 7ff9b68

File tree

9 files changed

+156
-231
lines changed

9 files changed

+156
-231
lines changed

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Bookmark.cs

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,19 @@
77

88
namespace Flow.Launcher.Plugin.BrowserBookmark
99
{
10-
public class Bookmark : IEquatable<Bookmark>, IEqualityComparer<Bookmark>
10+
// Source may be important in the future
11+
public record Bookmark(string Name, string Url, string Source = "")
1112
{
12-
private string m_Name;
13-
public string Name
14-
{
15-
get
16-
{
17-
return m_Name;
18-
}
19-
set
20-
{
21-
m_Name = value;
22-
}
23-
}
24-
public string Url { get; set; }
25-
public string Source { get; set; }
26-
public int Score { get; set; }
27-
28-
/* TODO: since Source maybe unimportant, we just need to compare Name and Url */
29-
public bool Equals(Bookmark other)
30-
{
31-
return Equals(this, other);
32-
}
33-
34-
public bool Equals(Bookmark x, Bookmark y)
35-
{
36-
if (Object.ReferenceEquals(x, y)) return true;
37-
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
38-
return false;
39-
40-
return x.Name == y.Name && x.Url == y.Url;
41-
}
42-
43-
public int GetHashCode(Bookmark bookmark)
13+
public override int GetHashCode()
4414
{
45-
if (Object.ReferenceEquals(bookmark, null)) return 0;
46-
int hashName = bookmark.Name == null ? 0 : bookmark.Name.GetHashCode();
47-
int hashUrl = bookmark.Url == null ? 0 : bookmark.Url.GetHashCode();
15+
var hashName = Name?.GetHashCode() ?? 0;
16+
var hashUrl = Url?.GetHashCode() ?? 0;
4817
return hashName ^ hashUrl;
4918
}
5019

51-
public override int GetHashCode()
20+
public virtual bool Equals(Bookmark other)
5221
{
53-
return GetHashCode(this);
22+
return other != null && Name == other.Name && Url == other.Url;
5423
}
5524
}
56-
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text.RegularExpressions;
6+
7+
namespace Flow.Launcher.Plugin.BrowserBookmark
8+
{
9+
public class ChromeBookmarkLoader : ChromiumBookmarkLoader
10+
{
11+
public override List<Bookmark> GetBookmarks()
12+
{
13+
return LoadChromeBookmarks();
14+
}
15+
16+
private List<Bookmark> LoadChromeBookmarks()
17+
{
18+
var bookmarks = new List<Bookmark>();
19+
String platformPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
20+
bookmarks.AddRange(LoadBookmarks(Path.Combine(platformPath, @"Google\Chrome\User Data"), "Google Chrome"));
21+
bookmarks.AddRange(LoadBookmarks(Path.Combine(platformPath, @"Google\Chrome SxS\User Data"), "Google Chrome Canary"));
22+
bookmarks.AddRange(LoadBookmarks(Path.Combine(platformPath, @"Chromium\User Data"), "Chromium"));
23+
return bookmarks;
24+
}
25+
}
26+
}

Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarks.cs

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text.Json;
5+
6+
namespace Flow.Launcher.Plugin.BrowserBookmark
7+
{
8+
public abstract class ChromiumBookmarkLoader : IBookmarkLoader
9+
{
10+
public abstract List<Bookmark> GetBookmarks();
11+
protected List<Bookmark> LoadBookmarks(string browserDataPath, string name)
12+
{
13+
var bookmarks = new List<Bookmark>();
14+
if (!Directory.Exists(browserDataPath)) return bookmarks;
15+
var paths = Directory.GetDirectories(browserDataPath);
16+
17+
foreach (var profile in paths)
18+
{
19+
var bookmarkPath = Path.Combine(profile, "Bookmarks");
20+
if (!File.Exists(bookmarkPath))
21+
continue;
22+
23+
var source = name + (Path.GetFileName(profile) == "Default" ? "" : $" ({Path.GetFileName(profile)})");
24+
bookmarks.AddRange(LoadBookmarksFromFile(bookmarkPath, source));
25+
}
26+
return bookmarks;
27+
}
28+
29+
protected List<Bookmark> LoadBookmarksFromFile(string path, string source)
30+
{
31+
var bookmarks = new List<Bookmark>();
32+
using var jsonDocument = JsonDocument.Parse(File.ReadAllText(path));
33+
if (!jsonDocument.RootElement.TryGetProperty("roots", out var rootElement))
34+
return new();
35+
foreach (var folder in rootElement.EnumerateObject())
36+
{
37+
EnumerateFolderBookmark(folder.Value, bookmarks, source);
38+
}
39+
return bookmarks;
40+
}
41+
42+
private void EnumerateFolderBookmark(JsonElement folderElement, List<Bookmark> bookmarks, string source)
43+
{
44+
foreach (var subElement in folderElement.GetProperty("children").EnumerateArray())
45+
{
46+
switch (subElement.GetProperty("type").GetString())
47+
{
48+
case "folder":
49+
EnumerateFolderBookmark(subElement, bookmarks, source);
50+
break;
51+
default:
52+
bookmarks.Add(new Bookmark(
53+
subElement.GetProperty("name").GetString(),
54+
subElement.GetProperty("url").GetString(),
55+
source));
56+
break;
57+
}
58+
}
59+
60+
}
61+
}
62+
}

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/Bookmarks.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ internal static List<Bookmark> LoadAllBookmarks()
2020
{
2121
var allbookmarks = new List<Bookmark>();
2222

23-
var chromeBookmarks = new ChromeBookmarks();
24-
var mozBookmarks = new FirefoxBookmarks();
25-
var edgeBookmarks = new EdgeBookmarks();
23+
var chromeBookmarks = new ChromeBookmarkLoader();
24+
var mozBookmarks = new FirefoxBookmarkLoader();
25+
var edgeBookmarks = new EdgeBookmarkLoader();
2626

2727
//TODO: Let the user select which browser's bookmarks are displayed
2828
// Add Firefox bookmarks
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text.Json;
6+
using System.Text.RegularExpressions;
7+
8+
namespace Flow.Launcher.Plugin.BrowserBookmark
9+
{
10+
public class EdgeBookmarkLoader : ChromiumBookmarkLoader
11+
{
12+
13+
private readonly List<Bookmark> _bookmarks = new();
14+
15+
private void LoadEdgeBookmarks()
16+
{
17+
var platformPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
18+
LoadBookmarks(Path.Combine(platformPath, @"Microsoft\Edge\User Data"), "Microsoft Edge");
19+
LoadBookmarks(Path.Combine(platformPath, @"Microsoft\Edge Dev\User Data"), "Microsoft Edge Dev");
20+
LoadBookmarks(Path.Combine(platformPath, @"Microsoft\Edge SxS\User Data"), "Microsoft Edge Canary");
21+
}
22+
23+
public override List<Bookmark> GetBookmarks()
24+
{
25+
_bookmarks.Clear();
26+
LoadEdgeBookmarks();
27+
return _bookmarks;
28+
}
29+
}
30+
}

Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarks.cs

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)