/* Generated by GOB (v2.0.15)   (do not edit directly) */

/* End world hunger, donate to the World Food Programme, http://www.wfp.org */

#define GOB_VERSION_MAJOR 2
#define GOB_VERSION_MINOR 0
#define GOB_VERSION_PATCHLEVEL 15

#define selfp (self->_priv)

#include <string.h> /* memset() */

#include "mn-evolution-mailbox.h"

#include "mn-evolution-mailbox-private.h"

#ifdef G_LIKELY
#define ___GOB_LIKELY(expr) G_LIKELY(expr)
#define ___GOB_UNLIKELY(expr) G_UNLIKELY(expr)
#else /* ! G_LIKELY */
#define ___GOB_LIKELY(expr) (expr)
#define ___GOB_UNLIKELY(expr) (expr)
#endif /* G_LIKELY */

#line 29 "src/mn-evolution-mailbox.gob"

#include <stdarg.h>
#include <gobject/gvaluecollector.h>
#include <glib/gi18n.h>
#include "mn-evolution.h"
#include "mn-mailbox-private.h"
#include "mn-message.h"
#include "mn-util.h"
#include "mn-stock.h"
#include "mn-evolution-message.h"
#include "mn-shell.h"
#include "mn-evolution-client-dbus.h"

typedef struct
{
  const char	*uid;
  unsigned int	sent_time;
  unsigned int	received_time;
  const char	*id;
  const char	*from;
  const char	*subject;
} MessageInfo;

#line 50 "mn-evolution-mailbox.c"
/* self casting macros */
#define SELF(x) MN_EVOLUTION_MAILBOX(x)
#define SELF_CONST(x) MN_EVOLUTION_MAILBOX_CONST(x)
#define IS_SELF(x) MN_IS_EVOLUTION_MAILBOX(x)
#define TYPE_SELF MN_TYPE_EVOLUTION_MAILBOX
#define SELF_CLASS(x) MN_EVOLUTION_MAILBOX_CLASS(x)

#define SELF_GET_CLASS(x) MN_EVOLUTION_MAILBOX_GET_CLASS(x)

/* self typedefs */
typedef MNEvolutionMailbox Self;
typedef MNEvolutionMailboxClass SelfClass;

/* here are local prototypes */
static void ___object_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec);
static void ___object_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec);
#line 71 "src/mn-evolution-mailbox.gob"
static void mn_evolution_mailbox_class_init (MNEvolutionMailboxClass * class);
#line 69 "mn-evolution-mailbox.c"
#line 76 "src/mn-evolution-mailbox.gob"
static void mn_evolution_mailbox_init (MNEvolutionMailbox * self);
#line 72 "mn-evolution-mailbox.c"
#line 85 "src/mn-evolution-mailbox.gob"
static void ___3_mn_evolution_mailbox_seal (MNMailbox * mailbox);
#line 75 "mn-evolution-mailbox.c"
#line 94 "src/mn-evolution-mailbox.gob"
static void mn_evolution_mailbox_finalize (MNEvolutionMailbox * self);
#line 78 "mn-evolution-mailbox.c"
#line 110 "src/mn-evolution-mailbox.gob"
static void mn_evolution_mailbox_connect_proxy_signals (MNEvolutionMailbox * self);
#line 81 "mn-evolution-mailbox.c"
#line 133 "src/mn-evolution-mailbox.gob"
static void mn_evolution_mailbox_disconnect_proxy_signals (MNEvolutionMailbox * self);
#line 84 "mn-evolution-mailbox.c"
#line 156 "src/mn-evolution-mailbox.gob"
static void mn_evolution_mailbox_folder_changed_h (DBusGProxy * proxy, const char * uri, gpointer user_data);
#line 87 "mn-evolution-mailbox.c"
#line 169 "src/mn-evolution-mailbox.gob"
static void mn_evolution_mailbox_message_reading_h (DBusGProxy * proxy, const char * uri, gpointer user_data);
#line 90 "mn-evolution-mailbox.c"
#line 184 "src/mn-evolution-mailbox.gob"
static void mn_evolution_mailbox_value_array_get_values (GValueArray * value_array, ...);
#line 93 "mn-evolution-mailbox.c"
#line 204 "src/mn-evolution-mailbox.gob"
static void ___a_mn_evolution_mailbox_check (MNMailbox * mailbox);
#line 96 "mn-evolution-mailbox.c"
#line 241 "src/mn-evolution-mailbox.gob"
static void mn_evolution_mailbox_get_unseen_messages_cb (DBusGProxy * proxy, GPtrArray * messages_array, GError * err, gpointer user_data);
#line 99 "mn-evolution-mailbox.c"

