ev-link-action.c 13.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* this file is part of evince, a gnome document viewer
 *
 *  Copyright (C) 2006 Carlos Garcia Campos <carlosgc@gnome.org>
 *  Copyright (C) 2005 Red Hat, Inc.
 *
 * Evince 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.
 *
 * Evince 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
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 20
 */

21
#include <config.h>
22
#include "ev-link-action.h"
23
#include "ev-document-type-builtins.h"
24 25 26 27 28 29 30

enum {
	PROP_0,
	PROP_TYPE,
	PROP_DEST,
	PROP_URI,
	PROP_FILENAME,
31
	PROP_PARAMS,
32 33 34 35
	PROP_NAME,
	PROP_SHOW_LIST,
	PROP_HIDE_LIST,
	PROP_TOGGLE_LIST
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
};

struct _EvLinkAction {
	GObject base_instance;

	EvLinkActionPrivate *priv;
};

struct _EvLinkActionClass {
	GObjectClass base_class;
};

struct _EvLinkActionPrivate {
	EvLinkActionType  type;
	EvLinkDest       *dest;
	gchar            *uri;
	gchar            *filename;
	gchar            *params;
54
	gchar            *name;
55 56 57
	GList            *show_list;
	GList            *hide_list;
	GList            *toggle_list;
58 59
};

60
G_DEFINE_TYPE_WITH_PRIVATE (EvLinkAction, ev_link_action, G_TYPE_OBJECT)
61 62 63 64 65 66 67 68 69

EvLinkActionType
ev_link_action_get_action_type (EvLinkAction *self)
{
	g_return_val_if_fail (EV_IS_LINK_ACTION (self), 0);

	return self->priv->type;
}

70 71 72 73 74 75
/**
 * ev_link_action_get_dest:
 * @self: an #EvLinkAction
 *
 * Returns: (transfer none): an #EvLinkDest
 */
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
EvLinkDest *
ev_link_action_get_dest (EvLinkAction *self)
{
	g_return_val_if_fail (EV_IS_LINK_ACTION (self), NULL);

	return self->priv->dest;
}

const gchar *
ev_link_action_get_uri (EvLinkAction *self)
{
	g_return_val_if_fail (EV_IS_LINK_ACTION (self), NULL);

	return self->priv->uri;
}

const gchar *
ev_link_action_get_filename (EvLinkAction *self)
{
	g_return_val_if_fail (EV_IS_LINK_ACTION (self), NULL);

	return self->priv->filename;
}

const gchar *
ev_link_action_get_params (EvLinkAction *self)
{
	g_return_val_if_fail (EV_IS_LINK_ACTION (self), NULL);

	return self->priv->params;
}

108 109 110 111 112 113 114 115
const gchar *
ev_link_action_get_name (EvLinkAction *self)
{
	g_return_val_if_fail (EV_IS_LINK_ACTION (self), NULL);

	return self->priv->name;
}

116 117 118 119 120 121
/**
 * ev_link_action_get_show_list:
 * @self: an #EvLinkAction
 *
 * Returns: (transfer none) (element-type EvLayer): a list of #EvLayer objects
 */
122 123 124 125 126 127 128 129
GList *
ev_link_action_get_show_list (EvLinkAction *self)
{
	g_return_val_if_fail (EV_IS_LINK_ACTION (self), NULL);

	return self->priv->show_list;
}

130 131 132 133 134 135
/**
 * ev_link_action_get_hide_list:
 * @self: an #EvLinkAction
 *
 * Returns: (transfer none) (element-type EvLayer): a list of #EvLayer objects
 */
136 137 138 139 140 141 142 143
GList *
ev_link_action_get_hide_list (EvLinkAction *self)
{
	g_return_val_if_fail (EV_IS_LINK_ACTION (self), NULL);

	return self->priv->hide_list;
}

144 145 146 147 148 149
/**
 * ev_link_action_get_toggle_list:
 * @self: an #EvLinkAction
 *
 * Returns: (transfer none) (element-type EvLayer): a list of #EvLayer objects
 */
