gnm-data-cache-source.c 6.92 KB
Newer Older
Jody Goldberg's avatar
Jody Goldberg committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * gnm-data-cache-source.c : GODataCacheSource from a Sheet
 *
 * Copyright (C) 2008 Jody Goldberg (jody@gnome.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include <gnumeric-config.h>
#include "gnm-data-cache-source.h"
#include "go-data-cache-source.h"
#include "go-data-cache.h"


#include <gnumeric.h>
#include <ranges.h>
#include <sheet.h>
#include <expr-name.h>

#include <gsf/gsf-impl-utils.h>
#include <glib/gi18n-lib.h>
#include <string.h>

struct _GnmDataCacheSource {
	GObject		base;

	Sheet	  *src_sheet;
	GnmRange   src_range;	/* treated as cache if src_name is non-NULL */
	GOString  *src_name;	/* optionally NULL */
};
typedef GObjectClass GnmDataCacheSourceClass;

enum {
	PROP_0,
	PROP_SHEET,	/* GnmSheet * */
	PROP_RANGE,	/* GnmRange */
	PROP_NAME	/* char *, optionally NULL */
};

static GODataCache *
gdcs_allocate (GODataCacheSource const *src)
{
	GnmDataCacheSource *gdcs = (GnmDataCacheSource *)src;
	GODataCache *res;

	g_return_val_if_fail (gdcs->src_sheet != NULL, NULL);

	if (NULL != gdcs->src_name) {
		GnmParsePos pp;
		GnmEvalPos ep;
		GnmNamedExpr *nexpr = expr_name_lookup (
			parse_pos_init_sheet (&pp, gdcs->src_sheet), gdcs->src_name->str);
		if (NULL != nexpr) {
			GnmValue *v = expr_name_eval (nexpr,
				eval_pos_init_sheet (&ep, gdcs->src_sheet),
				GNM_EXPR_EVAL_PERMIT_NON_SCALAR	| GNM_EXPR_EVAL_PERMIT_EMPTY);

			if (NULL != v) {
				value_release (v);
			}
		}
	}

	res = g_object_new (GO_DATA_CACHE_TYPE, NULL);

	return res;
}

static GError *
gdcs_validate (GODataCacheSource const *src)
{
	return NULL;
}

static gboolean
gdcs_needs_update (GODataCacheSource const *src)
{
	return FALSE;
}

static void
gnm_data_cache_source_init (GnmDataCacheSource *src)
{
	src->src_sheet = NULL;
	range_init_invalid (&src->src_range);
}

static GObjectClass *parent_klass;
static void
gnm_data_cache_source_finalize (GObject *obj)
{
	GnmDataCacheSource *src = (GnmDataCacheSource *)obj;
	go_string_unref (src->src_name);
	(parent_klass->finalize) (obj);
}
static void
gnm_data_cache_source_set_property (GObject *obj, guint property_id,
				    GValue const *value, GParamSpec *pspec)
{
	GnmDataCacheSource *src = (GnmDataCacheSource *)obj;

	switch (property_id) {
	case PROP_SHEET :
		gnm_data_cache_source_set_sheet (src, g_value_get_object (value));
		break;
	case PROP_RANGE :
		gnm_data_cache_source_set_range (src, g_value_get_boxed (value));
		break;
	case PROP_NAME :
		gnm_data_cache_source_set_name (src, g_value_get_string (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
	}
}

static void
gnm_data_cache_source_get_property (GObject *obj, guint property_id,
				    GValue *value, GParamSpec *pspec)
{
	GnmDataCacheSource const *src = (GnmDataCacheSource const *)obj;
	switch (property_id) {
	case PROP_SHEET :
		g_value_set_object (value, gnm_data_cache_source_get_sheet (src));
		break;
	case PROP_RANGE :
		g_value_set_boxed (value, gnm_data_cache_source_get_range (src));
		break;
	case PROP_NAME :
		g_value_set_string (value, gnm_data_cache_source_get_name (src));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
	}
}

static void
gnm_data_cache_source_class_init (GnmDataCacheSourceClass *klass)
{
	GObjectClass *gobject_class = (GObjectClass *)klass;
	gobject_class->set_property	= gnm_data_cache_source_set_property;
	gobject_class->get_property	= gnm_data_cache_source_get_property;
	gobject_class->finalize		= gnm_data_cache_source_finalize;

	g_object_class_install_property (gobject_class, PROP_SHEET,
		 g_param_spec_object ("src-sheet", "sheet",
			"The source sheet.",
			GNM_SHEET_TYPE, GSF_PARAM_STATIC | G_PARAM_READWRITE));
	g_object_class_install_property (gobject_class, PROP_RANGE,
		 g_param_spec_boxed ("src-range", "range",
			"Optional named expression to generate a source range.",
			gnm_range_get_type (), GSF_PARAM_STATIC | G_PARAM_READWRITE));
	g_object_class_install_property (gobject_class, PROP_NAME,
		 g_param_spec_string ("src-name", "src-name",
			"Optional named expression to generate a source range.", NULL,
			GSF_PARAM_STATIC | G_PARAM_READWRITE));

	parent_klass = g_type_class_peek_parent (klass);
}

static void
gnm_data_cache_source_iface_init (GODataCacheSourceClass *iface)
{
	iface->allocate	    = gdcs_allocate;
	iface->validate	    = gdcs_validate;
	iface->needs_update = gdcs_needs_update;
}

GSF_CLASS_FULL (GnmDataCacheSource, gnm_data_cache_source, NULL, NULL,
		gnm_data_cache_source_class_init, NULL,
		gnm_data_cache_source_init, G_TYPE_OBJECT, 0,
		GSF_INTERFACE (gnm_data_cache_source_iface_init, GO_DATA_CACHE_SOURCE_TYPE))

/**
 * gnm_data_cache_source_new:
 * @src_sheet : #Sheet
 * @src_range : #GnmRange
 * @src_name : char *, optionally %NULL
 *
 * Allocates a new Allocates a new #GnmDataCacheSource
 *
 * Returns : #GODataCacheSource
 **/
GODataCacheSource *
gnm_data_cache_source_new (Sheet *src_sheet,
			   GnmRange const *src_range, char const *src_name)
{
	GnmDataCacheSource *res;

	g_return_val_if_fail (IS_SHEET (src_sheet), NULL);
	g_return_val_if_fail (src_range != NULL, NULL);

	res = g_object_new (GNM_DATA_CACHE_SOURCE_TYPE, NULL);
	res->src_sheet = src_sheet;
	res->src_range = *src_range;
	gnm_data_cache_source_set_name (res, src_name);

	return GO_DATA_CACHE_SOURCE (res);
}
Sheet *
gnm_data_cache_source_get_sheet (GnmDataCacheSource const *src)
{
	g_return_val_if_fail (IS_GNM_DATA_CACHE_SOURCE (src), NULL);
	return src->src_sheet;
}

void
gnm_data_cache_source_set_sheet (GnmDataCacheSource *src, Sheet *sheet)
{
	g_return_if_fail (IS_GNM_DATA_CACHE_SOURCE (src));
}

GnmRange const	*
gnm_data_cache_source_get_range (GnmDataCacheSource const *src)
{
	g_return_val_if_fail (IS_GNM_DATA_CACHE_SOURCE (src), NULL);
	return &src->src_range;
}

void
gnm_data_cache_source_set_range (GnmDataCacheSource *src, GnmRange const *r)
{
	g_return_if_fail (IS_GNM_DATA_CACHE_SOURCE (src));
	src->src_range = *r;
}

char const *
gnm_data_cache_source_get_name  (GnmDataCacheSource const *src)
{
	g_return_val_if_fail (IS_GNM_DATA_CACHE_SOURCE (src), NULL);
	return src->src_name ? src->src_name->str : NULL;
}

void
gnm_data_cache_source_set_name (GnmDataCacheSource *src, char const *name)
{
	GOString *new_val;

	g_return_if_fail (IS_GNM_DATA_CACHE_SOURCE (src));

	new_val = go_string_new (name);
	go_string_unref (src->src_name);
	src->src_name =  new_val;
}