~ubuntu-branches/ubuntu/precise/gnome-do/precise-proposed

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2008-04-14 21:36:12 UTC
  • mto: (0.1.8 sid)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20080414213612-h0dfxdodm6vpcc1h
Tags: upstream-0.4.2.0
ImportĀ upstreamĀ versionĀ 0.4.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
using System.IO;
23
23
using System.Collections.Generic;
24
24
 
25
 
namespace Do.Universe
26
 
{
27
 
        public class FirefoxBookmarkItemSource : IItemSource
28
 
        {
 
25
namespace Do.Universe {
 
26
 
 
27
        public class FirefoxBookmarkItemSource : IItemSource {
 
28
 
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>";
37
37
                
38
 
                List<IItem> bookmarks;
 
38
                ICollection<IItem> items;
39
39
                
40
40
                /// <summary>
41
41
                /// Initialize the item source.
42
42
                /// </summary>
43
43
                public FirefoxBookmarkItemSource ()
44
44
                {
45
 
                        bookmarks = new List<IItem> ();
 
45
                        items = new IItem [0];
46
46
                        UpdateItems ();
47
47
                }
48
48
                
49
 
                public Type[] SupportedItemTypes
50
 
                {
 
49
                public Type[] SupportedItemTypes {
51
50
                        get {
52
51
                                return new Type[] {
53
52
                                        typeof (BookmarkItem),
55
54
                        }
56
55
                }
57
56
                
58
 
                public string Name
59
 
                {
 
57
                public string Name {
60
58
                        get { return "Firefox Bookmarks"; }
61
59
                }
62
60
                
63
 
                public string Description
64
 
                {
 
61
                public string Description {
65
62
                        get { return "Finds Firefox bookmarks in your default profile."; }
66
63
                }
67
64
                
68
 
                public string Icon
69
 
                {
 
65
                public string Icon {
70
66
                        get { return "firefox"; }
71
67
                }
72
68
                
73
 
                public ICollection<IItem> Items
74
 
                {
75
 
                        get { return bookmarks; }
 
69
                public ICollection<IItem> Items {
 
70
                        get { return items; }
76
71
                }
77
72
                
78
73
                public ICollection<IItem> ChildrenOfItem (IItem item)
82
77
                
83
78
                public void UpdateItems ()
84
79
                {
85
 
                        bookmarks.Clear ();
86
 
                        foreach (IItem item in ReadBookmarksFromFile (GetFirefoxBookmarkFilePath ()))
87
 
                                bookmarks.Add (item);
 
80
                        items = BookmarkItems;
88
81
                }
89
82
                
90
83
                /// <summary>
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.
98
91
                /// </returns>
99
 
                public static string GetFirefoxBookmarkFilePath ()
100
 
                {
101
 
                        string home, path, profile;
 
92
                static string BookmarkFilePath { get {
102
93
                        StreamReader reader;
 
94
                        string line, path, profile;
103
95
 
104
96
                        profile = null;
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");
107
99
                        try {
108
100
                                reader = File.OpenText (path);
109
101
                        } catch {
110
102
                                return null;
111
103
                        }
112
104
                        
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 ();
120
112
                        }
121
113
                        reader.Close ();
122
114
                        
123
 
                        if (profile == null) {
124
 
                                return null;
125
 
                        }
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");
129
118
                        return path;
130
 
                }
 
119
                } }
131
120
                
132
 
                /// <summary>
133
 
                /// Given a bookmarks file, create a BookmarkItem for each bookmark found
134
 
                /// in the file, returning a collection of BookmarkItems created.
135
 
                /// </summary>
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).
139
 
                /// </param>
140
 
                /// <returns>
141
 
                /// A <see cref="ICollection`1"/> of BookmarkItems.
142
 
                /// </returns>
143
 
                protected ICollection<BookmarkItem> ReadBookmarksFromFile (string file)
144
 
                {
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;
149
126
                        
150
 
                        list = new List<BookmarkItem> ();
151
 
 
 
127
                        bookmarks = new List<IItem> ();
152
128
                        try {
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 ())) {
155
131
                                        try {
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));
160
136
 
161
 
                                                // See if this bookmark has a shortcut (SHORTCUTURL="blog")
 
137
                                                // See if this bookmark has a shortcut
 
138
                                                // (SHORTCUTURL="blog")
162
139
                                                shortcut = null;
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));
167
146
                                                }
168
147
                                                
169
148
                                                nameIndex = line.IndexOf (BeginName);
173
152
                                        } catch {
174
153
                                                continue;
175
154
                                        }
176
 
 
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));
180
158
                                }       
181
159
                        } catch {
182
 
                                list.Clear ();
 
160
                                bookmarks.Clear ();
183
161
                        }
184
 
                        return list;
185
 
                }
 
162
                        return bookmarks;
 
163
                } }
186
164
        }
187
165
}