enum {
	PROP_0,
	PROP_URI,
	PROP_FOLDER_NAME
};

/* pointer to the class of our parent */
static MNMailboxClass *parent_class = NULL;

/* Short form macros */
#define self_connect_proxy_signals mn_evolution_mailbox_connect_proxy_signals
#define self_disconnect_proxy_signals mn_evolution_mailbox_disconnect_proxy_signals
#define self_folder_changed_h mn_evolution_mailbox_folder_changed_h
#define self_message_reading_h mn_evolution_mailbox_message_reading_h
#define self_value_array_get_values mn_evolution_mailbox_value_array_get_values
#define self_get_unseen_messages_cb mn_evolution_mailbox_get_unseen_messages_cb
GType
mn_evolution_mailbox_get_type (void)
{
	static GType type = 0;

	if ___GOB_UNLIKELY(type == 0) {
		static const GTypeInfo info = {
			sizeof (MNEvolutionMailboxClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) mn_evolution_mailbox_class_init,
			(GClassFinalizeFunc) NULL,
			NULL /* class_data */,
			sizeof (MNEvolutionMailbox),
			0 /* n_preallocs */,
			(GInstanceInitFunc) mn_evolution_mailbox_init,
			NULL
		};

		type = g_type_register_static (MN_TYPE_MAILBOX, "MNEvolutionMailbox", &info, (GTypeFlags)0);
	}

	return type;
}

/* a macro for creating a new object of our type */
#define GET_NEW ((MNEvolutionMailbox *)g_object_new(mn_evolution_mailbox_get_type(), NULL))

/* a function for creating a new object of our type */
#include <stdarg.h>
static MNEvolutionMailbox * GET_NEW_VARG (const char *first, ...) G_GNUC_UNUSED;
static MNEvolutionMailbox *
GET_NEW_VARG (const char *first, ...)
{
	MNEvolutionMailbox *ret;
	va_list ap;
	va_start (ap, first);
	ret = (MNEvolutionMailbox *)g_object_new_valist (mn_evolution_mailbox_get_type (), first, ap);
	va_end (ap);
	return ret;
}


static void
___finalize(GObject *obj_self)
{
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::finalize"
	MNEvolutionMailbox *self G_GNUC_UNUSED = MN_EVOLUTION_MAILBOX (obj_self);
	gpointer priv G_GNUC_UNUSED = self->_priv;
#line 94 "src/mn-evolution-mailbox.gob"
	mn_evolution_mailbox_finalize (self);
#line 168 "mn-evolution-mailbox.c"
	if(G_OBJECT_CLASS(parent_class)->finalize) \
		(* G_OBJECT_CLASS(parent_class)->finalize)(obj_self);
#line 55 "src/mn-evolution-mailbox.gob"
	if(self->uri) { g_free ((gpointer) self->uri); self->uri = NULL; }
#line 173 "mn-evolution-mailbox.c"
#line 62 "src/mn-evolution-mailbox.gob"
	if(self->folder_name) { g_free ((gpointer) self->folder_name); self->folder_name = NULL; }
#line 176 "mn-evolution-mailbox.c"
}
#undef __GOB_FUNCTION__

