[baobab/wip/vala: 50/53] Add facilities for volume handling



commit 1b327f8d833361b997bad8c9a6dec236f02ecd40
Author: Stefano Facchini <stefano facchini gmail com>
Date:   Thu Apr 5 20:58:44 2012 +0200

    Add facilities for volume handling
    
    Introduce a class Location abstracting the gio concepts of GVolume
    and GMount. Introduce also a LocationMonitor which allows a
    conveniently handling of known locations.

 src/Makefile.am                  |   10 ++-
 src/baobab-location-monitor.vala |  120 ++++++++++++++++++++++++++++++++
 src/baobab-location.vala         |  141 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 269 insertions(+), 2 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 792df96..259d513 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -15,7 +15,11 @@ bin_PROGRAMS = baobab
 
 BUILT_SOURCES = baobab-resources.c
 
-baobab_VALAFLAGS = --pkg gtk+-3.0
+baobab_VALAFLAGS = \
+	--pkg gtk+-3.0		\
+	--pkg gio-2.0		\
+	--pkg gio-unix-2.0
+
 baobab_SOURCES = \
 	fixes.vapi			\
 	baobab.vapi			\
@@ -30,6 +34,9 @@ baobab_SOURCES = \
 	baobab-application.vala		\
 	baobab-window.vala		\
 	baobab-connect-server.vala	\
+	baobab-location.vala		\
+	baobab-location-monitor.vala	\
+	baobab-location-widget.vala	\
 	main.vala			\
 	$(BUILT_SOURCES)
 
@@ -63,7 +70,6 @@ MAINTAINERCLEANFILES = \
 EXTRA_DIST = \
 	baobab.gresource.xml \
 	baobab-main-window.ui \
-	baobab-preferences-dialog.ui \
 	baobab-menu.ui
 
 -include $(top_srcdir)/git.mk
