/**
 * this function is called to launch the assistant to export archives
 *
 * \param
 *
 * \return a GtkResponseType containing the return value at the end of the assistant
 * */
GtkResponseType gsb_assistant_archive_export_run ( void )
{
    GtkResponseType return_value;
    GtkWidget *assistant;

    /* create the assistant */
    assistant = gsb_assistant_new ( _("Export an archive"),
				    _("This assistant will help you to export an archive into gsb, QIF or CSV format."
				      "Note that nothing will be deleted in Grisbi, it's just an export."
				      "If you want to delete the archive from Grisbi, you need to do it from the preference window."),
				    "archive.png",
				    G_CALLBACK (gsb_assistant_archive_switch));
    gsb_assistant_add_page ( assistant,
			     gsb_assistant_archive_export_page_choose (assistant),
			     ARCHIVE_EXPORT_ASSISTANT_CHOOSE,
			     ARCHIVE_EXPORT_ASSISTANT_INTRO,
			     ARCHIVE_EXPORT_ASSISTANT_NAME,
			     G_CALLBACK (gsb_assistant_archive_switch));
    gsb_assistant_add_page ( assistant,
			     gsb_assistant_archive_export_page_name (assistant),
			     ARCHIVE_EXPORT_ASSISTANT_NAME,
			     ARCHIVE_EXPORT_ASSISTANT_CHOOSE,
			     ARCHIVE_EXPORT_ASSISTANT_SUCCESS,
			     G_CALLBACK (gsb_assistant_archive_switch));
    gsb_assistant_add_page ( assistant,
			     gsb_assistant_archive_export_page_succes (assistant),
			     ARCHIVE_EXPORT_ASSISTANT_SUCCESS,
			     ARCHIVE_EXPORT_ASSISTANT_NAME,
			     FALSE,
			     G_CALLBACK (gsb_assistant_archive_switch));
    return_value = gsb_assistant_run (assistant);
    gtk_widget_destroy (assistant);

    return return_value;
}
/**
 * function called to launch the reconcile_config assistant
 * calculate the number of transactions to link, and refuse to run the assistant if none
 *
 * \param
 *
 * \return GtkResponseType, the returned value from the assistant
 * */