#line 71 "src/mn-evolution-mailbox.gob"
static void 
mn_evolution_mailbox_class_init (MNEvolutionMailboxClass * class G_GNUC_UNUSED)
{
#line 184 "mn-evolution-mailbox.c"
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::class_init"
	GObjectClass *g_object_class G_GNUC_UNUSED = (GObjectClass*) class;
	MNMailboxClass *mn_mailbox_class = (MNMailboxClass *)class;

	g_type_class_add_private(class,sizeof(MNEvolutionMailboxPrivate));

	parent_class = g_type_class_ref (MN_TYPE_MAILBOX);

#line 85 "src/mn-evolution-mailbox.gob"
	mn_mailbox_class->seal = ___3_mn_evolution_mailbox_seal;
#line 204 "src/mn-evolution-mailbox.gob"
	mn_mailbox_class->check = ___a_mn_evolution_mailbox_check;
#line 197 "mn-evolution-mailbox.c"
	g_object_class->finalize = ___finalize;
	g_object_class->get_property = ___object_get_property;
	g_object_class->set_property = ___object_set_property;
    {
	GParamSpec   *param_spec;

	param_spec = g_param_spec_string
		("uri" /* name */,
		 NULL /* nick */,
		 NULL /* blurb */,
		 NULL /* default_value */,
		 (GParamFlags)(G_PARAM_READABLE | G_PARAM_WRITABLE | MN_MAILBOX_PARAM_REQUIRED | MN_MAILBOX_PARAM_LOAD_SAVE));
	g_object_class_install_property (g_object_class,
		PROP_URI,
		param_spec);
	param_spec = g_param_spec_string
		("folder_name" /* name */,
		 NULL /* nick */,
		 NULL /* blurb */,
		 NULL /* default_value */,
		 (GParamFlags)(G_PARAM_READABLE | G_PARAM_WRITABLE | MN_MAILBOX_PARAM_REQUIRED | MN_MAILBOX_PARAM_LOAD_SAVE));
	g_object_class_install_property (g_object_class,
		PROP_FOLDER_NAME,
		param_spec);
    }
 {
#line 72 "src/mn-evolution-mailbox.gob"

    MN_MAILBOX_CLASS(class)->type = "evolution";
  
#line 228 "mn-evolution-mailbox.c"
 }
}
#undef __GOB_FUNCTION__
#line 76 "src/mn-evolution-mailbox.gob"
static void 
mn_evolution_mailbox_init (MNEvolutionMailbox * self G_GNUC_UNUSED)
{
#line 236 "mn-evolution-mailbox.c"
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::init"
	self->_priv = G_TYPE_INSTANCE_GET_PRIVATE(self,MN_TYPE_EVOLUTION_MAILBOX,MNEvolutionMailboxPrivate);
 {
#line 77 "src/mn-evolution-mailbox.gob"

    mn_mailbox_set_format(MN_MAILBOX(self), "Evolution");
    mn_mailbox_set_stock_id(MN_MAILBOX(self), MN_STOCK_EVOLUTION_MAILBOX);

    /* we receive notifications from Evolution, no need to poll */
    mn_mailbox_set_poll(MN_MAILBOX(self), FALSE);
  
#line 248 "mn-evolution-mailbox.c"
 }
}
#undef __GOB_FUNCTION__

