Back to index

im-sdk  12.3.91
preference.c
Go to the documentation of this file.
00001 /* GNOME INPUT METHOD SWITCHER
00002  * Copyright 2003 Sun Microsystems Inc.
00003  *
00004  * This is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 #include <config.h>
00021 
00022 #include <gtk/gtkmain.h>
00023 #include <gtk/gtkcheckbutton.h>
00024 #include <gtk/gtksignal.h>
00025 #include <panel-applet.h>
00026 #include <panel-applet-gconf.h>
00027 #include <gnome.h>
00028 
00029 #include "gnome-im-switcher.h"
00030 #include "language.h"
00031 #include "preference.h"
00032 #include "status-placement.h"
00033 #include "utils.h"
00034 #include "widgets.h"
00035 
00036 #include <gconf/gconf-client.h>
00037 
00038 static GConfClient *default_client = NULL;
00039 
00040 static void
00041 input_language_policy_update (GimletWindow *gimlet)
00042 {
00043   panel_applet_gconf_set_int (PANEL_APPLET (gimlet->applet),
00044                            "input_language_policy",
00045                            gimlet->input_lang_policy,
00046                            NULL);
00047 }
00048 
00049 static void
00050 on_follow_application_toggled (GtkWidget *button,
00051                             GimletWindow *gimlet)
00052 {
00053   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
00054     {
00055       gimlet->input_lang_policy = FOLLOW_APPLICATION;
00056 #ifdef MEANINGLESS_OPTION_PERHAPS
00057       gtk_widget_set_sensitive (gimlet->input_language_option_menu, FALSE);
00058 #endif
00059     }
00060 
00061   input_language_policy_update (gimlet);
00062 }
00063 
00064 static void
00065 on_follow_quick_access_menu_toggled (GtkWidget *button,
00066                                  GimletWindow *gimlet)
00067 {
00068   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
00069     {
00070       gimlet->input_lang_policy = FOLLOW_QUICK_ACCESS_MENU;
00071 #ifdef MEANINGLESS_OPTION_PERHAPS
00072       gtk_widget_set_sensitive (gimlet->input_language_option_menu, FALSE);
00073 #endif
00074     }
00075 
00076   input_language_policy_update (gimlet);
00077 }
00078 
00079 #ifdef MEANINGLESS_OPTION_PERHAPS
00080 static void
00081 on_follow_current_locale_toggled (GtkWidget *button,
00082                               GimletWindow *gimlet)
00083 {
00084   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
00085     {
00086       gimlet->input_lang_policy = FOLLOW_CURRENT_LOCALE;
00087       gtk_widget_set_sensitive (gimlet->input_language_option_menu, FALSE);
00088     }
00089 
00090   input_language_policy_update (gimlet);
00091 }
00092 
00093 static void
00094 on_follow_option_menu_toggled (GtkWidget *button,
00095                             GimletWindow *gimlet)
00096 {
00097   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
00098     {
00099       gimlet->input_lang_policy = FOLLOW_OPTION_MENU;
00100       gtk_widget_set_sensitive (gimlet->input_language_option_menu, TRUE);
00101     }
00102 
00103   input_language_policy_update (gimlet);
00104 }
00105 #endif
00106 
00107 #define IM_KEY_DIR    "/desktop/gnome/input_methods"
00108 #define ENABLE_IM_KEY IM_KEY_DIR "/support"
00109 #define ENABLE_STATUS_KEY IM_KEY_DIR "/status"
00110 #define ENABLE_LOOKUP_KEY IM_KEY_DIR "/lookup"
00111 #define STATUS_PLACEMENT_KEY IM_KEY_DIR "/status_placement"
00112 
00113 /* static const char *enabled = "enabled"; */
00114 static const char *disabled = "disabled";
00115 
00116 static void
00117 init_im_settings (GimletWindow *gimlet)
00118 {
00119   gchar *str;
00120 
00121   str = NULL;
00122   str = gconf_client_get_string (default_client,
00123                              ENABLE_IM_KEY, NULL);
00124   if (str && strcmp (str, disabled) == 0)
00125     gimlet->im_enabled = FALSE;
00126   else
00127     gimlet->im_enabled = TRUE;
00128   g_free (str);
00129 
00130   str = NULL;
00131   str = gconf_client_get_string (default_client,
00132                              ENABLE_STATUS_KEY, NULL);
00133   if (str && strcmp (str, disabled) == 0)
00134     gimlet->status_enabled = FALSE;
00135   else
00136     gimlet->status_enabled = TRUE;
00137   g_free (str);
00138 
00139   str = NULL;
00140   str = gconf_client_get_string (default_client,
00141                              ENABLE_LOOKUP_KEY, NULL);
00142   if (str && strcmp (str, disabled) == 0)
00143     gimlet->lookup_enabled = FALSE;
00144   else
00145     gimlet->lookup_enabled = TRUE;
00146   g_free (str);
00147 
00148   str = NULL;
00149   str = gconf_client_get_string (default_client,
00150                              STATUS_PLACEMENT_KEY, NULL);
00151   gimlet->status_placement = gimlet_status_placement (str);
00152 
00153   g_free (str);
00154 
00155 }
00156 
00157 static void
00158 im_setting_changed (GConfClient  *client,
00159                   guint         cnxn_id,
00160                   GConfEntry   *entry,
00161                   GimletWindow *gimlet)
00162 {
00163   gchar *str = NULL;
00164 
00165   if (!entry->value || entry->value->type != GCONF_VALUE_STRING)
00166     return;
00167 
00168   if (strcmp (entry->key, ENABLE_IM_KEY) == 0)
00169     {
00170       str = g_strdup (gconf_value_get_string (entry->value));
00171       if (strcmp (str, disabled) == 0)
00172        gimlet->im_enabled = FALSE;
00173       else
00174        gimlet->im_enabled = TRUE;
00175     }
00176   else if (strcmp (entry->key, ENABLE_STATUS_KEY) == 0)
00177     {
00178       str = g_strdup (gconf_value_get_string (entry->value));
00179       if (strcmp (str, disabled) == 0)
00180        gimlet->status_enabled = FALSE;
00181       else
00182        gimlet->status_enabled = TRUE;
00183     }
00184   else if (strcmp (entry->key, ENABLE_LOOKUP_KEY) == 0)
00185     {
00186       str = g_strdup (gconf_value_get_string (entry->value));
00187       if (strcmp (str, disabled) == 0)
00188        gimlet->lookup_enabled = FALSE;
00189       else
00190        gimlet->lookup_enabled = TRUE;
00191     }
00192   else if (strcmp (entry->key, STATUS_PLACEMENT_KEY) == 0)
00193     {
00194       str = g_strdup (gconf_value_get_string (entry->value));
00195       gimlet->status_placement = gimlet_status_placement (str);
00196     }
00197   gimlet_update_lang (gimlet);
00198   gimlet_status_show_or_hide (gimlet);
00199   g_free (str);
00200 }
00201 
00202 void
00203 gimlet_preference_init (GimletWindow *gimlet)
00204 {
00205   int int_value;
00206 
00207   panel_applet_add_preferences (gimlet->applet,
00208                             "/schemas/apps/gimlet/preference",
00209                             NULL);
00210   if (default_client == NULL)
00211     {
00212       default_client = gconf_client_get_default ();
00213       g_object_ref (G_OBJECT (default_client));
00214     }
00215 
00216   /* Input Language */
00217   int_value = panel_applet_gconf_get_int (gimlet->applet,
00218                                      "input_language_policy", NULL);
00219   gimlet->input_lang_policy = int_value;
00220 
00221   gimlet->conversion_on_start = panel_applet_gconf_get_bool (gimlet->applet,
00222                                                       "conversion_on_start", NULL);
00223 
00224   /* input method settings */
00225   init_im_settings (gimlet);
00226   gconf_client_notify_add (default_client, ENABLE_IM_KEY,
00227                         (GConfClientNotifyFunc)im_setting_changed,
00228                         gimlet, NULL, NULL);
00229   gconf_client_notify_add (default_client, ENABLE_STATUS_KEY,
00230                         (GConfClientNotifyFunc)im_setting_changed,
00231                         gimlet, NULL, NULL);
00232   gconf_client_notify_add (default_client, STATUS_PLACEMENT_KEY,
00233                         (GConfClientNotifyFunc)im_setting_changed,
00234                         gimlet, NULL, NULL);
00235 }
00236 
00237 static void
00238 response_callback (GtkWidget *window,
00239                    int        id,
00240                    void      *data)
00241 {
00242   if (id == GTK_RESPONSE_HELP)
00243     gimlet_util_show_help ("imswitcher-prefs-inputlanguage",
00244                         GTK_WINDOW (window));
00245   else
00246     {
00247       gtk_widget_destroy (GTK_WIDGET (window));
00248     }
00249 }
00250 
00251 static void
00252 conversion_on_start_checkbutton_toggled (GtkToggleButton *button,
00253                                     GtkWidget *dialog)
00254 {
00255   GimletWindow *gimlet = g_object_get_data (G_OBJECT (dialog), "gimlet");
00256 
00257   if (gtk_toggle_button_get_active (button))
00258     gimlet->conversion_on_start = TRUE;
00259   else
00260     gimlet->conversion_on_start = FALSE;
00261 
00262   panel_applet_gconf_set_bool (PANEL_APPLET (gimlet->applet),
00263                             "conversion_on_start",
00264                             gimlet->conversion_on_start,
00265                             NULL);
00266   return;
00267 }
00268 
00269 #ifdef MEANINGLESS_OPTION_PERHAPS
00270 static void
00271 activate_cb (GtkWidget *menuitem, GimletWindow *gimlet)
00272 {
00273   if (GTK_CHECK_MENU_ITEM (menuitem)->active)
00274     {
00275       gchar *iiim_lang;
00276       gchar *name;
00277       gchar *conversion_mode;
00278 
00279       iiim_lang = g_object_get_data (G_OBJECT (menuitem), "iiim-lang-name");
00280 
00281       if (gimlet->iiim_lang_in_option_menu)
00282        g_free (gimlet->iiim_lang_in_option_menu);
00283       gimlet->iiim_lang_in_option_menu = g_strdup (iiim_lang);
00284     }
00285 }
00286 
00287 static void
00288 add_languages_in_option_menu (GimletWindow *gimlet)
00289 {
00290   GtkWidget *menu;
00291   GtkWidget *menuitem;
00292   GSList *group = NULL;
00293   GSList *active_languages;
00294   GSList *tmp;
00295 
00296   menu = gtk_menu_new ();
00297   group = NULL;
00298 
00299   if (gimlet->quick_access_menu == NULL)
00300     gimlet_quick_access_menu_init (gimlet);
00301 
00302   active_languages = gimlet->quick_access_menu->active_languages;
00303 
00304   tmp = active_languages;
00305   while (tmp != NULL)
00306     {
00307       char *name;
00308       char *iiim_lang_name;
00309       name = gimlet_language_get_name (tmp->data);
00310       iiim_lang_name = gimlet_language_get_iiim_lang_name (tmp->data);
00311 
00312       menuitem = gtk_radio_menu_item_new_with_label (group, name);
00313 
00314       if ((gimlet->iiim_lang_in_option_menu == NULL && group == NULL) ||
00315           (gimlet->iiim_lang_in_option_menu &&
00316            strcmp (iiim_lang_name, gimlet->iiim_lang_in_option_menu) == 0))
00317        gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem),
00318                                    TRUE);
00319        
00320       group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
00321 
00322       g_object_set_data (G_OBJECT (menuitem), "iiim-lang-name",
00323                       iiim_lang_name);
00324       g_object_set_data (G_OBJECT (menuitem), "iiim-display-name",
00325                       name);
00326       g_signal_connect (menuitem, "activate",
00327                      G_CALLBACK (activate_cb), gimlet);
00328 
00329       gtk_widget_show (menuitem);
00330       gtk_menu_shell_append (GTK_MENU_SHELL(menu), menuitem);
00331 
00332       tmp = tmp->next;
00333     }
00334 
00335   gtk_option_menu_set_menu (GTK_OPTION_MENU (gimlet->input_language_option_menu), menu);
00336 
00337 }
00338 #endif
00339 
00340 /* public */
00341 GtkWidget*
00342 gimlet_preference_dialog_new (GtkWindow *transient_parent,
00343                            GimletWindow *gimlet)
00344 {
00345   GladeXML *xml;
00346   GtkWidget *w;
00347   GtkWidget *dialog;
00348 
00349   xml = gimlet_util_load_glade_file (GIMLET_GLADE_FILE,
00350                                  "preference-dialog",
00351                                  transient_parent);
00352   if (xml == NULL)
00353     return NULL;
00354 
00355   /* The dialog itself */
00356   dialog = glade_xml_get_widget (xml, "preference-dialog");
00357   g_object_set_data (G_OBJECT (dialog), "gimlet", gimlet);
00358 
00359   gimlet_util_set_unique_role (GTK_WINDOW (dialog), "gimlet-preferences");
00360 
00361   g_signal_connect (G_OBJECT (dialog), "response",
00362                     G_CALLBACK (response_callback),
00363                     NULL);
00364 
00365   /* Input Language */
00366 
00367 #ifdef MEANINGLESS_OPTION_PERHAPS
00368   w = glade_xml_get_widget (xml, "default-input-language-option");
00369   gimlet->input_language_option_menu = w;
00370   gtk_widget_set_sensitive (gimlet->input_language_option_menu, FALSE);
00371 
00372   add_languages_in_option_menu (gimlet);
00373 
00374   w = glade_xml_get_widget (xml, "follow-current-locale");
00375   g_signal_connect (G_OBJECT (w), "toggled",
00376                   G_CALLBACK (on_follow_current_locale_toggled), gimlet);
00377   if (gimlet->input_lang_policy == FOLLOW_CURRENT_LOCALE)
00378     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
00379 
00380   w = glade_xml_get_widget (xml, "follow-option-menu");
00381   g_signal_connect (G_OBJECT (w), "toggled",
00382                   G_CALLBACK (on_follow_option_menu_toggled), gimlet);
00383   if (gimlet->input_lang_policy == FOLLOW_OPTION_MENU)
00384     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
00385 
00386 #endif
00387 
00388   w = glade_xml_get_widget (xml, "follow-application");
00389   g_signal_connect (G_OBJECT (w), "toggled",
00390                   G_CALLBACK (on_follow_application_toggled), gimlet);
00391   if (gimlet->input_lang_policy == FOLLOW_APPLICATION)
00392     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
00393 
00394   w = glade_xml_get_widget (xml, "follow-quick-access-menu");
00395   g_signal_connect (G_OBJECT (w), "toggled",
00396                   G_CALLBACK (on_follow_quick_access_menu_toggled), gimlet);
00397   if (gimlet->input_lang_policy == FOLLOW_QUICK_ACCESS_MENU)
00398     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
00399 
00400   w = glade_xml_get_widget (xml, "conversion_on_start_checkbutton");
00401   g_object_set_data (G_OBJECT (dialog),
00402                      "conversion-on-start-checkbutton",
00403                      w);
00404   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w),
00405                             gimlet->conversion_on_start);
00406 
00407   conversion_on_start_checkbutton_toggled (GTK_TOGGLE_BUTTON (w), dialog);
00408        
00409   g_signal_connect (G_OBJECT (w), "toggled", 
00410                   G_CALLBACK (conversion_on_start_checkbutton_toggled), 
00411                   dialog);
00412   return dialog;
00413 }