/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see
*
*
* Authors:
* Michael Zucchi
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
#ifdef HAVE_IMPORT_H
#include
#endif
#include
#include
#include
#include "e-import.h"
#include
#define d(x)
typedef struct _EImportImporters EImportImporters;
struct _EImportImporters {
EImportImporter *importer;
EImportImporterFunc free;
gpointer data;
};
static gpointer parent_class;
static void
import_finalize (GObject *object)
{
EImport *import = E_IMPORT (object);
g_free (import->id);
/* Chain up to parent's finalize () method. */
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
import_target_free (EImport *import,
EImportTarget *target)
{
switch (target->type) {
case E_IMPORT_TARGET_URI: {
EImportTargetURI *s = (EImportTargetURI *) target;
g_free (s->uri_src);
g_free (s->uri_dest);
break; }
default:
break;
}
g_datalist_clear (&target->data);
g_free (target);
g_object_unref (import);
}
static void
import_class_init (EImportClass *class)
{
GObjectClass *object_class;
parent_class = g_type_class_peek_parent (class);
object_class = G_OBJECT_CLASS (class);
object_class->finalize = import_finalize;
class->target_free = import_target_free;
}
/**
* e_import_get_type:
*
* Standard GObject method. Used to subclass for the concrete
* implementations.
*
* Return value: EImport type.
**/
GType
e_import_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (type == 0)) {
static const GTypeInfo type_info = {
sizeof (EImportClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) import_class_init,
(GClassFinalizeFunc) NULL,
NULL, /* class_data */
sizeof (EImport),
0, /* n_preallocs */
(GInstanceInitFunc) NULL,
NULL /* value_table */
};
type = g_type_register_static (
G_TYPE_OBJECT, "EImport", &type_info, 0);
}
return type;
}
/**
* e_import_construct:
* @ep: The instance to initialise.
* @id: The name of the instance.
*
* Used by implementing classes to initialise base parameters.
*
* Return value: @ep is returned.
**/
EImport *e_import_construct (EImport *ep, const gchar *id)
{
ep->id = g_strdup (id);
return ep;
}
EImport *
e_import_new (const gchar *id)
{
EImport *import;
import = g_object_new (E_TYPE_IMPORT, NULL);
return e_import_construct (import, id);
}
/**
* e_import_import:
* @import: an #EImport
* @t: Target to import.
* @im: Importer to use.
* @status: Status callback, called with progress information.
* @done: Complete callback, will always be called once complete.
* @data: user data for callback functions
*
* Run the import function of the selected importer. Once the
* importer has finished, it MUST call the e_import_complete ()
* function. This allows importers to run in synchronous or
* asynchronous mode.
*
* When complete, the @done callback will be called.
**/
void
e_import_import (EImport *import,
EImportTarget *t,
EImportImporter *im,
EImportStatusFunc status,
EImportCompleteFunc done,
gpointer data)
{
g_return_if_fail (im != NULL);
import->status = status;
import->done = done;
import->done_data = data;
im->import (import, t, im);
}
void
e_import_cancel (EImport *import,
EImportTarget *t,
EImportImporter *im)
{
if (im->cancel)
im->cancel (import, t, im);
}
/**
* e_import_get_widget:
* @import: an #EImport
* @target: Target of interest
* @im: Importer to get widget of
*
* Gets a widget that the importer uses to configure its
* destination. This widget should be packed into a container
* widget. It should not be shown_all.
*
* Return value: NULL if the importer doesn't support/require
* a destination.
**/
GtkWidget *
e_import_get_widget (EImport *import,
EImportTarget *target,
EImportImporter *im)
{
g_return_val_if_fail (im != NULL, NULL);
g_return_val_if_fail (target != NULL, NULL);
return im->get_widget (import, target, im);
}
/**
* e_import_complete:
* @import: an #EImport
* @target: Target just completed (unused currently)
*
* Signify that an import is complete. This must be called by
* importer implementations when they are done.
**/
void
e_import_complete (EImport *import,
EImportTarget *target)
{
if (import->done)
import->done (import, import->done_data);
}
void
e_import_status (EImport *import,
EImportTarget *target,
const gchar *what,
gint pc)
{
if (import->status)
import->status (import, what, pc, import->done_data);
}
/**
* e_import_get_importers:
* @emp: an #EImport
* @target: an #EImportTarget
*
* Get a list of importers. If @target is supplied, then only
* importers which support the type and location specified by the
* target are listed. If @target is NULL, then all importers are
* listed.
*
* Return value: A list of importers. The list should be freed when
* no longer needed.
**/
GSList *
e_import_get_importers (EImport *emp, EImportTarget *target)
{
GSList *importers = NULL;
GList *link;
link = E_IMPORT_GET_CLASS (emp)->importers;
while (link != NULL) {
EImportImporters *ei = link->data;
if (target == NULL
|| (ei->importer->type == target->type
&& ei->importer->supported (emp, target, ei->importer))) {
importers = g_slist_append (importers, ei->importer);
}
link = g_list_next (link);
}
return importers;
}
/* ********************************************************************** */
static gint
importer_compare (EImportImporters *node_a,
EImportImporters *node_b)
{
gint pri_a = node_a->importer->pri;
gint pri_b = node_b->importer->pri;
return (pri_a == pri_b) ? 0 : (pri_a < pri_b) ? -1 : 1;
}
/**
* e_import_class_add_importer:
* @ec: An initialised implementing instance of EImport.
* @importer: Importer to add.
* @freefunc: If supplied, called to free the importer node
* when it is no longer needed.
* @data: Data for the callback.
*
**/
void
e_import_class_add_importer (EImportClass *class,
EImportImporter *importer,
EImportImporterFunc freefunc,
gpointer data)
{
EImportImporters *node;
node = g_malloc (sizeof (*node));
node->importer = importer;
node->free = freefunc;
node->data = data;
class->importers = g_list_sort (
g_list_prepend (class->importers, node),
(GCompareFunc) importer_compare);
}
/**
* e_import_target_new:
* @ep: Parent EImport object.
* @type: type, up to implementor
* @size: Size of object to allocate.
*
* Allocate a new import target suitable for this class. Implementing
* classes will define the actual content of the target.
**/
gpointer
e_import_target_new (EImport *ep, gint type, gsize size)
{
EImportTarget *t;
if (size < sizeof (EImportTarget)) {
g_warning ("Size less than size of EImportTarget\n");
size = sizeof (EImportTarget);
}
t = g_malloc0 (size);
t->import = ep;
g_object_ref (ep);
t->type = type;
g_datalist_init (&t->data);
return t;
}
/**
* e_import_target_free:
* @ep: Parent EImport object.
* @o: The target to fre.
*
* Free a target. The implementing class can override this method to
* free custom targets.
**/
void
e_import_target_free (EImport *ep, gpointer o)
{
EImportTarget *t = o;
((EImportClass *)G_OBJECT_GET_CLASS (ep))->target_free (ep, t);
}
EImportTargetURI *
e_import_target_new_uri (EImport *import,
const gchar *uri_src,
const gchar *uri_dst)
{
EImportTargetURI *t;
t = e_import_target_new (import, E_IMPORT_TARGET_URI, sizeof (*t));
t->uri_src = g_strdup (uri_src);
t->uri_dest = g_strdup (uri_dst);
return t;
}
EImportTargetHome *
e_import_target_new_home (EImport *import)
{
return e_import_target_new (
import, E_IMPORT_TARGET_HOME, sizeof (EImportTargetHome));
}
/* ********************************************************************** */
/* Import menu plugin handler */
/*