seahorse-source.c 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Seahorse
 *
 * Copyright (C) 2004,2005 Stefan Walter
 *
 * 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
Daniel Mustieles's avatar
Daniel Mustieles committed
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25 26
 */
 
#include "config.h"

#include "seahorse-context.h"
#include "seahorse-marshal.h"
#include "seahorse-object.h"
#include "seahorse-source.h"
#include "seahorse-util.h"

27 28 29 30 31 32 33 34
/**
 * SECTION:seahorse-source
 * @short_description: This class stores and handles key sources
 * @include:seahorse-source.h
 *
 **/


35 36 37
/* ---------------------------------------------------------------------------------
 * INTERFACE
 */
38

39 40 41 42 43 44
/**
* gobject_class: The object class to init
*
* Adds the interfaces "source-tag" and "source-location"
*
**/
45
static void
46
seahorse_source_base_init (gpointer gobject_class)
47
{
48 49 50 51 52
	static gboolean initialized = FALSE;
	if (!initialized) {
		
		/* Add properties and signals to the interface */
		g_object_interface_install_property (gobject_class,
53
		        g_param_spec_uint ("source-tag", "Source Tag", "Tag of objects that come from this source.", 
54
		                           0, G_MAXUINT, SEAHORSE_TAG_INVALID, G_PARAM_READABLE));
55 56

		g_object_interface_install_property (gobject_class, 
57
		        g_param_spec_enum ("source-location", "Source Location", "Objects in this source are at this location. See SeahorseLocation", 
58
		                           SEAHORSE_TYPE_LOCATION, SEAHORSE_LOCATION_LOCAL, G_PARAM_READABLE));
59 60 61
		
		initialized = TRUE;
	}
62 63
}

64 65 66 67 68 69 70
/**
 * seahorse_source_get_type:
 *
 * Registers the type of G_TYPE_INTERFACE
 *
 * Returns: the type id
 */
71 72
GType
seahorse_source_get_type (void)
73
{
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	static GType type = 0;
	if (!type) {
		static const GTypeInfo info = {
			sizeof (SeahorseSourceIface),
			seahorse_source_base_init,               /* base init */
			NULL,             /* base finalize */
			NULL,             /* class_init */
			NULL,             /* class finalize */
			NULL,             /* class data */
			0,
			0,                /* n_preallocs */
			NULL,             /* instance init */
		};
		type = g_type_register_static (G_TYPE_INTERFACE, "SeahorseSourceIface", &info, 0);
		g_type_interface_add_prerequisite (type, G_TYPE_OBJECT);
	}
	
	return type;
92 93
}

94 95 96
/* ---------------------------------------------------------------------------------
 * PUBLIC
 */
97 98

/**
99
 * seahorse_source_load:
100
 * @sksrc: A #SeahorseSource object
101 102 103 104 105
 *
 * Refreshes the #SeahorseSource's internal object listing.
 *
 * Returns: the asynchronous refresh operation.
 **/
106
SeahorseOperation*
107
seahorse_source_load (SeahorseSource *sksrc)
108
{
109
    SeahorseSourceIface *klass;
110 111
    
    g_return_val_if_fail (SEAHORSE_IS_SOURCE (sksrc), NULL);
112
    klass = SEAHORSE_SOURCE_GET_INTERFACE (sksrc);
113 114
    g_return_val_if_fail (klass->load != NULL, NULL);
    
115
    return (*klass->load) (sksrc);
116 117 118
}

/**
119
 * seahorse_source_load_sync:
120 121 122
 * @sksrc: A #SeahorseSource object
 * 
 * Refreshes the #SeahorseSource's internal object listing, and waits for it to complete.
123
 **/
124
void
125
seahorse_source_load_sync (SeahorseSource *sksrc)
126
{
127
    SeahorseOperation *op = seahorse_source_load (sksrc);
128 129 130 131 132 133
    g_return_if_fail (op != NULL);
    seahorse_operation_wait (op);
    g_object_unref (op);
}

/**
134
 * seahorse_source_load_sync:
135 136 137
 * @sksrc: A #SeahorseSource object
 * 
 * Refreshes the #SeahorseSource's internal object listing. Completes in the background.
138
 **/
139
void
140
seahorse_source_load_async (SeahorseSource *sksrc)
141
{
142
    SeahorseOperation *op = seahorse_source_load (sksrc);
143 144 145 146 147
    g_return_if_fail (op != NULL);
    g_object_unref (op);
}

