~bas-dotbas/do/win32-next

« back to all changes in this revision

Viewing changes to Do.Addins/src/Do.Universe/FirefoxBookmarkItemSource.cs

  • Committer: djsiegel at gmail
  • Date: 2007-10-13 21:55:22 UTC
  • Revision ID: djsiegel@gmail.com-20071013215522-n0vjgog0tyjfnpno
Restructured plugins library.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
 
 
4
using Gnome;
 
5
using Gnome.Vfs;
 
6
using System.IO;
 
7
 
 
8
namespace Do.Universe
 
9
{
 
10
        public class FirefoxBookmarkItemSource : IItemSource
 
11
        {
 
12
        
 
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>";
 
19
                
 
20
                List<IItem> bookmarks;
 
21
                
 
22
                public static string GetFirefoxBookmarkFilePath ()
 
23
                {
 
24
                        string home, path, profile;
 
25
                        StreamReader reader;
 
26
 
 
27
                        profile = null;
 
28
                        home = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
 
29
                        path = System.IO.Path.Combine (home, ".mozilla/firefox/profiles.ini");
 
30
                        try {
 
31
                                reader = System.IO.File.OpenText (path);
 
32
                        } catch {
 
33
                                return null;
 
34
                        }
 
35
                        
 
36
                        bool got_default = false;
 
37
                        for (string line = reader.ReadLine (); line != null; line = reader.ReadLine ()) {
 
38
                                if (got_default && line.StartsWith (BeginProfileName)) {
 
39
                                        line = line.Trim ();
 
40
                                        line = line.Substring (BeginProfileName.Length);
 
41
                                        profile = line;
 
42
                                        break;
 
43
                                }
 
44
                                else if (line.StartsWith (BeginDefaultProfile)) {
 
45
                                        got_default = true;
 
46
                                }
 
47
                        }
 
48
                        reader.Close ();
 
49
                        
 
50
                        if (profile == null) {
 
51
                                return null;
 
52
                        }
 
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");
 
56
                        return path;
 
57
                        
 
58
                }
 
59
        
 
60
                public FirefoxBookmarkItemSource ()
 
61
                {
 
62
                        bookmarks = new List<IItem> ();
 
63
                        UpdateItems ();
 
64
                }
 
65
                
 
66
                public string Name {
 
67
                        get { return "Firefox Bookmarks"; }
 
68
                }
 
69
                
 
70
                public string Description {
 
71
                        get { return "Finds Firefox bookmarks in your default profile."; }
 
72
                }
 
73
                
 
74
                public string Icon {
 
75
                        get { return "www"; }
 
76
                }
 
77
                
 
78
                public ICollection<IItem> Items {
 
79
                        get { return bookmarks; }
 
80
                }
 
81
                
 
82
                public bool UpdateItems ()
 
83
                {
 
84
                        bookmarks.AddRange (ReadBookmarksFromFile (GetFirefoxBookmarkFilePath ()));
 
85
                        return true;
 
86
                }
 
87
                
 
88
                protected ICollection<IItem> ReadBookmarksFromFile (string file)
 
89
                {
 
90
                        ICollection<IItem> list;
 
91
                        StreamReader reader;
 
92
                        int urlIndex, nameIndex;
 
93
                        string url, name;
 
94
                        
 
95
                        list = new List<IItem> ();
 
96
 
 
97
                        try {
 
98
                                reader = System.IO.File.OpenText (file);
 
99
                                for (string line = reader.ReadLine (); line != null; line = reader.ReadLine ()) {
 
100
                                        try {
 
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));
 
105
                                                
 
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));
 
110
                                        } catch {
 
111
                                                continue;
 
112
                                        }
 
113
                                        list.Add (new BookmarkItem (name, url));
 
114
                                }       
 
115
                        } catch {
 
116
                                list.Clear ();
 
117
                        }
 
118
                        return list;
 
119
                }
 
120
                
 
121
        }
 
122
}