0

I have some .desktop files and am looking for c functions that make it as if user double clicked on the .desktop files.

Here are my desktop files: https://www.youtube.com/watch?v=Yc19BzLTnDE

Thanks

Noitidart
  • 417
  • 1
    I am not sure what is the goal of it. C? doubleclick? why not simply run the command(s) in the file(s). – Jacob Vlijm Mar 02 '15 at 08:32
  • Thanks @JacobVlijm if I run the .desktop then it gets the custom icon :) So by double click i mean just basically launch that .desktop file please :) – Noitidart Mar 02 '15 at 08:43
  • Also another big issue is I am trying to launch "dev" profile from the "default" profile. "default" is the .desktop without the "beta" sash. So when I run the command line it opens it but it (without the custom icon) and also puts the launched process ("dev" profile, the second one) PID on the fcntl'ed files of the launching process, "default" profile. :( – Noitidart Mar 02 '15 at 09:18
  • Ah also if I dont launch double click on the desktop file, seperate profiles dont get their own item in the unity launcher, they are all lumped into one. Making those notes here for myself too :D haha – Noitidart Mar 02 '15 at 09:33
  • Do they get separate icons if you open firefox from different profiles??? It shouldn't... – Jacob Vlijm Mar 02 '15 at 09:34
  • No they don't but that's my intent :P I'm trying to make them separate for my addon Profilist i accomplished this on windows nad mac now just to hit linux and ubuntu :) mac: https://github.com/Noitidart/Profilist/issues/18#issuecomment-76653957 windows: https://github.com/Noitidart/Profilist/issues/18#issue-34872404 – Noitidart Mar 02 '15 at 09:43
  • AH, now I remember, we met before on this question: http://askubuntu.com/questions/534098/getting-icon-of-window-and-then-changing-icon – Jacob Vlijm Mar 02 '15 at 09:45
  • @JacobVlijm Haha yes sir I was just looking at that to implement it :) The beginnings were to move to desktop shortcut so I did that and now I need to launch them some how haha. I had this issue with OSX but I worked around that by launching them with open but linux doesnt have open :( – Noitidart Mar 02 '15 at 09:59
  • I also abnadone that need for dynamic icon, as the .desktop shortcut offers so much more :) btw any new developments allowing for icon change while program is running? :P haha – Noitidart Mar 02 '15 at 10:01
  • Hey @JacobVlijm i found somethin, is it possible to do launcher = Gio.DesktopAppInfo.new_from_filename(desktop) launcher.launch_uris(uris, None) from C code? i found this here: http://askubuntu.com/a/239883/321266 – Noitidart Mar 02 '15 at 21:29

1 Answers1

0

This solution uses Gio, it uses the g_app_info_launch_uris method. It works it launches the .desktop with the icon applied to the .desktop file:

This is js-ctypes code, can run from fiefox extension. Or firefox scratchpad with develope prefs enabled. I adapated it from this solution here: https://askubuntu.com/a/239883/321266

Cu.import('resource://gre/modules/ctypes.jsm');

var gio = ctypes.open('libgio-2.0.so.0');

// BASIC TYPES
var TYPES = {
    gchar: ctypes.char,
    gint: ctypes.int,
    GAppInfo: ctypes.StructType('GAppInfo'),
    GAppLaunchContext: ctypes.StructType('GAppLaunchContext'),
    GDesktopAppInfo: ctypes.StructType('GDesktopAppInfo'),
    GList: new ctypes.StructType('GList', [
        {'data': ctypes.voidptr_t},
        {'next': ctypes.voidptr_t},
        {'prev': ctypes.voidptr_t}
    ]),
    GQuark: ctypes.uint32_t
};

// ADVANCED TYPES
TYPES.gboolean = TYPES.gint;
TYPES.GError = new ctypes.StructType('GError', [
    {'domain': TYPES.GQuark},
    {'code': ctypes.int},
    {'message': ctypes.char.ptr}
]);

// FUNCTIONS
/* https://developer.gnome.org/gio/unstable/gio-Desktop-file-based-GAppInfo.html#g-desktop-app-info-new-from-filename
 * GDesktopAppInfo * g_desktop_app_info_new_from_filename(
 *   const char *filename
 * );
 */
var new_from_filename = gio.declare('g_desktop_app_info_new_from_filename', ctypes.default_abi,
    TYPES.GDesktopAppInfo.ptr,  // return
    TYPES.gchar.ptr             // *filename
);

/* https://developer.gnome.org/gio/unstable/GAppInfo.html#g-app-info-launch-uris
 * gboolean g_app_info_launch_uris (
 *   GAppInfo *appinfo,
 *   GList *uris,
 *   GAppLaunchContext *launch_context,
 *   GError **error
 * );
 */
var launch_uris = gio.declare('g_app_info_launch_uris', ctypes.default_abi,
    TYPES.gboolean,                 // return
    TYPES.GAppInfo.ptr,             // *appinfo
    TYPES.GList.ptr,                // *uris
    TYPES.GAppLaunchContext.ptr,    // *launch_context
    TYPES.GError.ptr.ptr            // **error
);

// start - helper functions

// end - helper functions

var shutdown = function() {

    gio.close();
    console.log('succesfully shutdown');
}

function main() {
    var jsStr_pathToDesktopFile = OS.Path.join(OS.Constants.Path.desktopDir, 'Firefox - Profile Manager.desktop');
    var launcher = new_from_filename(OS.Path.join(OS.Constants.Path.desktopDir, jsStr_pathToDesktopFile));
    console.info('launcher:', launcher, launcher.toString(), uneval(launcher));

    if (launcher.isNull()) {
        throw new Error('No file exists at path: "' + jsStr_pathToDesktopFile + '"');
    }

    launcher = ctypes.cast(launcher, TYPES.GAppInfo.ptr);
    var uris = new TYPES.GList(); // can use `null`
    var launch_context = null; // have to use null due o this explanation here: // cannot use `var launch_context = new TYPES.GAppLaunchContext();` //throws `Error: cannot construct an opaque StructType` so i have to get launch_context from something like `gdk_display_get_app_launch_context` because i dont know he structure to it, and i obviously cannto create opaque structures
    var error = new TYPES.GError.ptr(); // can use `null`

    var rez_launch_uris = launch_uris(launcher, uris.address(), launch_context/*launch_context.address()*/, error.address());
    console.info('rez_launch_uris:', rez_launch_uris, rez_launch_uris.toString(), uneval(rez_launch_uris));
    console.info('error:', error, error.toString(), uneval(error));
}

try {
    main();
} catch (ex) {
    console.error('Error Occured:', ex);
} finally {
    shutdown();
}
Noitidart
  • 417