packagekit/client/pk-console.c
2007-09-17 23:20:28 +01:00

482 lines
14 KiB
C

/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2007 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <dbus/dbus-glib.h>
#include <pk-debug.h>
#include <pk-task-client.h>
#include <pk-package-id.h>
#include <pk-enum-list.h>
/**
* pk_console_make_space:
**/
static gchar *
pk_console_make_space (const gchar *data, guint length, guint *extra)
{
gint size;
gchar *padding;
*extra = 0;
size = length;
if (data != NULL) {
size = (length - strlen(data));
if (size < 0) {
*extra = -size;
size = 0;
}
}
padding = g_strnfill (size, ' ');
return padding;
}
/**
* pk_console_package_cb:
**/
static void
pk_console_package_cb (PkTaskClient *tclient, guint value, const gchar *package_id, const gchar *summary, gpointer data)
{
PkPackageId *ident;
PkPackageId *spacing;
const gchar *installed;
guint extra;
if (value == 0) {
installed = "no ";
} else {
installed = "yes ";
}
spacing = pk_package_id_new ();
ident = pk_package_id_new_from_string (package_id);
/* these numbers are guesses */
extra = 0;
spacing->name = pk_console_make_space (ident->name, 20, &extra);
spacing->version = pk_console_make_space (ident->version, 15-extra, &extra);
spacing->arch = pk_console_make_space (ident->arch, 7-extra, &extra);
spacing->data = pk_console_make_space (ident->data, 7-extra, &extra);
/* pretty print */
g_print ("%s %s%s %s%s %s%s %s%s %s\n", installed,
ident->name, spacing->name,
ident->version, spacing->version,
ident->arch, spacing->arch,
ident->data, spacing->data,
summary);
/* free all the data */
pk_package_id_free (ident);
pk_package_id_free (spacing);
}
/**
* pk_console_transaction_cb:
**/
static void
pk_console_transaction_cb (PkTaskClient *tclient, const gchar *tid, const gchar *timespec,
gboolean succeeded, const gchar *role, guint duration, gpointer data)
{
g_print ("tid : %s\n", tid);
g_print (" timespec : %s\n", timespec);
g_print (" succeeded : %i\n", succeeded);
g_print (" role : %s\n", role /*pk_role_enum_to_text (role)*/);
g_print (" duration : %i (seconds)\n", duration);
}
/**
* pk_console_update_detail_cb:
**/
static void
pk_console_update_detail_cb (PkTaskClient *tclient, const gchar *package_id,
const gchar *updates, const gchar *obsoletes,
const gchar *url, const gchar *restart,
const gchar *update_text, gpointer data)
{
g_print ("update-detail\n");
g_print (" package: '%s'\n", package_id);
g_print (" updates: '%s'\n", updates);
g_print (" obsoletes: '%s'\n", obsoletes);
g_print (" url: '%s'\n", url);
g_print (" restart: '%s'\n", restart);
g_print (" update_text:'%s'\n", update_text);
}
/**
* pk_console_percentage_changed_cb:
**/
static void
pk_console_percentage_changed_cb (PkTaskClient *tclient, guint percentage, gpointer data)
{
g_print ("%i%%\n", percentage);
}
/**
* pk_console_usage:
**/
static void
pk_console_usage (const gchar *error)
{
if (error != NULL) {
g_print ("Error: %s\n", error);
}
g_print ("usage:\n");
g_print (" pkcon [sync] [verbose] search name|details|group|file data\n");
g_print (" pkcon [sync] [verbose] install <package_id>\n");
g_print (" pkcon [sync] [verbose] remove <package_id>\n");
g_print (" pkcon [sync] [verbose] update <package_id>\n");
g_print (" pkcon [sync] [verbose] refresh\n");
g_print (" pkcon [sync] [verbose] force-refresh\n");
g_print (" pkcon [sync] [verbose] update-system\n");
g_print (" pkcon [sync] [verbose] get updates\n");
g_print (" pkcon [sync] [verbose] get depends <package_id>\n");
g_print (" pkcon [sync] [verbose] get requires <package_id>\n");
g_print (" pkcon [sync] [verbose] get description <package_id>\n");
g_print (" pkcon [sync] [verbose] get updatedetail <package_id>\n");
g_print (" pkcon [sync] [verbose] get actions\n");
g_print (" pkcon [sync] [verbose] get groups\n");
g_print (" pkcon [sync] [verbose] get filters\n");
g_print (" pkcon [sync] [verbose] get transactions\n");
g_print ("\n");
g_print (" package_id is typically gimp;2:2.4.0-0.rc1.1.fc8;i386;development\n");
}
/**
* pk_console_parse_multiple_commands:
**/
static void
pk_console_parse_multiple_commands (PkTaskClient *tclient, GPtrArray *array)
{
const gchar *mode;
const gchar *value = NULL;
const gchar *details = NULL;
guint remove;
PkEnumList *elist;
mode = g_ptr_array_index (array, 0);
if (array->len > 1) {
value = g_ptr_array_index (array, 1);
}
if (array->len > 2) {
details = g_ptr_array_index (array, 2);
}
remove = 1;
if (strcmp (mode, "search") == 0) {
if (value == NULL) {
pk_console_usage ("you need to specify a search type");
remove = 1;
goto out;
} else if (strcmp (value, "name") == 0) {
if (details == NULL) {
pk_console_usage ("you need to specify a search term");
remove = 2;
goto out;
} else {
pk_task_client_set_sync (tclient, TRUE);
pk_task_client_search_name (tclient, "none", details);
remove = 3;
}
} else if (strcmp (value, "details") == 0) {
if (details == NULL) {
pk_console_usage ("you need to specify a search term");
remove = 2;
goto out;
} else {
pk_task_client_set_sync (tclient, TRUE);
pk_task_client_search_details (tclient, "none", details);
remove = 3;
}
} else if (strcmp (value, "group") == 0) {
if (details == NULL) {
pk_console_usage ("you need to specify a search term");
remove = 2;
goto out;
} else {
pk_task_client_set_sync (tclient, TRUE);
pk_task_client_search_group (tclient, "none", details);
remove = 3;
}
} else if (strcmp (value, "file") == 0) {
if (details == NULL) {
pk_console_usage ("you need to specify a search term");
remove = 2;
goto out;
} else {
pk_task_client_set_sync (tclient, TRUE);
pk_task_client_search_file (tclient, "none", details);
remove = 3;
}
} else {
pk_console_usage ("invalid search type");
}
} else if (strcmp (mode, "install") == 0) {
if (value == NULL) {
pk_console_usage ("you need to specify a package to install");
remove = 1;
goto out;
} else {
pk_task_client_install_package (tclient, value);
remove = 2;
}
} else if (strcmp (mode, "remove") == 0) {
if (value == NULL) {
pk_console_usage ("you need to specify a package to remove");
remove = 1;
goto out;
} else {
pk_task_client_remove_package (tclient, value, FALSE);
remove = 2;
}
} else if (strcmp (mode, "get") == 0) {
if (value == NULL) {
pk_console_usage ("you need to specify a get type");
remove = 1;
goto out;
} else if (strcmp (value, "depends") == 0) {
if (details == NULL) {
pk_console_usage ("you need to specify a search term");
remove = 2;
goto out;
} else {
pk_task_client_set_sync (tclient, TRUE);
pk_task_client_get_depends (tclient, details);
remove = 3;
}
} else if (strcmp (value, "updatedetail") == 0) {
if (details == NULL) {
pk_console_usage ("you need to specify a search term");
remove = 2;
goto out;
} else {
pk_task_client_set_sync (tclient, TRUE);
pk_task_client_get_update_detail (tclient, details);
remove = 3;
}
} else if (strcmp (value, "requires") == 0) {
if (details == NULL) {
pk_console_usage ("you need to specify a search term");
remove = 2;
goto out;
} else {
pk_task_client_set_sync (tclient, TRUE);
pk_task_client_get_requires (tclient, details);
remove = 3;
}
} else if (strcmp (value, "description") == 0) {
if (details == NULL) {
pk_console_usage ("you need to specify a package to find the description for");
remove = 2;
goto out;
} else {
pk_task_client_set_sync (tclient, TRUE);
pk_task_client_get_description (tclient, details);
remove = 3;
}
} else if (strcmp (value, "updates") == 0) {
pk_task_client_set_sync (tclient, TRUE);
pk_task_client_get_updates (tclient);
remove = 2;
} else if (strcmp (value, "actions") == 0) {
elist = pk_task_client_get_actions (tclient);
pk_enum_list_print (elist);
g_object_unref (elist);
remove = 2;
} else if (strcmp (value, "filters") == 0) {
elist = pk_task_client_get_filters (tclient);
pk_enum_list_print (elist);
g_object_unref (elist);
remove = 2;
} else if (strcmp (value, "groups") == 0) {
elist = pk_task_client_get_groups (tclient);
pk_enum_list_print (elist);
g_object_unref (elist);
remove = 2;
} else if (strcmp (value, "transactions") == 0) {
pk_task_client_set_sync (tclient, TRUE);
pk_task_client_get_old_transactions (tclient, 10);
remove = 2;
} else {
pk_console_usage ("invalid get type");
}
} else if (strcmp (mode, "debug") == 0) {
pk_debug_init (TRUE);
} else if (strcmp (mode, "verbose") == 0) {
pk_debug_init (TRUE);
} else if (strcmp (mode, "update-system") == 0) {
pk_task_client_update_system (tclient);
} else if (strcmp (mode, "refresh") == 0) {
pk_task_client_refresh_cache (tclient, FALSE);
} else if (strcmp (mode, "force-refresh") == 0) {
pk_task_client_refresh_cache (tclient, TRUE);
} else if (strcmp (mode, "sync") == 0) {
pk_task_client_set_sync (tclient, TRUE);
} else if (strcmp (mode, "async") == 0) {
pk_task_client_set_sync (tclient, FALSE);
} else {
pk_console_usage ("option not yet supported");
}
out:
/* remove the right number of items from the pointer index */
g_ptr_array_remove_index (array, 0);
if (remove > 1) {
g_ptr_array_remove_index (array, 0);
}
if (remove > 2) {
g_ptr_array_remove_index (array, 0);
}
}
/**
* pk_console_tidy_up_sync:
**/
static void
pk_console_tidy_up_sync (PkTaskClient *tclient)
{
PkTaskClientPackageItem *item;
GPtrArray *packages;
guint i;
guint length;
gboolean sync;
sync = pk_task_client_get_sync (tclient);
if (sync == TRUE) {
packages = pk_task_client_get_package_buffer (tclient);
length = packages->len;
for (i=0; i<length; i++) {
item = g_ptr_array_index (packages, i);
pk_console_package_cb (tclient, item->value, item->package_id, item->summary, NULL);
}
}
}
/**
* pk_console_finished_cb:
**/
static void
pk_console_finished_cb (PkTaskClient *tclient, PkStatusEnum status, guint runtime, gpointer data)
{
g_print ("Runtime was %i seconds\n", runtime);
}
/**
* pk_console_error_code_cb:
**/
static void
pk_console_error_code_cb (PkTaskClient *tclient, PkErrorCodeEnum error_code, const gchar *details, gpointer data)
{
g_print ("Error: %s : %s\n", pk_error_enum_to_text (error_code), details);
}
/**
* pk_console_description_cb:
**/
static void
pk_console_description_cb (PkTaskClient *tclient, const gchar *package_id,
const gchar *licence, PkGroupEnum group,
const gchar *description, const gchar *url, gpointer data)
{
g_print ("description\n");
g_print (" package: '%s'\n", package_id);
g_print (" licence: '%s'\n", licence);
g_print (" group: '%s'\n", pk_group_enum_to_text (group));
g_print (" description: '%s'\n", description);
g_print (" url: '%s'\n", url);
}
/**
* main:
**/
int
main (int argc, char *argv[])
{
DBusGConnection *system_connection;
GError *error = NULL;
PkTaskClient *tclient;
GPtrArray *array;
guint i;
if (! g_thread_supported ()) {
g_thread_init (NULL);
}
dbus_g_thread_init ();
g_type_init ();
if (!g_thread_supported ())
g_thread_init (NULL);
dbus_g_thread_init ();
/* check dbus connections, exit if not valid */
system_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
if (error) {
pk_warning ("%s", error->message);
g_error_free (error);
g_error ("This program cannot start until you start the dbus system service.");
}
if (argc < 2) {
pk_console_usage (NULL);
return 1;
}
tclient = pk_task_client_new ();
g_signal_connect (tclient, "package",
G_CALLBACK (pk_console_package_cb), NULL);
g_signal_connect (tclient, "transaction",
G_CALLBACK (pk_console_transaction_cb), NULL);
g_signal_connect (tclient, "description",
G_CALLBACK (pk_console_description_cb), NULL);
g_signal_connect (tclient, "update-detail",
G_CALLBACK (pk_console_update_detail_cb), NULL);
g_signal_connect (tclient, "percentage-changed",
G_CALLBACK (pk_console_percentage_changed_cb), NULL);
g_signal_connect (tclient, "finished",
G_CALLBACK (pk_console_finished_cb), NULL);
g_signal_connect (tclient, "error-code",
G_CALLBACK (pk_console_error_code_cb), NULL);
/* add argv to a pointer array */
array = g_ptr_array_new ();
for (i=1; i<argc; i++) {
g_ptr_array_add (array, (gpointer) argv[i]);
}
/* process all the commands */
while (array->len > 0) {
pk_console_parse_multiple_commands (tclient, array);
}
/* if we are sync then print the package lists */
pk_console_tidy_up_sync (tclient);
g_ptr_array_free (array, TRUE);
g_object_unref (tclient);
return 0;
}