diff --git a/src/baobab-location-monitor.vala b/src/baobab-location-monitor.vala
new file mode 100644
index 0000000..23f41d5
--- /dev/null
+++ b/src/baobab-location-monitor.vala
@@ -0,0 +1,120 @@
+/* -*- indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* Baobab - disk usage analyzer
+ *
+ * Copyright (C) 2012  Stefano Facchini <stefano facchini gmail com>
+ *
+ * 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 the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+namespace Baobab {
+    public class LocationMonitor {
+        private static LocationMonitor? instance = null;
+
+        private VolumeMonitor monitor;
+
+        private List<Location> locations = null;
+
+        public signal void changed ();
+
+        public static LocationMonitor get () {
+            if (instance == null) {
+                instance = new LocationMonitor ();
+            }
+
+            return instance;
+        }
+
+        private LocationMonitor () {
+            monitor = VolumeMonitor.get ();
+            monitor.mount_changed.connect (mount_changed);
+            monitor.mount_removed.connect (mount_removed);
+            monitor.mount_added.connect (mount_added);
+            monitor.volume_changed.connect (volume_changed);
+            monitor.volume_removed.connect (volume_removed);
+            monitor.volume_added.connect (volume_added);
+
+            initial_fill ();
+        }
+
+        public unowned List<Location> get_locations () {
+            return locations;
+        }
+
+        void volume_changed (Volume volume) {
+            changed ();
+        }
+
+        void volume_removed (Volume volume) {
+            foreach (var location in locations) {
+                if (location.volume == volume) {
+                    locations.remove (location);
+                    break;
+                }
+            }
+
+            changed ();
+        }
+
+        void volume_added (Volume volume) {
+            locations.append (new Location.from_volume (volume));
+
+            changed ();
+        }
+
+        void mount_changed (Mount mount) {
+        }
+
+        void mount_removed (Mount mount) {
+            foreach (var location in locations) {
+                if (location.mount == mount) {
+                    locations.remove (location);
+                    break;
+                }
+            }
+
+            changed ();
+        }
+
+        void mount_added (Mount mount) {
+            if (mount.get_volume () == null) {
+                locations.append (new Location.from_mount (mount));
+            }
+
+            changed ();
+        }
+
+        void initial_fill () {
+            locations.append (new Location.for_main_volume ());
+
+            foreach (var volume in monitor.get_volumes ()) {
+                locations.append (new Location.from_volume (volume));
+            }
+
+            foreach (var mount in monitor.get_mounts ()) {
+                if (mount.get_volume () == null) {
+                    locations.append (new Location.from_mount (mount));
+                } else {
+                    // Already added as volume
+                }
+            }
+
+            if (Location.get_home_location () == null) {
+                locations.append(new Location.for_home_folder ());
+            }
+
+            changed ();
+        }
+    }
+}
diff --git a/src/baobab-location.vala b/src/baobab-location.vala
new file mode 100644
index 0000000..8f384c3
--- /dev/null
+++ b/src/baobab-location.vala
@@ -0,0 +1,141 @@
+/* -*- indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* Baobab - disk usage analyzer
+ *
+ * Copyright (C) 2012  Stefano Facchini <stefano facchini gmail com>
+ *
+ * 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 the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+namespace Baobab {
+    public class Location {
+        public string name { get; private set; }
+        public string? mount_point { get; private set; }
+
+        public uint64? size { get; private set; }
+        public uint64? used { get; private set; }
+        public Icon? icon { get; private set; }
+
+        public Volume? volume { get; private set; }
+        public Mount? mount { get; private set; }
+
+        protected static const string FS_ATTRIBUTES =
+            FileAttribute.FILESYSTEM_SIZE + "," +
+            FileAttribute.FILESYSTEM_USED;
+
+        private static Location? home_location = null;
+
+        public static Location get_home_location () {
+            return home_location;
+        }
+
+        public bool is_home_location {
+            get {
+                return (this == home_location);
+            }
+        }
+
+        public Location.from_volume (Volume volume_) {
+            volume = volume_;
+            volume.changed.connect((vol) => {
+                update_volume_info ();
+            });
+            update_volume_info ();
+        }
+
+        public Location.from_mount (Mount mount_) {
+            mount = mount_;
+            fill_from_mount ();
+        }
+
+        public Location.for_main_volume () {
+            name = _("Main volume");
+            mount_point = "/";
+            icon = new ThemedIcon ("drive-harddisk-system");
+
+            uint64? size_ = null;
+            uint64? used_ = null;
+            get_fs_size (File.new_for_path ("/"), out size_, out used_);
+
+            size = size_;
+            used = used_;
+        }
+
+        public Location.for_home_folder () {
+            mount_point = Environment.get_home_dir ();
+            make_this_home_location ();
+        }
+
+        void make_this_home_location () {
+            name = _("Home folder");
+            icon = new ThemedIcon ("user-home");
+
+            home_location = this;
+        }
+
+        void update_volume_info () {
+            mount = volume.get_mount ();
+
+            if (mount != null) {
+                fill_from_mount ();
+            } else {
+                name = volume.get_name ();
+                icon = volume.get_icon ();
+                mount_point = null;
+                size = null;
+                used = null;
+            }
+        }
+
+        void fill_from_mount () {
+            name = mount.get_name ();
+            icon = mount.get_icon ();
+
+            var file = mount.get_root ();
+            mount_point = file.get_path ();
+
+            if (mount_point == Environment.get_home_dir ()) {
+                make_this_home_location ();
+            }
+
+            uint64? size_ = null;
+            uint64? used_ = null;
+            get_fs_size (file, out size_, out used_);
+
+            size = size_;
+            used = used_;
+        }
+
+        static void get_fs_size (File file, out uint64? size, out uint64? used) {
+            try {
+                var info = file.query_filesystem_info (FS_ATTRIBUTES, null);
+                if (info.has_attribute (FileAttribute.FILESYSTEM_SIZE))
+                    size = info.get_attribute_uint64 (FileAttribute.FILESYSTEM_SIZE);
+                if (info.has_attribute (FileAttribute.FILESYSTEM_USED))
+                    used = info.get_attribute_uint64 (FileAttribute.FILESYSTEM_USED);
+            } catch (Error e) {
+            }
+        }
+
+        public async void mount_volume () throws Error {
+            if (mount != null || volume == null)
+                return;
+
+            var mount_op = new Gtk.MountOperation (null);
+            yield volume.mount (MountMountFlags.NONE, mount_op, null);
+
+            update_volume_info ();
+        }
+    }
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]