58
58
File desktop_file = File.new_for_path (Environment.get_user_special_dir (UserDirectory.DESKTOP));
59
59
desktop_uri = desktop_file.get_uri ();
62
62
FileUtils.get_contents (bookmarks_file, out contents);
64
64
} catch (FileError e) {
65
65
warning ("Failed to read favorites: %s", e.message);
69
69
string[] favorites = contents.split ("\n");
70
70
string mimetype = "inode/directory";
72
72
foreach (var uri in favorites)
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;
77
81
if (uri == desktop_uri) has_desktop_in_favourites = true;
79
83
string[] parts = uri.split (" ", 2);
80
84
string display_name;
82
86
if (parts.length == 1)
84
88
display_name = Uri.unescape_string (uri);
183
187
return filter_bookmarks (matches);
186
190
public bool launch_if_bookmark (string uri) throws Error
188
192
if (!uri.has_prefix ("bookmark:"))
191
195
var launcher = AppInfo.get_default_for_type ("inode/directory", true);
193
197
uri = uri.offset (9); // Remove "bookmark:" prefix from uri
195
199
if (launcher == null)
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);
201
205
var uris = new List<string> ();
202
206
uris.append (uri);
204
208
launcher.launch_uris (uris, null);
211
215
public class Bookmark : Object
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; }
219
223
private List<string> index_terms;
221
225
public Bookmark (string uri, string mimetype, string display_name)
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);
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));
231
235
public unowned List<string> get_index_terms ()
233
237
return index_terms;
243
247
return uri.substring (9);
252
public class Devices : Object
254
private List<Device> devices;
255
private VolumeMonitor volume_monitor;
257
public signal void changed ();
261
volume_monitor = VolumeMonitor.get ();
265
volume_monitor.volume_added.connect ((mon, volume) => {
270
volume_monitor.volume_removed.connect ((mon, volume) => {
275
volume_monitor.volume_changed.connect ((mon, volume) => {
281
private void update ()
283
devices = new List<Device> ();
285
foreach ( Volume volume in volume_monitor.get_volumes ())
287
var label = volume.get_identifier (VolumeIdentifier.LABEL);
288
var uuid = volume.get_identifier (VolumeIdentifier.UUID);
290
if ((label == null || label.length == 0) && (uuid == null || uuid.length == 0))
293
var device = new Device (volume);
294
devices.append (device);
298
public List<Device> list ()
300
var result = new GLib.List<Device> ();
302
foreach (var device in devices)
304
result.append (device);
310
public List<Device> search (string search)
312
var normalized_search = Utils.normalize_string (search);
313
var matches = new List<Device> ();
315
foreach (var device in devices)
317
foreach (var term in device.get_index_terms ())
319
if (term.contains (normalized_search))
321
matches.append (device);
330
public bool launch_if_device (string uri) throws Error
332
if (!Device.is_device_uri (uri))
335
foreach (var device in devices)
337
if (device.uri == uri)
339
device.mount_and_open ();
347
public Device? get_device_from_uri (string uri)
349
if (!Device.is_device_uri (uri))
352
foreach (var device in devices)
354
if (device.uri == uri)
363
public class Device : Object
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; }
373
private List<string> index_terms_;
375
public Device (Volume volume)
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;
384
Object (volume:volume, name:name, uri:id, icon:icon,icon_name:icon_name, display_name:name, dnd_uri:id);
386
index_terms_ = new List<string> ();
387
index_terms_.append (Utils.normalize_string (name));
390
public unowned List<string> get_index_terms ()
395
public static bool is_device_uri (string uri)
397
return uri.has_prefix ("device://");
400
public void mount_and_open () throws Error
404
open_in_file_manager ();
408
volume.mount.begin (MountMountFlags.NONE, null, null, (obj, res) => {
410
if (volume.mount.end (res))
411
open_in_file_manager ();
413
warning ("Failed to mount %s: %s", display_name, e.message);
419
private bool is_mounted ()
421
var mount = volume.get_mount ();
422
return mount != null;
425
private void open_in_file_manager () throws Error
430
AppInfo.launch_default_for_uri (get_volume_uri (), null);
433
private string? get_volume_uri ()
435
var root = get_root_file ();
440
return root.get_uri ();
443
public File? get_root_file ()
447
var mount = volume.get_mount ();
452
return mount.get_root ();
456
return volume.get_activation_root ();
248
461
} /* end: namespace */