23
23
using System.Collections.Generic;
27
public class FirefoxBookmarkItemSource : IItemSource
25
namespace Do.Universe {
27
public class FirefoxBookmarkItemSource : IItemSource {
29
29
const string BeginProfileName = "Path=";
30
30
const string BeginDefaultProfile = "Default=1";
31
31
const string BeginURL = "<DT><A HREF=\"";
35
35
const string BeginName = "\">";
36
36
const string EndName = "</A>";
38
List<IItem> bookmarks;
38
ICollection<IItem> items;
41
41
/// Initialize the item source.
43
43
public FirefoxBookmarkItemSource ()
45
bookmarks = new List<IItem> ();
45
items = new IItem [0];
49
public Type[] SupportedItemTypes
49
public Type[] SupportedItemTypes {
52
51
return new Type[] {
53
52
typeof (BookmarkItem),
60
58
get { return "Firefox Bookmarks"; }
63
public string Description
61
public string Description {
65
62
get { return "Finds Firefox bookmarks in your default profile."; }
70
66
get { return "firefox"; }
73
public ICollection<IItem> Items
75
get { return bookmarks; }
69
public ICollection<IItem> Items {
78
73
public ICollection<IItem> ChildrenOfItem (IItem item)
96
89
/// A <see cref="System.String"/> containing the absolute path to the
97
90
/// bookmarks.html file of the default firefox profile for the current user.
99
public static string GetFirefoxBookmarkFilePath ()
101
string home, path, profile;
92
static string BookmarkFilePath { get {
102
93
StreamReader reader;
94
string line, path, profile;
105
home = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
106
path = Path.Combine (home, ".mozilla/firefox/profiles.ini");
97
path = Path.Combine (Paths.UserHome,
98
".mozilla/firefox/profiles.ini");
108
100
reader = File.OpenText (path);
113
for (string line = reader.ReadLine (); line != null; line = reader.ReadLine ()) {
105
while (null != (line = reader.ReadLine ())) {
114
106
if (line.StartsWith (BeginDefaultProfile)) break;
115
107
if (line.StartsWith (BeginProfileName)) {
116
108
line = line.Trim ();
123
if (profile == null) {
126
path = Path.Combine (home, ".mozilla/firefox");
127
path = Path.Combine (path, profile);
128
path = Path.Combine (path, "bookmarks.html");
115
if (profile == null) return null;
116
path = Paths.Combine (Paths.UserHome,
117
".mozilla/firefox", profile, "bookmarks.html");
133
/// Given a bookmarks file, create a BookmarkItem for each bookmark found
134
/// in the file, returning a collection of BookmarkItems created.
136
/// <param name="file">
137
/// A <see cref="System.String"/> containing the absolute path to a Firefox
138
/// bookmarks.html file (e.g. the path returned by GetFirefoxBookmarkFilePath).
141
/// A <see cref="ICollection`1"/> of BookmarkItems.
143
protected ICollection<BookmarkItem> ReadBookmarksFromFile (string file)
145
ICollection<BookmarkItem> list;
121
protected ICollection<IItem> BookmarkItems { get {
146
122
StreamReader reader;
123
List<IItem> bookmarks;
124
string line, url, name, shortcut;
147
125
int urlIndex, nameIndex, shortcutIndex;
148
string url, name, shortcut;
150
list = new List<BookmarkItem> ();
127
bookmarks = new List<IItem> ();
153
reader = File.OpenText (file);
154
for (string line = reader.ReadLine (); line != null; line = reader.ReadLine ()) {
129
reader = File.OpenText (BookmarkFilePath);
130
while (null != (line = reader.ReadLine ())) {
156
132
urlIndex = line.IndexOf (BeginURL);
157
133
if (urlIndex < 0) continue;
158
134
line = line.Substring (urlIndex + BeginURL.Length);
159
135
url = line.Substring (0, line.IndexOf (EndURL));
161
// See if this bookmark has a shortcut (SHORTCUTURL="blog")
137
// See if this bookmark has a shortcut
138
// (SHORTCUTURL="blog")
163
140
shortcutIndex = line.IndexOf (BeginShortcut);
164
141
if (shortcutIndex > 0) {
165
line = line.Substring (shortcutIndex + BeginShortcut.Length);
166
shortcut = line.Substring (0, line.IndexOf (EndShortcut));
142
line = line.Substring (shortcutIndex +
143
BeginShortcut.Length);
144
shortcut = line.Substring (0,
145
line.IndexOf (EndShortcut));
169
148
nameIndex = line.IndexOf (BeginName);
177
list.Add (new BookmarkItem (name, url));
155
bookmarks.Add (new BookmarkItem (name, url));
178
156
if (shortcut != null)
179
list.Add (new BookmarkItem (shortcut, url));
157
bookmarks.Add (new BookmarkItem (shortcut, url));