From d48d40c7cea47dc69e10988417e99c525b877a16 Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Fri, 31 Jan 2003 19:40:31 +0000 Subject: New function to return a cached list of labels. 2003-01-31 Jeffrey Stedfast * mail-config.c (mail_config_get_labels): New function to return a cached list of labels. (mail_config_get_label_color_by_name): New convenience function to search the cached labels. (mail_config_get_label_color_by_index): Same. (mail_config_init): Cache the labels and also listen for changes to them in the gconf db. (config_cache_labels): Internal function to cache the labels. * folder-browser.c (on_right_click): Fixed the label colours in the menu by using the cached linked list of labels. * mail-preferences.c (colorpicker_set_color): Now takes a string argument allowing us to get rid of converting a string into an rgb guint32 all over the place when trying to set defaults, since we now store colors in gconf as strings. (mail_preferences_construct): Use the cached labels (they are already parsed for us). svn path=/trunk/; revision=19694 --- mail/ChangeLog | 21 +++++ mail/folder-browser.c | 90 ++++++---------------- mail/mail-accounts.c | 4 +- mail/mail-config.c | 198 ++++++++++++++++++++++++++++++++---------------- mail/mail-config.h | 12 ++- mail/mail-preferences.c | 55 +++++--------- mail/message-list.c | 8 +- 7 files changed, 210 insertions(+), 178 deletions(-) (limited to 'mail') diff --git a/mail/ChangeLog b/mail/ChangeLog index a1f461d9c9..3a5e91682c 100644 --- a/mail/ChangeLog +++ b/mail/ChangeLog @@ -1,3 +1,24 @@ +2003-01-31 Jeffrey Stedfast + + * mail-config.c (mail_config_get_labels): New function to return a + cached list of labels. + (mail_config_get_label_color_by_name): New convenience function to + search the cached labels. + (mail_config_get_label_color_by_index): Same. + (mail_config_init): Cache the labels and also listen for changes + to them in the gconf db. + (config_cache_labels): Internal function to cache the labels. + + * folder-browser.c (on_right_click): Fixed the label colours in + the menu by using the cached linked list of labels. + + * mail-preferences.c (colorpicker_set_color): Now takes a string + argument allowing us to get rid of converting a string into an rgb + guint32 all over the place when trying to set defaults, since we + now store colors in gconf as strings. + (mail_preferences_construct): Use the cached labels (they are + already parsed for us). + 2003-01-27 Jeffrey Stedfast * component-factory.c (storage_remove_folder): Change diff --git a/mail/folder-browser.c b/mail/folder-browser.c index f83921b459..a29031d84b 100644 --- a/mail/folder-browser.c +++ b/mail/folder-browser.c @@ -26,6 +26,7 @@ #include #endif +#include #include #include @@ -1633,51 +1634,6 @@ hide_sender (GtkWidget *w, FolderBrowser *fb) } } -#if 0 -struct _colour_data { - FolderBrowser *fb; - guint32 rgb; -}; - -#define COLOUR_NONE (~0) - -static void -colourise_msg (GtkWidget *widget, gpointer user_data) -{ - struct _colour_data *data = user_data; - char *colour = NULL; - GPtrArray *uids; - int i; - - if (data->rgb != COLOUR_NONE) { - colour = alloca (8); - sprintf (colour, "#%.2x%.2x%.2x", (data->rgb & 0xff0000) >> 16, - (data->rgb & 0xff00) >> 8, data->rgb & 0xff); - } - - uids = g_ptr_array_new (); - message_list_foreach (data->fb->message_list, enumerate_msg, uids); - for (i = 0; i < uids->len; i++) { - camel_folder_set_message_user_tag (data->fb->folder, uids->pdata[i], "colour", colour); - } - g_ptr_array_free (uids, TRUE); -} - -static void -colour_closures_free (GPtrArray *closures) -{ - struct _colour_data *data; - int i; - - for (i = 0; i < closures->len; i++) { - data = closures->pdata[i]; - g_object_unref (data->fb); - g_free (data); - } - g_ptr_array_free (closures, TRUE); -} -#endif - struct _label_data { FolderBrowser *fb; const char *label; @@ -1900,7 +1856,7 @@ setup_popup_icons (void) char *filename; filename = g_strdup_printf ("%s/%s", EVOLUTION_IMAGES, context_pixmaps[i]); - context_menu[i].pixmap_widget = gtk_image_new_from_file(filename); + context_menu[i].pixmap_widget = gtk_image_new_from_file (filename); g_free (filename); } } @@ -1910,14 +1866,15 @@ setup_popup_icons (void) static int on_right_click (ETree *tree, gint row, ETreePath path, gint col, GdkEvent *event, FolderBrowser *fb) { - CamelMessageInfo *info; + struct _filter_data *fdata = NULL; GPtrArray *uids, *closures; + CamelMessageInfo *info; + GSList *labels, *next; int enable_mask = 0; int hide_mask = 0; - int i; char *mlist = NULL; GtkMenu *menu; - struct _filter_data *fdata = NULL; + int i; if (!folder_browser_is_sent (fb)) { enable_mask |= CAN_RESEND; @@ -2103,41 +2060,40 @@ on_right_click (ETree *tree, gint row, ETreePath path, gint col, GdkEvent *event ((struct _label_data *) label_menu[0].closure)->fb = fb; ((struct _label_data *) label_menu[0].closure)->label = NULL; - /* FIXME: don't hard code the label values */ -#define NUM_LABELS (sizeof (label_defaults) / sizeof (label_defaults[0])) - for (i = 0; i < NUM_LABELS; i++) { + i = 0; + labels = mail_config_get_labels (); + while (labels != NULL && i < 5) { struct _label_data *closure; + MailConfigLabel *label; GdkPixmap *pixmap; GdkColormap *map; - GdkColor color; - guint32 rgb; + GdkColor colour; GdkGC *gc; - rgb = mail_config_get_label_color (i); - - color.red = ((rgb & 0xff0000) >> 8) | 0xff; - color.green = (rgb & 0xff00) | 0xff; - color.blue = ((rgb & 0xff) << 8) | 0xff; - + label = labels->data; + gdk_color_parse (label->colour, &colour); map = gdk_colormap_get_system (); - gdk_color_alloc (map, &color); + gdk_color_alloc (map, &colour); pixmap = gdk_pixmap_new (GTK_WIDGET (fb)->window, 16, 16, -1); gc = gdk_gc_new (GTK_WIDGET (fb)->window); - gdk_gc_set_foreground (gc, &color); + gdk_gc_set_foreground (gc, &colour); gdk_draw_rectangle (pixmap, gc, TRUE, 0, 0, 16, 16); gdk_gc_unref (gc); closure = g_new (struct _label_data, 1); g_object_ref (fb); closure->fb = fb; - closure->label = mail_config_get_label_name (i); + closure->label = label->name; g_ptr_array_add (closures, closure); - label_menu[i + 2].name = (char *)mail_config_get_label_name (i); - label_menu[i + 2].pixmap_widget = gtk_image_new_from_pixmap(pixmap, NULL); + label_menu[i + 2].name = label->name; + label_menu[i + 2].pixmap_widget = gtk_image_new_from_pixmap (pixmap, NULL); label_menu[i + 2].closure = closure; + + i++; + labels = labels->next; } setup_popup_icons (); @@ -2148,10 +2104,10 @@ on_right_click (ETree *tree, gint row, ETreePath path, gint col, GdkEvent *event menu = e_popup_menu_create (context_menu, enable_mask, hide_mask, fb); e_auto_kill_popup_menu_on_hide (menu); - g_object_set_data_full (G_OBJECT(menu), "label_closures", closures, (GtkDestroyNotify) label_closures_free); + g_object_set_data_full ((GObject *) menu, "label_closures", closures, (GtkDestroyNotify) label_closures_free); if (fdata) - g_object_set_data_full (G_OBJECT(menu), "filter_data", fdata, (GtkDestroyNotify) filter_data_free); + g_object_set_data_full ((GObject *) menu, "filter_data", fdata, (GtkDestroyNotify) filter_data_free); if (event->type == GDK_KEY_PRESS) { struct cmpf_data closure; diff --git a/mail/mail-accounts.c b/mail/mail-accounts.c index f3c16f50b2..ddd92a1bbc 100644 --- a/mail/mail-accounts.c +++ b/mail/mail-accounts.c @@ -87,8 +87,6 @@ mail_accounts_tab_class_init (MailAccountsTabClass *klass) parent_class = g_type_class_ref (gtk_vbox_get_type ()); object_class->finalize = mail_accounts_tab_finalise; - /* override methods */ - /* setup static data */ disabled_pixbuf = NULL; @@ -113,7 +111,7 @@ mail_accounts_tab_finalise (GObject *obj) gdk_pixmap_unref (prefs->mark_pixmap); g_object_unref (prefs->mark_bitmap); - ((GObjectClass *)(parent_class))->finalize (obj); + G_OBJECT_CLASS (parent_class)->finalize (obj); } static void diff --git a/mail/mail-config.c b/mail/mail-config.c index 895a0d89b3..4415453729 100644 --- a/mail/mail-config.c +++ b/mail/mail-config.c @@ -65,11 +65,11 @@ MailConfigLabel label_defaults[5] = { - { N_("Important"), 0x00ff0000, NULL }, /* red */ - { N_("Work"), 0x00ff8c00, NULL }, /* orange */ - { N_("Personal"), 0x00008b00, NULL }, /* forest green */ - { N_("To Do"), 0x000000ff, NULL }, /* blue */ - { N_("Later"), 0x008b008b, NULL } /* magenta */ + { N_("Important"), "#ff0000" }, /* red */ + { N_("Work"), "#ff8c00" }, /* orange */ + { N_("Personal"), "#008b00" }, /* forest green */ + { N_("To Do"), "#0000ff" }, /* blue */ + { N_("Later"), "#8b008b" } /* magenta */ }; typedef struct { @@ -78,15 +78,14 @@ typedef struct { gboolean corrupt; EAccountList *accounts; - guint accounts_notify_id; GSList *signatures; int sig_nextid; - - MailConfigLabel labels[5]; - gboolean signature_info; + GSList *labels; + guint label_notify_id; + /* readonly fields from calendar */ int time_24hour; } MailConfig; @@ -359,6 +358,101 @@ config_write_signatures (void) gconf_client_suggest_sync (config->gconf, NULL); } +static void +config_clear_labels (void) +{ + MailConfigLabel *label; + GSList *list, *n; + + list = config->labels; + while (list != NULL) { + label = list->data; + g_free (label->name); + g_free (label->colour); + g_free (label); + + n = list->next; + g_slist_free_1 (list); + list = n; + } + + config->labels = NULL; +} + +static void +config_cache_labels (void) +{ + GSList *labels, *list, *tail, *n; + char *buf, *name, *colour; + MailConfigLabel *label; + int num = 0; + + tail = labels = NULL; + + list = gconf_client_get_list (config->gconf, "/apps/evolution/mail/labels", GCONF_VALUE_STRING, NULL); + + while (list != NULL) { + buf = list->data; + + if ((colour = strrchr (buf, ':'))) { + label = g_new (MailConfigLabel, 1); + + *colour++ = '\0'; + label->name = g_strdup (buf); + label->colour = g_strdup (colour); + + n = g_slist_alloc (); + n->next = NULL; + n->data = label; + + if (tail == NULL) + labels = n; + else + tail->next = n; + + tail = n; + + num++; + } + + g_free (buf); + + n = list->next; + g_slist_free_1 (list); + list = n; + } + + while (num < 5) { + /* complete the list with defaults */ + label = g_new (MailConfigLabel, 1); + label->name = g_strdup (_(label_defaults[num].name)); + label->colour = g_strdup (label_defaults[num].colour); + + n = g_slist_alloc (); + n->next = NULL; + n->data = label; + + if (tail == NULL) + labels = n; + else + tail->next = n; + + tail = n; + + num++; + } + + config->labels = labels; +} + +static void +gconf_labels_changed (GConfClient *client, guint cnxn_id, + GConfEntry *entry, gpointer user_data) +{ + config_clear_labels (); + config_cache_labels (); +} + /* Config struct routines */ void mail_config_init (void) @@ -371,6 +465,13 @@ mail_config_init (void) mail_config_clear (); + gconf_client_add_dir (config->gconf, "/apps/evolution/mail/labels", + GCONF_CLIENT_PRELOAD_ONELEVEL, NULL); + config->label_notify_id = + gconf_client_notify_add (config->gconf, "/apps/evolution/mail/labels", + gconf_labels_changed, NULL, NULL, NULL); + + config_cache_labels (); config_read_signatures (); config->accounts = e_account_list_new (config->gconf); @@ -379,8 +480,6 @@ mail_config_init (void) void mail_config_clear (void) { - int i; - if (!config) return; @@ -389,12 +488,7 @@ mail_config_clear (void) config->accounts = NULL; } - for (i = 0; i < 5; i++) { - g_free (config->labels[i].name); - config->labels[i].name = NULL; - g_free (config->labels[i].string); - config->labels[i].string = NULL; - } + config_clear_labels (); } void @@ -510,66 +604,40 @@ uri_to_key (const char *uri) return rval; } -const char * -mail_config_get_label_name (int label) -{ - g_return_val_if_fail (label >= 0 && label < 5, NULL); - - if (!config->labels[label].name) - config->labels[label].name = g_strdup (_(label_defaults[label].name)); - - return config->labels[label].name; -} - -void -mail_config_set_label_name (int label, const char *name) -{ - g_return_if_fail (label >= 0 && label < 5); - - if (!name) - name = _(label_defaults[label].name); - - g_free (config->labels[label].name); - config->labels[label].name = g_strdup (name); -} - -guint32 -mail_config_get_label_color (int label) +GSList * +mail_config_get_labels (void) { - g_return_val_if_fail (label >= 0 && label < 5, 0); - - return config->labels[label].color; + return config->labels; } -void -mail_config_set_label_color (int label, guint32 color) +const char * +mail_config_get_label_color_by_name (const char *name) { - g_return_if_fail (label >= 0 && label < 5); + MailConfigLabel *label; + GSList *node; - g_free (config->labels[label].string); - config->labels[label].string = NULL; + node = config->labels; + while (node != NULL) { + label = node->data; + if (!strcmp (label->name, name)) + return label->colour; + node = node->next; + } - config->labels[label].color = color; + return NULL; } const char * -mail_config_get_label_color_string (int label) +mail_config_get_label_color_by_index (int index) { - g_return_val_if_fail (label >= 0 && label < 5, NULL); + MailConfigLabel *label; - if (!config->labels[label].string) { - guint32 rgb = config->labels[label].color; - char *colour; - - colour = g_strdup_printf ("#%.2x%.2x%.2x", - (rgb & 0xff0000) >> 16, - (rgb & 0xff00) >> 8, - rgb & 0xff); - - config->labels[label].string = colour; - } + label = g_slist_nth_data (config->labels, index); - return config->labels[label].string; + if (label) + return label->colour; + + return NULL; } gboolean diff --git a/mail/mail-config.h b/mail/mail-config.h index ba253cccdb..e66c729c80 100644 --- a/mail/mail-config.h +++ b/mail/mail-config.h @@ -83,10 +83,10 @@ typedef enum { typedef struct { char *name; - guint32 color; - char *string; + char *colour; } MailConfigLabel; +#define LABEL_DEFAULTS_NUM 5 extern MailConfigLabel label_defaults[5]; /* signatures */ @@ -103,11 +103,9 @@ void mail_config_write_on_exit (void); gboolean mail_config_is_configured (void); gboolean mail_config_is_corrupt (void); -const char *mail_config_get_label_name (int label); -void mail_config_set_label_name (int label, const char *name); -guint32 mail_config_get_label_color (int label); -void mail_config_set_label_color (int label, guint32 color); -const char *mail_config_get_label_color_string (int label); +GSList *mail_config_get_labels (void); +const char *mail_config_get_label_color_by_name (const char *name); +const char *mail_config_get_label_color_by_index (int index); void mail_config_service_set_save_passwd (EAccountService *service, gboolean save_passwd); diff --git a/mail/mail-preferences.c b/mail/mail-preferences.c index 465f229ca0..60638fff93 100644 --- a/mail/mail-preferences.c +++ b/mail/mail-preferences.c @@ -100,8 +100,14 @@ mail_preferences_finalise (GObject *obj) static void -colorpicker_set_color (GnomeColorPicker *color, guint32 rgb) +colorpicker_set_color (GnomeColorPicker *color, const char *str) { + GdkColor colour; + guint32 rgb; + + gdk_color_parse (str, &colour); + rgb = ((colour.red & 0xff00) << 8) | (colour.green & 0xff00) | ((colour.blue & 0xff00) >> 8); + gnome_color_picker_set_i8 (color, (rgb & 0xff0000) >> 16, (rgb & 0xff00) >> 8, rgb & 0xff, 0xff); } @@ -148,7 +154,7 @@ restore_labels_clicked (GtkWidget *widget, gpointer user_data) for (i = 0; i < 5; i++) { gtk_entry_set_text (prefs->labels[i].name, _(label_defaults[i].name)); - colorpicker_set_color (prefs->labels[i].color, label_defaults[i].color); + colorpicker_set_color (prefs->labels[i].color, label_defaults[i].colour); } } @@ -183,10 +189,8 @@ mail_preferences_construct (MailPreferences *prefs) GtkWidget *widget, *toplevel, *menu; const char *text; GSList *list, *l; - GdkColor colour; GladeXML *gui; gboolean bool; - guint32 rgb; int i, val; char *buf; char *names[][2] = { @@ -234,9 +238,7 @@ mail_preferences_construct (MailPreferences *prefs) prefs->citation_color = GNOME_COLOR_PICKER (glade_xml_get_widget (gui, "colorpickerHighlightCitations")); buf = gconf_client_get_string (prefs->gconf, "/apps/evolution/mail/display/citation_colour", NULL); - gdk_color_parse (buf ? buf : "#737373", &colour); - rgb = ((colour.red & 0xff00) << 8) | (colour.green & 0xff00) | ((colour.blue & 0xff00) >> 8); - colorpicker_set_color (prefs->citation_color, rgb); + colorpicker_set_color (prefs->citation_color, buf ? buf : "#737373"); g_signal_connect (prefs->citation_color, "color-set", G_CALLBACK (color_set), prefs); g_free (buf); @@ -307,11 +309,13 @@ mail_preferences_construct (MailPreferences *prefs) gtk_toggle_button_set_active (prefs->prompt_unwanted_html, bool); g_signal_connect (prefs->prompt_unwanted_html, "toggled", G_CALLBACK (settings_changed), prefs); - l = list = gconf_client_get_list (prefs->gconf, "/apps/evolution/mail/labels", GCONF_VALUE_STRING, NULL); - - /* Labels and Colours tab */ - for (i = 0; i < 5; i++) { - char *widget_name, *label, *p; + i = 0; + list = mail_config_get_labels (); + while (list != NULL && i < 5) { + MailConfigLabel *label; + char *widget_name; + + label = list->data; widget_name = g_strdup_printf ("txtLabel%d", i); prefs->labels[i].name = GTK_ENTRY (glade_xml_get_widget (gui, widget_name)); @@ -321,35 +325,16 @@ mail_preferences_construct (MailPreferences *prefs) prefs->labels[i].color = GNOME_COLOR_PICKER (glade_xml_get_widget (gui, widget_name)); g_free (widget_name); - label = l ? (char *) l->data : g_strdup (_(label_defaults[i].name)); - if ((p = strrchr (label, ':'))) { - *p++ = '\0'; - gdk_color_parse (p, &colour); - rgb = ((colour.red & 0xff00) << 8) | (colour.green & 0xff00) | ((colour.blue & 0xff) >> 8); - } else { - rgb = label_defaults[i].color; - } - - gtk_entry_set_text (prefs->labels[i].name, label); - g_free (label); - + gtk_entry_set_text (prefs->labels[i].name, label->name); g_signal_connect (prefs->labels[i].name, "changed", G_CALLBACK (settings_changed), prefs); - colorpicker_set_color (prefs->labels[i].color, rgb); + colorpicker_set_color (prefs->labels[i].color, label->colour); g_signal_connect (prefs->labels[i].color, "color_set", G_CALLBACK (color_set), prefs); - if (l != NULL) - l = l->next; - } - - /* this is in case somehow the gconf list is longer than 5... */ - while (l != NULL) { - g_free (l->data); - l = l->next; + i++; + list = list->next; } - g_slist_free (list); - prefs->restore_labels = GTK_BUTTON (glade_xml_get_widget (gui, "cmdRestoreLabels")); g_signal_connect (prefs->restore_labels, "clicked", G_CALLBACK (restore_labels_clicked), prefs); } diff --git a/mail/message-list.c b/mail/message-list.c index b050259cfc..17278893db 100644 --- a/mail/message-list.c +++ b/mail/message-list.c @@ -990,8 +990,14 @@ ml_tree_value_at (ETreeModel *etm, ETreePath path, int col, void *model_data) completed = camel_tag_get ((CamelTag **) &msg_info->user_tags, "completed-on"); label = camel_tag_get ((CamelTag **) &msg_info->user_tags, "label"); if (colour == NULL) { + find_colour: if (label != NULL) { - colour = mail_config_get_label_color_string (filter_label_index (label)); + colour = mail_config_get_label_color_by_name (label); + if (colour == NULL) { + /* dead label? */ + label = NULL; + goto find_colour; + } } else if (msg_info->flags & CAMEL_MESSAGE_FLAGGED) { /* FIXME: extract from the xpm somehow. */ colour = "#A7453E"; -- cgit