/**
148
 * seahorse_source_search:
149 150 151 152 153
 * @sksrc: A #SeahorseSource object
 * @match: Text to search for
 * 
 * Refreshes the #SeahorseSource's internal listing. 
 * 
154
 * Returns: the asynchronous refresh operation.
155
 **/
156 157 158
SeahorseOperation*
seahorse_source_search (SeahorseSource *sksrc, const gchar *match)
{
159
    SeahorseSourceIface *klass;
160 161
    
    g_return_val_if_fail (SEAHORSE_IS_SOURCE (sksrc), NULL);
162
    klass = SEAHORSE_SOURCE_GET_INTERFACE (sksrc);
163 164 165 166 167
    g_return_val_if_fail (klass->search != NULL, NULL);
    
    return (*klass->search) (sksrc, match);
}

168 169 170 171 172 173 174 175 176
/**
 * seahorse_source_import:
 * @sksrc: A #SeahorseSource object
 * @input: A stream of data to import
 *
 * Imports data from the stream
 *
 * Returns: the asynchronous import operation
 */
177 178 179
SeahorseOperation* 
seahorse_source_import (SeahorseSource *sksrc, GInputStream *input)
{
180
	SeahorseSourceIface *klass;
181 182 183 184
    
	g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
    
	g_return_val_if_fail (SEAHORSE_IS_SOURCE (sksrc), NULL);
185
	klass = SEAHORSE_SOURCE_GET_INTERFACE (sksrc);   
186 187 188 189 190
	g_return_val_if_fail (klass->import != NULL, NULL);
    
	return (*klass->import) (sksrc, input);  
}

191 192 193 194 195 196 197 198 199 200
/**
 * seahorse_source_import_sync:
 * @sksrc: The #SeahorseSource
 * @input: the input data
 * @err: error
 *
 * Imports data from the stream
 *
 * Returns: Imports the stream, synchronous
 */
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
gboolean            
seahorse_source_import_sync (SeahorseSource *sksrc, GInputStream *input,
                             GError **err)
{
	SeahorseOperation *op;
    	gboolean ret;

	g_return_val_if_fail (G_IS_INPUT_STREAM (input), FALSE);

	op = seahorse_source_import (sksrc, input);
	g_return_val_if_fail (op != NULL, FALSE);
    
	seahorse_operation_wait (op);
	ret = seahorse_operation_is_successful (op);
	if (!ret)
		seahorse_operation_copy_error (op, err);
    
	g_object_unref (op);
	return ret;    
}

222 223 224 225 226 227 228 229 230
/**
 * seahorse_source_export_objects:
 * @objects: The objects to export
 * @output: The output stream to export the objects to
 *
 * Exports objects. The objects are sorted by source.
 *
 * Returns: The #SeahorseOperation created to export the data
 */
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 258 259 260 261 262 263 264 265
SeahorseOperation*
seahorse_source_export_objects (GList *objects, GOutputStream *output)
{
    SeahorseOperation *op = NULL;
    SeahorseMultiOperation *mop = NULL;
    SeahorseSource *sksrc;
    SeahorseObject *sobj;
    GList *next;
    
	g_return_val_if_fail (G_IS_OUTPUT_STREAM (output), NULL);
	g_object_ref (output);

    /* Sort by object source */
    objects = g_list_copy (objects);
    objects = seahorse_util_objects_sort (objects);
    
    while (objects) {
     
        /* Break off one set (same source) */
        next = seahorse_util_objects_splice (objects);

        g_assert (SEAHORSE_IS_OBJECT (objects->data));
        sobj = SEAHORSE_OBJECT (objects->data);

        /* Export from this object source */        
        sksrc = seahorse_object_get_source (sobj);
        g_return_val_if_fail (sksrc != NULL, FALSE);
        
        if (op != NULL) {
            if (mop == NULL)
                mop = seahorse_multi_operation_new ();
            seahorse_multi_operation_take (mop, op);
        }
        
        /* We pass our own data object, to which data is appended */
266
        op = seahorse_source_export (sksrc, objects, output);
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
        g_return_val_if_fail (op != NULL, FALSE);

        g_list_free (objects);
        objects = next;
    }
    
    if (mop) {
        op = SEAHORSE_OPERATION (mop);
        
        /* 
         * Setup the result data properly, as we would if it was a 
         * single export operation.
         */
        seahorse_operation_mark_result (op, output, g_object_unref);
    }
    
    if (!op) 
        op = seahorse_operation_new_complete (NULL);
    
    return op;
}

