2
* Copyright (C) 2011 Canonical Ltd
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
20
namespace Unity.FilesPlace {
22
public class Bookmarks : Object
25
private List<Bookmark> bookmarks;
26
private string bookmarks_file;
27
private FileMonitor monitor;
29
public signal void changed ();
33
bookmarks_file = @"$(Environment.get_home_dir())/.gtk-bookmarks";
36
/* Update the bookmarks list whener the bookmarks file changes */
38
monitor = File.new_for_path (bookmarks_file).monitor (FileMonitorFlags.NONE);
39
monitor.set_rate_limit (2000); // max 1 update every 2s
40
monitor.changed.connect ((mon, file, other_file, event_type) => {
41
debug ("Bookmarks changed. Updating.");
46
warning ("Failed to install file monitor on %s. Bookmarks monitoring disabled: %s",
47
bookmarks_file, e.message);
51
private void update ()
53
bookmarks = new List<Bookmark> ();
57
FileUtils.get_contents (bookmarks_file, out contents);
59
} catch (FileError e) {
60
warning ("Failed to read favorites: %s", e.message);
64
string[] favorites = contents.split ("\n");
65
string mimetype = "inode/directory";
68
foreach (var uri in favorites)
73
string[] parts = uri.split (" ", 2);
76
if (parts.length == 1)
78
display_name = Uri.unescape_string (uri);
79
display_name = Filename.display_basename (display_name);
81
else if (parts.length == 2)
84
display_name = parts[1];
88
warning ("Internal error computing display name for favorite '%s'",
93
var bookmark = new Bookmark (uri, mimetype, display_name);
94
bookmarks.append (bookmark);
98
public unowned List<Bookmark> list ()
103
public List<Bookmark> prefix_search (string search)
105
var prefix = Utils.normalize_string (search);
106
var matches = new List<Bookmark> ();
108
foreach (var bookmark in bookmarks)
110
foreach (var term in bookmark.get_index_terms ())
112
if (term.has_prefix (prefix))
114
/* Register a hit for this bookmark */
115
matches.append (bookmark);
121
return (owned) matches;
126
private class Bookmark : Object
128
public string uri { get; set construct; }
129
public string icon { get; set construct; }
130
public string mimetype { get; set construct; }
131
public string display_name { get; set construct; }
133
private List<string> index_terms;
135
public Bookmark (string uri, string mimetype, string display_name)
137
Object (uri:uri, icon:Utils.get_icon_for_uri (uri, mimetype),
138
mimetype:mimetype, display_name:display_name);
140
index_terms = new List<string> ();
141
index_terms.append (Utils.normalize_string (Path.get_basename (uri)));
142
index_terms.append (Utils.normalize_string (display_name));
145
public unowned List<string> get_index_terms ()
152
} /* end: namespace */