~kamstrup/unity-lens-files/gi-friendly-api-tweaks

« back to all changes in this revision

Viewing changes to src/folder.vala

  • Committer: Mikkel Kamstrup Erlandsen
  • Date: 2011-03-23 10:17:22 UTC
  • mfrom: (162.1.3 folder-search)
  • Revision ID: mikkel.kamstrup@gmail.com-20110323101722-o3dd05bazz8r4jce
Merge Mikkel's branch lp:~kamstrup/unity-place-files/folder-search:

 * Make bookmarked folders searchable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Canonical Ltd
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
 
17
 *
 
18
 */
 
19
 
 
20
namespace Unity.FilesPlace {
 
21
 
 
22
  public class Bookmarks : Object
 
23
  {
 
24
    
 
25
    private List<Bookmark> bookmarks;
 
26
    private string bookmarks_file;
 
27
    private FileMonitor monitor;
 
28
    
 
29
    public signal void changed ();
 
30
    
 
31
    public Bookmarks ()
 
32
    {
 
33
      bookmarks_file = @"$(Environment.get_home_dir())/.gtk-bookmarks";
 
34
      update();
 
35
      
 
36
      /* Update the bookmarks list whener the bookmarks file changes */
 
37
      try {
 
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.");
 
42
          update ();
 
43
          changed ();
 
44
        });
 
45
      } catch (Error e) {
 
46
        warning ("Failed to install file monitor on %s. Bookmarks monitoring disabled: %s",
 
47
                 bookmarks_file, e.message);
 
48
      }
 
49
    }
 
50
    
 
51
    private void update ()
 
52
    {
 
53
      bookmarks = new List<Bookmark> ();
 
54
      string contents;
 
55
      
 
56
      try {
 
57
        FileUtils.get_contents (bookmarks_file, out contents);
 
58
        
 
59
      } catch (FileError e) {
 
60
        warning ("Failed to read favorites: %s", e.message);
 
61
        return;
 
62
      }
 
63
      
 
64
      string[] favorites = contents.split ("\n");
 
65
      string mimetype = "inode/directory";
 
66
      
 
67
      
 
68
      foreach (var uri in favorites)
 
69
      {
 
70
        if (uri == "")
 
71
          continue;
 
72
              
 
73
        string[] parts = uri.split (" ", 2);
 
74
        string display_name;
 
75
        
 
76
        if (parts.length == 1)
 
77
          {
 
78
            display_name = Uri.unescape_string (uri);
 
79
            display_name = Filename.display_basename (display_name);
 
80
          }
 
81
        else if (parts.length == 2)
 
82
          {
 
83
            uri = parts[0];
 
84
            display_name = parts[1];
 
85
          }
 
86
        else
 
87
          {
 
88
            warning ("Internal error computing display name for favorite '%s'",
 
89
                     uri);
 
90
            display_name = uri;
 
91
          }
 
92
        
 
93
        var bookmark = new Bookmark (uri, mimetype, display_name);
 
94
        bookmarks.append (bookmark);
 
95
      }
 
96
    }
 
97
    
 
98
    public unowned List<Bookmark> list ()
 
99
    {
 
100
      return bookmarks;
 
101
    }
 
102
    
 
103
    public List<Bookmark> prefix_search (string search)
 
104
    {
 
105
      var prefix = Utils.normalize_string (search);
 
106
      var matches = new List<Bookmark> ();
 
107
      
 
108
      foreach (var bookmark in bookmarks)
 
109
      {
 
110
        foreach (var term in bookmark.get_index_terms ())
 
111
        {
 
112
          if (term.has_prefix (prefix))
 
113
            {
 
114
              /* Register a hit for this bookmark */
 
115
              matches.append (bookmark);
 
116
              break;
 
117
            }
 
118
        }
 
119
      }
 
120
      
 
121
      return (owned) matches;
 
122
    }
 
123
  
 
124
  }
 
125
  
 
126
  private class Bookmark : Object
 
127
  {
 
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; }
 
132
    
 
133
    private List<string> index_terms;
 
134
  
 
135
    public Bookmark (string uri, string mimetype, string display_name)
 
136
    {
 
137
      Object (uri:uri, icon:Utils.get_icon_for_uri (uri, mimetype),
 
138
              mimetype:mimetype, display_name:display_name);
 
139
      
 
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));
 
143
    }
 
144
    
 
145
    public unowned List<string> get_index_terms ()
 
146
    {
 
147
      return index_terms;
 
148
    }
 
149
  }
 
150
  
 
151
 
 
152
} /* end: namespace */