/* preferences.c generated by valac, the Vala compiler
 * generated from preferences.vala, do not modify */

/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 2 -*- */
/*
    This file is part of Déjà Dup.
    © 2008,2009 Michael Terry <mike@mterry.name>

    Déjà Dup 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 3 of the License, or
    (at your option) any later version.

    Déjà Dup 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 Déjà Dup.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <glib/gi18n-lib.h>
#include <config.h>
#include <unique/unique.h>
#include <common.h>
#include <stdlib.h>
#include <string.h>


#define TYPE_DEJA_DUP_PREFERENCES (deja_dup_preferences_get_type ())
#define DEJA_DUP_PREFERENCES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_DEJA_DUP_PREFERENCES, DejaDupPreferences))
#define DEJA_DUP_PREFERENCES_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_DEJA_DUP_PREFERENCES, DejaDupPreferencesClass))
#define IS_DEJA_DUP_PREFERENCES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_DEJA_DUP_PREFERENCES))
#define IS_DEJA_DUP_PREFERENCES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_DEJA_DUP_PREFERENCES))
#define DEJA_DUP_PREFERENCES_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_DEJA_DUP_PREFERENCES, DejaDupPreferencesClass))

typedef struct _DejaDupPreferences DejaDupPreferences;
typedef struct _DejaDupPreferencesClass DejaDupPreferencesClass;
typedef struct _DejaDupPreferencesPrivate DejaDupPreferencesPrivate;

#define TYPE_PREFERENCES_DIALOG (preferences_dialog_get_type ())
#define PREFERENCES_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_PREFERENCES_DIALOG, PreferencesDialog))
#define PREFERENCES_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_PREFERENCES_DIALOG, PreferencesDialogClass))
#define IS_PREFERENCES_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_PREFERENCES_DIALOG))
#define IS_PREFERENCES_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_PREFERENCES_DIALOG))
#define PREFERENCES_DIALOG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_PREFERENCES_DIALOG, PreferencesDialogClass))

typedef struct _PreferencesDialog PreferencesDialog;
typedef struct _PreferencesDialogClass PreferencesDialogClass;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_option_context_free0(var) ((var == NULL) ? NULL : (var = (g_option_context_free (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _DejaDupPreferences {
	GObject parent_instance;
	DejaDupPreferencesPrivate * priv;
};

struct _DejaDupPreferencesClass {
	GObjectClass parent_class;
};


static gboolean deja_dup_preferences_show_version;
static gboolean deja_dup_preferences_show_version = FALSE;
static PreferencesDialog* deja_dup_preferences_pref_window;
static PreferencesDialog* deja_dup_preferences_pref_window = NULL;
static gpointer deja_dup_preferences_parent_class = NULL;

GType deja_dup_preferences_get_type (void);
enum  {
	DEJA_DUP_PREFERENCES_DUMMY_PROPERTY
};
GType preferences_dialog_get_type (void);
static gboolean deja_dup_preferences_handle_console_options (gint* status);
static UniqueResponse deja_dup_preferences_message_received (UniqueApp* app, gint command, UniqueMessageData* message_data, guint time_);
PreferencesDialog* preferences_dialog_new (GtkWindow* parent);
PreferencesDialog* preferences_dialog_construct (GType object_type, GtkWindow* parent);
static UniqueResponse _deja_dup_preferences_message_received_unique_app_message_received (UniqueApp* _sender, gint command, UniqueMessageData* message_data, guint time_, gpointer self);
gint deja_dup_preferences_main (char** args, int args_length1);
DejaDupPreferences* deja_dup_preferences_new (void);
DejaDupPreferences* deja_dup_preferences_construct (GType object_type);
static void deja_dup_preferences_finalize (GObject* obj);

static const GOptionEntry DEJA_DUP_PREFERENCES_options[2] = {{"version", (gchar) 0, 0, G_OPTION_ARG_NONE, &deja_dup_preferences_show_version, N_ ("Show version"), NULL}, {NULL}};


static gboolean deja_dup_preferences_handle_console_options (gint* status) {
	gboolean result = FALSE;
	*status = 0;
	if (deja_dup_preferences_show_version) {
		g_print ("%s %s\n", _ ("Déjà Dup Preferences"), VERSION);
		result = FALSE;
		return result;
	}
	result = TRUE;
	return result;
}


static UniqueResponse deja_dup_preferences_message_received (UniqueApp* app, gint command, UniqueMessageData* message_data, guint time_) {
	UniqueResponse result = 0;
	UniqueResponse res;
	g_return_val_if_fail (app != NULL, 0);
	g_return_val_if_fail (message_data != NULL, 0);
	res = UNIQUE_RESPONSE_OK;
	switch (command) {
		case UNIQUE_ACTIVATE:
		{
			gtk_window_present_with_time ((GtkWindow*) deja_dup_preferences_pref_window, (guint32) time_);
			break;
		}
	}
	result = res;
	return result;
}


static UniqueResponse _deja_dup_preferences_message_received_unique_app_message_received (UniqueApp* _sender, gint command, UniqueMessageData* message_data, guint time_, gpointer self) {
	return deja_dup_preferences_message_received (_sender, command, message_data, time_);
}


gint deja_dup_preferences_main (char** args, int args_length1) {
	gint result = 0;
	GError * _inner_error_;
	GOptionContext* context;
	gint status = 0;
	UniqueApp* app;
	gboolean _tmp1_;
	_inner_error_ = NULL;
	textdomain (GETTEXT_PACKAGE);
	bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	g_set_application_name (_ ("Déjà Dup Preferences"));
	context = g_option_context_new ("");
	g_option_context_add_main_entries (context, DEJA_DUP_PREFERENCES_options, GETTEXT_PACKAGE);
	g_option_context_add_group (context, gtk_get_option_group (FALSE));
	{
		g_option_context_parse (context, &args_length1, &args, &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch1_g_error;
		}
	}
	goto __finally1;
	__catch1_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		{
			char* _tmp0_;
			g_printerr ("%s\n\n%s", e->message, _tmp0_ = g_option_context_get_help (context, TRUE, NULL));
			_g_free0 (_tmp0_);
			result = 1;
			_g_error_free0 (e);
			_g_option_context_free0 (context);
			return result;
		}
	}
	__finally1:
	if (_inner_error_ != NULL) {
		_g_option_context_free0 (context);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return 0;
	}
	if (!deja_dup_preferences_handle_console_options (&status)) {
		result = status;
		_g_option_context_free0 (context);
		return result;
	}
	deja_dup_initialize ();
	gtk_init (&args_length1, &args);
	app = unique_app_new ("net.launchpad.deja-dup.preferences", NULL);
	if ((g_object_get (app, "is-running", &_tmp1_, NULL), _tmp1_)) {
		unique_app_send_message (app, (gint) UNIQUE_ACTIVATE, NULL);
	} else {
		PreferencesDialog* _tmp2_;
		gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (), THEME_DIR);
		gtk_window_set_default_icon_name (PACKAGE);
		deja_dup_preferences_pref_window = (_tmp2_ = g_object_ref_sink (preferences_dialog_new (NULL)), _g_object_unref0 (deja_dup_preferences_pref_window), _tmp2_);
		g_signal_connect (app, "message-received", (GCallback) _deja_dup_preferences_message_received_unique_app_message_received, NULL);
		unique_app_watch_window (app, (GtkWindow*) deja_dup_preferences_pref_window);
		gtk_widget_show_all ((GtkWidget*) deja_dup_preferences_pref_window);
		gtk_main ();
	}
	result = 0;
	_g_option_context_free0 (context);
	_g_object_unref0 (app);
	return result;
}


int main (int argc, char ** argv) {
	g_type_init ();
	return deja_dup_preferences_main (argv, argc);
}


DejaDupPreferences* deja_dup_preferences_construct (GType object_type) {
	DejaDupPreferences * self;
	self = (DejaDupPreferences*) g_object_new (object_type, NULL);
	return self;
}


DejaDupPreferences* deja_dup_preferences_new (void) {
	return deja_dup_preferences_construct (TYPE_DEJA_DUP_PREFERENCES);
}


static void deja_dup_preferences_class_init (DejaDupPreferencesClass * klass) {
	deja_dup_preferences_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = deja_dup_preferences_finalize;
}


static void deja_dup_preferences_instance_init (DejaDupPreferences * self) {
}


static void deja_dup_preferences_finalize (GObject* obj) {
	DejaDupPreferences * self;
	self = DEJA_DUP_PREFERENCES (obj);
	G_OBJECT_CLASS (deja_dup_preferences_parent_class)->finalize (obj);
}


GType deja_dup_preferences_get_type (void) {
	static volatile gsize deja_dup_preferences_type_id__volatile = 0;
	if (g_once_init_enter (&deja_dup_preferences_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (DejaDupPreferencesClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) deja_dup_preferences_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DejaDupPreferences), 0, (GInstanceInitFunc) deja_dup_preferences_instance_init, NULL };
		GType deja_dup_preferences_type_id;
		deja_dup_preferences_type_id = g_type_register_static (G_TYPE_OBJECT, "DejaDupPreferences", &g_define_type_info, 0);
		g_once_init_leave (&deja_dup_preferences_type_id__volatile, deja_dup_preferences_type_id);
	}
	return deja_dup_preferences_type_id__volatile;
}




