[baobab/wip/vala: 7/53] scanner: split interface from implementation
- From: Paolo Borelli <pborelli src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [baobab/wip/vala: 7/53] scanner: split interface from implementation
- Date: Thu, 5 Apr 2012 22:02:08 +0000 (UTC)
commit 6ae95b1fec76128b112d5ec6875fc0e4155aa569
Author: Ryan Lortie <desrt desrt ca>
Date: Thu Jan 5 16:48:45 2012 -0500
scanner: split interface from implementation
src/Makefile.am | 2 +
src/baobab-scanner.vala | 137 +++-------------------------------------
src/baobab-sync-scanner.vala | 144 ++++++++++++++++++++++++++++++++++++++++++
src/baobab-window.vala | 4 +-
4 files changed, 157 insertions(+), 130 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 740b776..5b0a300 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -20,6 +20,7 @@ baobab_SOURCES = \
baobab-chart.c \
baobab-treemap.c \
baobab-ringschart.c \
+ baobab-sync-scanner.vala \
baobab-scanner.vala \
baobab-cellrenderers.vala \
baobab-application.vala \
@@ -41,6 +42,7 @@ baobab_LDADD = \
MAINTAINERCLEANFILES = \
baobab-application.c \
+ baobab-sync-scanner.c \
baobab-scanner.c \
baobab-cellrenderers.c \
baobab-window.c \
diff --git a/src/baobab-scanner.vala b/src/baobab-scanner.vala
index 66453ed..d8050e4 100644
--- a/src/baobab-scanner.vala
+++ b/src/baobab-scanner.vala
@@ -18,7 +18,7 @@
*/
namespace Baobab {
- class Scanner : Gtk.TreeStore {
+ abstract class Scanner : Gtk.TreeStore {
public enum Columns {
DISPLAY_NAME,
PARSE_NAME,
@@ -37,7 +37,7 @@ namespace Baobab {
DONE
}
- struct HardLink {
+ protected struct HardLink {
uint64 inode;
uint32 device;
@@ -47,17 +47,10 @@ namespace Baobab {
}
}
- struct Results {
- uint64 size;
- uint64 alloc_size;
- uint64 elements;
- int max_depth;
- }
-
- Cancellable? cancellable;
- HardLink[] hardlinks;
+ protected Cancellable? cancellable;
+ protected HardLink[] hardlinks;
- static const string ATTRIBUTES =
+ protected static const string ATTRIBUTES =
FILE_ATTRIBUTE_STANDARD_NAME + "," +
FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + "," +
FILE_ATTRIBUTE_STANDARD_TYPE + "," +
@@ -68,124 +61,11 @@ namespace Baobab {
FILE_ATTRIBUTE_UNIX_DEVICE + "," +
FILE_ATTRIBUTE_ACCESS_CAN_READ;
- Results add_directory (File directory, FileInfo info, Gtk.TreeIter? parent_iter = null) {
- var results = Results ();
- Gtk.TreeIter iter;
-
- if (Application.is_excluded_location (directory)) {
- return results;
- }
-
- var display_name = info.get_display_name ();
- var parse_name = directory.get_parse_name ();
-
- append (out iter, parent_iter);
- set (iter,
- Columns.DISPLAY_NAME, display_name,
- Columns.PARSE_NAME, parse_name);
-
- if (info.has_attribute (FILE_ATTRIBUTE_STANDARD_SIZE)) {
- results.size = info.get_size ();
- }
-
- if (info.has_attribute (FILE_ATTRIBUTE_UNIX_BLOCKS)) {
- results.alloc_size = 512 * info.get_attribute_uint64 (FILE_ATTRIBUTE_UNIX_BLOCKS);
- }
-
- results.elements = 1;
-
- try {
- var children = directory.enumerate_children (ATTRIBUTES, FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
- FileInfo? child_info;
- while ((child_info = children.next_file (cancellable)) != null) {
- if (cancellable.is_cancelled ()) {
- break;
- }
-
- switch (child_info.get_file_type ()) {
- case FileType.DIRECTORY:
- var child = directory.get_child (child_info.get_name ());
- var child_results = add_directory (child, child_info, iter);
-
- results.size += child_results.size;
- results.alloc_size += child_results.size;
- results.elements += child_results.elements;
- results.max_depth = int.max (results.max_depth, child_results.max_depth + 1);
- break;
-
- case FileType.REGULAR:
- if (child_info.has_attribute (FILE_ATTRIBUTE_UNIX_NLINK)) {
- if (child_info.get_attribute_uint32 (FILE_ATTRIBUTE_UNIX_NLINK) > 1) {
- var hl = HardLink (child_info);
-
- /* check if we've already encountered this file */
- if (hl in hardlinks) {
- continue;
- }
-
- hardlinks += hl;
- }
- }
-
- if (child_info.has_attribute (FILE_ATTRIBUTE_UNIX_BLOCKS)) {
- results.alloc_size += 512 * child_info.get_attribute_uint64 (FILE_ATTRIBUTE_UNIX_BLOCKS);
- }
- results.size += child_info.get_size ();
- results.elements++;
- break;
-
- default:
- /* ignore other types (symlinks, sockets, devices, etc) */
- break;
- }
- }
- } catch (IOError.PERMISSION_DENIED e) {
- } catch (Error e) {
- warning ("couldn't iterate %s: %s", parse_name, e.message);
- }
-
- add_percent (results.size, iter);
-
- if (!cancellable.is_cancelled ()) {
- set (iter,
- Columns.SIZE, results.size,
- Columns.ALLOC_SIZE, results.alloc_size,
- Columns.ELEMENTS, results.elements,
- Columns.STATE, State.NEED_PERCENT);
- } else {
- set (iter,
- Columns.STATE, State.CANCELLED);
- }
-
- return results;
- }
-
- void add_percent (uint64 parent_size, Gtk.TreeIter? parent = null) {
- Gtk.TreeIter iter;
-
- if (iter_children (out iter, parent)) {
- do {
- uint64 size;
- get (iter, Columns.SIZE, out size);
- set (iter,
- Columns.PERCENT, 100 * ((double) size) / ((double) parent_size),
- Columns.STATE, State.DONE);
- } while (iter_next (ref iter));
- }
- }
-
- void scan (File directory) {
- try {
- var info = directory.query_info (ATTRIBUTES, 0, cancellable);
- var results = add_directory (directory, info);
- add_percent (results.size);
- max_depth = results.max_depth;
- } catch { }
- }
+ public abstract void scan (File directory);
- public int max_depth { get; private set; }
+ public int max_depth { get; protected set; }
- public Scanner (File directory) {
+ public Scanner () {
set_column_types (new Type[] {
typeof (string), /* DIR_NAME */
typeof (string), /* PARSE_NAME */
@@ -194,7 +74,6 @@ namespace Baobab {
typeof (uint64), /* ALLOC_SIZE */
typeof (int), /* ELEMENTS */
typeof (State)}); /* STATE */
- scan (directory);
}
}
}
diff --git a/src/baobab-sync-scanner.vala b/src/baobab-sync-scanner.vala
new file mode 100644
index 0000000..dd8b772
--- /dev/null
+++ b/src/baobab-sync-scanner.vala
@@ -0,0 +1,144 @@
+/* Baobab - disk usage analyzer
+ *
+ * Copyright (C) 2012 Ryan Lortie <desrt desrt ca>
+ *
+ * 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 {
+ class SyncScanner : Scanner {
+ struct Results {
+ uint64 size;
+ uint64 alloc_size;
+ uint64 elements;
+ int max_depth;
+ }
+
+ Results add_directory (File directory, FileInfo info, Gtk.TreeIter? parent_iter = null) {
+ var results = Results ();
+ Gtk.TreeIter iter;
+
+ if (Application.is_excluded_location (directory)) {
+ return results;
+ }
+
+ var display_name = info.get_display_name ();
+ var parse_name = directory.get_parse_name ();
+
+ append (out iter, parent_iter);
+ set (iter,
+ Columns.DISPLAY_NAME, display_name,
+ Columns.PARSE_NAME, parse_name);
+
+ if (info.has_attribute (FILE_ATTRIBUTE_STANDARD_SIZE)) {
+ results.size = info.get_size ();
+ }
+
+ if (info.has_attribute (FILE_ATTRIBUTE_UNIX_BLOCKS)) {
+ results.alloc_size = 512 * info.get_attribute_uint64 (FILE_ATTRIBUTE_UNIX_BLOCKS);
+ }
+
+ results.elements = 1;
+
+ try {
+ var children = directory.enumerate_children (ATTRIBUTES, FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
+ FileInfo? child_info;
+ while ((child_info = children.next_file (cancellable)) != null) {
+ if (cancellable.is_cancelled ()) {
+ break;
+ }
+
+ switch (child_info.get_file_type ()) {
+ case FileType.DIRECTORY:
+ var child = directory.get_child (child_info.get_name ());
+ var child_results = add_directory (child, child_info, iter);
+
+ results.size += child_results.size;
+ results.alloc_size += child_results.size;
+ results.elements += child_results.elements;
+ results.max_depth = int.max (results.max_depth, child_results.max_depth + 1);
+ break;
+
+ case FileType.REGULAR:
+ if (child_info.has_attribute (FILE_ATTRIBUTE_UNIX_NLINK)) {
+ if (child_info.get_attribute_uint32 (FILE_ATTRIBUTE_UNIX_NLINK) > 1) {
+ var hl = HardLink (child_info);
+
+ /* check if we've already encountered this file */
+ if (hl in hardlinks) {
+ continue;
+ }
+
+ hardlinks += hl;
+ }
+ }
+
+ if (child_info.has_attribute (FILE_ATTRIBUTE_UNIX_BLOCKS)) {
+ results.alloc_size += 512 * child_info.get_attribute_uint64 (FILE_ATTRIBUTE_UNIX_BLOCKS);
+ }
+ results.size += child_info.get_size ();
+ results.elements++;
+ break;
+
+ default:
+ /* ignore other types (symlinks, sockets, devices, etc) */
+ break;
+ }
+ }
+ } catch (IOError.PERMISSION_DENIED e) {
+ } catch (Error e) {
+ warning ("couldn't iterate %s: %s", parse_name, e.message);
+ }
+
+ add_percent (results.size, iter);
+
+ if (!cancellable.is_cancelled ()) {
+ set (iter,
+ Columns.SIZE, results.size,
+ Columns.ALLOC_SIZE, results.alloc_size,
+ Columns.ELEMENTS, results.elements,
+ Columns.STATE, State.NEED_PERCENT);
+ } else {
+ set (iter,
+ Columns.STATE, State.CANCELLED);
+ }
+
+ return results;
+ }
+
+ void add_percent (uint64 parent_size, Gtk.TreeIter? parent = null) {
+ Gtk.TreeIter iter;
+
+ if (iter_children (out iter, parent)) {
+ do {
+ uint64 size;
+ get (iter, Columns.SIZE, out size);
+ set (iter,
+ Columns.PERCENT, 100 * ((double) size) / ((double) parent_size),
+ Columns.STATE, State.DONE);
+ } while (iter_next (ref iter));
+ }
+ }
+
+ protected override void scan (File directory) {
+ try {
+ var info = directory.query_info (ATTRIBUTES, 0, cancellable);
+ var results = add_directory (directory, info);
+ add_percent (results.size);
+ max_depth = results.max_depth;
+ } catch { }
+ }
+ }
+}
diff --git a/src/baobab-window.vala b/src/baobab-window.vala
index 7bc8444..a2ba3d5 100644
--- a/src/baobab-window.vala
+++ b/src/baobab-window.vala
@@ -85,7 +85,9 @@ namespace Baobab {
return;
}
- model = new Scanner (directory);
+ var scanner = new SyncScanner ();
+ scanner.scan (directory);
+ model = scanner;
var rings_chart = builder.get_object ("rings-chart") as Chart;
var treemap = builder.get_object ("treemap") as Chart;
model.bind_property ("max-depth", rings_chart, "max-depth", BindingFlags.SYNC_CREATE);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]