150 151 152 153 154 155 156 157
GList *
ev_link_action_get_toggle_list (EvLinkAction *self)
{
	g_return_val_if_fail (EV_IS_LINK_ACTION (self), NULL);

	return self->priv->toggle_list;
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
static void
ev_link_action_get_property (GObject    *object,
			     guint       prop_id,
			     GValue     *value,
			     GParamSpec *param_spec)
{
	EvLinkAction *self;

	self = EV_LINK_ACTION (object);

	switch (prop_id) {
	        case PROP_TYPE:
		        g_value_set_enum (value, self->priv->type);
		        break;
	        case PROP_DEST:
173
		        g_value_set_object (value, self->priv->dest);
174 175 176 177 178 179 180 181 182 183
			break;
	        case PROP_URI:
			g_value_set_string (value, self->priv->uri);
			break;
	        case PROP_FILENAME:
			g_value_set_string (value, self->priv->filename);
			break;
	        case PROP_PARAMS:
			g_value_set_string (value, self->priv->params);
			break;
184 185 186
	        case PROP_NAME:
			g_value_set_string (value, self->priv->name);
			break;
187 188 189 190 191 192 193 194 195
	        case PROP_SHOW_LIST:
			g_value_set_pointer (value, self->priv->show_list);
			break;
	        case PROP_HIDE_LIST:
			g_value_set_pointer (value, self->priv->hide_list);
			break;
	        case PROP_TOGGLE_LIST:
			g_value_set_pointer (value, self->priv->toggle_list);
			break;
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	        default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
							   prop_id,
							   param_spec);
			break;
	}
}

static void
ev_link_action_set_property (GObject      *object,
			     guint         prop_id,
			     const GValue *value,
			     GParamSpec   *param_spec)
{
	EvLinkAction *self = EV_LINK_ACTION (object);

	switch (prop_id) {
	        case PROP_TYPE:
			self->priv->type = g_value_get_enum (value);
			break;
	        case PROP_DEST:
217
			self->priv->dest = g_value_dup_object (value);
218 219 220 221 222 223 224 225 226 227 228 229 230
			break;
	        case PROP_URI:
			g_free (self->priv->uri);
			self->priv->uri = g_value_dup_string (value);
			break;
	        case PROP_FILENAME:
			g_free (self->priv->filename);
			self->priv->filename = g_value_dup_string (value);
			break;
	        case PROP_PARAMS:
			g_free (self->priv->params);
			self->priv->params = g_value_dup_string (value);
			break;
231 232 233 234
	        case PROP_NAME:
			g_free (self->priv->name);
			self->priv->name = g_value_dup_string (value);
			break;
235 236 237 238 239 240 241 242 243
	        case PROP_SHOW_LIST:
			self->priv->show_list = g_value_get_pointer (value);
			break;
	        case PROP_HIDE_LIST:
			self->priv->hide_list = g_value_get_pointer (value);
			break;
	        case PROP_TOGGLE_LIST:
			self->priv->toggle_list = g_value_get_pointer (value);
			break;
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	        default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
							   prop_id,
							   param_spec);
			break;
	}
}

static void
ev_link_action_finalize (GObject *object)
{
	EvLinkActionPrivate *priv;

	priv = EV_LINK_ACTION (object)->priv;

259
	g_clear_object (&priv->dest);
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

	if (priv->uri) {
		g_free (priv->uri);
		priv->uri = NULL;
	}

	if (priv->filename) {
		g_free (priv->filename);
		priv->filename = NULL;
	}

	if (priv->params) {
		g_free (priv->params);
		priv->params = NULL;
	}

276 277 278 279 280
	if (priv->name) {
		g_free (priv->name);
		priv->name = NULL;
	}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	if (priv->show_list) {
		g_list_foreach (priv->show_list, (GFunc)g_object_unref, NULL);
		g_list_free (priv->show_list);
		priv->show_list = NULL;
	}

	if (priv->hide_list) {
		g_list_foreach (priv->hide_list, (GFunc)g_object_unref, NULL);
		g_list_free (priv->hide_list);
		priv->hide_list = NULL;
	}

	if (priv->toggle_list) {
		g_list_foreach (priv->toggle_list, (GFunc)g_object_unref, NULL);
		g_list_free (priv->toggle_list);
		priv->toggle_list = NULL;
	}

299 300 301 302 303 304
	G_OBJECT_CLASS (ev_link_action_parent_class)->finalize (object);
}

