2015-03-23 11:17:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2014 Red Hat, Inc
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2015-01-15 01:56:25 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "libgsystem.h"
|
2015-03-20 15:21:19 +00:00
|
|
|
#include "libglnx/libglnx.h"
|
2015-01-15 01:56:25 +00:00
|
|
|
|
|
|
|
#include "xdg-app-builtins.h"
|
|
|
|
#include "xdg-app-utils.h"
|
|
|
|
|
|
|
|
static gboolean opt_show_details;
|
2015-01-23 12:40:25 +00:00
|
|
|
static gboolean opt_user;
|
|
|
|
static gboolean opt_system;
|
2015-01-15 01:56:25 +00:00
|
|
|
|
|
|
|
static GOptionEntry options[] = {
|
2015-01-23 12:40:25 +00:00
|
|
|
{ "user", 0, 0, G_OPTION_ARG_NONE, &opt_user, "Show user installations", NULL },
|
|
|
|
{ "system", 0, 0, G_OPTION_ARG_NONE, &opt_system, "Show system-wide installations", NULL },
|
2015-09-25 07:28:49 +00:00
|
|
|
{ "show-details", 'd', 0, G_OPTION_ARG_NONE, &opt_show_details, "Show arches and branches", NULL },
|
2015-01-15 01:56:25 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2015-01-15 02:25:37 +00:00
|
|
|
static gboolean
|
2015-03-10 14:31:38 +00:00
|
|
|
print_installed_refs (const char *kind, gboolean print_system, gboolean print_user, GCancellable *cancellable, GError **error)
|
2015-01-15 02:25:37 +00:00
|
|
|
{
|
2015-03-20 15:21:19 +00:00
|
|
|
g_autofree char *last = NULL;
|
2015-08-31 07:51:48 +00:00
|
|
|
g_auto(GStrv) system = NULL;
|
|
|
|
g_auto(GStrv) user = NULL;
|
2015-03-10 14:31:38 +00:00
|
|
|
int s, u;
|
|
|
|
|
|
|
|
if (print_user)
|
|
|
|
{
|
2015-03-20 15:21:19 +00:00
|
|
|
g_autoptr(XdgAppDir) dir = NULL;
|
2015-03-10 14:31:38 +00:00
|
|
|
|
|
|
|
dir = xdg_app_dir_get (TRUE);
|
|
|
|
if (!xdg_app_dir_list_refs (dir, kind, &user, cancellable, error))
|
2015-09-28 14:40:19 +00:00
|
|
|
return FALSE;
|
2015-03-10 14:31:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
user = g_new0 (char *, 1);
|
2015-01-15 01:56:25 +00:00
|
|
|
|
2015-03-10 14:31:38 +00:00
|
|
|
if (print_system)
|
2015-01-15 01:56:25 +00:00
|
|
|
{
|
2015-03-20 15:21:19 +00:00
|
|
|
g_autoptr(XdgAppDir) dir = NULL;
|
2015-03-10 14:31:38 +00:00
|
|
|
|
|
|
|
dir = xdg_app_dir_get (FALSE);
|
|
|
|
if (!xdg_app_dir_list_refs (dir, kind, &system, cancellable, error))
|
2015-09-28 14:40:19 +00:00
|
|
|
return FALSE;
|
2015-03-10 14:31:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
system = g_new0 (char *, 1);
|
2015-01-15 02:25:37 +00:00
|
|
|
|
2015-09-24 10:14:19 +00:00
|
|
|
XdgAppTablePrinter *printer = xdg_app_table_printer_new ();
|
|
|
|
|
2015-03-10 14:31:38 +00:00
|
|
|
for (s = 0, u = 0; system[s] != NULL || user[u] != NULL; )
|
|
|
|
{
|
2015-09-24 10:14:19 +00:00
|
|
|
char *ref, *partial_ref;
|
2015-08-31 07:51:48 +00:00
|
|
|
g_auto(GStrv) parts = NULL;
|
2015-06-01 11:28:19 +00:00
|
|
|
g_autofree char *repo = NULL;
|
2015-03-10 14:31:38 +00:00
|
|
|
gboolean is_user;
|
2015-06-01 11:28:19 +00:00
|
|
|
g_autoptr(XdgAppDir) dir = NULL;
|
2015-03-10 14:31:38 +00:00
|
|
|
|
|
|
|
if (system[s] == NULL)
|
|
|
|
is_user = TRUE;
|
|
|
|
else if (user[u] == NULL)
|
|
|
|
is_user = FALSE;
|
|
|
|
else if (strcmp (system[s], user[u]) <= 0)
|
|
|
|
is_user = FALSE;
|
|
|
|
else
|
|
|
|
is_user = TRUE;
|
|
|
|
|
|
|
|
if (is_user)
|
|
|
|
ref = user[u++];
|
|
|
|
else
|
|
|
|
ref = system[s++];
|
|
|
|
|
|
|
|
parts = g_strsplit (ref, "/", -1);
|
2015-09-24 10:14:19 +00:00
|
|
|
partial_ref = strchr(ref, '/') + 1;
|
2015-03-10 14:31:38 +00:00
|
|
|
|
2015-06-01 11:28:19 +00:00
|
|
|
dir = xdg_app_dir_get (is_user);
|
|
|
|
repo = xdg_app_dir_get_origin (dir, ref, NULL, NULL);
|
|
|
|
|
2015-03-10 14:31:38 +00:00
|
|
|
if (opt_show_details)
|
|
|
|
{
|
2015-12-18 10:11:14 +00:00
|
|
|
g_autofree char *active = xdg_app_dir_read_active (dir, ref, NULL);
|
|
|
|
|
2015-09-24 10:14:19 +00:00
|
|
|
xdg_app_table_printer_add_column (printer, partial_ref);
|
|
|
|
xdg_app_table_printer_add_column (printer, repo);
|
2015-12-18 10:11:14 +00:00
|
|
|
xdg_app_table_printer_add_column (printer, active);
|
|
|
|
|
2015-03-10 15:26:51 +00:00
|
|
|
|
2015-09-24 10:14:19 +00:00
|
|
|
xdg_app_table_printer_add_column (printer, ""); /* Options */
|
2015-03-10 15:26:51 +00:00
|
|
|
|
2015-03-10 14:31:38 +00:00
|
|
|
if (print_user && print_system)
|
2015-09-24 10:14:19 +00:00
|
|
|
xdg_app_table_printer_append_with_comma (printer, is_user ? "user" : "system");
|
2015-03-10 15:26:51 +00:00
|
|
|
|
|
|
|
if (strcmp (kind, "app") == 0)
|
|
|
|
{
|
2015-03-20 15:21:19 +00:00
|
|
|
g_autofree char *current;
|
2015-03-10 15:26:51 +00:00
|
|
|
|
|
|
|
current = xdg_app_dir_current_ref (dir, parts[1], cancellable);
|
|
|
|
if (current && strcmp (ref, current) == 0)
|
2015-09-24 10:14:19 +00:00
|
|
|
xdg_app_table_printer_append_with_comma (printer, "current");
|
2015-03-10 15:26:51 +00:00
|
|
|
}
|
2015-03-10 14:31:38 +00:00
|
|
|
}
|
|
|
|
else
|
2015-01-15 01:56:25 +00:00
|
|
|
{
|
2015-03-10 14:31:38 +00:00
|
|
|
if (last == NULL || strcmp (last, parts[1]) != 0)
|
2015-01-15 01:56:25 +00:00
|
|
|
{
|
2015-09-24 10:14:19 +00:00
|
|
|
xdg_app_table_printer_add_column (printer, parts[1]);
|
2015-03-10 14:31:38 +00:00
|
|
|
g_clear_pointer (&last, g_free);
|
|
|
|
last = g_strdup (parts[1]);
|
2015-01-15 01:56:25 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-24 10:14:19 +00:00
|
|
|
xdg_app_table_printer_finish_row (printer);
|
2015-01-15 02:25:37 +00:00
|
|
|
}
|
2015-01-15 01:56:25 +00:00
|
|
|
|
2015-09-24 10:14:19 +00:00
|
|
|
xdg_app_table_printer_print (printer);
|
|
|
|
xdg_app_table_printer_free (printer);
|
2015-01-15 01:56:25 +00:00
|
|
|
|
2015-09-28 14:40:19 +00:00
|
|
|
return TRUE;
|
2015-01-15 02:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
xdg_app_builtin_list_runtimes (int argc, char **argv, GCancellable *cancellable, GError **error)
|
|
|
|
{
|
2015-09-28 14:40:19 +00:00
|
|
|
g_autoptr(GOptionContext) context = NULL;
|
2015-01-15 02:25:37 +00:00
|
|
|
|
2015-01-16 02:46:51 +00:00
|
|
|
context = g_option_context_new (" - List installed runtimes");
|
2015-01-15 02:25:37 +00:00
|
|
|
|
2015-01-23 12:40:25 +00:00
|
|
|
if (!xdg_app_option_context_parse (context, options, &argc, &argv, XDG_APP_BUILTIN_FLAG_NO_DIR, NULL, cancellable, error))
|
2015-09-28 14:40:19 +00:00
|
|
|
return FALSE;
|
2015-01-15 02:25:37 +00:00
|
|
|
|
2015-03-10 14:31:38 +00:00
|
|
|
if (!print_installed_refs ("runtime",
|
|
|
|
opt_system || (!opt_user && !opt_system),
|
|
|
|
opt_user || (!opt_user && !opt_system),
|
|
|
|
cancellable, error))
|
2015-09-28 14:40:19 +00:00
|
|
|
return FALSE;
|
2015-01-15 01:56:25 +00:00
|
|
|
|
2015-09-28 14:40:19 +00:00
|
|
|
return TRUE;
|
2015-01-15 01:56:25 +00:00
|
|
|
}
|
2015-01-15 02:31:39 +00:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
xdg_app_builtin_list_apps (int argc, char **argv, GCancellable *cancellable, GError **error)
|
|
|
|
{
|
2015-09-28 14:40:19 +00:00
|
|
|
g_autoptr(GOptionContext) context = NULL;
|
2015-01-15 02:31:39 +00:00
|
|
|
|
2015-01-16 02:46:51 +00:00
|
|
|
context = g_option_context_new (" - List installed applications");
|
2015-01-15 02:31:39 +00:00
|
|
|
|
2015-01-23 12:40:25 +00:00
|
|
|
if (!xdg_app_option_context_parse (context, options, &argc, &argv, XDG_APP_BUILTIN_FLAG_NO_DIR, NULL, cancellable, error))
|
2015-09-28 14:40:19 +00:00
|
|
|
return FALSE;
|
2015-01-15 02:31:39 +00:00
|
|
|
|
2015-03-10 14:31:38 +00:00
|
|
|
if (!print_installed_refs ("app",
|
|
|
|
opt_system || (!opt_user && !opt_system),
|
|
|
|
opt_user || (!opt_user && !opt_system),
|
|
|
|
cancellable, error))
|
2015-09-28 14:40:19 +00:00
|
|
|
return FALSE;
|
2015-01-15 02:31:39 +00:00
|
|
|
|
2015-09-28 14:40:19 +00:00
|
|
|
return TRUE;
|
2015-01-15 02:31:39 +00:00
|
|
|
}
|