gnomedbusgnome-shellgnome-shell-extensionsgjs

How to extract values from a dbus signal and write it to a file?


I was looking for signals using:

dbus-monitor --session > gnome-extension-log.txt

I have a signal like:

signal time=1684395360.751890 sender=:1.93 -> destination=(null destination) serial=971 path=/org/gtk/gio/DesktopAppInfo; interface=org.gtk.gio.DesktopAppInfo; member=Launched
   array of bytes "/home/ismail/.local/share/applications/com.github.johnfactotum.Foliate.desktop" + \0
   string ""
   int64 85979
   array [
      string "file:///media/ismail/SSDWorking/book-collection/_Books/Eleven%20Rings%20The%20Soul%20of%20Success%20(Phil%20Jackson,%20Hugh%20Delehanty)%20.epub"
   ]
   array [
      dict entry(
         string "origin-desktop-file"
         variant             array of bytes "/home/ismail/.local/share/applications/nemo.desktop" + \0
      )
      dict entry(
         string "origin-prgname"
         variant             array of bytes "nemo" + \0
      )
      dict entry(
         string "origin-pid"
         variant             int64 4877
      )
   ]

When a signal is created I want to extract following values and save it in a file.

  1. /home/ismail/.local/share/applications/com.github.johnfactotum.Foliate.desktop

  2. 85979

  3. file:///media/ismail/SSDWorking/book-collection/_Books/Eleven%20Rings%20The%20Soul%20of%20Success%20(Phil%20Jackson,%20Hugh%20Delehanty)%20.epub

What I have done so far is:

const { Gio, GLib } = imports.gi;

const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const connection = Gio.DBus.session;
var handlerId;

class Extension {
    constructor() {
    }

    enable() {

        handlerId = connection.signal_subscribe(null, "org.gtk.gio.DesktopAppInfo", "Launched", "/org/gtk/gio/DesktopAppInfo", null, 0, _parseSignal);

        function _parseSignal(connection, sender, path, iface, signal, params) {
            log("Calling _parseSignal");

            var apppath = params.get_child_value(0).get_bytestring();
            var apppid = params.get_child_value(2).get_int64();
            var openedfilepath = params.get_child_value(3).get_strv();

            log("params.get_child_value", apppath);
            log("params.get_child_value", apppid);
            log("params.get_child_value", openedfilepath);

            const filepath = GLib.build_filenamev([GLib.get_home_dir(), 'test-file.txt']);


            log("home: ", GLib.get_home_dir());
            log("filepath: ", filepath);

            const file = Gio.File.new_for_path(filepath);

            const outputStream = file.create(Gio.FileCreateFlags.NONE, null);
        }
    }

    disable() {
        connection.signal_unsubscribe(handlerId);
        log(`disabling ${Me.metadata.name}`);
    }
}

function init() {
    log(`initializing ${Me.metadata.name}`);

    return new Extension();
}

So, the only issue remaining is, how to append the values to a file.

I am looking at:

https://gjs-docs.gnome.org/gio20~2.0/gio.file#method-append_to

and

https://gjs.guide/guides/gio/file-operations.html#creating-files-and-folders

but not understanding how to precede from here.

Just to be clear, I have to append values to the file (And afterwards, will have to delete some lines though i have not reach that point yet).


Solution

  • OP Here. The solution is:

    const { Gio, GLib } = imports.gi;
    
    const ExtensionUtils = imports.misc.extensionUtils;
    const Me = ExtensionUtils.getCurrentExtension();
    const connection = Gio.DBus.session;
    var handlerId;
    
    class Extension {
        constructor() {
        }
    
        enable() {
    
            handlerId = connection.signal_subscribe(null, "org.gtk.gio.DesktopAppInfo", "Launched", "/org/gtk/gio/DesktopAppInfo", null, 0, _parseSignal);
    
            function _parseSignal(connection, sender, path, iface, signal, params) {
                log("Calling _parseSignal");
    
                var apppath = params.get_child_value(0).get_bytestring();
                var apppid = params.get_child_value(2).get_int64();
                var openedfilepath = params.get_child_value(3).get_strv();
    
                log("apppath : " + apppath);
                log("apppid : " + apppid);
                log("openedfilepath : " +  openedfilepath);
    
                const filepath = GLib.build_filenamev([GLib.get_home_dir(), 'test-file.txt']);
    
                const file = Gio.File.new_for_path(filepath);
    
                // const outputStreamCreate = file.create(Gio.FileCreateFlags.NONE, null);
                const outputStreamAppend = file.append_to(Gio.FileCreateFlags.NONE, null);
    
                var to_write = apppath + ' ' + apppid + ' ' + openedfilepath + '\n'
    
                const bytesWritten = outputStreamAppend.write_all(to_write, null);
            }
        }
    
        disable() {
            connection.signal_unsubscribe(handlerId);
            log(`disabling ${Me.metadata.name}`);
        }
    }
    
    function init() {
        log(`initializing ${Me.metadata.name}`);
    
        return new Extension();
    }