GtkResponseType gsb_assistant_reconcile_config_run ( void )
{
    GtkResponseType return_value;
    GSList *transactions_list = NULL;
    GSList *tmp_list;
    gint transaction_number;
    GtkWidget *assistant;
	gchar* tmpstr;

    tmp_list = gsb_data_transaction_get_transactions_list ();
    while (tmp_list)
    {
	transaction_number = gsb_data_transaction_get_transaction_number (tmp_list -> data);

	if ( gsb_data_transaction_get_marked_transaction (transaction_number) == OPERATION_RAPPROCHEE
	     &&
	     !gsb_data_transaction_get_reconcile_number (transaction_number))
	    transactions_list = g_slist_append ( transactions_list,
						 GINT_TO_POINTER (transaction_number));
	tmp_list = tmp_list -> next;
    }

    if (!transactions_list)
    {
	dialogue (_("No marked transactions without reconciliation found!"));
	return GTK_RESPONSE_CANCEL;
    }

    /* get the number of transactions to associate, we will decrease it for each association */
    transactions_to_link = g_slist_length (transactions_list);

    /* come here if we have some orphan transactions
     * this can happen by 2 ways :
     * for old users of Grisbi, before i don't remember what version, there were no reconcile number,
     * 		the reconciled transactions were juste marked R
     * before the 0.6.0, ctrl R didn't permit to choose a reconciliation
     *
     * for the 2nd item, no problem, we show a list of reconciles and the user can choose what reconcile he wants
     * for the first item the problem is there is no reconcile number to go with that transactions...
     * so we will use the assistant to
     * 	-permit to create a reconcile directly (not possible normaly in the configuration
     * 	-permit to choose a reconcile number for each transactions without reconcile
     * 	- do an automatic find for reconcile, usefull in the first item, when very much
     * 		transactions without reconcile, but we need to make the old reconciles before,
     * 		and set the good date for all the reconciles (because grisbi set them automatically
     * 		at the first update to grisbi 0.6.0 )*/

    /* first, create the assistant */
    tmpstr = g_strdup_printf (_("Grisbi found %d marked transactions not associated with a reconciliation number, "
						       "this can happen for old users of Grisbi or from a misuse of the Ctrl-R shortcut.\n\n"
						       "This assistant will help you make the link between such transactions and a reconciliation.\n\n"
						       "Before continuing, you should first check if all the dates of the existing reconciliations are good "
						       "because Grisbi will try to guess them not very precisely "
						       "(you will be able to create new reconciliations in the next step). "
						       "Previous reconciliations will be available too."),
						       transactions_to_link );
    assistant = gsb_assistant_new ( _("Associate orphan transactions to a reconciliation"),
				    tmpstr,
				    "reconat.png",
				    NULL );
    g_free ( tmpstr );

    gsb_assistant_add_page ( assistant,
			     gsb_assistant_reconcile_config_page_menu (assistant),
			     RECONCILE_ASSISTANT_MENU,
			     RECONCILE_ASSISTANT_INTRO,
			     RECONCILE_ASSISTANT_NEW_RECONCILE,
			     NULL );
    gsb_assistant_add_page ( assistant,
			     gsb_assistant_reconcile_config_page_new_reconcile (),
			     RECONCILE_ASSISTANT_NEW_RECONCILE,
			     RECONCILE_ASSISTANT_MENU,
			     RECONCILE_ASSISTANT_MENU,
			     NULL );
    gsb_assistant_add_page ( assistant,
			     gsb_assistant_reconcile_config_page_automatically_associate (assistant),
			     RECONCILE_ASSISTANT_AUTOMATICALLY_ASSOCIATE,
			     RECONCILE_ASSISTANT_MENU,
			     RECONCILE_ASSISTANT_MENU,
			     G_CALLBACK (gsb_assistant_reconcile_config_update_auto_asso));
    gsb_assistant_add_page ( assistant,
			     gsb_assistant_reconcile_config_page_manually_associate (assistant),
			     RECONCILE_ASSISTANT_MANUALLY_ASSOCIATE,
			     RECONCILE_ASSISTANT_MENU,
			     RECONCILE_ASSISTANT_MENU,
			     G_CALLBACK (gsb_assistant_reconcile_config_update_manu_asso));
    gsb_assistant_add_page ( assistant,
			     gsb_assistant_reconcile_config_page_success (),
			     RECONCILE_ASSISTANT_SUCCESS,
			     RECONCILE_ASSISTANT_MENU,
			     RECONCILE_ASSISTANT_MENU,
			     NULL );
    return_value = gsb_assistant_run (assistant);
    gtk_widget_destroy (assistant);
    return return_value;
}
/**
 * this function is called to launch the file opening assistant
 *
 * \param first_opening TRUE if this is the first opening of Grisbi,
 * 			and that assistant is continuing from the general configuration assistant
 * \param import	TRUE if we come from import (possible when we do file->import without anything
 * 			opened, because we need to set up the variables and currencies)
 *
 * \return a GtkResponseType containing the return value at the end of the assistant
 * */