static void
ev_link_action_init (EvLinkAction *ev_link_action)
{
305
	ev_link_action->priv = ev_link_action_get_instance_private (ev_link_action);
306 307 308 309 310

	ev_link_action->priv->dest = NULL;
	ev_link_action->priv->uri = NULL;
	ev_link_action->priv->filename = NULL;
	ev_link_action->priv->params = NULL;
311
	ev_link_action->priv->name = NULL;
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
}

static void
ev_link_action_class_init (EvLinkActionClass *ev_link_action_class)
{
	GObjectClass *g_object_class;

	g_object_class = G_OBJECT_CLASS (ev_link_action_class);

	g_object_class->set_property = ev_link_action_set_property;
	g_object_class->get_property = ev_link_action_get_property;

	g_object_class->finalize = ev_link_action_finalize;

	g_object_class_install_property (g_object_class,
					 PROP_TYPE,
					 g_param_spec_enum  ("type",
							     "Action Type",
							     "The link action type",
							     EV_TYPE_LINK_ACTION_TYPE,
							     EV_LINK_ACTION_TYPE_GOTO_DEST,
							     G_PARAM_READWRITE |
334 335
							     G_PARAM_CONSTRUCT_ONLY |
                                                             G_PARAM_STATIC_STRINGS));
336 337
	g_object_class_install_property (g_object_class,
					 PROP_DEST,
338 339 340 341 342 343 344
					 g_param_spec_object ("dest",
							      "Action destination",
							      "The link action destination",
							      EV_TYPE_LINK_DEST,
							      G_PARAM_READWRITE |
							      G_PARAM_CONSTRUCT_ONLY |
                                                              G_PARAM_STATIC_STRINGS));
345 346 347 348 349 350 351
	g_object_class_install_property (g_object_class,
					 PROP_URI,
					 g_param_spec_string ("uri",
							      "Link Action URI",
							      "The link action URI",
							      NULL,
							      G_PARAM_READWRITE |
352 353
							      G_PARAM_CONSTRUCT_ONLY |
                                                              G_PARAM_STATIC_STRINGS));
354 355 356 357 358 359 360
	g_object_class_install_property (g_object_class,
					 PROP_FILENAME,
					 g_param_spec_string ("filename",
							      "Filename",
							      "The link action filename",
							      NULL,
							      G_PARAM_READWRITE |
361 362
							      G_PARAM_CONSTRUCT_ONLY |
                                                              G_PARAM_STATIC_STRINGS));
363 364 365 366 367 368 369
	g_object_class_install_property (g_object_class,
					 PROP_PARAMS,
					 g_param_spec_string ("params",
							      "Params",
							      "The link action params",
							      NULL,
							      G_PARAM_READWRITE |
370 371
							      G_PARAM_CONSTRUCT_ONLY |
                                                              G_PARAM_STATIC_STRINGS));
372 373 374 375 376 377 378
	g_object_class_install_property (g_object_class,
					 PROP_NAME,
					 g_param_spec_string ("name",
							      "Name",
							      "The link action name",
							      NULL,
							      G_PARAM_READWRITE |
379 380
							      G_PARAM_CONSTRUCT_ONLY |
                                                              G_PARAM_STATIC_STRINGS));
381 382 383 384 385 386
	g_object_class_install_property (g_object_class,
					 PROP_SHOW_LIST,
					 g_param_spec_pointer ("show-list",
							       "ShowList",
							       "The list of layers that should be shown",
							       G_PARAM_READWRITE |
387 388
							       G_PARAM_CONSTRUCT_ONLY |
                                                               G_PARAM_STATIC_STRINGS));
389 390 391 392 393 394
	g_object_class_install_property (g_object_class,
					 PROP_HIDE_LIST,
					 g_param_spec_pointer ("hide-list",
							       "HideList",
							       "The list of layers that should be hidden",
							       G_PARAM_READWRITE |
395 396
							       G_PARAM_CONSTRUCT_ONLY |
                                                               G_PARAM_STATIC_STRINGS));
397 398 399 400 401 402
	g_object_class_install_property (g_object_class,
					 PROP_TOGGLE_LIST,
					 g_param_spec_pointer ("toggle-list",
							       "ToggleList",
							       "The list of layers that should be toggled",
							       G_PARAM_READWRITE |
403 404
							       G_PARAM_CONSTRUCT_ONLY |
                                                               G_PARAM_STATIC_STRINGS));
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
}