289 290 291 292 293 294 295 296 297 298
/**
 * seahorse_source_export:
 * @sksrc: The SeahorseSource
 * @objects: The objects to export
 * @output: The resulting output stream
 *
 *
 *
 * Returns: An export Operation (#SeahorseOperation)
 */
299
SeahorseOperation* 
300
seahorse_source_export (SeahorseSource *sksrc, GList *objects, GOutputStream *output)
301
{
302
	SeahorseSourceIface *klass;
303 304 305 306 307 308 309
	SeahorseOperation *op;
	GSList *ids = NULL;
	GList *l;
    
	g_return_val_if_fail (SEAHORSE_IS_SOURCE (sksrc), NULL);
	g_return_val_if_fail (G_IS_OUTPUT_STREAM (output), NULL);
	
310
	klass = SEAHORSE_SOURCE_GET_INTERFACE (sksrc);   
311
	if (klass->export)
312
		return (*klass->export) (sksrc, objects, output);
313 314 315 316 317 318 319 320 321 322 323 324 325

	/* Either export or export_raw must be implemented */
	g_return_val_if_fail (klass->export_raw != NULL, NULL);
    
	for (l = objects; l; l = g_list_next (l)) 
		ids = g_slist_prepend (ids, GUINT_TO_POINTER (seahorse_object_get_id (l->data)));
    
	ids = g_slist_reverse (ids);
	op = (*klass->export_raw) (sksrc, ids, output);	
	g_slist_free (ids);
	return op;
}

326 327 328 329 330 331 332 333 334 335
/**
 * seahorse_source_export_raw:
 * @sksrc: The SeahorseSource
 * @ids: A list of IDs to export
 * @output: The resulting output stream
 *
 *
 *
 * Returns: An export Operation (#SeahorseOperation)
 */
336 337 338
SeahorseOperation* 
seahorse_source_export_raw (SeahorseSource *sksrc, GSList *ids, GOutputStream *output)
{
339
	SeahorseSourceIface *klass;
340 341 342 343 344 345 346 347
	SeahorseOperation *op;
	SeahorseObject *sobj;
	GList *objects = NULL;
	GSList *l;
    
	g_return_val_if_fail (SEAHORSE_IS_SOURCE (sksrc), NULL);
	g_return_val_if_fail (output == NULL || G_IS_OUTPUT_STREAM (output), NULL);

348
	klass = SEAHORSE_SOURCE_GET_INTERFACE (sksrc);   
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    
	/* Either export or export_raw must be implemented */
	if (klass->export_raw)
		return (*klass->export_raw)(sksrc, ids, output);
    
	g_return_val_if_fail (klass->export != NULL, NULL);
        
	for (l = ids; l; l = g_slist_next (l)) {
		sobj = seahorse_context_get_object (SCTX_APP (), sksrc, GPOINTER_TO_UINT (l->data));
        
		/* TODO: A proper error message here 'not found' */
		if (sobj)
			objects = g_list_prepend (objects, sobj);
	}
    
	objects = g_list_reverse (objects);
365
	op = (*klass->export) (sksrc, objects, output);
366 367 368
	g_list_free (objects);
	return op;
}
369 370 371 372 373 374 375 376

/**
* seahorse_source_get_tag:
* @sksrc: The seahorse source object
*
*
* Returns: The source-tag property of the object. As #GQuark
*/
377
GQuark              
378
seahorse_source_get_tag (SeahorseSource *sksrc)
379 380
{
    GQuark ktype;
381
    g_object_get (sksrc, "source-tag", &ktype, NULL);
382 383 384
    return ktype;
}

385 386 387 388 389 390 391 392
/**
 * seahorse_source_get_location:
 * @sksrc: The seahorse source object
 *
 *
 *
 * Returns: The location (#SeahorseLocation) of this object
 */
393 394 395 396
SeahorseLocation   
seahorse_source_get_location (SeahorseSource *sksrc)
{
    SeahorseLocation loc;
397
    g_object_get (sksrc, "source-location", &loc, NULL);
398 399
    return loc;
}