static void
___object_set_property (GObject *object,
	guint property_id,
	const GValue *VAL G_GNUC_UNUSED,
	GParamSpec *pspec G_GNUC_UNUSED)
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::set_property"
{
	MNEvolutionMailbox *self G_GNUC_UNUSED;

	self = MN_EVOLUTION_MAILBOX (object);

	switch (property_id) {
	case PROP_URI:
		{
#line 56 "src/mn-evolution-mailbox.gob"
{ char *old = self->uri; self->uri = g_value_dup_string (VAL); g_free (old); }
#line 269 "mn-evolution-mailbox.c"
		}
		break;
	case PROP_FOLDER_NAME:
		{
#line 63 "src/mn-evolution-mailbox.gob"
{ char *old = self->folder_name; self->folder_name = g_value_dup_string (VAL); g_free (old); }
#line 276 "mn-evolution-mailbox.c"
		}
		break;
	default:
/* Apparently in g++ this is needed, glib is b0rk */
#ifndef __PRETTY_FUNCTION__
#  undef G_STRLOC
#  define G_STRLOC	__FILE__ ":" G_STRINGIFY (__LINE__)
#endif
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}
#undef __GOB_FUNCTION__

static void
___object_get_property (GObject *object,
	guint property_id,
	GValue *VAL G_GNUC_UNUSED,
	GParamSpec *pspec G_GNUC_UNUSED)
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::get_property"
{
	MNEvolutionMailbox *self G_GNUC_UNUSED;

	self = MN_EVOLUTION_MAILBOX (object);

	switch (property_id) {
	case PROP_URI:
		{
#line 56 "src/mn-evolution-mailbox.gob"
g_value_set_string (VAL, self->uri);
#line 307 "mn-evolution-mailbox.c"
		}
		break;
	case PROP_FOLDER_NAME:
		{
#line 63 "src/mn-evolution-mailbox.gob"
g_value_set_string (VAL, self->folder_name);
#line 314 "mn-evolution-mailbox.c"
		}
		break;
	default:
/* Apparently in g++ this is needed, glib is b0rk */
#ifndef __PRETTY_FUNCTION__
#  undef G_STRLOC
#  define G_STRLOC	__FILE__ ":" G_STRINGIFY (__LINE__)
#endif
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}
#undef __GOB_FUNCTION__



#line 85 "src/mn-evolution-mailbox.gob"
static void 
___3_mn_evolution_mailbox_seal (MNMailbox * mailbox G_GNUC_UNUSED)
#line 334 "mn-evolution-mailbox.c"
#define PARENT_HANDLER(___mailbox) \
	{ if(MN_MAILBOX_CLASS(parent_class)->seal) \
		(* MN_MAILBOX_CLASS(parent_class)->seal)(___mailbox); }
{
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::seal"
{
#line 87 "src/mn-evolution-mailbox.gob"
	
    PARENT_HANDLER(mailbox);

    if (! mailbox->runtime_name)
      mailbox->runtime_name = g_strdup(SELF(mailbox)->folder_name);
  }}
#line 348 "mn-evolution-mailbox.c"
#undef __GOB_FUNCTION__
#undef PARENT_HANDLER

#line 94 "src/mn-evolution-mailbox.gob"
static void 
mn_evolution_mailbox_finalize (MNEvolutionMailbox * self)
{
#line 356 "mn-evolution-mailbox.c"
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::finalize"
{
#line 95 "src/mn-evolution-mailbox.gob"
	
    if (selfp->client)
      {
	if (selfp->pending_call)
	  {
	    g_assert(selfp->client->proxy != NULL);
	    dbus_g_proxy_cancel_call(selfp->client->proxy, selfp->pending_call);
	  }

	self_disconnect_proxy_signals(self);

	g_signal_handlers_disconnect_by_func(selfp->client, mn_mailbox_check, self);
      }
  }}
#line 374 "mn-evolution-mailbox.c"
#undef __GOB_FUNCTION__

#line 110 "src/mn-evolution-mailbox.gob"
static void 
mn_evolution_mailbox_connect_proxy_signals (MNEvolutionMailbox * self)
{
#line 381 "mn-evolution-mailbox.c"
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::connect_proxy_signals"
#line 110 "src/mn-evolution-mailbox.gob"
	g_return_if_fail (self != NULL);
#line 110 "src/mn-evolution-mailbox.gob"
	g_return_if_fail (MN_IS_EVOLUTION_MAILBOX (self));
#line 387 "mn-evolution-mailbox.c"
{
#line 112 "src/mn-evolution-mailbox.gob"
	
    g_return_if_fail(selfp->client != NULL);
    g_return_if_fail(selfp->client->proxy != NULL);

    if (selfp->signals_connected)
      return;

    dbus_g_proxy_connect_signal(selfp->client->proxy,
				MN_EVOLUTION_SERVER_SIGNAL_FOLDER_CHANGED,
				G_CALLBACK(self_folder_changed_h),
				self,
				NULL);
    dbus_g_proxy_connect_signal(selfp->client->proxy,
				MN_EVOLUTION_SERVER_SIGNAL_MESSAGE_READING,
				G_CALLBACK(self_message_reading_h),
				self,
				NULL);

    selfp->signals_connected = TRUE;
  }}
#line 410 "mn-evolution-mailbox.c"
#undef __GOB_FUNCTION__

#line 133 "src/mn-evolution-mailbox.gob"
static void 
mn_evolution_mailbox_disconnect_proxy_signals (MNEvolutionMailbox * self)
{
#line 417 "mn-evolution-mailbox.c"
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::disconnect_proxy_signals"
#line 133 "src/mn-evolution-mailbox.gob"
	g_return_if_fail (self != NULL);
#line 133 "src/mn-evolution-mailbox.gob"
	g_return_if_fail (MN_IS_EVOLUTION_MAILBOX (self));
#line 423 "mn-evolution-mailbox.c"
{
#line 135 "src/mn-evolution-mailbox.gob"
	
    g_return_if_fail(selfp->client != NULL);

    if (! selfp->signals_connected)
      return;

    selfp->signals_connected = FALSE;

    if (! selfp->client->proxy)
      return;

    dbus_g_proxy_disconnect_signal(selfp->client->proxy,
				   MN_EVOLUTION_SERVER_SIGNAL_FOLDER_CHANGED,
				   G_CALLBACK(self_folder_changed_h),
				   self);
    dbus_g_proxy_disconnect_signal(selfp->client->proxy,
				   MN_EVOLUTION_SERVER_SIGNAL_MESSAGE_READING,
				   G_CALLBACK(self_message_reading_h),
				   self);
  }}
#line 446 "mn-evolution-mailbox.c"
#undef __GOB_FUNCTION__

#line 156 "src/mn-evolution-mailbox.gob"
static void 
mn_evolution_mailbox_folder_changed_h (DBusGProxy * proxy, const char * uri, gpointer user_data)
{
#line 453 "mn-evolution-mailbox.c"
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::folder_changed_h"
{
#line 158 "src/mn-evolution-mailbox.gob"
	
    Self *self = user_data;

    if (! strcmp(uri, self->uri))
      {
	GDK_THREADS_ENTER();
	mn_mailbox_check(MN_MAILBOX(self));
	GDK_THREADS_LEAVE();
      }
  }}
#line 467 "mn-evolution-mailbox.c"
#undef __GOB_FUNCTION__

#line 169 "src/mn-evolution-mailbox.gob"
static void 
mn_evolution_mailbox_message_reading_h (DBusGProxy * proxy, const char * uri, gpointer user_data)
{
#line 474 "mn-evolution-mailbox.c"
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::message_reading_h"
{
#line 171 "src/mn-evolution-mailbox.gob"
	
    Self *self = user_data;

    if (! strcmp(uri, self->uri))
      {
	selfp->last_browsed = mn_time();

	GDK_THREADS_ENTER();
	mn_mailbox_check(MN_MAILBOX(self));
	GDK_THREADS_LEAVE();
      }
  }}
#line 490 "mn-evolution-mailbox.c"
#undef __GOB_FUNCTION__

#line 184 "src/mn-evolution-mailbox.gob"
static void 
mn_evolution_mailbox_value_array_get_values (GValueArray * value_array, ...)
{
#line 497 "mn-evolution-mailbox.c"
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::value_array_get_values"
#line 184 "src/mn-evolution-mailbox.gob"
	g_return_if_fail (value_array != NULL);
#line 501 "mn-evolution-mailbox.c"
{
#line 186 "src/mn-evolution-mailbox.gob"
	
    va_list args;
    int i;

    va_start(args, value_array);

    for (i = 0; i < value_array->n_values; i++)
      {
	GValue *value = g_value_array_get_nth(value_array, i);
	char *error = NULL;

	G_VALUE_LCOPY(value, args, G_VALUE_NOCOPY_CONTENTS, &error);
	g_assert(error == NULL);
      }

    va_end(args);
  }}
#line 521 "mn-evolution-mailbox.c"
#undef __GOB_FUNCTION__

#line 204 "src/mn-evolution-mailbox.gob"
static void 
___a_mn_evolution_mailbox_check (MNMailbox * mailbox G_GNUC_UNUSED)
#line 527 "mn-evolution-mailbox.c"
#define PARENT_HANDLER(___mailbox) \
	{ if(MN_MAILBOX_CLASS(parent_class)->check) \
		(* MN_MAILBOX_CLASS(parent_class)->check)(___mailbox); }
{
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::check"
{
#line 206 "src/mn-evolution-mailbox.gob"
	
    Self *self = SELF(mailbox);

    PARENT_HANDLER(mailbox);

    mn_mailbox_set_error(mailbox, NULL);

    if (! selfp->client)
      {
	selfp->client = mn_evolution_client_get();
	g_signal_connect_swapped(selfp->client, "notify::proxy", G_CALLBACK(mn_mailbox_check), self);
      }

    if (! selfp->client->proxy)	/* not connected */
      {
	selfp->pending_call = NULL;

	self_disconnect_proxy_signals(self);

	mn_mailbox_set_error(mailbox, _("unable to contact Evolution"));

	return;
      }

    self_connect_proxy_signals(self);

    if (selfp->pending_call)
      dbus_g_proxy_cancel_call(selfp->client->proxy, selfp->pending_call);

    selfp->pending_call = org_gnome_MailNotification_Evolution_get_unseen_messages_async(selfp->client->proxy,
											 self->uri,
											 self_get_unseen_messages_cb,
											 self);
  }}
#line 569 "mn-evolution-mailbox.c"
#undef __GOB_FUNCTION__
#undef PARENT_HANDLER

#line 241 "src/mn-evolution-mailbox.gob"
static void 
mn_evolution_mailbox_get_unseen_messages_cb (DBusGProxy * proxy, GPtrArray * messages_array, GError * err, gpointer user_data)
{
#line 577 "mn-evolution-mailbox.c"
#define __GOB_FUNCTION__ "MN:Evolution:Mailbox::get_unseen_messages_cb"
{
#line 246 "src/mn-evolution-mailbox.gob"
	
    Self *self = user_data;

    GDK_THREADS_ENTER();

    if (err)
      {
	mn_mailbox_set_error(MN_MAILBOX(self), "%s", err->message);
	g_error_free(err);
      }
    else
      {
	int i;
	GSList *messages = NULL;

	for (i = 0; i < messages_array->len; i++)
	  {
	    GValueArray *value_array = g_ptr_array_index(messages_array, i);
	    MessageInfo info;
	    MNMessageFlags flags = 0;

	    self_value_array_get_values(value_array,
					&info.uid,
					&info.sent_time,
					&info.received_time,
					&info.id,
					&info.from,
					&info.subject);

	    if (info.received_time > selfp->last_browsed)
	      flags |= MN_MESSAGE_NEW;

	    messages = g_slist_prepend(messages,
				       mn_evolution_message_new(MN_MAILBOX(self),
								info.sent_time,
								info.id,
								info.from,
								info.subject,
								flags,
								info.uid));
	  }

	mn_g_ptr_array_free_deep_custom(messages_array, (GFunc) g_value_array_free, NULL);

	mn_mailbox_set_messages(MN_MAILBOX(self), messages);
	mn_g_object_slist_free(messages);
      }

    selfp->pending_call = NULL;

    GDK_THREADS_LEAVE();
  }}
#line 633 "mn-evolution-mailbox.c"
#undef __GOB_FUNCTION__