EvLinkAction *
ev_link_action_new_dest (EvLinkDest *dest)
{
	return EV_LINK_ACTION (g_object_new (EV_TYPE_LINK_ACTION,
					     "dest", dest,
					     "type", EV_LINK_ACTION_TYPE_GOTO_DEST,
					     NULL));
}

EvLinkAction *
ev_link_action_new_remote (EvLinkDest  *dest,
			   const gchar *filename)
{
	return EV_LINK_ACTION (g_object_new (EV_TYPE_LINK_ACTION,
					     "dest", dest,
					     "filename", filename,
					     "type", EV_LINK_ACTION_TYPE_GOTO_REMOTE,
					     NULL));
}

EvLinkAction *
ev_link_action_new_external_uri (const gchar *uri)
{
	return EV_LINK_ACTION (g_object_new (EV_TYPE_LINK_ACTION,
					     "uri", uri,
					     "type", EV_LINK_ACTION_TYPE_EXTERNAL_URI,
					     NULL));
}

EvLinkAction *
ev_link_action_new_launch (const gchar *filename,
			   const gchar *params)
{
	return EV_LINK_ACTION (g_object_new (EV_TYPE_LINK_ACTION,
					     "filename", filename,
					     "params", params,
					     "type", EV_LINK_ACTION_TYPE_LAUNCH,
					     NULL));
}
446 447 448 449 450 451 452 453 454

EvLinkAction *
ev_link_action_new_named (const gchar *name)
{
	return EV_LINK_ACTION (g_object_new (EV_TYPE_LINK_ACTION,
					     "name", name,
					     "type", EV_LINK_ACTION_TYPE_NAMED,
					     NULL));
}
455

456 457 458 459 460 461 462 463
/**
 * ev_link_action_new_layers_state:
 * @show_list: (element-type EvLayer): a list of #EvLayer objects
 * @hide_list: (element-type EvLayer): a list of #EvLayer objects
 * @toggle_list: (element-type EvLayer): a list of #EvLayer objects
 *
 * Returns: (transfer full): a new #EvLinkAction
 */
464 465 466 467 468 469 470 471 472 473 474 475
EvLinkAction *
ev_link_action_new_layers_state (GList *show_list,
				 GList *hide_list,
				 GList *toggle_list)
{
	return EV_LINK_ACTION (g_object_new (EV_TYPE_LINK_ACTION,
					     "show-list", show_list,
					     "hide-list", hide_list,
					     "toggle-list", toggle_list,
					     "type", EV_LINK_ACTION_TYPE_LAYERS_STATE,
					     NULL));
}
476

477 478 479 480 481 482 483 484 485 486 487
/**
 * ev_link_action_equal:
 * @a: a #EvLinkAction
 * @b: a #EvLinkAction
 *
 * Checks whether @a and @b are equal.
 *
 * Returns: %TRUE iff @a and @b are equal
 *
 * Since: 3.8
 */
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
gboolean
ev_link_action_equal (EvLinkAction *a,
                      EvLinkAction *b)
{
        g_return_val_if_fail (EV_IS_LINK_ACTION (a), FALSE);
        g_return_val_if_fail (EV_IS_LINK_ACTION (b), FALSE);

        if (a == b)
                return TRUE;

        if (a->priv->type != b->priv->type)
                return FALSE;

        switch (a->priv->type) {
        case EV_LINK_ACTION_TYPE_GOTO_DEST:
                return ev_link_dest_equal (a->priv->dest, b->priv->dest);

        case EV_LINK_ACTION_TYPE_GOTO_REMOTE:
                return ev_link_dest_equal (a->priv->dest, b->priv->dest) &&
                        !g_strcmp0 (a->priv->filename, b->priv->filename);

        case EV_LINK_ACTION_TYPE_EXTERNAL_URI:
                return !g_strcmp0 (a->priv->uri, b->priv->uri);

        case EV_LINK_ACTION_TYPE_LAUNCH:
                return !g_strcmp0 (a->priv->filename, b->priv->filename) &&
                        !g_strcmp0 (a->priv->params, b->priv->params);

        case EV_LINK_ACTION_TYPE_NAMED:
                return !g_strcmp0 (a->priv->name, b->priv->name);

        default:
                return FALSE;
        }

        return FALSE;
}