/*
 * 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-dialog.h"
%}

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

%{
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include "mn-conf.h"
#include "mn-util.h"
#include "mn-mailbox-view.h"
#include "mn-shell.h"
#include "mn-file-chooser-button.h"
#include "mn-sound-file-chooser-dialog.h"
#include "mn-test-mailbox.h"
#include "mn-non-linear-range.h"

static const MNNonLinearRangeBlock expiration_blocks[] = {
  { MN_SECS(1),		MN_SECS(10),	MN_SECS(1)	},
  { MN_SECS(15),	MN_SECS(60),	MN_SECS(5)	},
  { MN_MINS(2),		MN_MINS(10),	MN_MINS(1)	},
  { MN_MINS(15),	MN_MINS(60),	MN_MINS(5)	}
};
%}

class MN:Properties:Dialog from MN:Dialog
{
  private GtkWidget *notebook;

  /* general tab */
  private GtkWidget *play_sound_hbox;
  private GtkWidget *play_sound_check;
  private GtkWidget *play_sound_file_chooser_button;
  private GtkWidget *scrolled;
  private GtkWidget *list;
  private GtkWidget *selected_label;
  private GtkWidget *add;
  private GtkWidget *remove;
  private GtkWidget *properties;

  /* status icon tab */
  private GtkWidget *icon_action_mail_reader_radio;
  private GtkWidget *icon_action_open_latest_message_radio;
  private GtkWidget *icon_action_consider_new_mail_as_read_radio;
  private GtkWidget *icon_action_update_radio;

  /* message popups tab */
  private GtkWidget *popups_enable_check;
  private GtkWidget *popups_position_section_label;
  private GtkWidget *popups_position_attached_radio;
  private GtkWidget *popups_position_free_radio;
  private GtkWidget *popups_expiration_section_label;
  private GtkWidget *popups_expiration_default_radio;
  private GtkWidget *popups_expiration_never_radio;
  private GtkWidget *popups_expiration_after_radio;
  private GtkWidget *popups_expiration_scale;

  private MNMailbox *test_mailbox destroy { self_remove_test_mailbox(self); };

  init (self)
  {
    GtkTreeSelection *selection;

    mn_container_create_interface(GTK_CONTAINER(self),
				  PKGDATADIR G_DIR_SEPARATOR_S "properties-dialog.ui",
				  "main_vbox",
				  "mn_properties_dialog_",
				  "notebook", &selfp->notebook,
				  "play_sound_hbox", &selfp->play_sound_hbox,
				  "play_sound_check", &selfp->play_sound_check,
				  "scrolled", &selfp->scrolled,
				  "selected_label", &selfp->selected_label,
				  "add", &selfp->add,
				  "remove", &selfp->remove,
				  "properties", &selfp->properties,
				  "icon_action_mail_reader_radio", &selfp->icon_action_mail_reader_radio,
				  "icon_action_open_latest_message_radio", &selfp->icon_action_open_latest_message_radio,
				  "icon_action_consider_new_mail_as_read_radio", &selfp->icon_action_consider_new_mail_as_read_radio,
				  "icon_action_update_radio", &selfp->icon_action_update_radio,
				  "popups_enable_check", &selfp->popups_enable_check,
				  "popups_position_section_label", &selfp->popups_position_section_label,
				  "popups_position_attached_radio", &selfp->popups_position_attached_radio,
				  "popups_position_free_radio", &selfp->popups_position_free_radio,
				  "popups_expiration_section_label", &selfp->popups_expiration_section_label,
				  "popups_expiration_default_radio", &selfp->popups_expiration_default_radio,
				  "popups_expiration_never_radio", &selfp->popups_expiration_never_radio,
				  "popups_expiration_after_radio", &selfp->popups_expiration_after_radio,
				  "popups_expiration_scale", &selfp->popups_expiration_scale,
				  NULL);

    /* translators: header capitalization */
    gtk_window_set_title(GTK_WINDOW(self), _("Mail Notification Properties"));

    gtk_dialog_add_buttons(GTK_DIALOG(self),
			   GTK_STOCK_HELP, GTK_RESPONSE_HELP,
			   GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
			   NULL);

    selfp->play_sound_file_chooser_button = mn_file_chooser_button_new(self_play_sound_file_chooser_create_dialog_cb, NULL);
    gtk_box_pack_start(GTK_BOX(selfp->play_sound_hbox), selfp->play_sound_file_chooser_button, TRUE, TRUE, 0);
    gtk_widget_show(selfp->play_sound_file_chooser_button);

    selfp->list = mn_mailbox_view_new();
    gtk_container_add(GTK_CONTAINER(selfp->scrolled), selfp->list);
    gtk_widget_show(selfp->list);

    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(selfp->list));
    g_object_connect(selection,
		     "swapped-signal::changed", self_update_selected_label, self,
		     "swapped-signal::changed", self_update_sensitivity, self,
		     NULL);

    mn_setup_dnd(selfp->scrolled);

    mn_non_linear_range_setup_static(GTK_RANGE(selfp->popups_expiration_scale), expiration_blocks, G_N_ELEMENTS(expiration_blocks));

    mn_conf_link_window(GTK_WINDOW(self), MN_CONF_PROPERTIES_DIALOG);
    mn_conf_link_object(selfp->play_sound_check, MN_CONF_SOUNDS_NEW_MAIL_ENABLED, "active");
    mn_conf_link_object(selfp->play_sound_file_chooser_button, MN_CONF_SOUNDS_NEW_MAIL_FILE, "filename");
    mn_conf_link_object(selfp->popups_enable_check, MN_CONF_POPUPS_ENABLED, "active");
    mn_conf_link_non_linear_range(GTK_RANGE(selfp->popups_expiration_scale), MN_CONF_POPUPS_EXPIRATION_DELAY);
    mn_conf_link_radio_group_to_enum(MN_TYPE_ACTION,
				     MN_CONF_CLICK_ACTION,
				     selfp->icon_action_mail_reader_radio, MN_ACTION_LAUNCH_MAIL_READER,
				     selfp->icon_action_open_latest_message_radio, MN_ACTION_OPEN_LATEST_MESSAGE,
				     selfp->icon_action_consider_new_mail_as_read_radio, MN_ACTION_CONSIDER_NEW_MAIL_AS_READ,
				     selfp->icon_action_update_radio, MN_ACTION_UPDATE_MAIL_STATUS,
				     NULL);
    mn_conf_link_radio_group_to_enum(MN_TYPE_POPUP_POSITION,
				     MN_CONF_POPUPS_POSITION,
				     selfp->popups_position_attached_radio, MN_POPUP_POSITION_ATTACHED,
				     selfp->popups_position_free_radio, MN_POPUP_POSITION_FREE,
				     NULL);
    mn_conf_link_radio_group_to_enum(MN_TYPE_EXPIRATION_ENABLED,
				     MN_CONF_POPUPS_EXPIRATION_ENABLED,
				     selfp->popups_expiration_default_radio, MN_EXPIRATION_ENABLED_DEFAULT,
				     selfp->popups_expiration_never_radio, MN_EXPIRATION_ENABLED_FALSE,
				     selfp->popups_expiration_after_radio, MN_EXPIRATION_ENABLED_TRUE,
				     NULL);

    self_update_selected_label(self);
    self_update_sensitivity(self);

    /*
     * HIG chapter 3:
     * "When opening a dialog, provide initial keyboard focus to the
     * component that you expect users to operate first. This focus is
     * especially important for users who must use a keyboard to
     * navigate your application."
     */
    gtk_widget_grab_focus(selfp->add);

    g_signal_connect(self, "response", G_CALLBACK(self_response_h), NULL);
  }

  private GtkFileChooserDialog *
    play_sound_file_chooser_create_dialog_cb (gpointer data)
  {
    return GTK_FILE_CHOOSER_DIALOG(mn_sound_file_chooser_dialog_new());
  }

  private void
    remove_test_mailbox (self)
  {
    if (selfp->test_mailbox)
      {
	mn_mailboxes_remove(mn_shell->mailboxes, selfp->test_mailbox);
	g_object_unref(selfp->test_mailbox);
	selfp->test_mailbox = NULL;
      }
  }

  private void
    response_h (self, int response, gpointer user_data)
  {
    switch (response)
      {
      case GTK_RESPONSE_HELP:
	{
	  int current_page;
	  static const char *sections[] = {
	    "properties-general",
	    "properties-status-icon",
	    "properties-message-popups"
	  };

	  current_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(selfp->notebook));
	  g_assert(current_page >= 0 && current_page < G_N_ELEMENTS(sections));

	  mn_show_help(GTK_WINDOW(self), sections[current_page]);
	  break;
	}

      case GTK_RESPONSE_CLOSE:
	gtk_widget_destroy(GTK_WIDGET(self));
	break;
      }
  }

  private void
    update_selected_label (self)
  {
    GtkTreeSelection *selection;
    int n_rows;

    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(selfp->list));
    n_rows = gtk_tree_selection_count_selected_rows(selection);

    if (n_rows == 0)
      gtk_label_set_text(GTK_LABEL(selfp->selected_label), _("No mailbox selected."));
    else
      {
	char *str;

	str = g_strdup_printf(ngettext("%i mailbox selected.",
				       "%i mailboxes selected.",
				       n_rows), n_rows);
	gtk_label_set_text(GTK_LABEL(selfp->selected_label), str);
	g_free(str);
      }
  }

  private void
    update_sensitivity (self)
  {
    gboolean play_sound_enabled;
    GtkTreeSelection *selection;
    gboolean has_selection;
    gboolean popups_enabled;
    gboolean popups_expiration_after_enabled;

    play_sound_enabled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selfp->play_sound_check));

    gtk_widget_set_sensitive(selfp->play_sound_file_chooser_button, play_sound_enabled);

    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(selfp->list));
    has_selection = gtk_tree_selection_count_selected_rows(selection) > 0;

    gtk_widget_set_sensitive(selfp->remove, has_selection);
    gtk_widget_set_sensitive(selfp->properties, has_selection);

    popups_enabled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selfp->popups_enable_check));
    popups_expiration_after_enabled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selfp->popups_expiration_after_radio));

    gtk_widget_set_sensitive(selfp->popups_position_section_label, popups_enabled);
    gtk_widget_set_sensitive(selfp->popups_position_attached_radio, popups_enabled);
    gtk_widget_set_sensitive(selfp->popups_position_free_radio, popups_enabled);
    gtk_widget_set_sensitive(selfp->popups_expiration_section_label, popups_enabled);
    gtk_widget_set_sensitive(selfp->popups_expiration_default_radio, popups_enabled);
    gtk_widget_set_sensitive(selfp->popups_expiration_never_radio, popups_enabled);
    gtk_widget_set_sensitive(selfp->popups_expiration_after_radio, popups_enabled);
    gtk_widget_set_sensitive(selfp->popups_expiration_scale, popups_enabled && popups_expiration_after_enabled);
  }

  /* libglade callbacks */

  protected void
    toggled_h (self, GtkToggleButton *button)
  {
    self_update_sensitivity(self);
  }

  protected void
    add_clicked_h (self, GtkButton *button)
  {
    mn_mailbox_view_activate_add(MN_MAILBOX_VIEW(selfp->list));
  }

  protected void
    remove_clicked_h (self, GtkButton *button)
  {
    mn_mailbox_view_activate_remove(MN_MAILBOX_VIEW(selfp->list));
  }

  protected void
    properties_clicked_h (self, GtkButton *button)
  {
    mn_mailbox_view_activate_properties(MN_MAILBOX_VIEW(selfp->list));
  }

  protected char *
    popups_expiration_format_value_h (self, double arg, GtkScale *scale)
  {
    return mn_format_seconds(mn_non_linear_range_get_value(GTK_RANGE(scale)));
  }

  protected void
    test_messages_toggled_h (self, GtkToggleButton *button)
  {
    if (gtk_toggle_button_get_active(button))
      {
	if (! selfp->test_mailbox)
	  {
	    selfp->test_mailbox = mn_test_mailbox_new();
	    mn_mailbox_seal(selfp->test_mailbox);
	    mn_mailboxes_add(mn_shell->mailboxes, selfp->test_mailbox);
	  }
      }
    else
      self_remove_test_mailbox(self);
  }
}
