[gnome-shell/hotplug: 5/14] autorun: integrate with the shell sniffer process



commit c0d1e28619c62363dbda4d000bba4599d0f9235b
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue Jun 21 18:32:32 2011 -0400

    autorun: integrate with the shell sniffer process
    
    https://bugzilla.gnome.org/show_bug.cgi?id=653520

 js/ui/autorunManager.js |  115 ++++++++++++++++++++++++++++++-----------------
 1 files changed, 73 insertions(+), 42 deletions(-)
---
diff --git a/js/ui/autorunManager.js b/js/ui/autorunManager.js
index 66f07bb..39ac9f3 100644
--- a/js/ui/autorunManager.js
+++ b/js/ui/autorunManager.js
@@ -1,6 +1,7 @@
 /* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
 
 const Lang = imports.lang;
+const DBus = imports.dbus;
 const Gio = imports.gi.Gio;
 const St = imports.gi.St;
 
@@ -51,6 +52,26 @@ function startAppForMount(app, mount) {
 
 /******************************************/
 
+const HotplugSnifferIface = {
+    name: 'org.gnome.Shell.HotplugSniffer',
+    methods: [{ name: 'SniffURI',
+                inSignature: 's',
+                outSignature: 'as' }]
+};
+
+const HotplugSniffer = function() {
+    this._init();
+};
+
+HotplugSniffer.prototype = {
+    _init: function() {
+        DBus.session.proxifyObject(this,
+                                   'org.gnome.Shell.HotplugSniffer',
+                                   '/org/gnome/Shell/HotplugSniffer');
+    },
+};
+DBus.proxifyPrototype(HotplugSniffer.prototype, HotplugSnifferIface);
+
 function ContentTypeDiscoverer(callback) {
     this._init(callback);
 }
@@ -77,12 +98,41 @@ ContentTypeDiscoverer.prototype = {
                 + ': ' + e.toString());
         }
 
+        if (contentTypes.length) {
+            this._emitCallback(mount, contentTypes);
+        } else {
+            let root = mount.get_root();
+
+            let hotplugSniffer = new HotplugSniffer();
+            hotplugSniffer.SniffURIRemote
+                (root.get_uri(), DBus.CALL_FLAG_START,
+                 Lang.bind(this, function(contentTypes) {
+                     this._emitCallback(mount, contentTypes);
+                 }));
+        }
+    },
+
+    _emitCallback: function(mount, contentTypes) {
+        if (!contentTypes || !contentTypes.length)
+            contentTypes.push('inode/directory');
+
         // we're not interested in win32 software content types here
         contentTypes = contentTypes.filter(function(type) {
             return (type != 'x-content/win32-software');
         });
 
-        this._callback(mount, contentTypes);
+        let apps = new Array();
+        contentTypes.forEach(function(type) {
+            let app = Gio.app_info_get_default_for_type(type, false);
+
+            if (app)
+                apps.push(app);
+        });
+
+        if (!apps.length)
+            apps.push(Gio.app_info_get_default_for_type('inode/directory', false));
+
+        this._callback(mount, apps, contentTypes);
     }
 }
 
