src/mn-auth-combo-box.gob (4429B) - 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 <gtk/gtk.h> 22 %} 23 24 %{ 25 #include <glib/gi18n.h> 26 #if WITH_SASL 27 #include <sasl/sasl.h> 28 #include "mn-sasl.h" 29 #endif /* WITH_SASL */ 30 #include "mn-util.h" 31 32 enum 33 { 34 COLUMN_MECHANISM, 35 COLUMN_LABEL, 36 N_COLUMNS 37 }; 38 %} 39 40 class MN:Auth:Combo:Box from Gtk:Combo:Box 41 { 42 property STRING active_mechanism (export) 43 set 44 { 45 const char *mechanism = g_value_get_string(VAL); 46 GtkTreeModel *model; 47 GtkTreeIter iter; 48 gboolean valid; 49 50 model = gtk_combo_box_get_model(GTK_COMBO_BOX(self)); 51 52 MN_TREE_MODEL_FOREACH(valid, &iter, model) 53 { 54 char *this_mech; 55 gboolean found; 56 57 gtk_tree_model_get(model, &iter, COLUMN_MECHANISM, &this_mech, -1); 58 found = (! this_mech && ! mechanism) 59 || (this_mech && mechanism && ! strcmp(this_mech, mechanism)); 60 g_free(this_mech); 61 62 if (found) 63 { 64 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(self), &iter); 65 break; 66 } 67 } 68 } 69 get 70 { 71 char *mechanism = NULL; 72 GtkTreeIter iter; 73 74 if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(self), &iter)) 75 { 76 GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(self)); 77 gtk_tree_model_get(model, &iter, COLUMN_MECHANISM, &mechanism, -1); 78 } 79 80 g_value_take_string(VAL, mechanism); 81 }; 82 83 init (self) 84 { 85 GtkListStore *store; 86 GtkCellRenderer *renderer; 87 88 store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING); 89 gtk_combo_box_set_model(GTK_COMBO_BOX(self), GTK_TREE_MODEL(store)); 90 g_object_unref(store); 91 92 renderer = gtk_cell_renderer_text_new(); 93 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(self), renderer, TRUE); 94 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(self), renderer, 95 "text", COLUMN_LABEL, 96 NULL); 97 98 self_append(self, NULL, _("Autodetect")); 99 #if WITH_SASL 100 if (mn_sasl_init(NULL)) 101 { 102 const char **mechanisms; 103 104 mechanisms = sasl_global_listmech(); 105 if (mechanisms) 106 { 107 int i; 108 for (i = 0; mechanisms[i]; i++) 109 self_append(self, mechanisms[i], mechanisms[i]); 110 } 111 } 112 #endif /* WITH_SASL */ 113 114 gtk_tree_sortable_set_default_sort_func(GTK_TREE_SORTABLE(store), self_sort_cb, NULL, NULL); 115 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING); 116 117 self_set_active_mechanism(self, NULL); 118 } 119 120 private int 121 sort_cb (GtkTreeModel *model, 122 GtkTreeIter *a, 123 GtkTreeIter *b, 124 gpointer user_data) 125 { 126 char *mechanism_a; 127 char *label_a; 128 char *mechanism_b; 129 char *label_b; 130 int coll; 131 132 gtk_tree_model_get(model, a, 133 COLUMN_MECHANISM, &mechanism_a, 134 COLUMN_LABEL, &label_a, 135 -1); 136 gtk_tree_model_get(model, b, 137 COLUMN_MECHANISM, &mechanism_b, 138 COLUMN_LABEL, &label_b, 139 -1); 140 141 /* put "autodetect" (NULL mechanism) on top of the list */ 142 if (mechanism_a && mechanism_b) 143 coll = g_utf8_collate(label_a, label_b); 144 else if (mechanism_a || mechanism_b) 145 coll = mechanism_a ? 1 : -1; 146 else 147 coll = 0; 148 149 g_free(mechanism_a); 150 g_free(mechanism_b); 151 152 return coll; 153 } 154 155 public void 156 append (self, const char *mechanism, const char *label (check null)) 157 { 158 GtkTreeModel *model; 159 GtkTreeIter iter; 160 161 model = gtk_combo_box_get_model(GTK_COMBO_BOX(self)); 162 163 gtk_list_store_append(GTK_LIST_STORE(model), &iter); 164 gtk_list_store_set(GTK_LIST_STORE(model), &iter, 165 COLUMN_MECHANISM, mechanism, 166 COLUMN_LABEL, label, 167 -1); 168 } 169 170 public GtkWidget * 171 new (void) 172 { 173 return GTK_WIDGET(GET_NEW); 174 } 175 }