/*
 * 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 "mn-pi-mailbox-properties.h"
%}

%privateheader{
#include "mn-imap-mailbox.h"
%}

%{
#include <glib/gi18n.h>
#include "mn-mailbox-properties.h"
#include "mn-mailbox-properties-private.h"
#include "mn-authenticated-mailbox-properties-private.h"
#include "mn-pi-mailbox-properties-private.h"
#include "mn-auth-combo-box.h"
#include "mn-util.h"
#include "mn-properties-dialog.h"
%}

class MN:IMAP:Mailbox:Properties from MN:PI:Mailbox:Properties
{
  private GtkWidget *inbox_radio;
  private GtkWidget *other_radio;
  private GtkWidget *mailbox_entry;
  private GtkWidget *use_idle_radio[MN_IMAP_MAILBOX_N_USE_IDLE];

  property BOOLEAN complete (override)
    get
    {
      gboolean complete;

      complete = mn_pi_mailbox_properties_is_complete(MN_PI_MAILBOX_PROPERTIES(self));
      if (complete)
	{
	  gboolean other_active;
	  const char *mailbox;

	  other_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selfp->other_radio));
	  mailbox = gtk_entry_get_text(GTK_ENTRY(selfp->mailbox_entry));

	  if (other_active && ! *mailbox)
	    complete = FALSE;
	}

      g_value_set_boolean(VAL, complete);
    };

  property STRING default_name (override)
    get
    {
      const char *username;
      const char *server;
      gboolean other_active;
      const char *mailbox;

      mn_authenticated_mailbox_properties_get_contents(MN_AUTHENTICATED_MAILBOX_PROPERTIES(self),
						       &username,
						       NULL);
      mn_pi_mailbox_properties_get_contents(MN_PI_MAILBOX_PROPERTIES(self),
					    NULL,
					    NULL,
					    &server,
					    NULL);

      other_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selfp->other_radio));
      mailbox = gtk_entry_get_text(GTK_ENTRY(selfp->mailbox_entry));

      g_value_take_string(VAL, mn_imap_mailbox_build_name(username, server, other_active ? mailbox : NULL));
    };

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

    p_class->type = "imap";
    p_class->combo_label = "IMAP";
  }

  init (self)
  {
    MNMailboxProperties *properties = MN_MAILBOX_PROPERTIES(self);
    MNPIMailboxProperties *pi = MN_PI_MAILBOX_PROPERTIES(self);
    GtkWidget *hbox;
    GtkWidget *folder_vbox;
    GtkWidget *idle_section;
    GtkWidget *idle_vbox;
    int i;

    /* translators: header capitalization */
    folder_vbox = mn_mailbox_properties_add_general_section(properties, _("IMAP Mailbox"));

    selfp->inbox_radio = gtk_radio_button_new_with_mnemonic(NULL, _("In_box"));
    gtk_size_group_add_widget(properties->label_size_group, selfp->inbox_radio);

    gtk_box_pack_start(GTK_BOX(folder_vbox), selfp->inbox_radio, FALSE, FALSE, 0);

    selfp->other_radio = gtk_radio_button_new_with_mnemonic_from_widget(GTK_RADIO_BUTTON(selfp->inbox_radio), _("_Other:"));
    gtk_size_group_add_widget(properties->label_size_group, selfp->other_radio);

    selfp->mailbox_entry = gtk_entry_new();
    gtk_widget_set_sensitive(selfp->mailbox_entry, FALSE);

    hbox = gtk_hbox_new(FALSE, 12);
    gtk_box_pack_start(GTK_BOX(hbox), selfp->other_radio, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(hbox), selfp->mailbox_entry, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(folder_vbox), hbox, FALSE, FALSE, 0);
    gtk_widget_show_all(folder_vbox);

    /* translators: header capitalization */
    idle_section = mn_hig_section_new_with_box(_("Use the IDLE Extension"), NULL, &idle_vbox);
    gtk_box_pack_start(GTK_BOX(pi->connection_page), idle_section, FALSE, FALSE, 0);
    gtk_widget_show(idle_section);

    self_add_idle_usage(self, GTK_BOX(idle_vbox), MN_IMAP_MAILBOX_USE_IDLE_NEVER, Q_("IMAP connection tab|_Never"));
    self_add_idle_usage(self, GTK_BOX(idle_vbox), MN_IMAP_MAILBOX_USE_IDLE_AUTODETECT, _("A_utodetect"));
    self_add_idle_usage(self, GTK_BOX(idle_vbox), MN_IMAP_MAILBOX_USE_IDLE_ALWAYS, _("Al_ways"));

    /* by default, autodetect is enabled */
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(selfp->use_idle_radio[MN_IMAP_MAILBOX_USE_IDLE_AUTODETECT]), TRUE);

    mn_auth_combo_box_append(MN_AUTH_COMBO_BOX(pi->auth_combo), "+LOGIN", "IMAP LOGIN");

    for (i = 0; i < MN_PI_MAILBOX_N_CONNECTION_TYPES; i++)
      gtk_spin_button_set_value(GTK_SPIN_BUTTON(pi->port_spin[i]), mn_imap_mailbox_default_ports[i]);

    properties->entries = g_slist_append(properties->entries, selfp->mailbox_entry);

    g_signal_connect(selfp->inbox_radio, "toggled", G_CALLBACK(self_radio_toggled_h), self);
    g_signal_connect(selfp->other_radio, "toggled", G_CALLBACK(self_radio_toggled_h), self);

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

  private void
    add_idle_usage (self,
		    Gtk:Box *idle_vbox (check null),
		    MNIMAPMailboxUseIDLE usage,
		    const char *mnemonic (check null))
  {
    selfp->use_idle_radio[usage] = usage == 0
      ? gtk_radio_button_new_with_mnemonic(NULL, mnemonic)
      : gtk_radio_button_new_with_mnemonic_from_widget(GTK_RADIO_BUTTON(selfp->use_idle_radio[0]), mnemonic);

    gtk_box_pack_start(idle_vbox, selfp->use_idle_radio[usage], TRUE, TRUE, 0);
    gtk_widget_show(selfp->use_idle_radio[usage]);
  }

  private void
    radio_toggled_h (GtkToggleButton *togglebutton, gpointer user_data)
  {
    Self *self = user_data;

    gtk_widget_set_sensitive(selfp->mailbox_entry, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selfp->other_radio)));

    g_object_notify(G_OBJECT(self), "complete");
    g_object_notify(G_OBJECT(self), "default-name");
  }

  override (MN:Mailbox:Properties) void
    set_mailbox (MNMailboxProperties *properties, MNMailbox *mailbox)
  {
    Self *self = SELF(properties);
    MNIMAPMailbox *imap_mailbox = MN_IMAP_MAILBOX(mailbox);

    PARENT_HANDLER(properties, mailbox);

    if (! mn_utf8_strcasecmp(imap_mailbox->mailbox, "INBOX"))
      gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(selfp->inbox_radio), TRUE);
    else
      {
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(selfp->other_radio), TRUE);
	gtk_entry_set_text(GTK_ENTRY(selfp->mailbox_entry), imap_mailbox->mailbox);
      }

    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(selfp->use_idle_radio[imap_mailbox->use_idle_extension]), TRUE);
  }

  override (MN:Mailbox:Properties) MNMailbox *
    get_mailbox (MNMailboxProperties *properties)
  {
    Self *self = SELF(properties);
    MNIMAPMailboxUseIDLE use_idle;
    MNMailbox *mailbox;

    mailbox = PARENT_HANDLER(properties);

    for (use_idle = 0; use_idle < MN_IMAP_MAILBOX_N_USE_IDLE; use_idle++)
      if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selfp->use_idle_radio[use_idle])))
	break;

    g_object_set(mailbox, MN_IMAP_MAILBOX_PROP_USE_IDLE_EXTENSION(use_idle), NULL);

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selfp->other_radio)))
      g_object_set(mailbox, MN_IMAP_MAILBOX_PROP_MAILBOX((char *) gtk_entry_get_text(GTK_ENTRY(selfp->mailbox_entry))), NULL);

    return mailbox;
  }
}
