/*
 * Mail Notification
 * Copyright (C) 2003-2008 Jean-Yves Lefort <jylefort@brutele.be>
 *
 * This program 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.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

%headertop{
#include <gtk/gtk.h>
#include "mn-mailbox-properties.h"
%}

%{
#include <glib/gi18n.h>
#include "mn-mailbox-properties.h"
#include "mn-mailbox-properties-private.h"
#include "mn-util.h"
#include "mn-properties-dialog.h"
#if WITH_MBOX || WITH_MOZILLA || WITH_MH || WITH_MAILDIR || WITH_SYLPHEED
#include "mn-custom-vfs-mailbox.h"
#endif
#include "mn-stock.h"

static char *current_folder_uri = NULL;
%}

class MN:Autodetect:Mailbox:Properties from MN:Mailbox:Properties
{
  private GtkWidget *location_entry;
  private GtkWidget *chooser;

  property BOOLEAN complete (override)
    get
    {
      const char *location;
      gboolean complete = FALSE;

      location = gtk_entry_get_text(GTK_ENTRY(SELF(self)->_priv->location_entry));
      if (*location)
	{
	  MNMailbox *mailbox;

	  mailbox = mn_mailbox_new_from_uri(location);
	  if (mailbox)
	    {
	      complete = TRUE;
	      g_object_unref(mailbox);
	    }
	}

      g_value_set_boolean(VAL, complete);
    };

  property STRING default_name (override)
    get
    {
      char *name = NULL;
      MNMailbox *mailbox;

      mailbox = mn_mailbox_new_from_uri(gtk_entry_get_text(GTK_ENTRY(selfp->location_entry)));
      if (mailbox)
	{
	  mn_mailbox_seal(mailbox);
	  name = g_strdup(mailbox->runtime_name);
	  g_object_unref(mailbox);
	}

      g_value_take_string(VAL, name);
    };

  property INT default_check_delay (override)
    get
    {
      int check_delay = -1;
      MNMailbox *mailbox;

      mailbox = mn_mailbox_new_from_uri(gtk_entry_get_text(GTK_ENTRY(selfp->location_entry)));
      if (mailbox)
	{
	  mn_mailbox_seal(mailbox);
	  check_delay = mailbox->runtime_check_delay;
	  g_object_unref(mailbox);
	}

      g_value_set_int(VAL, check_delay);
    };

  class_init (class)
  {
    MNMailboxPropertiesClass *p_class = MN_MAILBOX_PROPERTIES_CLASS(class);

    p_class->type = "custom-vfs";
    p_class->stock_id = MN_STOCK_UNKNOWN;
    p_class->combo_label = _("Autodetect");
  }

  init (self)
  {
    MNMailboxProperties *properties = MN_MAILBOX_PROPERTIES(self);
    GtkWidget *location_vbox;
    GtkWidget *outer_hbox;
    GtkWidget *hbox;
    GtkWidget *label;
    GtkWidget *button;

    /* translators: header capitalization */
    location_vbox = mn_mailbox_properties_add_general_section(properties, _("Location"));

    label = gtk_label_new_with_mnemonic(_("_Location:"));
    gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);

    outer_hbox = gtk_hbox_new(FALSE, 12);
    hbox = gtk_hbox_new(FALSE, 6);

    selfp->location_entry = gtk_entry_new();
    /* translators: header capitalization */
    button = gtk_button_new_with_mnemonic(_("_Browse..."));

    gtk_label_set_mnemonic_widget(GTK_LABEL(label), selfp->location_entry);
    properties->entries = g_slist_append(properties->entries, selfp->location_entry);

    gtk_box_pack_start(GTK_BOX(hbox), selfp->location_entry, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);

    gtk_box_pack_start(GTK_BOX(outer_hbox), label, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(outer_hbox), hbox, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(location_vbox), outer_hbox, FALSE, FALSE, 0);

    gtk_widget_show_all(outer_hbox);

    gtk_size_group_add_widget(MN_MAILBOX_PROPERTIES(self)->label_size_group, label);

    g_object_connect(selfp->location_entry,
		     "swapped-signal::changed", mn_mailbox_properties_notify_complete, self,
		     "swapped-signal::changed", mn_mailbox_properties_notify_default_name, self,
		     "swapped-signal::changed", mn_mailbox_properties_notify_default_check_delay, self,
		     NULL);

    g_signal_connect(button, "clicked", G_CALLBACK(self_browse_clicked_h), self);
  }

  private void
    browse_clicked_h (GtkButton *button, gpointer user_data)
  {
    Self *self = user_data;
    GtkWidget *toplevel;
    const char *location;

    if (selfp->chooser)
      {
	mn_window_present_from_event(GTK_WINDOW(selfp->chooser));
	return;
      }

    toplevel = gtk_widget_get_toplevel(GTK_WIDGET(button));
    /* translators: header capitalization */
    selfp->chooser = gtk_file_chooser_dialog_new(_("Select a File or Folder"),
							      GTK_WINDOW(toplevel),
							      GTK_FILE_CHOOSER_ACTION_OPEN,
							      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
							      GTK_STOCK_OPEN, 1,
							      NULL);
    mn_add_weak_pointer(&selfp->chooser);

    gtk_window_set_destroy_with_parent(GTK_WINDOW(selfp->chooser), TRUE);
    gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(selfp->chooser), FALSE);
    mn_file_chooser_dialog_allow_select_folder(GTK_FILE_CHOOSER_DIALOG(selfp->chooser), 1);

    location = gtk_entry_get_text(GTK_ENTRY(selfp->location_entry));
    if (*location)
      gtk_file_chooser_set_uri(GTK_FILE_CHOOSER(selfp->chooser), location);
    else if (current_folder_uri)
      gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(selfp->chooser), current_folder_uri);

    g_object_connect(selfp->chooser,
		     "signal::current-folder-changed", self_current_folder_changed_h, self,
		     "signal::response", self_chooser_response_h, self,
		     NULL);

    gtk_widget_show(selfp->chooser);
  }

  private void
    current_folder_changed_h (GtkFileChooser *chooser, gpointer user_data)
  {
    g_free(current_folder_uri);
    current_folder_uri = gtk_file_chooser_get_current_folder_uri(chooser);
  }

  private void
    chooser_response_h (GtkDialog *dialog, int response, gpointer user_data)
  {
    if (response == 1)
      {
	Self *self = user_data;
	char *uri;

	uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
	gtk_entry_set_text(GTK_ENTRY(selfp->location_entry), uri);
	g_free(uri);
      }

    gtk_widget_destroy(GTK_WIDGET(dialog));
  }

  override (MN:Mailbox:Properties) void
    set_mailbox (MNMailboxProperties *properties, MNMailbox *mailbox)
  {
#if WITH_MBOX || WITH_MOZILLA || WITH_MH || WITH_MAILDIR || WITH_SYLPHEED
    g_return_if_fail(MN_IS_CUSTOM_VFS_MAILBOX(mailbox));

    gtk_entry_set_text(GTK_ENTRY(SELF(properties)->_priv->location_entry), MN_CUSTOM_VFS_MAILBOX(mailbox)->uri);
#else
    g_assert_not_reached();
#endif
  }

  override (MN:Mailbox:Properties) MNMailbox *
    get_mailbox (MNMailboxProperties *properties)
  {
    const char *uri;

    uri = gtk_entry_get_text(GTK_ENTRY(SELF(properties)->_priv->location_entry));
    return mn_mailbox_new_from_uri(uri);
  }
}
