plover-gtk/packageset.c
changeset 9 8d3b1ddf789c
child 10 8b50be3e2998
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plover-gtk/packageset.c	Sat Feb 20 12:11:02 2010 +0000
     1.3 @@ -0,0 +1,171 @@
     1.4 +/*
     1.5 + * Copyright (C) 2010  J. Ali Harlow <ali@juiblex.co.uk>
     1.6 + *
     1.7 + * This program is free software; you can redistribute it and/or modify
     1.8 + * it under the terms of the GNU General Public License as published by
     1.9 + * the Free Software Foundation; either version 2 of the License, or
    1.10 + * (at your option) any later version.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License along
    1.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
    1.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    1.20 + */
    1.21 +
    1.22 +#include "config.h"
    1.23 +#include <stdlib.h>
    1.24 +#include <string.h>
    1.25 +#include <fcntl.h>
    1.26 +#include <errno.h>
    1.27 +#include <glib-object.h>
    1.28 +#include <razor.h>
    1.29 +#include "plover/plover.h"
    1.30 +#include "plover-gtk/error.h"
    1.31 +#include "plover-gtk/packageset.h"
    1.32 +#include "plover-gtk/package.h"
    1.33 +
    1.34 +G_DEFINE_TYPE(PloverPackageSet,plover_package_set,G_TYPE_OBJECT);
    1.35 +
    1.36 +typedef struct _PloverPackageSetPrivate {
    1.37 +    struct razor_root *root;
    1.38 +    struct razor_set *set;
    1.39 +    GSList *packages;
    1.40 +} PloverPackageSetPrivate;
    1.41 +
    1.42 +#define PLOVER_PACKAGE_SET_GET_PRIVATE(obj)\
    1.43 +				G_TYPE_INSTANCE_GET_PRIVATE(obj,\
    1.44 +				  PLOVER_TYPE_PACKAGE_SET,\
    1.45 +				  PloverPackageSetPrivate)
    1.46 +
    1.47 +enum {
    1.48 +    CHANGED=0,
    1.49 +    N_SIGNALS
    1.50 +};
    1.51 +
    1.52 +static guint signals[N_SIGNALS];
    1.53 +
    1.54 +static void plover_package_set_finalize(GObject *obj)
    1.55 +{
    1.56 +    PloverPackageSetPrivate *priv=PLOVER_PACKAGE_SET_GET_PRIVATE(obj);
    1.57 +    if (priv->root)
    1.58 +    {
    1.59 +	/* priv->set, if set, is owned by priv->root and should not
    1.60 +	 * be destroyed.
    1.61 +	 */
    1.62 +	razor_root_close(priv->root);
    1.63 +    }
    1.64 +    else if (priv->set)
    1.65 +	razor_set_destroy(priv->set);
    1.66 +    if (G_OBJECT_CLASS(plover_package_set_parent_class)->finalize)
    1.67 +	G_OBJECT_CLASS(plover_package_set_parent_class)->finalize(obj);
    1.68 +}
    1.69 +
    1.70 +static void plover_package_set_dispose(GObject *obj)
    1.71 +{
    1.72 +    PloverPackageSetPrivate *priv=PLOVER_PACKAGE_SET_GET_PRIVATE(obj);
    1.73 +    if (G_OBJECT_CLASS(plover_package_set_parent_class)->dispose)
    1.74 +	G_OBJECT_CLASS(plover_package_set_parent_class)->dispose(obj);
    1.75 +}
    1.76 +
    1.77 +static void plover_package_set_class_init(PloverPackageSetClass *klass)
    1.78 +{
    1.79 +    GObjectClass *oclass=G_OBJECT_CLASS(klass);
    1.80 +    oclass->finalize=plover_package_set_finalize;
    1.81 +    oclass->dispose=plover_package_set_dispose;
    1.82 +    g_type_class_add_private(klass,sizeof(PloverPackageSetPrivate));
    1.83 +    signals[CHANGED]=g_signal_newv("changed",
    1.84 +      G_TYPE_FROM_CLASS(klass),G_SIGNAL_RUN_LAST,NULL,NULL,NULL,
    1.85 +      g_cclosure_marshal_VOID__VOID,G_TYPE_NONE,0,NULL);
    1.86 +}
    1.87 +
    1.88 +static void plover_package_set_init(PloverPackageSet *set)
    1.89 +{
    1.90 +}
    1.91 +
    1.92 +PloverPackageSet *plover_package_set_new(void)
    1.93 +{
    1.94 +    return g_object_new(PLOVER_TYPE_PACKAGE_SET,NULL);
    1.95 +}
    1.96 +
    1.97 +PloverPackageSet *plover_package_set_new_from_installed(const char *root,
    1.98 +  GError **err)
    1.99 +{
   1.100 +    PloverPackageSet *set;
   1.101 +    PloverPackageSetPrivate *priv;
   1.102 +    set=plover_package_set_new();
   1.103 +    priv=PLOVER_PACKAGE_SET_GET_PRIVATE(set);
   1.104 +    priv->root=razor_root_open(root);
   1.105 +    if (!priv->root)
   1.106 +    {
   1.107 +	g_set_error(err,PLOVER_RAZOR_ERROR,PLOVER_RAZOR_ERROR_FAILED,
   1.108 +	  "Failed to open %s as razor root",root);
   1.109 +	g_object_unref(set);
   1.110 +	return NULL;
   1.111 +    }
   1.112 +    priv->set=razor_root_get_system_set(priv->root);
   1.113 +    if (!priv->set)
   1.114 +    {
   1.115 +	g_set_error(err,PLOVER_RAZOR_ERROR,PLOVER_RAZOR_ERROR_FAILED,
   1.116 +	  "Failed to get system set from %s",root);
   1.117 +	g_object_unref(set);
   1.118 +	return NULL;
   1.119 +    }
   1.120 +    return set;
   1.121 +}
   1.122 +
   1.123 +PloverPackageSet *plover_package_set_new_from_repository(const char *base,
   1.124 +  GError **err)
   1.125 +{
   1.126 +    int fd;
   1.127 +    gchar *s;
   1.128 +    PloverPackageSet *set;
   1.129 +    PloverPackageSetPrivate *priv;
   1.130 +    set=plover_package_set_new();
   1.131 +    priv=PLOVER_PACKAGE_SET_GET_PRIVATE(set);
   1.132 +    fd=open(".",O_RDONLY);
   1.133 +    s=g_build_filename(base,"repodata",NULL);
   1.134 +    if (chdir(s)<0)
   1.135 +    {
   1.136 +	g_set_error(err,G_FILE_ERROR,g_file_error_from_errno(errno),
   1.137 +	  "%s: %s",s,g_strerror(errno));
   1.138 +	g_object_unref(set);
   1.139 +	return NULL;
   1.140 +    }
   1.141 +    g_free(s);
   1.142 +    priv->set=plover_razor_set_create_from_yum("..");
   1.143 +    fchdir(fd);
   1.144 +    close(fd);
   1.145 +    if (!priv->set)
   1.146 +    {
   1.147 +	g_set_error(err,PLOVER_RAZOR_ERROR,PLOVER_RAZOR_ERROR_FAILED,
   1.148 +	  "Failed to create package set from repository %s",base);
   1.149 +	g_object_unref(set);
   1.150 +	return NULL;
   1.151 +    }
   1.152 +    return set;
   1.153 +}
   1.154 +
   1.155 +GSList *plover_package_set_get_packages(PloverPackageSet *set)
   1.156 +{
   1.157 +    struct razor_package_iterator *iter;
   1.158 +    struct razor_package *pkg;
   1.159 +    PloverPackageSetPrivate *priv;
   1.160 +    PloverPackage *package;
   1.161 +    g_return_val_if_fail(PLOVER_IS_PACKAGE_SET(set),NULL);
   1.162 +    priv=PLOVER_PACKAGE_SET_GET_PRIVATE(set);
   1.163 +    if (priv->set && !priv->packages)
   1.164 +    {
   1.165 +	iter=razor_package_iterator_create(priv->set);
   1.166 +	while(razor_package_iterator_next(iter,&pkg,RAZOR_DETAIL_LAST))
   1.167 +	{
   1.168 +	    package=plover_package_new(priv->set,pkg);
   1.169 +	    priv->packages=g_slist_prepend(priv->packages,package);
   1.170 +	}
   1.171 +	razor_package_iterator_destroy(iter);
   1.172 +    }
   1.173 +    return priv->packages;
   1.174 +}