~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to Firefox/src/FirefoxBookmarkItemSource.cs

* Cleanup Tomboy.
* Add Firefox.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* FirefoxBookmarkItemSource.cs
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * COPYRIGHT file distributed with this
 
5
 * source distribution.
 
6
 *
 
7
 * This program is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
using System;
 
22
using System.IO;
 
23
using System.Collections.Generic;
 
24
 
 
25
using Do;
 
26
using Do.Universe;
 
27
 
 
28
namespace Mozilla {
 
29
 
 
30
        public class FirefoxBookmarkItemSource : IItemSource {
 
31
 
 
32
                const string BeginProfileName = "Path=";
 
33
                const string BeginDefaultProfile = "Default=1";
 
34
                const string BeginURL = "<DT><A HREF=\"";
 
35
                const string EndURL = "\"";
 
36
                const string BeginShortcut = "SHORTCUTURL=\"";
 
37
                const string EndShortcut = "\"";
 
38
                const string BeginName = "\">";
 
39
                const string EndName = "</A>";
 
40
                
 
41
                ICollection<IItem> items;
 
42
                
 
43
                /// <summary>
 
44
                /// Initialize the item source.
 
45
                /// </summary>
 
46
                public FirefoxBookmarkItemSource ()
 
47
                {
 
48
                        items = new IItem [0];
 
49
                        UpdateItems ();
 
50
                }
 
51
                
 
52
                public Type[] SupportedItemTypes {
 
53
                        get {
 
54
                                return new Type[] {
 
55
                                        typeof (BookmarkItem),
 
56
                                };
 
57
                        }
 
58
                }
 
59
                
 
60
                public string Name {
 
61
                        get { return "Firefox Bookmarks"; }
 
62
                }
 
63
                
 
64
                public string Description {
 
65
                        get { return "Finds Firefox bookmarks in your default profile."; }
 
66
                }
 
67
                
 
68
                public string Icon {
 
69
                        get { return "firefox"; }
 
70
                }
 
71
                
 
72
                public ICollection<IItem> Items {
 
73
                        get { return items; }
 
74
                }
 
75
                
 
76
                public ICollection<IItem> ChildrenOfItem (IItem item)
 
77
                {
 
78
                        return null;
 
79
                }
 
80
                
 
81
                public void UpdateItems ()
 
82
                {
 
83
                        items = BookmarkItems;
 
84
                }
 
85
                
 
86
                /// <summary>
 
87
                /// Looks in the firefox profiles file (~/.mozilla/firefox/profiles.ini)
 
88
                /// for the name of the default profile, and returns the path to the
 
89
                /// default profile.
 
90
                /// </summary>
 
91
                /// <returns>
 
92
                /// A <see cref="System.String"/> containing the absolute path to the
 
93
                /// bookmarks.html file of the default firefox profile for the current user.
 
94
                /// </returns>
 
95
                static string BookmarkFilePath { get {
 
96
                        StreamReader reader;
 
97
                        string line, path, profile;
 
98
 
 
99
                        profile = null;
 
100
                        path = Path.Combine (Paths.UserHome,
 
101
                                ".mozilla/firefox/profiles.ini");
 
102
                        try {
 
103
                                reader = File.OpenText (path);
 
104
                        } catch {
 
105
                                return null;
 
106
                        }
 
107
                        
 
108
                        while (null != (line = reader.ReadLine ())) {
 
109
                                if (line.StartsWith (BeginDefaultProfile)) break;
 
110
                                if (line.StartsWith (BeginProfileName)) {
 
111
                                        line = line.Trim ();
 
112
                                        line = line.Substring (BeginProfileName.Length);
 
113
                                        profile = line;
 
114
                                }
 
115
                        }
 
116
                        reader.Close ();
 
117
                        
 
118
                        if (profile == null) return null;
 
119
                        path = Paths.Combine (Paths.UserHome,
 
120
                                ".mozilla/firefox", profile, "bookmarks.html");
 
121
                        return path;
 
122
                } }
 
123
                
 
124
                protected ICollection<IItem> BookmarkItems { get {
 
125
                        StreamReader reader;
 
126
                        List<IItem> bookmarks;
 
127
                        string line, url, name, shortcut;
 
128
                        int urlIndex, nameIndex, shortcutIndex;
 
129
                        
 
130
                        bookmarks = new List<IItem> ();
 
131
                        try {
 
132
                                reader = File.OpenText (BookmarkFilePath);
 
133
                                while (null != (line = reader.ReadLine ())) {
 
134
                                        try {
 
135
                                                urlIndex = line.IndexOf (BeginURL);
 
136
                                                if (urlIndex < 0) continue;
 
137
                                                line = line.Substring (urlIndex + BeginURL.Length);
 
138
                                                url = line.Substring (0, line.IndexOf (EndURL));
 
139
 
 
140
                                                // See if this bookmark has a shortcut
 
141
                                                // (SHORTCUTURL="blog")
 
142
                                                shortcut = null;
 
143
                                                shortcutIndex = line.IndexOf (BeginShortcut);
 
144
                                                if (shortcutIndex > 0) {
 
145
                                                        line = line.Substring (shortcutIndex +
 
146
                                                                BeginShortcut.Length);
 
147
                                                        shortcut = line.Substring (0,
 
148
                                                                line.IndexOf (EndShortcut));
 
149
                                                }
 
150
                                                
 
151
                                                nameIndex = line.IndexOf (BeginName);
 
152
                                                if (nameIndex < 0) continue;
 
153
                                                line = line.Substring (nameIndex + BeginName.Length);
 
154
                                                name = line.Substring (0, line.IndexOf (EndName));
 
155
                                        } catch {
 
156
                                                continue;
 
157
                                        }
 
158
                                        bookmarks.Add (new BookmarkItem (name, url));
 
159
                                        if (shortcut != null)
 
160
                                                bookmarks.Add (new BookmarkItem (shortcut, url));
 
161
                                }       
 
162
                        } catch {
 
163
                                bookmarks.Clear ();
 
164
                        }
 
165
                        return bookmarks;
 
166
                } }
 
167
        }
 
168
}