@@ -111,8 +161,8 @@ AutorunManager.prototype = {
 
         mounts.forEach(Lang.bind(this, function (mount) {
             let discoverer = new ContentTypeDiscoverer(
-                Lang.bind (this, function (mount, contentTypes) {
-                    this._residentSource.addMount(mount, contentTypes);
+                Lang.bind (this, function (mount, apps) {
+                    this._residentSource.addMount(mount, apps);
                 }));
 
             discoverer.guessContentTypes(mount);
@@ -137,9 +187,9 @@ AutorunManager.prototype = {
             return;
 
         let discoverer = new ContentTypeDiscoverer
-            (Lang.bind (this, function (mount, contentTypes) {
-                this._transDispatcher.addMount(mount, contentTypes);
-                this._residentSource.addMount(mount, contentTypes);
+            (Lang.bind (this, function (mount, apps, contentTypes) {
+                this._transDispatcher.addMount(mount, apps, contentTypes);
+                this._residentSource.addMount(mount, apps);
             }));
 
         discoverer.guessContentTypes(mount);
@@ -193,7 +243,7 @@ AutorunResidentSource.prototype = {
         this._setSummaryIcon(this.createNotificationIcon(HOTPLUG_ICON_SIZE));
     },
 
-    addMount: function(mount, contentTypes) {
+    addMount: function(mount, apps) {
         if (ignoreAutorunForMount(mount))
             return;
 
@@ -204,7 +254,7 @@ AutorunResidentSource.prototype = {
         if (filtered.length != 0)
             return;
 
-        let element = { mount: mount, contentTypes: contentTypes };
+        let element = { mount: mount, apps: apps };
         this._mounts.push(element);
         this._redisplay();
     },
@@ -274,13 +324,13 @@ AutorunResidentNotification.prototype = {
         for (let idx = 0; idx < mounts.length; idx++) {
             let element = mounts[idx];
 
-            let actor = this._itemForMount(element.mount, element.contentTypes);
+            let actor = this._itemForMount(element.mount, element.apps);
             this._layout.add(actor, { x_fill: true,
                                       expand: true });
         }
     },
 
-    _itemForMount: function(mount, contentTypes) {
+    _itemForMount: function(mount, apps) {
         let item = new St.BoxLayout();
 
         let mountLayout = new St.BoxLayout({ style_class: 'hotplug-resident-mount',
@@ -311,21 +361,15 @@ AutorunResidentNotification.prototype = {
 
         item.add(ejectButton, { x_align: St.Align.END });
 
-        // TODO: need to do something better here...
-        if (!contentTypes.length)
-            contentTypes.push('inode/directory');
-
         // now connect signals
         mountLayout.connect('button-press-event', Lang.bind(this, function(actor, event) {
             // ignore clicks not coming from the left mouse button
             if (event.get_button() != 1)
                 return false;
 
-            let app = Gio.app_info_get_default_for_type(contentTypes[0], false);
+            let app = apps[0];
+            startAppForMount(app, mount);
 
-            if (app)
-                startAppForMount(app, mount);
-            
             return true;
         }));
 
@@ -384,17 +428,17 @@ AutorunTransientDispatcher.prototype = {
         return null;
     },
 
-    _addSource: function(mount, contentTypes) {
+    _addSource: function(mount, apps) {
         // if we already have a source showing for this 
         // mount, return
         if (this._getSourceForMount(mount))
             return;
      
         // add a new source
-        this._sources.push(new AutorunTransientSource(mount, contentTypes));
+        this._sources.push(new AutorunTransientSource(mount, apps));
     },
 
-    addMount: function(mount, contentTypes) {
+    addMount: function(mount, apps, contentTypes) {
         // if autorun is disabled globally, return
         if (this._settings.get_boolean(SETTING_DISABLE_AUTORUN))
             return;
@@ -429,7 +473,7 @@ AutorunTransientDispatcher.prototype = {
         // we fallback here also in case the settings did not specify 'ask',
         // but we failed launching the default app or the default file manager
         if (!success)
-            this._addSource(mount, contentTypes);
+            this._addSource(mount, apps);
     },
 
     removeMount: function(mount) {
@@ -444,18 +488,18 @@ AutorunTransientDispatcher.prototype = {
     }
 }
 
-function AutorunTransientSource(mount, contentTypes) {
-    this._init(mount, contentTypes);
+function AutorunTransientSource(mount, apps) {
+    this._init(mount, apps);
 }
 
 AutorunTransientSource.prototype = {
     __proto__: MessageTray.Source.prototype,
 
-    _init: function(mount, contentTypes) {
+    _init: function(mount, apps) {
         MessageTray.Source.prototype._init.call(this, mount.get_name());
 
         this._mount = mount;
-        this._contentTypes = contentTypes;
+        this._apps = apps;
 
         this._buildNotification();
     },
@@ -468,22 +512,14 @@ AutorunTransientSource.prototype = {
                                        vertical: true });
         this._notification.addActor(this._box);
 
-        this._contentTypes.forEach(Lang.bind(this, function (type) {
-            let actor = this._buttonForContentType(type);
+        this._apps.forEach(Lang.bind(this, function (app) {
+            let actor = this._buttonForApp(app);
 
             if (actor)
                 this._box.add(actor, { x_fill: true,
                                        x_align: St.Align.START });
         }));
 
-        // TODO: ideally we never want to show the file manager entry here,
-        // but we want to detect which kind of files are present on the device,
-        // and use those to present a more meaningful choice.
-        if (this._contentTypes.length == 0)
-            this._box.add (this._buttonForContentType('inode/directory'),
-                           { x_fill: true,
-                             x_align: St.Align.START });
-
         this._box.add(this._buttonForEject(), { x_fill: true,
                                                 x_align: St.Align.START });
 
@@ -492,12 +528,7 @@ AutorunTransientSource.prototype = {
         this.notify(this._notification);
     },
 
-    _buttonForContentType: function(type) {
-        let app = Gio.app_info_get_default_for_type(type, false);
-
-        if (!app)
-            return null;
-
+    _buttonForApp: function(app) {
         let box = new St.BoxLayout({ style_class: 'hotplug-notification-item',
                                      track_hover: true,
                                      reactive: true });



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