(root)/
glib-2.79.0/
gio/
tests/
apps.c
       1  #include <gio/gio.h>
       2  #include <gio/gdesktopappinfo.h>
       3  #include <locale.h>
       4  #include <stdlib.h>
       5  
       6  static void
       7  print (const gchar *str)
       8  {
       9    g_print ("%s\n", str ? str : "nil");
      10  }
      11  
      12  static void
      13  print_app_list (GList *list)
      14  {
      15    while (list)
      16      {
      17        GAppInfo *info = list->data;
      18        print (g_app_info_get_id (info));
      19        list = g_list_delete_link (list, list);
      20        g_object_unref (info);
      21      }
      22  }
      23  
      24  static void
      25  quit (gpointer user_data)
      26  {
      27    g_print ("appinfo database changed.\n");
      28    exit (0);
      29  }
      30  
      31  int
      32  main (int argc, char **argv)
      33  {
      34    setlocale (LC_ALL, "");
      35  
      36    if (argv[1] == NULL || g_str_equal (argv[1], "--help"))
      37      g_print ("Usage:\n"
      38        "  apps --help\n"
      39        "  apps COMMAND [COMMAND_OPTIONS]\n"
      40        "\n"
      41        "COMMANDS:\n"
      42        "  list\n"
      43        "  search [--should-show-only] TEXT_TO_SEARCH\n"
      44        "  implementations INTERFACE_NAME\n"
      45        "  show-info DESKTOP_FILE\n"
      46        "  default-for-type MIME_TYPE\n"
      47        "  recommended-for-type MIME_TYPE\n"
      48        "  all-for-type MIME_TYPE\n"
      49        "  fallback-for-type MIME_TYPE\n"
      50        "  should-show DESKTOP_FILE\n"
      51        "  monitor\n"
      52        "\n"
      53        "Examples:\n"
      54        "  apps search --should-show-only ter\n"
      55        "  apps show-info org.gnome.Nautilus.desktop\n"
      56        "  apps default-for-type image/png\n"
      57        "\n");
      58    else if (g_str_equal (argv[1], "list"))
      59      {
      60        GList *all, *i;
      61  
      62        all = g_app_info_get_all ();
      63        for (i = all; i; i = i->next)
      64          g_print ("%s%s", g_app_info_get_id (i->data), i->next ? " " : "\n");
      65        g_list_free_full (all, g_object_unref);
      66      }
      67    else if (g_str_equal (argv[1], "search"))
      68      {
      69        gchar ***results;
      70        gboolean should_show_only;
      71        gint i, j;
      72  
      73        should_show_only = argc > 3 && g_str_equal (argv[2], "--should-show-only");
      74        results = g_desktop_app_info_search (argv[ should_show_only ? 3 : 2 ]);
      75        for (i = 0; results[i]; i++)
      76          {
      77            for (j = 0; results[i][j]; j++)
      78              {
      79                if (should_show_only)
      80                  {
      81                    GDesktopAppInfo *info;
      82                    gboolean should_show;
      83  
      84                    info = g_desktop_app_info_new (results[i][j]);
      85                    if (info)
      86                      {
      87                        should_show = g_app_info_should_show (G_APP_INFO (info));
      88                        g_object_unref (info);
      89                        if (!should_show)
      90                          continue;
      91                      }
      92                  }
      93                g_print ("%s%s", j ? " " : "", results[i][j]);
      94              }
      95            g_print ("\n");
      96            g_strfreev (results[i]);
      97          }
      98        g_free (results);
      99      }
     100    else if (g_str_equal (argv[1], "implementations"))
     101      {
     102        GList *results;
     103  
     104        results = g_desktop_app_info_get_implementations (argv[2]);
     105        print_app_list (results);
     106      }
     107    else if (g_str_equal (argv[1], "show-info"))
     108      {
     109        GAppInfo *info;
     110  
     111        info = (GAppInfo *) g_desktop_app_info_new (argv[2]);
     112        if (info)
     113          {
     114            print (g_app_info_get_id (info));
     115            print (g_app_info_get_name (info));
     116            print (g_app_info_get_display_name (info));
     117            print (g_app_info_get_description (info));
     118            g_object_unref (info);
     119          }
     120      }
     121    else if (g_str_equal (argv[1], "default-for-type"))
     122      {
     123        GAppInfo *info;
     124  
     125        info = g_app_info_get_default_for_type (argv[2], FALSE);
     126  
     127        if (info)
     128          {
     129            print (g_app_info_get_id (info));
     130            g_object_unref (info);
     131          }
     132      }
     133    else if (g_str_equal (argv[1], "recommended-for-type"))
     134      {
     135        GList *list;
     136  
     137        list = g_app_info_get_recommended_for_type (argv[2]);
     138        print_app_list (list);
     139      }
     140    else if (g_str_equal (argv[1], "all-for-type"))
     141      {
     142        GList *list;
     143  
     144        list = g_app_info_get_all_for_type (argv[2]);
     145        print_app_list (list);
     146      }
     147  
     148    else if (g_str_equal (argv[1], "fallback-for-type"))
     149      {
     150        GList *list;
     151  
     152        list = g_app_info_get_fallback_for_type (argv[2]);
     153        print_app_list (list);
     154      }
     155  
     156    else if (g_str_equal (argv[1], "should-show"))
     157      {
     158        GAppInfo *info;
     159  
     160        info = (GAppInfo *) g_desktop_app_info_new (argv[2]);
     161        if (info)
     162          {
     163            g_print ("%s\n", g_app_info_should_show (info) ? "true" : "false");
     164            g_object_unref (info);
     165          }
     166      }
     167  
     168    else if (g_str_equal (argv[1], "monitor"))
     169      {
     170        GAppInfoMonitor *monitor;
     171        GAppInfo *info;
     172  
     173        monitor = g_app_info_monitor_get ();
     174  
     175        info = (GAppInfo *) g_desktop_app_info_new ("this-desktop-file-does-not-exist");
     176        g_assert (!info);
     177  
     178        g_signal_connect (monitor, "changed", G_CALLBACK (quit), NULL);
     179  
     180        while (1)
     181          g_main_context_iteration (NULL, TRUE);
     182      }
     183  
     184    return 0;
     185  }