GtkResponseType gsb_assistant_file_run ( gboolean first_opening,
        gboolean import )
{
    GtkResponseType return_value;
    GtkWidget *assistant;
    gchar *currency_name, *currency_iso_code, *currency_nickname;
    gint currency_floating;
    GtkTreeIter iter;
    gboolean launch_account_assistant;
    const gchar *text_1, *text_2;

    /* create the assistant */
    if (first_opening)
    {
        text_1 = _("Welcome to Grisbi!");
        text_2 = _("General configuration of Grisbi is done.\n\n"
                   "Next assistant will now help you to create and configure a new account file.\n"
                   "Don't forget that you can change everything later in the 'Preferences' dialog.\n\n"
                   "Once the file created, you will be able to create a new account "
                   "or to import some previous datas");
    }
    else
    {
        if (import)
        {
            text_1 = _("New file Assistant to import");
            text_2 = _("Before importing files, you need to create a new file and fill "
                       "some default values as created a new currency.\n"
                       "This assistant will help you to create a new account file.\n\n"
                       "Every configuration step from this assistant can be edited "
                       "later in the 'Preferences' dialog.\n"
                       "Once you have created your account file, you will be able to "
                       "create a new account from scratch or to import previous data, i.e. "
                       "from your previous accounting software or your bank website.");
        }
        else
        {
            text_1 = _("New file Assistant");
            text_2 = _("This assistant will help you to create a new account file.\n\n"
                       "Every configuration step from this assistant can be edited "
                       "later in the 'Preferences' dialog.\n"
                       "Once you have created your account file, you will be able to "
                       "create a new account from scratch or to import previous data, i.e. "
                       "from your previous accounting software or your bank website.");
        }
    }

    assistant = gsb_assistant_new (text_1, text_2,
                                   "grisbi.png", NULL);

    gsb_assistant_add_page ( assistant,
                             gsb_assistant_file_page_2 (assistant),
                             FILE_ASSISTANT_PAGE_2,
                             FILE_ASSISTANT_INTRO,
                             FILE_ASSISTANT_PAGE_3,
                             NULL );
    gsb_assistant_add_page ( assistant,
                             gsb_assistant_file_page_3 (assistant),
                             FILE_ASSISTANT_PAGE_3,
                             FILE_ASSISTANT_PAGE_2,
                             FILE_ASSISTANT_PAGE_4,
                             NULL );
    gsb_assistant_add_page ( assistant,
                             gsb_assistant_file_page_4 (assistant),
                             FILE_ASSISTANT_PAGE_4,
                             FILE_ASSISTANT_PAGE_3,
                             FILE_ASSISTANT_PAGE_5,
                             NULL );
    gsb_assistant_add_page ( assistant,
                             gsb_assistant_file_page_5 (assistant),
                             FILE_ASSISTANT_PAGE_5,
                             FILE_ASSISTANT_PAGE_4,
                             FILE_ASSISTANT_PAGE_FINISH,
                             NULL );
    gsb_assistant_add_page ( assistant,
                             gsb_assistant_file_page_finish (assistant, import),
                             FILE_ASSISTANT_PAGE_FINISH,
                             FILE_ASSISTANT_PAGE_5,
                             0,
                             NULL );

    /* now we launch the assistant */
    return_value = gsb_assistant_run (assistant);

    if (return_value == GTK_RESPONSE_CANCEL)
    {
        /* the user stopped the assistant, we delete all the configured data */
        init_variables ();
        gtk_widget_destroy (assistant);
        return return_value;
    }

    /* the assistant is finished, we save the values not saved before */

    /* get the currency */
    if (gtk_tree_selection_get_selected ( gtk_tree_view_get_selection (GTK_TREE_VIEW (g_object_get_data (G_OBJECT (currency_list_box),
                                          "treeview"))),
                                          NULL,
                                          &iter ))
    {
        /* there is a selection, normaly, always the case */
        gtk_tree_model_get ( GTK_TREE_MODEL (g_object_get_data(G_OBJECT (currency_list_box),
                                             "model")),
                             &iter,
                             CURRENCY_NAME_COLUMN, &currency_name,
                             CURRENCY_ISO_CODE_COLUMN, &currency_iso_code,
                             CURRENCY_NICKNAME_COLUMN, &currency_nickname,
                             CURRENCY_FLOATING_COLUMN, &currency_floating,
                             -1 );
        gsb_currency_config_create_currency ( currency_name, currency_nickname,
                                              currency_iso_code, currency_floating);

        /* update the currency list for combobox */
        gsb_currency_update_combobox_currency_list ();
    }

    /* create the list of categories
     * the choice is in the assistant widget under the key "choice_value" */
    gsb_category_assistant_create_categories (assistant);

    /* get the next assistant to launch */
    launch_account_assistant = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
                               (button_create_account_next));
    gtk_widget_destroy (assistant);

    /* initialise le logo accueil */
    gsb_select_icon_set_logo_pixbuf (
        gsb_select_icon_get_default_logo_pixbuf ( ) );

    /* and now, launch the next assistant */
    if (launch_account_assistant)
        gsb_file_new_finish ();
    else
        importer_fichier ();

    return return_value;
}
/**
 * called by menubar to obfuscate the file
 *
 * \param
 *
 * \return TRUE
 * */
