~timo-jyrinki/ubuntu/quantal/unity-lens-files/ubuntu.660

« back to all changes in this revision

Viewing changes to src/folder.vala

  • Committer: timo.jyrinki at canonical
  • Date: 2012-09-19 15:56:19 UTC
  • mfrom: (14.4.39)
  • Revision ID: timo.jyrinki@canonical.com-20120919155619-gh3fni23290uxt1r
* New upstream release.
  - Filter out dummy 'x-desktop-nautilus' entry in .gtk-bookmarks
    (LP: #1044309)
  - Set provide_personal_content flag (LP: #1049593)
  - Add filesystem icons in the "Folders" category of the files lens.
    (LP: #713423) (LP: #1046774)
  - Use uuid+label as volume IDs
  - Don't crash on startup if device label or uuid are null (LP: #1052691)
* Bump dependency on libunity-dev to 5.96.0+bzr176

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
  public class Bookmarks : Object
23
23
  {
24
 
    
 
24
 
25
25
    private List<Bookmark> bookmarks;
26
26
    private string bookmarks_file;
27
27
    private FileMonitor monitor;
28
 
    
 
28
 
29
29
    public signal void changed ();
30
 
    
 
30
 
31
31
    public Bookmarks ()
32
32
    {
33
33
      bookmarks_file = @"$(Environment.get_home_dir())/.gtk-bookmarks";
34
34
      update();
35
 
      
 
35
 
36
36
      /* Update the bookmarks list whener the bookmarks file changes */
37
37
      try {
38
38
        monitor = File.new_for_path (bookmarks_file).monitor (FileMonitorFlags.NONE);
47
47
                 bookmarks_file, e.message);
48
48
      }
49
49
    }
50
 
    
 
50
 
51
51
    private void update ()
52
52
    {
53
53
      bookmarks = new List<Bookmark> ();
57
57
 
58
58
      File desktop_file = File.new_for_path (Environment.get_user_special_dir (UserDirectory.DESKTOP));
59
59
      desktop_uri = desktop_file.get_uri ();
60
 
      
 
60
 
61
61
      try {
62
62
        FileUtils.get_contents (bookmarks_file, out contents);
63
 
        
 
63
 
64
64
      } catch (FileError e) {
65
65
        warning ("Failed to read favorites: %s", e.message);
66
66
        return;
67
67
      }
68
 
      
 
68
 
69
69
      string[] favorites = contents.split ("\n");
70
70
      string mimetype = "inode/directory";
71
 
      
 
71
 
72
72
      foreach (var uri in favorites)
73
73
      {
74
74
        if (uri == "")
75
75
          continue;
76
 
          
 
76
 
 
77
        // Filter out useless bookmark that is created by Nautilus.
 
78
        // https://bugs.launchpad.net/unity-lens-files/+bug/1044309
 
79
        if (uri.has_prefix("x-nautilus-desktop:")) continue;
 
80
 
77
81
        if (uri == desktop_uri) has_desktop_in_favourites = true;
78
 
              
 
82
 
79
83
        string[] parts = uri.split (" ", 2);
80
84
        string display_name;
81
 
        
 
85
 
82
86
        if (parts.length == 1)
83
87
          {
84
88
            display_name = Uri.unescape_string (uri);
95
99
                     uri);
96
100
            display_name = uri;
97
101
          }
98
 
        
 
102
 
99
103
        var bookmark = new Bookmark (uri, mimetype, display_name);
100
104
        bookmarks.append (bookmark);
101
105
      }
102
 
      
 
106
 
103
107
      /* Add desktop statically */
104
108
      if (!has_desktop_in_favourites)
105
109
      {
161
165
    {
162
166
      return filter_bookmarks (bookmarks);
163
167
    }
164
 
    
 
168
 
165
169
    public List<Bookmark> prefix_search (string search)
166
170
    {
167
171
      var prefix = Utils.normalize_string (search);
168
172
      var matches = new List<Bookmark> ();
169
 
      
 
173
 
170
174
      foreach (var bookmark in bookmarks)
171
175
      {
172
176
        foreach (var term in bookmark.get_index_terms ())
179
183
            }
180
184
        }
181
185
      }
182
 
      
 
186
 
183
187
      return filter_bookmarks (matches);
184
188
    }
185
 
      
 
189
 
186
190
    public bool launch_if_bookmark (string uri) throws Error
187
191
    {
188
192
      if (!uri.has_prefix ("bookmark:"))
189
193
        return false;
190
 
      
 
194
 
191
195
      var launcher = AppInfo.get_default_for_type ("inode/directory", true);
192
 
      
 
196
 
193
197
      uri = uri.offset (9); // Remove "bookmark:" prefix from uri
194
 
      
 
198
 
195
199
      if (launcher == null)
196
200
        {
197
201
          warning ("No default handler for inode/directory. Unable to open bookmark '%s'", uri);
198
202
          throw new IOError.FAILED ("No default handler for inode/directory. Unable to open bookmark '%s'", uri);
199
203
        }
200
 
      
 
204
 
201
205
      var uris = new List<string> ();
202
206
      uris.append (uri);
203
 
      
 
207
 
204
208
      launcher.launch_uris (uris, null);
205
 
      
 
209
 
206
210
      return true;
207
211
    }
208
 
  
 
212
 
209
213
  }
210
 
  
 
214
 
211
215
  public class Bookmark : Object
212
216
  {
213
217
    public string uri { get; set construct; }
215
219
    public string mimetype { get; set construct; }
216
220
    public string display_name { get; set construct; }
217
221
    public string dnd_uri { get; set construct; }
218
 
    
 
222
 
219
223
    private List<string> index_terms;
220
 
  
 
224
 
221
225
    public Bookmark (string uri, string mimetype, string display_name)
222
226
    {
223
227
      Object (uri:"bookmark:"+uri, icon:Utils.get_icon_for_uri (uri, mimetype),
224
228
              mimetype:mimetype, display_name:display_name, dnd_uri:uri);
225
 
      
 
229
 
226
230
      index_terms = new List<string> ();
227
231
      index_terms.append (Utils.normalize_string (Path.get_basename (uri)));
228
232
      index_terms.append (Utils.normalize_string (display_name));
229
233
    }
230
 
    
 
234
 
231
235
    public unowned List<string> get_index_terms ()
232
236
    {
233
237
      return index_terms;
243
247
          return uri.substring (9);
244
248
        }
245
249
  }
 
250
 
 
251
 
 
252
  public class Devices : Object
 
253
  {
 
254
    private List<Device> devices;
 
255
    private VolumeMonitor volume_monitor;
 
256
 
 
257
    public signal void changed ();
 
258
 
 
259
    public Devices ()
 
260
    {
 
261
      volume_monitor = VolumeMonitor.get ();
 
262
 
 
263
      update();
 
264
 
 
265
      volume_monitor.volume_added.connect ((mon, volume) => {
 
266
          update ();
 
267
          changed ();
 
268
       });
 
269
 
 
270
      volume_monitor.volume_removed.connect ((mon, volume) => {
 
271
          update ();
 
272
          changed ();
 
273
       });
 
274
 
 
275
      volume_monitor.volume_changed.connect ((mon, volume) => {
 
276
          update ();
 
277
          changed ();
 
278
       });
 
279
    }
 
280
 
 
281
    private void update ()
 
282
    {
 
283
      devices = new List<Device> ();
 
284
 
 
285
      foreach ( Volume volume in volume_monitor.get_volumes ())
 
286
      {
 
287
        var label = volume.get_identifier (VolumeIdentifier.LABEL);
 
288
        var uuid = volume.get_identifier (VolumeIdentifier.UUID);
246
289
  
 
290
        if ((label == null || label.length == 0) && (uuid == null || uuid.length == 0))
 
291
          continue;
 
292
 
 
293
        var device = new Device (volume);
 
294
        devices.append (device);
 
295
      }
 
296
    }
 
297
 
 
298
    public List<Device> list ()
 
299
    {
 
300
      var result = new GLib.List<Device> ();
 
301
 
 
302
      foreach (var device in devices)
 
303
      {
 
304
        result.append (device);
 
305
      }
 
306
 
 
307
      return result;
 
308
    }
 
309
 
 
310
    public List<Device> search (string search)
 
311
    {
 
312
      var normalized_search = Utils.normalize_string (search);
 
313
      var matches = new List<Device> ();
 
314
 
 
315
      foreach (var device in devices)
 
316
      {
 
317
        foreach (var term in device.get_index_terms ())
 
318
        {
 
319
          if (term.contains (normalized_search))
 
320
          {
 
321
            matches.append (device);
 
322
            break;
 
323
          }
 
324
        }
 
325
      }
 
326
 
 
327
      return matches;
 
328
    }
 
329
 
 
330
    public bool launch_if_device (string uri) throws Error
 
331
    {
 
332
      if (!Device.is_device_uri (uri))
 
333
        return false;
 
334
 
 
335
      foreach (var device in devices)
 
336
      {
 
337
        if (device.uri == uri)
 
338
        {
 
339
          device.mount_and_open ();
 
340
          return true;
 
341
        }
 
342
      }
 
343
 
 
344
      return false;
 
345
    }
 
346
 
 
347
    public Device? get_device_from_uri (string uri)
 
348
    {
 
349
      if (!Device.is_device_uri (uri))
 
350
        return null;
 
351
 
 
352
      foreach (var device in devices)
 
353
      {
 
354
        if (device.uri == uri)
 
355
          return device;
 
356
      }
 
357
 
 
358
      return null;
 
359
    }
 
360
  }
 
361
 
 
362
 
 
363
  public class Device : Object
 
364
  {
 
365
    public Volume volume { get; set construct; }
 
366
    public string name { get; set construct; }
 
367
    public string uri { get; set construct; }
 
368
    public Icon icon { get; set construct; }
 
369
    public string icon_name { get; set construct; }
 
370
    public string display_name { get; set construct; }
 
371
    public string dnd_uri { get; set construct; }
 
372
 
 
373
    private List<string> index_terms_;
 
374
 
 
375
    public Device (Volume volume)
 
376
    {
 
377
      var name = volume.get_name ();
 
378
      var icon = volume.get_icon ();
 
379
      var icon_name = icon.to_string ();
 
380
      var label = volume.get_identifier (VolumeIdentifier.LABEL) ?? "";
 
381
      var uuid = volume.get_identifier (VolumeIdentifier.UUID) ?? "";
 
382
      var id = "device://" + uuid + "-" + label;
 
383
 
 
384
      Object (volume:volume, name:name, uri:id, icon:icon,icon_name:icon_name, display_name:name, dnd_uri:id);
 
385
 
 
386
      index_terms_ = new List<string> ();
 
387
      index_terms_.append (Utils.normalize_string (name));
 
388
    }
 
389
 
 
390
    public unowned List<string> get_index_terms ()
 
391
    {
 
392
      return index_terms_;
 
393
    }
 
394
 
 
395
    public static bool is_device_uri (string uri)
 
396
    {
 
397
      return uri.has_prefix ("device://");
 
398
    }
 
399
 
 
400
    public void mount_and_open () throws Error
 
401
    {
 
402
      if (is_mounted ())
 
403
      {
 
404
        open_in_file_manager ();
 
405
      }
 
406
      else
 
407
      {
 
408
        volume.mount.begin (MountMountFlags.NONE, null, null, (obj, res) =>  {
 
409
          try {
 
410
            if (volume.mount.end (res))
 
411
              open_in_file_manager ();
 
412
          } catch (Error e) {
 
413
            warning ("Failed to mount %s: %s", display_name, e.message);
 
414
          }
 
415
        });
 
416
      } 
 
417
    }
 
418
 
 
419
    private bool is_mounted ()
 
420
    {
 
421
      var mount = volume.get_mount ();
 
422
      return mount != null;
 
423
    }
 
424
 
 
425
    private void open_in_file_manager () throws Error
 
426
    {
 
427
      if (!is_mounted ())
 
428
        return;
 
429
 
 
430
      AppInfo.launch_default_for_uri (get_volume_uri (), null);
 
431
    }
 
432
 
 
433
    private string? get_volume_uri ()
 
434
    {
 
435
      var root = get_root_file ();
 
436
 
 
437
      if (root == null)
 
438
        return null;
 
439
 
 
440
      return root.get_uri ();
 
441
    }
 
442
 
 
443
    public File? get_root_file ()
 
444
    {
 
445
      if (is_mounted())
 
446
      {
 
447
        var mount = volume.get_mount ();
 
448
 
 
449
        if (mount == null)
 
450
          return null;
 
451
 
 
452
        return mount.get_root ();
 
453
      }
 
454
      else
 
455
      {
 
456
        return volume.get_activation_root ();
 
457
      }
 
458
    }
 
459
  }
247
460
 
248
461
} /* end: namespace */