From 309d161f6d464fcea2de200bb3cb5a7cb784b6d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Mon, 24 Jul 2017 19:58:27 +0200 Subject: New upstream version 0.7.1 --- src/actionGroups/bookmarkGroup.vala | 93 ++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 48 deletions(-) (limited to 'src/actionGroups/bookmarkGroup.vala') diff --git a/src/actionGroups/bookmarkGroup.vala b/src/actionGroups/bookmarkGroup.vala index 3a36be6..dc859fa 100644 --- a/src/actionGroups/bookmarkGroup.vala +++ b/src/actionGroups/bookmarkGroup.vala @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////// -// Copyright (c) 2011-2016 by Simon Schneegans +// Copyright (c) 2011-2017 by Simon Schneegans // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -41,12 +41,10 @@ public class BookmarkGroup : ActionGroup { } ///////////////////////////////////////////////////////////////////// - /// Two members needed to avoid useless, frequent changes of the - /// stored Actions. + /// Used to track changes in the bookmarks file. ///////////////////////////////////////////////////////////////////// - private bool changing = false; - private bool changed_again = false; + private GLib.FileMonitor monitor = null; ///////////////////////////////////////////////////////////////////// /// C'tor, initializes all members. @@ -65,20 +63,43 @@ public class BookmarkGroup : ActionGroup { construct { this.load(); - // add monitor - var bookmark_file = GLib.File.new_for_path( - GLib.Environment.get_home_dir()).get_child(".gtk-bookmarks"); + var bookmarks_file = get_bookmarks_file(); - if (bookmark_file.query_exists()) { + // add monitor + if (bookmarks_file != null) { try { - var monitor = bookmark_file.monitor(GLib.FileMonitorFlags.NONE); - monitor.changed.connect(this.reload); + this.monitor = bookmarks_file.monitor(GLib.FileMonitorFlags.NONE); + this.monitor.set_rate_limit(500); + this.monitor.changed.connect((file, other, type) => { + if(type == GLib.FileMonitorEvent.CHANGES_DONE_HINT) { + this.delete_all(); + this.load(); + } + }); } catch (GLib.Error e) { warning(e.message); } } } + ///////////////////////////////////////////////////////////////////// + /// Returns either ~/.gtk-bookmarks or ~/.config/gtk-3.0/bookmarks + ///////////////////////////////////////////////////////////////////// + + private GLib.File? get_bookmarks_file() { + var bookmarks_file = GLib.File.new_for_path( + GLib.Environment.get_home_dir()).get_child(".gtk-bookmarks"); + + if (bookmarks_file.query_exists()) return bookmarks_file; + + bookmarks_file = GLib.File.new_for_path( + GLib.Environment.get_home_dir()).get_child(".config/gtk-3.0/bookmarks"); + + if (bookmarks_file.query_exists()) return bookmarks_file; + + return null; + } + ///////////////////////////////////////////////////////////////////// /// Adds Actions for each gtk-bookmark of the user and for his home /// folder, desktop and trash. @@ -88,23 +109,27 @@ public class BookmarkGroup : ActionGroup { // add home folder this.add_action(ActionRegistry.new_for_uri("file://" + GLib.Environment.get_home_dir())); - // add .gtk-bookmarks - var bookmark_file = GLib.File.new_for_path( - GLib.Environment.get_home_dir()).get_child(".gtk-bookmarks"); + // add bookmarks + var bookmarks_file = get_bookmarks_file(); - if (!bookmark_file.query_exists()) { - warning("Failed to find file \".gtk-bookmarks\"!"); + if (bookmarks_file == null) { + warning("Failed to find bookmarks file!"); return; } try { - var dis = new DataInputStream(bookmark_file.read()); + var dis = new DataInputStream(bookmarks_file.read()); string line; while ((line = dis.read_line(null)) != null) { - var parts = line.split(" "); - string uri = parts[0]; - string name = parts[1]; + string uri = line; + string name = null; + + int first_space = line.index_of(" "); + if (first_space > 0) { + uri = line.slice(0, first_space); + name = line.slice(first_space+1, line.length); + } this.add_action(ActionRegistry.new_for_uri(uri, name)); } @@ -118,34 +143,6 @@ public class BookmarkGroup : ActionGroup { // add desktop this.add_action(ActionRegistry.new_for_uri("file://" + GLib.Environment.get_user_special_dir(GLib.UserDirectory.DESKTOP))); } - - ///////////////////////////////////////////////////////////////////// - /// Reloads all Bookmarks. Is called when the user's gtk-bookmarks - /// file changes. - ///////////////////////////////////////////////////////////////////// - - private void reload() { - // avoid too frequent changes... - if (!this.changing) { - this.changing = true; - Timeout.add(200, () => { - if (this.changed_again) { - this.changed_again = false; - return true; - } - - // reload - message("Bookmarks changed..."); - this.delete_all(); - this.load(); - - this.changing = false; - return false; - }); - } else { - this.changed_again = true; - } - } } } -- cgit v1.2.3