gboolean file_obfuscate_run ( void )
{
    GtkWidget *assistant;
    gint result;

    gsb_status_message ( _("Obfuscating file...") );

    assistant = gsb_assistant_new ( _("Grisbi file obfuscation"),
				    _("This assistant produces anonymized copies of account files, with "
				      "all personal data replaced with harmless random data, in order to "
				      "attach an anonimized copy of your Grisbi file with any bug report "
				      "you submit."
				      "\n\n"
				      "That said, please check that bugs you submit are still valid with "
				      "anonymized version of your files.\n"
				      "\n"
				      "To avoid any problems in your file, after saving the modified file, "
				      "Grisbi will close without letting you saving anything.  "
				      "So if you didn't save your changes, please stop this assistant, "
				      "save your work and restart the obfuscation process.\n\n" 
				      "In next page, you will be able to select individual features to "
				      "obfuscate or to keep depending on the level of privacy needed."),
				    "bug.png",
				    NULL );

    gsb_assistant_add_page ( assistant,
			     file_obfuscate_page_1 (),
			     1, 0, 2, NULL ); 
    gsb_assistant_add_page ( assistant,
			     file_obfuscate_page_2 (),
			     2, 1, -1, NULL ); 
    
    result = gsb_assistant_run ( assistant );

    if (result == GTK_RESPONSE_APPLY)
    {
	/* obfuscate the file */
	GSList *tmp_list;
	gchar *filename;
	
	/*  remove the swp file */
	gsb_file_util_modify_lock (FALSE);

	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_accounts_details)))
	{
	    /* hide the details of account but not the names */
	    tmp_list = gsb_data_account_get_list_accounts ();
	    while (tmp_list)
	    {
		gint account_number = gsb_data_account_get_no_account (tmp_list -> data);
		
		gsb_data_account_set_id (account_number,
					 g_strdup_printf ("id account %d", account_number));
		gsb_data_account_set_comment (account_number, NULL);
		gsb_data_account_set_holder_name (account_number, NULL);
		gsb_data_account_set_holder_address (account_number, NULL);
		gsb_data_account_set_init_balance (account_number, null_real);
		gsb_data_account_set_mini_balance_wanted (account_number, null_real);
		gsb_data_account_set_mini_balance_authorized (account_number, null_real);
		gsb_data_account_set_bank_branch_code (account_number, NULL);
		gsb_data_account_set_bank_account_number (account_number, NULL);
		gsb_data_account_set_bank_account_key (account_number, NULL);

		tmp_list = tmp_list -> next;
	    }
	}

	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_amount)))
	{
	    /* hide the amounts of transactions */
	    tmp_list = gsb_data_transaction_get_complete_transactions_list ();
	    while (tmp_list)
	    {
		gint transaction_number = gsb_data_transaction_get_transaction_number (tmp_list -> data);

		gsb_data_transaction_set_amount (transaction_number, null_real);
		gsb_data_transaction_set_voucher (transaction_number, NULL);
		gsb_data_transaction_set_bank_references (transaction_number, NULL);

		tmp_list = tmp_list -> next;
	    }

	    /* hide the amounts of scheduled transactions */
	    tmp_list = gsb_data_scheduled_get_scheduled_list ();
	    while (tmp_list)
	    {
		gint scheduled_number = gsb_data_scheduled_get_scheduled_number (tmp_list -> data);

		gsb_data_scheduled_set_amount (scheduled_number, null_real);
		tmp_list = tmp_list -> next;
	    }
	}

	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_accounts_names)))
	{
	    /* hide the accounts names */
	    tmp_list = gsb_data_account_get_list_accounts ();
	    while (tmp_list)
	    {
		gint account_number = gsb_data_account_get_no_account (tmp_list -> data);
		
		gsb_data_account_set_name (account_number,
					   g_strdup_printf ("Account n°%d", account_number));

		tmp_list = tmp_list -> next;
	    }
	}

	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_payee)))
	{
	    /* hide the payees names */
	    tmp_list = gsb_data_payee_get_payees_list ();
	    while (tmp_list)
	    {
		gint payee_number = gsb_data_payee_get_no_payee (tmp_list -> data);

		gsb_data_payee_set_name (payee_number,
					 g_strdup_printf ( "Payee n°%d", payee_number));
		gsb_data_payee_set_description (payee_number, NULL);

		tmp_list = tmp_list -> next;
	    }
	}

	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_categories)))
	{
	    /* hide the categories */
	    tmp_list = gsb_data_category_get_categories_list ();
	    while (tmp_list)
	    {
		GSList *list_sub_categ;
		gint category_number = gsb_data_category_get_no_category (tmp_list -> data);

		gsb_data_category_set_name (category_number,
					    g_strdup_printf ( "Category n°%d", category_number));

		list_sub_categ = gsb_data_category_get_sub_category_list (category_number);
		while (list_sub_categ)
		{
		    gint sub_categ_number = gsb_data_category_get_no_sub_category (list_sub_categ -> data);

		    gsb_data_category_set_sub_category_name (category_number, sub_categ_number,
							     g_strdup_printf ("Sub-category n°%d", sub_categ_number));
		    list_sub_categ = list_sub_categ -> next;
		}
		tmp_list = tmp_list -> next;
	    }
	}

	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_budgets)))
	{
	    /* hide the budgets */
	    tmp_list = gsb_data_budget_get_budgets_list ();
	    while (tmp_list)
	    {
		GSList *list_sub_budget;
		gint budget_number = gsb_data_budget_get_no_budget (tmp_list -> data);

		gsb_data_budget_set_name (budget_number,
					  g_strdup_printf ( "Budget n°%d", budget_number));

		list_sub_budget = gsb_data_budget_get_sub_budget_list (budget_number);
		while (list_sub_budget)
		{
		    gint sub_budget_number = gsb_data_budget_get_no_sub_budget (list_sub_budget -> data);

		    gsb_data_budget_set_sub_budget_name (budget_number, sub_budget_number,
							 g_strdup_printf ("Sub-budget n°%d", sub_budget_number));
		    list_sub_budget = list_sub_budget -> next;
		}
		tmp_list = tmp_list -> next;
	    }
	}

	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_notes)))
	{
	    /* hide the notes */
	    tmp_list = gsb_data_transaction_get_complete_transactions_list ();
	    while (tmp_list)
	    {
		gint transaction_number = gsb_data_transaction_get_transaction_number (tmp_list -> data);

		gsb_data_transaction_set_notes (transaction_number, NULL);

		tmp_list = tmp_list -> next;
	    }

	    /* hide the notes of scheduled transactions */
	    tmp_list = gsb_data_scheduled_get_scheduled_list ();
	    while (tmp_list)
	    {
		gint scheduled_number = gsb_data_scheduled_get_scheduled_number (tmp_list -> data);

		gsb_data_scheduled_set_notes (scheduled_number, NULL);
		tmp_list = tmp_list -> next;
	    }
	}

	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_banks)))
	{
	    /* hide the banks */
	    tmp_list = gsb_data_bank_get_bank_list ();
	    while (tmp_list)
	    {
		gint bank_number = gsb_data_bank_get_no_bank (tmp_list -> data);

		gsb_data_bank_set_name (bank_number,
					g_strdup_printf ("Bank n°%d", bank_number));
		gsb_data_bank_set_code (bank_number, NULL);
		gsb_data_bank_set_bank_address (bank_number, NULL);
		gsb_data_bank_set_bank_tel (bank_number, NULL);
		gsb_data_bank_set_bank_mail (bank_number, NULL);
		gsb_data_bank_set_bank_web (bank_number, NULL);
		gsb_data_bank_set_bank_note (bank_number, NULL);
		gsb_data_bank_set_correspondent_name (bank_number, NULL);
		gsb_data_bank_set_correspondent_tel (bank_number, NULL);
		gsb_data_bank_set_correspondent_mail (bank_number, NULL);
		gsb_data_bank_set_correspondent_fax (bank_number, NULL);

		tmp_list = tmp_list -> next;
	    }


	}

	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_reports)))
	{
	    /* hide the reports names*/
	    tmp_list = gsb_data_report_get_report_list ();
	    while (tmp_list)
	    {
		gint report_number = gsb_data_report_get_report_number (tmp_list -> data);

		gsb_data_report_set_report_name ( report_number, 
						  g_strdup_printf ( "Report n°%d", report_number));

		tmp_list = tmp_list -> next;
	    }
	}

	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_reconcile)))
	{
	    /* hide the reconciles */
	    GList *reconcile_list;
	    reconcile_list = gsb_data_reconcile_get_reconcile_list ();
	    while (reconcile_list)
	    {
		gint reconcile_number = gsb_data_reconcile_get_no_reconcile (reconcile_list -> data);

		gsb_data_reconcile_set_init_balance (reconcile_number, null_real);
		gsb_data_reconcile_set_final_balance (reconcile_number, null_real);

		reconcile_list = reconcile_list -> next;
	    }
	}

	if (nom_fichier_comptes)
	{
	    /* remove the .gsb */
	    nom_fichier_comptes[strlen(nom_fichier_comptes) -4] = 0;
	    filename = g_strconcat ( nom_fichier_comptes, "-obfuscated.gsb", NULL);
	}
	else
	    filename = g_strconcat ( my_get_gsb_file_default_dir (), "No_name-obfuscated.gsb", NULL);

	if (gsb_file_save_save_file (filename, FALSE, FALSE))
	    dialogue_hint ( g_strdup_printf ( _("Obfuscated file saved as\n'%s'"), filename ),
				    _("Obfuscation succeeded") );
	else
	    dialogue_error_hint (g_strdup_printf (_("Grisbi couldn't save the file\n'%s'"), filename ),
				 _("Obfuscation failed") );

	/* bye bye */
	exit (0);
    }

    gtk_widget_destroy ( assistant );
    gsb_status_message ( _("Done.") );

    return FALSE;
}