Skip to content

Instantly share code, notes, and snippets.

@hartwork
Created February 1, 2015 16:51
Show Gist options
  • Save hartwork/e180e225dcc99465a33d to your computer and use it in GitHub Desktop.
Save hartwork/e180e225dcc99465a33d to your computer and use it in GitHub Desktop.
Tool to query which installed applications handle a given MIME type
/*
** Copyright (C) Sebastian Pipping <sebastian@pipping.org>
** Licensed under GPL v2 or later
**
** 2013-06-01
**
** Compilation
** ===========
**
** # gcc $(pkg-config --libs --cflags-only-I gio-2.0) -o mimetest mimetest.c
**
**
** Example output
** ==============
**
** # ./mimetest text/plain
** Applications supporting MIME type "text/plain":
** [1]
** id = "kde4-kwrite.desktop"
** name = "KWrite"
** command line = "kwrite %U"
**
** [2]
** id = "kde4-kate.desktop"
** name = "Kate"
** command line = "kate -b %U"
**
** [3]
** id = "kde4-okularApplication_txt.desktop"
** name = "Okular"
** command line = "okular %U %i -caption %c"
**
** [4]
** id = "diffuse.desktop"
** name = "Diffuse Merge Tool"
** command line = "diffuse -s %F"
*/
#include <stdio.h>
#include <gio/gio.h>
#define INDENT " "
int main(int argc, char ** argv) {
if (argc != 2) {
fprintf(stderr, "USAGE: %s MIME_TYPE\n", argv[0]);
return 1;
}
g_type_init();
const char * const mime_type = argv[1];
GList * p_supporting_app_list = g_app_info_get_recommended_for_type(mime_type);
int index = 0;
if (! p_supporting_app_list) {
fprintf(stderr, "ERROR: MIME type \"%s\" invalid or not supported by any installed application.\n", mime_type);
return 2;
}
printf("Applications supporting MIME type \"%s\":\n", mime_type);
for(; p_supporting_app_list; p_supporting_app_list = p_supporting_app_list->next, index++) {
printf("[%d]\n"
INDENT "id = \"%s\"\n"
INDENT "name = \"%s\"\n"
INDENT "command line = \"%s\"\n"
"%s",
index + 1,
g_app_info_get_id(p_supporting_app_list->data),
g_app_info_get_name(p_supporting_app_list->data),
g_app_info_get_commandline(p_supporting_app_list->data),
(p_supporting_app_list->next ? "\n" : ""));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment