/*
 * This file is part of roccat-tools.
 *
 * roccat-tools 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.
 *
 * roccat-tools 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 roccat-tools. If not, see <http://www.gnu.org/licenses/>.
 */

#include "ryos_rkp.h"
#include "ryos_dbus_services.h"
#include "ryos_reset.h"
#include "ryos_profile.h"
#include "ryos_info.h"
#include "ryos_rkp_accessors.h"
#include "ryosconfig_window.h"
#include "ryosconfig_configuration_dialog.h"
#include "ryosconfig_profile_page.h"
#include "ryosconfig_update_assistant.h"
#include "ryosconfig_layer_illumination_dialog.h"
#include "ryos_config.h"
#include "ryos_device.h"
#include "roccat_helper.h"
#include "roccat_warning_dialog.h"
#include "roccat_save_dialog.h"
#include "roccat_continue_dialog.h"
#include "roccat_info_dialog.h"
#include "dbus_roccat_helper.h"
#include "roccat.h"

#define RYOSCONFIG_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), RYOSCONFIG_WINDOW_TYPE, RyosconfigWindowClass))
#define IS_RYOSCONFIG_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), RYOSCONFIG_WINDOW_TYPE))
#define RYOSCONFIG_WINDOW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), RYOSCONFIG_WINDOW_TYPE, RyosconfigWindowPrivate))

typedef struct _RyosconfigWindowClass RyosconfigWindowClass;
typedef struct _RyosconfigWindowPrivate RyosconfigWindowPrivate;

struct _RyosconfigWindow {
	RoccatConfigWindow parent;
	RyosconfigWindowPrivate *priv;
};

struct _RyosconfigWindowClass {
	RoccatConfigWindowClass parent_class;
};

struct _RyosconfigWindowPrivate {
	RyosconfigProfilePage *profile_pages[RYOS_PROFILE_NUM];
	RoccatKeyFile *config;
	DBusGProxy *dbus_proxy;
	guint actual_profile_index;
};

G_DEFINE_TYPE(RyosconfigWindow, ryosconfig_window, ROCCAT_CONFIG_WINDOW_TYPE);

static gboolean change_profile_handler(gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(user_data);
	RyosconfigWindowPrivate *priv = window->priv;
	guint profile_index = roccat_config_window_get_active_page(ROCCAT_CONFIG_WINDOW(window));
	GError *local_error = NULL;

	if (!roccat_config_window_warn_if_no_device(ROCCAT_CONFIG_WINDOW(window)))
		return FALSE;

	if (priv->actual_profile_index != profile_index) {
		priv->actual_profile_index = profile_index;
		ryos_profile_write(roccat_config_window_get_device(ROCCAT_CONFIG_WINDOW(window)), profile_index, &local_error);
		if (!roccat_handle_error_dialog(GTK_WINDOW(window), "Error activating profile", &local_error))
			return FALSE;
		ryos_dbus_emit_profile_changed_outside(priv->dbus_proxy, profile_index);
	}

	return FALSE;
}

static void window_active_page_changed_cb(RoccatConfigWindow *window, gpointer user_data) {
	g_idle_add(change_profile_handler, window);
}

static void actual_profile_changed_from_device_cb(DBusGProxy *proxy, guchar profile_index, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(user_data);
	RyosconfigWindowPrivate *priv = window->priv;

	if (priv->actual_profile_index != profile_index) {
		priv->actual_profile_index = profile_index;
		roccat_config_window_set_active_page(ROCCAT_CONFIG_WINDOW(window), profile_index);
	}
}

static void brightness_changed_from_device_cb(DBusGProxy *proxy, guchar profile_index, guchar brightness_index, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(user_data);
	ryosconfig_profile_page_update_brightness(window->priv->profile_pages[profile_index], brightness_index);
}

static void macro_changed_from_device_cb(DBusGProxy *proxy, guchar profile_index, guchar button_index, gchar const *data, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(user_data);
	RyosMacro *macro;
	gsize length;

	macro = (RyosMacro *)g_base64_decode(data, &length);
	if (length == sizeof(RyosMacro))
		ryosconfig_profile_page_update_key(window->priv->profile_pages[profile_index], button_index, macro);
	g_free(macro);
}

static void device_add_cb(RoccatConfigWindow *roccat_window, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(roccat_window);
	RyosconfigWindowPrivate *priv = window->priv;
	GError *local_error = NULL;
	guint i;
	RyosRkp *rkp;
	RoccatDevice *device;

	device = roccat_config_window_get_device(roccat_window);

	priv->actual_profile_index = ryos_profile_read(device, &local_error);
	if (!roccat_handle_error_dialog(GTK_WINDOW(window), "Can't read actual profile:", &local_error))
		return;
	roccat_config_window_set_active_page(roccat_window, priv->actual_profile_index);

	for (i = 0; i < RYOS_PROFILE_NUM; ++i) {
		ryosconfig_profile_page_set_device(priv->profile_pages[i], device);

		rkp = ryosconfig_profile_page_get_rkp(priv->profile_pages[i]);
		ryos_rkp_update_from_device(device, rkp, i, &local_error);

		if (!roccat_handle_error_dialog(GTK_WINDOW(window), "Can't read hardware profile data:", &local_error)) {
			ryos_rkp_free(rkp);
			break;
		}

		ryosconfig_profile_page_set_rkp(priv->profile_pages[i], rkp);

		ryos_rkp_free(rkp);
	}

	dbus_g_proxy_connect_signal(priv->dbus_proxy, "ProfileChanged", G_CALLBACK(actual_profile_changed_from_device_cb), window, NULL);
	dbus_g_proxy_connect_signal(priv->dbus_proxy, "BrightnessChanged", G_CALLBACK(brightness_changed_from_device_cb), window, NULL);
	dbus_g_proxy_connect_signal(priv->dbus_proxy, "MacroChanged", G_CALLBACK(macro_changed_from_device_cb), window, NULL);
}

static void device_remove_cb(RoccatConfigWindow *roccat_window, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(roccat_window);
	RyosconfigWindowPrivate *priv = window->priv;

	dbus_g_proxy_disconnect_signal(priv->dbus_proxy, "ProfileChanged", G_CALLBACK(actual_profile_changed_from_device_cb), window);
	dbus_g_proxy_disconnect_signal(priv->dbus_proxy, "BrightnessChanged", G_CALLBACK(brightness_changed_from_device_cb), window);
	dbus_g_proxy_disconnect_signal(priv->dbus_proxy, "MacroChanged", G_CALLBACK(macro_changed_from_device_cb), window);
}

static gboolean save_unsaved(RyosconfigWindow *window, RyosRkp *rkp, guint profile_index, GError **error) {
	RyosconfigWindowPrivate *priv = window->priv;
	RoccatDevice *device = roccat_config_window_get_device(ROCCAT_CONFIG_WINDOW(window));

	if (device)
		ryos_rkp_save(device, rkp, profile_index, error);
	else
		ryos_rkp_save_actual(rkp, profile_index, error);

	if (*error)
		return FALSE;
	else {
		ryos_dbus_emit_profile_data_changed_outside(priv->dbus_proxy, profile_index);
		ryosconfig_profile_page_set_rkp(priv->profile_pages[profile_index], rkp);
		return TRUE;
	}
}

static gboolean display_quit_dialog(RyosconfigWindow *window) {
	guint i;
	gboolean modified;
	RyosRkp *rkps[RYOS_PROFILE_NUM];
	gboolean retval;
	GError *error = NULL;

	modified = FALSE;
	for (i = 0; i < RYOS_PROFILE_NUM; ++i) {
		rkps[i] = ryosconfig_profile_page_get_rkp(window->priv->profile_pages[i]);
		if (ryos_rkp_get_modified(rkps[i]))
			modified = TRUE;
	}
	if (!modified) {
		for (i = 0; i < RYOS_PROFILE_NUM; ++i)
			ryos_rkp_free(rkps[i]);
		return TRUE;
	}

	switch (roccat_save_unsaved_dialog(GTK_WINDOW(window), TRUE)) {
	case GTK_RESPONSE_CANCEL:
		retval = FALSE;
		break;
	case GTK_RESPONSE_ACCEPT:
		retval = TRUE;
		roccat_config_window_warn_if_no_device(ROCCAT_CONFIG_WINDOW(window));
		for (i = 0; i < RYOS_PROFILE_NUM; ++i) {
			if (!save_unsaved(window, rkps[i], i, &error)) {
				retval = FALSE;
				break;
			}
		}
		roccat_handle_error_dialog(GTK_WINDOW(window), "Error storing unsaved data", &error);
		break;
	default:
		retval = TRUE;
		break;
	}

	for (i = 0; i < RYOS_PROFILE_NUM; ++i)
		ryos_rkp_free(rkps[i]);

	return retval;
}

static gboolean delete_event_cb(GtkWidget *window, GdkEvent *event, gpointer user_data) {
	return !display_quit_dialog(RYOSCONFIG_WINDOW(window));
}

static void destroy_event_cb(GtkWidget *window, gpointer user_data) {
	gtk_main_quit();
}

static void save_profile_to_device_cb(RoccatProfilePage *profile_page, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(user_data);
	RyosRkp *rkp = ryosconfig_profile_page_get_rkp(RYOSCONFIG_PROFILE_PAGE(profile_page));
	guint profile_index = roccat_profile_page_get_index(profile_page);
	GError *error = NULL;

	roccat_config_window_warn_if_no_device(ROCCAT_CONFIG_WINDOW(window));
	save_unsaved(window, rkp, profile_index, &error);
	roccat_handle_error_dialog(GTK_WINDOW(window), "Error storing unsaved data", &error);

	ryos_rkp_free(rkp);
}

static void add_file_filter_to_file_chooser(GtkFileChooser *chooser) {
	GtkFileFilter *filter;
	filter = gtk_file_filter_new();
	gtk_file_filter_set_name(filter, "Ryos Windows profiles");
	gtk_file_filter_add_pattern(filter, "*." ROCCAT_KEYBOARD_PROFILE_EXTENSION);
	gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(chooser), filter);

	filter = gtk_file_filter_new();
	gtk_file_filter_set_name(filter, "All files");
	gtk_file_filter_add_pattern(filter, "*");
	gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(chooser), filter);
}

static gchar *ryos_create_filename_proposition(RyosRkp const *rkp, guint profile_index) {
	gchar *profile_name;
	gchar *filename;

	profile_name = ryos_rkp_get_profile_name(rkp);
	filename = roccat_profile_filename_proposition(profile_name, profile_index, ROCCAT_KEYBOARD_PROFILE_EXTENSION);
	g_free(profile_name);

	return filename;
}

static void save_profile_to_file_cb(RoccatProfilePage *profile_page, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(user_data);
	RyosconfigWindowPrivate *priv = window->priv;
	GtkWidget *dialog;
	gchar *filename, *path;
	gchar *temp_filename;
	GError *error = NULL;
	guint profile_index = roccat_profile_page_get_index(profile_page);
	RyosRkp *rkp;

	rkp = ryosconfig_profile_page_get_rkp(priv->profile_pages[profile_index]);

	dialog = gtk_file_chooser_dialog_new("Save profile",
			GTK_WINDOW(window),
			GTK_FILE_CHOOSER_ACTION_SAVE,
			GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
			GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
			NULL);

	add_file_filter_to_file_chooser(GTK_FILE_CHOOSER(dialog));

	path = ryos_configuration_get_rkp_path(priv->config);
	gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), path);
	g_free(path);

	filename = ryos_create_filename_proposition(rkp, profile_index);
	gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), filename);
	g_free(filename);

	if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
		filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
		temp_filename = roccat_create_filename_with_extension(filename, ROCCAT_KEYBOARD_PROFILE_EXTENSION);
		g_free(filename);

		path = g_path_get_dirname(temp_filename);
		ryos_configuration_set_rkp_path(priv->config, path);
		g_free(path);

		ryos_rkp_write_with_path(temp_filename, rkp, &error);
		g_free(temp_filename);
		if (error) {
			roccat_warning_dialog(GTK_WINDOW(dialog), "Error saving rkp", error->message);
			g_error_free(error);
		}
	}

	gtk_widget_destroy(dialog);
	ryos_rkp_free(rkp);
}

static void load_profile_from_file_cb(RoccatProfilePage *profile_page, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(user_data);
	RyosconfigWindowPrivate *priv = window->priv;
	GtkWidget *dialog;
	gchar *filename, *path;
	GError *error = NULL;
	RyosRkp *rkp;
	guint profile_index = roccat_profile_page_get_index(profile_page);

	dialog = gtk_file_chooser_dialog_new("Load profile",
			GTK_WINDOW(window),
			GTK_FILE_CHOOSER_ACTION_OPEN,
			GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
			GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
			NULL);

	add_file_filter_to_file_chooser(GTK_FILE_CHOOSER(dialog));
	path = ryos_configuration_get_rkp_path(priv->config);
	gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), path);
	g_free(path);

	if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
		filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
		path = g_path_get_dirname(filename);
		ryos_configuration_set_rkp_path(priv->config, path);
		g_free(path);

		rkp = ryos_rkp_read_with_path(filename, &error);
		if (error) {
			roccat_warning_dialog(GTK_WINDOW(dialog), "Error loading rkp", error->message);
			g_error_free(error);
		} else {
			ryos_rkp_set_modified(rkp);
			ryosconfig_profile_page_set_rkp(priv->profile_pages[profile_index], rkp);
			ryos_rkp_free(rkp);
		}

		g_free(filename);
	}

	gtk_widget_destroy(dialog);
}

static void save_all_cb(GtkMenuItem *item, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(user_data);
	RyosRkp *rkp;
	guint i;
	GError *error = NULL;

	roccat_config_window_warn_if_no_device(ROCCAT_CONFIG_WINDOW(window));
	for (i = 0; i < RYOS_PROFILE_NUM; ++i) {
		rkp = ryosconfig_profile_page_get_rkp(window->priv->profile_pages[i]);
		save_unsaved(window, rkp, i, &error);
		roccat_handle_error_dialog(GTK_WINDOW(window), "Error storing unsaved data", &error);

		ryos_rkp_free(rkp);
	}
}

static void menu_edit_preferences_cb(GtkMenuItem *item, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(user_data);
	RyosconfigWindowPrivate *priv = window->priv;
	ryosconfig_configuration_dialog(GTK_WINDOW(window), priv->config);
	ryos_dbus_emit_configuration_changed_outside(priv->dbus_proxy);
}

static void menu_device_firmware_update_cb(GtkMenuItem *item, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(user_data);
	if (roccat_config_window_warn_if_no_device(ROCCAT_CONFIG_WINDOW(window)))
		ryosconfig_update_assistant(GTK_WINDOW(window), roccat_config_window_get_device(ROCCAT_CONFIG_WINDOW(window)));
}

static void menu_device_info_cb(GtkMenuItem *item, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(user_data);
	RoccatInfoDialog *dialog;
	GError *local_error = NULL;
	RyosInfo *info;
	gchar *firmware;

	if (!roccat_config_window_warn_if_no_device(ROCCAT_CONFIG_WINDOW(window)))
		return;

	info = ryos_info_read(roccat_config_window_get_device(ROCCAT_CONFIG_WINDOW(window)), &local_error);
	if (!roccat_handle_error_dialog(GTK_WINDOW(window), "Can't read info:", &local_error))
		return;

	dialog = ROCCAT_INFO_DIALOG(roccat_info_dialog_new(GTK_WINDOW(window)));

	firmware = ryos_firmware_version_to_string(info->firmware_version);
	roccat_info_dialog_add_line(dialog, "Firmware", gtk_label_new(firmware));
	g_free(firmware);

	g_free(info);

	(void)gtk_dialog_run(GTK_DIALOG(dialog));
	gtk_widget_destroy(GTK_WIDGET(dialog));
}

static void menu_device_reset_cb(GtkMenuItem *item, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(user_data);
	RyosconfigWindowPrivate *priv = window->priv;
	RyosRkp *rkp;
	guint i;
	GError *error = NULL;
	RoccatDevice *device;

	if (!roccat_config_window_warn_if_no_device(ROCCAT_CONFIG_WINDOW(window)))
		return;

	if (!roccat_continue_dialog(GTK_WINDOW(window), "This resets the device to factory defaults."))
		return;

	device = roccat_config_window_get_device(ROCCAT_CONFIG_WINDOW(window));
	ryos_reset(device, RYOS_RESET_FUNCTION_RESET, &error);
	if (!roccat_handle_error_dialog(GTK_WINDOW(window), "Error resetting device", &error))
		return;

	for (i = 0; i < RYOS_PROFILE_NUM; ++i) {
		rkp = ryos_rkp_load_save_after_reset(device, i, &error);
		ryosconfig_profile_page_set_rkp(priv->profile_pages[i], rkp);
		ryos_rkp_free(rkp);
		ryos_dbus_emit_profile_data_changed_outside(priv->dbus_proxy, i);
	}
}

static gboolean layer_editor_cb(RyosconfigProfilePage *profile_page, gpointer light_layer, gpointer user_data) {
	RyosconfigWindow *window = RYOSCONFIG_WINDOW(user_data);
	return ryosconfig_layer_illumination_dialog_run(GTK_WINDOW(window),
			roccat_config_window_get_device(ROCCAT_CONFIG_WINDOW(window)), light_layer);
}

GtkWidget *ryosconfig_window_new(void) {
	return GTK_WIDGET(g_object_new(RYOSCONFIG_WINDOW_TYPE,
			"device-name", RYOS_DEVICE_NAME_COMBINED,
			"default-height", 480,
			"default-width", 640,
			NULL));
}

static GObject *constructor(GType gtype, guint n_properties, GObjectConstructParam *properties) {
	GObject *obj;
	RyosconfigWindow *window;
	RoccatConfigWindow *roccat_window;
	RyosconfigWindowPrivate *priv;
	RyosRkp *rkp;
	guint i;
	GtkMenuItem *menu_item;

	obj = G_OBJECT_CLASS(ryosconfig_window_parent_class)->constructor(gtype, n_properties, properties);
	window = RYOSCONFIG_WINDOW(obj);
	roccat_window = ROCCAT_CONFIG_WINDOW(obj);
	priv = window->priv;

	priv->config = ryos_configuration_load();

	for (i = 0; i < RYOS_PROFILE_NUM; ++i) {
		priv->profile_pages[i] = RYOSCONFIG_PROFILE_PAGE(ryosconfig_profile_page_new(i));
		roccat_config_window_append_page(roccat_window, ROCCAT_PROFILE_PAGE(priv->profile_pages[i]));

		rkp = ryos_rkp_load_actual(i);
		ryosconfig_profile_page_set_rkp(priv->profile_pages[i], rkp);
		ryos_rkp_free(rkp);

		g_signal_connect(G_OBJECT(priv->profile_pages[i]), "load-from-file", G_CALLBACK(load_profile_from_file_cb), window);
		g_signal_connect(G_OBJECT(priv->profile_pages[i]), "save-to-file", G_CALLBACK(save_profile_to_file_cb), window);
		g_signal_connect(G_OBJECT(priv->profile_pages[i]), "save-to-device", G_CALLBACK(save_profile_to_device_cb), window);
		g_signal_connect(G_OBJECT(priv->profile_pages[i]), "layer-editor", G_CALLBACK(layer_editor_cb), window);
	}

	menu_item = GTK_MENU_ITEM(gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, NULL));
	g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(menu_edit_preferences_cb), window);
	roccat_config_window_edit_menu_append(roccat_window, menu_item);

	menu_item = GTK_MENU_ITEM(gtk_image_menu_item_new_with_label("Firmware update"));
	g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(menu_device_firmware_update_cb), window);
	roccat_config_window_device_menu_append(roccat_window, menu_item);

	menu_item = GTK_MENU_ITEM(gtk_image_menu_item_new_with_label("Reset"));
	g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(menu_device_reset_cb), window);
	roccat_config_window_device_menu_append(roccat_window, menu_item);

	menu_item = GTK_MENU_ITEM(gtk_image_menu_item_new_with_label("Info"));
	g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(menu_device_info_cb), window);
	roccat_config_window_device_menu_append(roccat_window, menu_item);

	g_signal_connect(G_OBJECT(roccat_window), "active-changed", G_CALLBACK(window_active_page_changed_cb), NULL);
	g_signal_connect(G_OBJECT(roccat_window), "destroy", G_CALLBACK(destroy_event_cb), NULL);
	g_signal_connect(G_OBJECT(roccat_window), "delete-event", G_CALLBACK(delete_event_cb), NULL);
	g_signal_connect(G_OBJECT(roccat_window), "save-all", G_CALLBACK(save_all_cb), NULL);
	g_signal_connect(G_OBJECT(roccat_window), "device-added", G_CALLBACK(device_add_cb), NULL);
	g_signal_connect(G_OBJECT(roccat_window), "device-removed", G_CALLBACK(device_remove_cb), NULL);

	/* keep this order */
	priv->dbus_proxy = ryos_dbus_proxy_new();

	roccat_config_window_set_device_scanner(roccat_window, ryos_device_scanner_new());

	gtk_widget_show(GTK_WIDGET(window));

	/* if a device is already plugged in, add_cb has already been executed */
	roccat_config_window_warn_if_no_device(roccat_window);

	return obj;
}

static void ryosconfig_window_init(RyosconfigWindow *window) {
	window->priv = RYOSCONFIG_WINDOW_GET_PRIVATE(window);
}

static void finalize(GObject *object) {
	RyosconfigWindowPrivate *priv = RYOSCONFIG_WINDOW(object)->priv;

	dbus_roccat_proxy_free(priv->dbus_proxy);
	priv->dbus_proxy = NULL;
	
	ryosconfig_configuration_save(GTK_WINDOW(object), priv->config);
	ryos_configuration_free(priv->config);
	priv->config = NULL;

	G_OBJECT_CLASS(ryosconfig_window_parent_class)->finalize(object);
}

static void ryosconfig_window_class_init(RyosconfigWindowClass *klass) {
	GObjectClass *gobject_class = G_OBJECT_CLASS(klass);

	gobject_class->constructor = constructor;
	gobject_class->finalize = finalize;

	g_type_class_add_private(klass, sizeof(RyosconfigWindowClass));
}
