mail-notification

Fork of Jean-Yves Lefort's mail-notification, a tray icon to notify of new mail
git clone https://code.djc.id.au/git/mail-notification/

src/mn-properties-dialog.gob (11680B) - raw

      1 /*
      2  * Mail Notification
      3  * Copyright (C) 2003-2008 Jean-Yves Lefort <jylefort@brutele.be>
      4  *
      5  * This program is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation; either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This program is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License along
     16  * with this program; if not, write to the Free Software Foundation, Inc.,
     17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     18  */
     19 
     20 %headertop{
     21 #include "mn-dialog.h"
     22 %}
     23 
     24 %privateheader{
     25 #include "mn-mailbox.h"
     26 %}
     27 
     28 %{
     29 #include <glib/gi18n.h>
     30 #include <gtk/gtk.h>
     31 #include "mn-conf.h"
     32 #include "mn-util.h"
     33 #include "mn-mailbox-view.h"
     34 #include "mn-shell.h"
     35 #include "mn-file-chooser-button.h"
     36 #include "mn-sound-file-chooser-dialog.h"
     37 #include "mn-test-mailbox.h"
     38 #include "mn-non-linear-range.h"
     39 
     40 static const MNNonLinearRangeBlock expiration_blocks[] = {
     41   { MN_SECS(1),		MN_SECS(10),	MN_SECS(1)	},
     42   { MN_SECS(15),	MN_SECS(60),	MN_SECS(5)	},
     43   { MN_MINS(2),		MN_MINS(10),	MN_MINS(1)	},
     44   { MN_MINS(15),	MN_MINS(60),	MN_MINS(5)	}
     45 };
     46 %}
     47 
     48 class MN:Properties:Dialog from MN:Dialog
     49 {
     50   private GtkWidget *notebook;
     51 
     52   /* general tab */
     53   private GtkWidget *play_sound_hbox;
     54   private GtkWidget *play_sound_check;
     55   private GtkWidget *play_sound_file_chooser_button;
     56   private GtkWidget *scrolled;
     57   private GtkWidget *list;
     58   private GtkWidget *selected_label;
     59   private GtkWidget *add;
     60   private GtkWidget *remove;
     61   private GtkWidget *properties;
     62 
     63   /* status icon tab */
     64   private GtkWidget *icon_action_mail_reader_radio;
     65   private GtkWidget *icon_action_open_latest_message_radio;
     66   private GtkWidget *icon_action_consider_new_mail_as_read_radio;
     67   private GtkWidget *icon_action_update_radio;
     68 
     69   /* message popups tab */
     70   private GtkWidget *popups_enable_check;
     71   private GtkWidget *popups_position_section_label;
     72   private GtkWidget *popups_position_attached_radio;
     73   private GtkWidget *popups_position_free_radio;
     74   private GtkWidget *popups_expiration_section_label;
     75   private GtkWidget *popups_expiration_default_radio;
     76   private GtkWidget *popups_expiration_never_radio;
     77   private GtkWidget *popups_expiration_after_radio;
     78   private GtkWidget *popups_expiration_scale;
     79 
     80   private MNMailbox *test_mailbox destroy { self_remove_test_mailbox(self); };
     81 
     82   init (self)
     83   {
     84     GtkTreeSelection *selection;
     85 
     86     mn_container_create_interface(GTK_CONTAINER(self),
     87 				  PKGDATADIR G_DIR_SEPARATOR_S "properties-dialog.glade",
     88 				  "main_vbox",
     89 				  "mn_properties_dialog_",
     90 				  "notebook", &selfp->notebook,
     91 				  "play_sound_hbox", &selfp->play_sound_hbox,
     92 				  "play_sound_check", &selfp->play_sound_check,
     93 				  "scrolled", &selfp->scrolled,
     94 				  "selected_label", &selfp->selected_label,
     95 				  "add", &selfp->add,
     96 				  "remove", &selfp->remove,
     97 				  "properties", &selfp->properties,
     98 				  "icon_action_mail_reader_radio", &selfp->icon_action_mail_reader_radio,
     99 				  "icon_action_open_latest_message_radio", &selfp->icon_action_open_latest_message_radio,
    100 				  "icon_action_consider_new_mail_as_read_radio", &selfp->icon_action_consider_new_mail_as_read_radio,
    101 				  "icon_action_update_radio", &selfp->icon_action_update_radio,
    102 				  "popups_enable_check", &selfp->popups_enable_check,
    103 				  "popups_position_section_label", &selfp->popups_position_section_label,
    104 				  "popups_position_attached_radio", &selfp->popups_position_attached_radio,
    105 				  "popups_position_free_radio", &selfp->popups_position_free_radio,
    106 				  "popups_expiration_section_label", &selfp->popups_expiration_section_label,
    107 				  "popups_expiration_default_radio", &selfp->popups_expiration_default_radio,
    108 				  "popups_expiration_never_radio", &selfp->popups_expiration_never_radio,
    109 				  "popups_expiration_after_radio", &selfp->popups_expiration_after_radio,
    110 				  "popups_expiration_scale", &selfp->popups_expiration_scale,
    111 				  NULL);
    112 
    113     /* translators: header capitalization */
    114     gtk_window_set_title(GTK_WINDOW(self), _("Mail Notification Properties"));
    115 
    116     gtk_dialog_add_buttons(GTK_DIALOG(self),
    117 			   GTK_STOCK_HELP, GTK_RESPONSE_HELP,
    118 			   GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
    119 			   NULL);
    120 
    121     selfp->play_sound_file_chooser_button = mn_file_chooser_button_new(self_play_sound_file_chooser_create_dialog_cb, NULL);
    122     gtk_box_pack_start(GTK_BOX(selfp->play_sound_hbox), selfp->play_sound_file_chooser_button, TRUE, TRUE, 0);
    123     gtk_widget_show(selfp->play_sound_file_chooser_button);
    124 
    125     selfp->list = mn_mailbox_view_new();
    126     gtk_container_add(GTK_CONTAINER(selfp->scrolled), selfp->list);
    127     gtk_widget_show(selfp->list);
    128 
    129     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(selfp->list));
    130     g_object_connect(selection,
    131 		     "swapped-signal::changed", self_update_selected_label, self,
    132 		     "swapped-signal::changed", self_update_sensitivity, self,
    133 		     NULL);
    134 
    135     mn_setup_dnd(selfp->scrolled);
    136 
    137     mn_non_linear_range_setup_static(GTK_RANGE(selfp->popups_expiration_scale), expiration_blocks, G_N_ELEMENTS(expiration_blocks));
    138 
    139     mn_conf_link_window(GTK_WINDOW(self), MN_CONF_PROPERTIES_DIALOG);
    140     mn_conf_link_object(selfp->play_sound_check, MN_CONF_SOUNDS_NEW_MAIL_ENABLED, "active");
    141     mn_conf_link_object(selfp->play_sound_file_chooser_button, MN_CONF_SOUNDS_NEW_MAIL_FILE, "filename");
    142     mn_conf_link_object(selfp->popups_enable_check, MN_CONF_POPUPS_ENABLED, "active");
    143     mn_conf_link_non_linear_range(GTK_RANGE(selfp->popups_expiration_scale), MN_CONF_POPUPS_EXPIRATION_DELAY);
    144     mn_conf_link_radio_group_to_enum(MN_TYPE_ACTION,
    145 				     MN_CONF_CLICK_ACTION,
    146 				     selfp->icon_action_mail_reader_radio, MN_ACTION_LAUNCH_MAIL_READER,
    147 				     selfp->icon_action_open_latest_message_radio, MN_ACTION_OPEN_LATEST_MESSAGE,
    148 				     selfp->icon_action_consider_new_mail_as_read_radio, MN_ACTION_CONSIDER_NEW_MAIL_AS_READ,
    149 				     selfp->icon_action_update_radio, MN_ACTION_UPDATE_MAIL_STATUS,
    150 				     NULL);
    151     mn_conf_link_radio_group_to_enum(MN_TYPE_POPUP_POSITION,
    152 				     MN_CONF_POPUPS_POSITION,
    153 				     selfp->popups_position_attached_radio, MN_POPUP_POSITION_ATTACHED,
    154 				     selfp->popups_position_free_radio, MN_POPUP_POSITION_FREE,
    155 				     NULL);
    156     mn_conf_link_radio_group_to_enum(MN_TYPE_EXPIRATION_ENABLED,
    157 				     MN_CONF_POPUPS_EXPIRATION_ENABLED,
    158 				     selfp->popups_expiration_default_radio, MN_EXPIRATION_ENABLED_DEFAULT,
    159 				     selfp->popups_expiration_never_radio, MN_EXPIRATION_ENABLED_FALSE,
    160 				     selfp->popups_expiration_after_radio, MN_EXPIRATION_ENABLED_TRUE,
    161 				     NULL);
    162 
    163     self_update_selected_label(self);
    164     self_update_sensitivity(self);
    165 
    166     /*
    167      * HIG chapter 3:
    168      * "When opening a dialog, provide initial keyboard focus to the
    169      * component that you expect users to operate first. This focus is
    170      * especially important for users who must use a keyboard to
    171      * navigate your application."
    172      */
    173     gtk_widget_grab_focus(selfp->add);
    174 
    175     g_signal_connect(self, "response", G_CALLBACK(self_response_h), NULL);
    176   }
    177 
    178   private GtkFileChooserDialog *
    179     play_sound_file_chooser_create_dialog_cb (gpointer data)
    180   {
    181     return GTK_FILE_CHOOSER_DIALOG(mn_sound_file_chooser_dialog_new());
    182   }
    183 
    184   private void
    185     remove_test_mailbox (self)
    186   {
    187     if (selfp->test_mailbox)
    188       {
    189 	mn_mailboxes_remove(mn_shell->mailboxes, selfp->test_mailbox);
    190 	g_object_unref(selfp->test_mailbox);
    191 	selfp->test_mailbox = NULL;
    192       }
    193   }
    194 
    195   private void
    196     response_h (self, int response, gpointer user_data)
    197   {
    198     switch (response)
    199       {
    200       case GTK_RESPONSE_HELP:
    201 	{
    202 	  int current_page;
    203 	  static const char *sections[] = {
    204 	    "properties-general",
    205 	    "properties-status-icon",
    206 	    "properties-message-popups"
    207 	  };
    208 
    209 	  current_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(selfp->notebook));
    210 	  g_assert(current_page >= 0 && current_page < G_N_ELEMENTS(sections));
    211 
    212 	  mn_show_help(GTK_WINDOW(self), sections[current_page]);
    213 	  break;
    214 	}
    215 
    216       case GTK_RESPONSE_CLOSE:
    217 	gtk_widget_destroy(GTK_WIDGET(self));
    218 	break;
    219       }
    220   }
    221 
    222   private void
    223     update_selected_label (self)
    224   {
    225     GtkTreeSelection *selection;
    226     int n_rows;
    227 
    228     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(selfp->list));
    229     n_rows = gtk_tree_selection_count_selected_rows(selection);
    230 
    231     if (n_rows == 0)
    232       gtk_label_set_text(GTK_LABEL(selfp->selected_label), _("No mailbox selected."));
    233     else
    234       {
    235 	char *str;
    236 
    237 	str = g_strdup_printf(ngettext("%i mailbox selected.",
    238 				       "%i mailboxes selected.",
    239 				       n_rows), n_rows);
    240 	gtk_label_set_text(GTK_LABEL(selfp->selected_label), str);
    241 	g_free(str);
    242       }
    243   }
    244 
    245   private void
    246     update_sensitivity (self)
    247   {
    248     gboolean play_sound_enabled;
    249     GtkTreeSelection *selection;
    250     gboolean has_selection;
    251     gboolean popups_enabled;
    252     gboolean popups_expiration_after_enabled;
    253 
    254     play_sound_enabled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selfp->play_sound_check));
    255 
    256     gtk_widget_set_sensitive(selfp->play_sound_file_chooser_button, play_sound_enabled);
    257 
    258     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(selfp->list));
    259     has_selection = gtk_tree_selection_count_selected_rows(selection) > 0;
    260 
    261     gtk_widget_set_sensitive(selfp->remove, has_selection);
    262     gtk_widget_set_sensitive(selfp->properties, has_selection);
    263 
    264     popups_enabled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selfp->popups_enable_check));
    265     popups_expiration_after_enabled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selfp->popups_expiration_after_radio));
    266 
    267     gtk_widget_set_sensitive(selfp->popups_position_section_label, popups_enabled);
    268     gtk_widget_set_sensitive(selfp->popups_position_attached_radio, popups_enabled);
    269     gtk_widget_set_sensitive(selfp->popups_position_free_radio, popups_enabled);
    270     gtk_widget_set_sensitive(selfp->popups_expiration_section_label, popups_enabled);
    271     gtk_widget_set_sensitive(selfp->popups_expiration_default_radio, popups_enabled);
    272     gtk_widget_set_sensitive(selfp->popups_expiration_never_radio, popups_enabled);
    273     gtk_widget_set_sensitive(selfp->popups_expiration_after_radio, popups_enabled);
    274     gtk_widget_set_sensitive(selfp->popups_expiration_scale, popups_enabled && popups_expiration_after_enabled);
    275   }
    276 
    277   /* libglade callbacks */
    278 
    279   protected void
    280     toggled_h (self, GtkToggleButton *button)
    281   {
    282     self_update_sensitivity(self);
    283   }
    284 
    285   protected void
    286     add_clicked_h (self, GtkButton *button)
    287   {
    288     mn_mailbox_view_activate_add(MN_MAILBOX_VIEW(selfp->list));
    289   }
    290 
    291   protected void
    292     remove_clicked_h (self, GtkButton *button)
    293   {
    294     mn_mailbox_view_activate_remove(MN_MAILBOX_VIEW(selfp->list));
    295   }
    296 
    297   protected void
    298     properties_clicked_h (self, GtkButton *button)
    299   {
    300     mn_mailbox_view_activate_properties(MN_MAILBOX_VIEW(selfp->list));
    301   }
    302 
    303   protected char *
    304     popups_expiration_format_value_h (self, double arg, GtkScale *scale)
    305   {
    306     return mn_format_seconds(mn_non_linear_range_get_value(GTK_RANGE(scale)));
    307   }
    308 
    309   protected void
    310     test_messages_toggled_h (self, GtkToggleButton *button)
    311   {
    312     if (gtk_toggle_button_get_active(button))
    313       {
    314 	if (! selfp->test_mailbox)
    315 	  {
    316 	    selfp->test_mailbox = mn_test_mailbox_new();
    317 	    mn_mailbox_seal(selfp->test_mailbox);
    318 	    mn_mailboxes_add(mn_shell->mailboxes, selfp->test_mailbox);
    319 	  }
    320       }
    321     else
    322       self_remove_test_mailbox(self);
    323   }
    324 }