2
using System.Collections.Generic;
10
public class FirefoxBookmarkItemSource : IItemSource
13
const string BeginProfileName = "Path=";
14
const string BeginDefaultProfile = "Name=default";
15
const string BeginURL = "<DT><A HREF=\"";
16
const string EndURL = "\"";
17
const string BeginName = "\">";
18
const string EndName = "</A>";
20
List<IItem> bookmarks;
22
public static string GetFirefoxBookmarkFilePath ()
24
string home, path, profile;
28
home = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
29
path = System.IO.Path.Combine (home, ".mozilla/firefox/profiles.ini");
31
reader = System.IO.File.OpenText (path);
36
bool got_default = false;
37
for (string line = reader.ReadLine (); line != null; line = reader.ReadLine ()) {
38
if (got_default && line.StartsWith (BeginProfileName)) {
40
line = line.Substring (BeginProfileName.Length);
44
else if (line.StartsWith (BeginDefaultProfile)) {
50
if (profile == null) {
53
path = System.IO.Path.Combine (home, ".mozilla/firefox");
54
path = System.IO.Path.Combine (path, profile);
55
path = System.IO.Path.Combine (path, "bookmarks.html");
60
public FirefoxBookmarkItemSource ()
62
bookmarks = new List<IItem> ();
67
get { return "Firefox Bookmarks"; }
70
public string Description {
71
get { return "Finds Firefox bookmarks in your default profile."; }
78
public ICollection<IItem> Items {
79
get { return bookmarks; }
82
public bool UpdateItems ()
84
bookmarks.AddRange (ReadBookmarksFromFile (GetFirefoxBookmarkFilePath ()));
88
protected ICollection<IItem> ReadBookmarksFromFile (string file)
90
ICollection<IItem> list;
92
int urlIndex, nameIndex;
95
list = new List<IItem> ();
98
reader = System.IO.File.OpenText (file);
99
for (string line = reader.ReadLine (); line != null; line = reader.ReadLine ()) {
101
urlIndex = line.IndexOf (BeginURL);
102
if (urlIndex < 0) continue;
103
line = line.Substring (urlIndex + BeginURL.Length);
104
url = line.Substring (0, line.IndexOf (EndURL));
106
nameIndex = line.IndexOf (BeginName);
107
if (nameIndex < 0) continue;
108
line = line.Substring (nameIndex + BeginName.Length);
109
name = line.Substring (0, line.IndexOf (EndName));
113
list.Add (new BookmarkItem (name, url));