[PATCH] : still address-entry things
- From: Emmanuel <e allaud wanadoo fr>
- To: balsa-list gnome org
- Subject: [PATCH] : still address-entry things
- Date: Mon, 4 Feb 2002 13:57:38 +0100
Hi all,
here is the first patch to clean up address-entry.c. It applies on current
CVS (ie after snake-3 of Peter). It cleans up all bad coding style (yes I
finally decide to do it once for all, because I have to reread the whole
code each time, sorry Pawel, anyway tell me if you actually can't deal
with that), bad memory management, useless loops,
g_list_free->g_list_free_1 ( oh someone asked me what is the difference :
g_list_free_1 just frees one link, g_list_free frees the whole list).
More to come. I'll let Pawel commit what he wants before.
Bye
Manu
--- ../balsa-cvs/balsa/libbalsa/address-entry.c Mon Feb 4 10:16:48 2002
+++ balsa-cvs/balsa/libbalsa/address-entry.c Mon Feb 4 13:46:22 2002
@@ -357,9 +357,7 @@
g_return_if_fail(addy != NULL);
g_free(addy->user);
- addy->user = NULL;
g_free(addy->match);
- addy->match = NULL;
g_free(addy);
}
@@ -473,32 +471,22 @@
libbalsa_strsplit(const gchar *str, gchar delimiter)
{
GList *glist;
- gchar *data;
const gchar *old, *current;
- gint i, previous;
gboolean quoted;
- g_return_val_if_fail(str != NULL, NULL);
+ if (!str) return NULL;
quoted = FALSE;
glist = NULL;
- previous = 0;
- old = current = str;
- for (i = 0; str[i]; i++) {
- if (str[i] == '"') quoted = !quoted;
- if ( (!quoted) && (str[i] == delimiter) ) {
- data = g_strndup(old, i - previous);
- glist = g_list_append(glist, data);
- previous = i + 1;
- old = current;
- old++;
+ old = str;
+ for (current=str;*current;current++) {
+ if (*current == '"') quoted = !quoted;
+ else if ( (!quoted) && (*current == delimiter) ) {
+ glist = g_list_append(glist, g_strndup(old, current-old));
+ old=current+1;
}
- current++;
- }
- if (str) {
- data = g_strndup(old, i - previous);
- glist = g_list_append(glist, data);
}
+ glist = g_list_append(glist, g_strndup(old, current-old));
return glist;
}
@@ -522,9 +510,9 @@
i = 0;
if (addy->user)
- i = i + strlen(addy->user);
+ i += strlen(addy->user);
if (addy->match)
- i = i + 3 + strlen(addy->match);
+ i += 3 + strlen(addy->match);
return i;
}
@@ -641,7 +629,7 @@
static inputData *
libbalsa_fill_input(LibBalsaAddressEntry *address_entry)
{
- gint cursor = 0, size = 0, prev = 0;
+ gint cursor, size, prev=0;
gchar *typed = NULL;
GList *el, *current;
GList *list = NULL;
@@ -665,23 +653,21 @@
* Split the input string by comma, and store the result in
* input->list.
* str contains a list of e-mail addresses seperated by ','.
- *
- * FIXME: Breaks for '"Doe, John" <john@doe.com>'
*/
el = libbalsa_strsplit(typed, ',');
g_free(typed);
/*
* Store it all in a glist.
*/
- if (el != NULL) {
+ if (el) {
for (current = el;
- current != NULL;
+ current;
current = g_list_next(current)) {
addy = libbalsa_emailData_new();
- addy->user = g_strdup((gchar *)current->data);
+ addy->user = (gchar *)current->data;
input->list = g_list_append(input->list, addy);
}
- g_list_foreach(el, (GFunc)g_free, NULL);
+ g_list_free(el);
} else {
addy = libbalsa_emailData_new();
addy->user = g_strdup("");
@@ -693,27 +679,36 @@
* We have to match the cursor in GtkEntry to the list.
*/
g_assert(input->list != NULL);
- size = prev = 0;
- for (list = g_list_first(input->list);
- list != NULL;
- list = g_list_next(list)) {
- if (cursor >= size) {
- prev = size;
- input->active = list;
- }
+ list = g_list_first(input->list);
+ size = 0;
+ while (list) {
+ prev=size;
addy = (emailData *)list->data;
- size = size + strlen(addy->user) + 1;
+ size += strlen(addy->user) + 1;
addy->user = g_strchug(addy->user);
+ if (cursor<size) break;
+ list=g_list_next(list);
}
-
+ input->active = list;
+ /* Don't forget to strip leading space returned by
+ strsplit (this also takes care of spaces added
+ by user via cut-and-paste */
+ for (list = g_list_next(list);list;list=g_list_next(list)) {
+ addy = (emailData *)list->data;
+ addy->user = g_strchug(addy->user);
+ }
addy = (emailData *)input->active->data;
addy->cursor = cursor - prev;
if (input->active != input->list)
- addy->cursor = addy->cursor - 1; /* Compensate for the ',' */
- if (addy->cursor < 0) addy->cursor = 0;
- if (addy->cursor > (tmp = strlen(addy->user)))
- addy->cursor = tmp;
-
+ addy->cursor--; /* Compensate for the ',' */
+ if (addy->cursor < 0) addy->cursor = 0; /* FIXME : I think this is impossible */
+ tmp = strlen(addy->user);
+ if (addy->cursor > tmp)
+ addy->cursor = tmp; /* This one is mandatory because we strip leading spaces
+ during the calculus of prev so that cursor can be
+ actually much much greater that the end of the string
+ Just think of a cut/paste with lots of leading spaces
+ */
return input;
}
@@ -759,9 +754,9 @@
g_return_if_fail(LIBBALSA_IS_ADDRESS_ENTRY(address_entry));
input = address_entry->input;
- if (input == NULL)
+ if (!input)
return;
- if (input->list == NULL)
+ if (!input->list)
return;
addy = input->active->data;
@@ -769,9 +764,9 @@
/*
* Lets see if the user is at the end of an e-mail entry.
*/
- if (addy->user[addy->cursor] == '\0') {
+ if (!addy->user[addy->cursor]) {
list = g_list_next(input->active);
- if (list != NULL) {
+ if (list) {
input->list = g_list_remove_link(input->list, list);
libbalsa_emailData_free(list->data);
g_list_free_1(list);
@@ -782,21 +777,21 @@
*/
} else {
list = input->active;
- if (g_list_previous(input->active) != NULL)
+ if (g_list_previous(input->active))
input->active = g_list_previous(input->active);
- else if (g_list_next(input->active) != NULL)
+ else if (g_list_next(input->active))
input->active = g_list_next(input->active);
else
input->active = NULL;
input->list = g_list_remove_link(input->list, list);
libbalsa_emailData_free(list->data);
g_list_free_1(list);
- if (input->active != NULL) {
+ if (input->active) {
addy = input->active->data;
g_assert(addy != NULL);
addy->cursor = 0;
} else {
- gtk_entry_set_text(GTK_ENTRY(address_entry), "");
+ libbalsa_address_entry_set_text(address_entry, "");
libbalsa_inputData_free(address_entry->input);
address_entry->input = libbalsa_fill_input(address_entry);
}
@@ -828,9 +823,9 @@
g_return_if_fail(LIBBALSA_IS_ADDRESS_ENTRY(address_entry));
input = address_entry->input;
- if (input == NULL)
+ if (!input)
return;
- if (input->list == NULL)
+ if (!input->list)
return;
addy = input->active->data;
@@ -838,9 +833,9 @@
/*
* Lets see if the user is at the beginning of an e-mail entry.
*/
- if (addy->cursor == 0) {
+ if (!addy->cursor) {
list = g_list_previous(input->active);
- if (list != NULL) {
+ if (list) {
input->list = g_list_remove_link(input->list, list);
libbalsa_emailData_free(list->data);
g_list_free_1(list);
@@ -851,9 +846,9 @@
*/
} else {
list = input->active;
- if (g_list_next(input->active) != NULL) {
+ if (g_list_next(input->active)) {
input->active = g_list_next(input->active);
- } else if (g_list_previous(input->active) != NULL) {
+ } else if (g_list_previous(input->active)) {
input->active = g_list_previous(input->active);
} else {
input->active = NULL;
@@ -861,12 +856,12 @@
input->list = g_list_remove_link(input->list, list);
libbalsa_emailData_free(list->data);
g_list_free_1(list);
- if (input->active != NULL) {
+ if (input->active) {
addy = input->active->data;
g_assert(addy != NULL);
addy->cursor = strlen(addy->user);
} else {
- gtk_entry_set_text(GTK_ENTRY(address_entry), "");
+ libbalsa_address_entry_set_text(address_entry, "");
libbalsa_inputData_free(address_entry->input);
address_entry->input = libbalsa_fill_input(address_entry);
}
@@ -911,16 +906,12 @@
/*
* Free all the following data.
*/
- for (list = g_list_next(input->active);
- list != NULL;
- list = g_list_next(input->active)) {
- /*
- * Concatenate the two e-mails.
- */
- libbalsa_emailData_free(list->data);
- input->list = g_list_remove_link(input->list, list);
- g_list_free_1(list);
- }
+ g_list_foreach(g_list_next(input->active),(GFunc)libbalsa_emailData_free,NULL);
+ g_list_free(g_list_next(input->active));
+
+ /* input->active must be the last address */
+ input->active->next=NULL;
+
/* libbalsa_inputData_free(address_entry->input);
* look above: the line below is not necessary */
/* address_entry->input = input; */
@@ -1037,10 +1028,10 @@
/*
* Check if it is mouse button 2 (paste text)
*/
- if ( (event->button == 2) && (event->type != GDK_BUTTON_PRESS) &&
+ if ( (event->button == 2) && (event->type == GDK_BUTTON_PRESS) &&
editable->editable )
{
- if (address_entry->input != NULL)
+ if (address_entry->input)
libbalsa_inputData_free(address_entry->input);
address_entry->input = libbalsa_fill_input(address_entry);
address_entry->focus = FOCUS_CACHED;
@@ -1070,7 +1061,7 @@
{
GtkEditable *editable;
emailData *addy, *extra;
- gchar *left, *right, *str;
+ gchar *p,* str;
GList *list;
unsigned i;
inputData *input;
@@ -1092,7 +1083,7 @@
* This is only valid if the user has hit tab.
*/
if ((addy->cursor >= strlen(addy->user)) &&
- (addy->match != NULL) && (addy->tabs > 0)) {
+ addy->match && (addy->tabs > 0)) {
libbalsa_force_no_match(addy);
return;
}
@@ -1100,9 +1091,9 @@
/*
* Lets see if the user is at the beginning of an e-mail entry.
*/
- if (addy->cursor == 0) {
+ if (!addy->cursor) {
list = g_list_previous(input->active);
- if (list != NULL) {
+ if (list) {
/*
* Concatenate the two e-mails.
*/
@@ -1125,18 +1116,12 @@
* Normal character needs deleting.
*/
} else {
- left = g_strndup(addy->user, (addy->cursor - 1));
- right = addy->user;
- for (i = 0; i < addy->cursor; i++) right++;
- str = g_strconcat(left, right, NULL);
- g_free(addy->user);
- g_free(left);
- addy->user = str;
- addy->cursor--;
- if (*str == '\0')
- libbalsa_force_no_match(addy);
- else if (address_entry->find_match)
- (*address_entry->find_match) (addy, TRUE);
+ addy->cursor--;
+ for (p=addy->user+addy->cursor;*p;p++) *p=*(p+1);
+ if (!*(addy->user))
+ libbalsa_force_no_match(addy);
+ else if (address_entry->find_match)
+ (*address_entry->find_match) (addy, TRUE);
}
}
@@ -1164,7 +1149,7 @@
{
GtkEditable *editable;
emailData *addy, *extra;
- gchar *left, *right, *str;
+ gchar *p, *str;
GList *list;
inputData *input;
@@ -1179,8 +1164,7 @@
/*
* Check if the user wanted to delete a match.
*/
- if ((addy->cursor >= strlen(addy->user)) &&
- (addy->match != NULL)) {
+ if ((addy->cursor >= strlen(addy->user)) && addy->match) {
libbalsa_force_no_match(addy);
return;
}
@@ -1190,7 +1174,7 @@
*/
if (addy->cursor >= strlen(addy->user)) {
list = g_list_next(input->active);
- if (list != NULL) {
+ if (list) {
/*
* Concatenate the two e-mails.
*/
@@ -1210,15 +1194,8 @@
* Normal character needs deleting.
*/
} else {
- unsigned i;
- left = g_strndup(addy->user, addy->cursor);
- right = addy->user;
- for (i = 0; i <= addy->cursor; i++) right++;
- str = g_strconcat(left, right, NULL);
- g_free(addy->user);
- g_free(left);
- addy->user = str;
- }
+ for (p=addy->user+addy->cursor;*p;p++) *p=*(p+1);
+ }
}
@@ -1242,14 +1219,14 @@
g_assert(address_entry->input != NULL);
- if (address_entry->input->list == NULL)
+ if (!address_entry->input->list)
return TRUE;
g_assert(address_entry->input->active != NULL);
g_assert(address_entry->input->active->data != NULL);
addy = address_entry->input->active->data;
- if (addy->match != NULL) {
+ if (addy->match) {
libbalsa_alias_accept_match(addy);
return TRUE;
}
@@ -1288,14 +1265,14 @@
* Else no match was found. Check if there was a default
* domain to add to e-mail addresses.
*/
- if (address_entry->domain == NULL || *address_entry->domain == '\0')
+ if (!address_entry->domain || !*address_entry->domain)
return;
/*
* There is a default domain to add. Do we need to add it?
*/
addy = address_entry->input->active->data;
- if (libbalsa_is_an_email(addy->user) || *addy->user == '\0')
+ if (libbalsa_is_an_email(addy->user) || !*addy->user)
return;
/*
@@ -1349,7 +1326,7 @@
g_return_if_fail(LIBBALSA_IS_ADDRESS_ENTRY(address_entry));
addy = address_entry->input->active->data;
- if (addy->match != NULL)
+ if (addy->match)
libbalsa_alias_accept_match(addy);
address_entry->input->active = g_list_first(address_entry->input->list);
addy = address_entry->input->active->data;
@@ -1438,7 +1415,7 @@
input = address_entry->input;
addy = input->active->data;
- if (addy->match != NULL)
+ if (addy->match)
libbalsa_alias_accept_match(addy);
input->active = g_list_last(input->list);
addy = input->active->data;
@@ -1489,12 +1466,12 @@
input = address_entry->input;
addy = input->active->data;
left = addy->user;
- if (addy->match != NULL) {
+ if (addy->match) {
right = NULL;
libbalsa_alias_accept_match(addy);
} else {
if ((addy->cursor > 0) && (addy->cursor < strlen(addy->user))) {
- right = & addy->user[addy->cursor];
+ right = addy->user+addy->cursor;
} else {
right = NULL;
}
@@ -1510,11 +1487,15 @@
*/
extra = libbalsa_emailData_new();
list = g_list_next(input->active);
- if (list == NULL)
+
+ if (!list)
+ /* FIXME : this is overkill, but to do it another way I only see
+ manipulating prev and next fields directly
+ */
g_list_append(input->list, extra);
else
g_list_insert(input->list, extra, g_list_position(input->list, list));
- if (right != NULL) {
+ if (right) {
extra->user = g_strdup(right);
str = g_strndup(left, addy->cursor);
g_free(addy->user);
@@ -1531,8 +1512,8 @@
/*
* And we add the default domain to the original entry.
*/
- if (address_entry->domain == NULL ||
- *address_entry->domain == '\0' ||
+ if (!address_entry->domain ||
+ !*address_entry->domain ||
libbalsa_is_an_email(addy->user)) return;
str = g_strconcat(addy->user, "@", address_entry->domain, NULL);
@@ -1575,7 +1556,7 @@
* If this is at the beginning, and the user pressed ' ',
* ignore it.
*/
- if ((addy->cursor == 0) && (add[0] == (gchar) ' ')) return;
+ if (!addy->cursor && (add[0] == (gchar) ' ')) return;
editable = GTK_EDITABLE(address_entry);
if (editable->selection_start_pos != editable->selection_end_pos) {
@@ -1586,7 +1567,7 @@
* Split the string at the correct cursor position.
*/
left = g_strndup(addy->user, addy->cursor);
- right = & addy->user[addy->cursor];
+ right = addy->user+addy->cursor;
/*
* Add the keystroke to the end of user input.
@@ -1631,7 +1612,7 @@
/*
* Are we at the first cursor position in the GtkEntry box,
*/
- if (gtk_editable_get_position(GTK_EDITABLE(address_entry)) == 0) {
+ if (!gtk_editable_get_position(GTK_EDITABLE(address_entry))) {
gtk_widget_activate(GTK_WIDGET(address_entry));
return;
}
@@ -1641,8 +1622,8 @@
*/
input = address_entry->input;
addy = input->active->data;
- if (input->active == g_list_last(input->list)) {
- if (addy->cursor >= strlen(addy->user) && (addy->match == NULL)) {
+ if (!g_list_next(input->active)) {
+ if (addy->cursor >= strlen(addy->user) && !addy->match) {
libbalsa_accept_match(address_entry);
gtk_widget_activate(GTK_WIDGET(address_entry));
return;
@@ -1684,7 +1665,7 @@
gint address_start;
gboolean found;
emailData *addy;
- gint pos;
+ gint pos,tmp;
g_return_val_if_fail(address_entry != NULL, NULL);
g_return_val_if_fail(LIBBALSA_IS_ADDRESS_ENTRY(address_entry), NULL);
@@ -1694,23 +1675,26 @@
pos = *cursor;
*cursor = 0;
for (list = previous = address_entry->input->list;
- (list != NULL) && (found == FALSE);
+ list && !found;
list = g_list_next(list)) {
addy = (emailData *)list->data;
- address_start += libbalsa_length_of_address(addy);
+ tmp = libbalsa_length_of_address(addy);
+ address_start += tmp;
if (pos <= address_start) {
found = TRUE;
- *cursor = libbalsa_length_of_address(addy) - (address_start - pos);
+ *cursor = tmp - (address_start - pos);
}
address_start += 2; /* strlen(", ") */
previous = list;
}
g_assert(found == TRUE);
- if(*cursor<0) { /* error, correct it and print a warning.
- This needs to be fixed in long term. */
+ if(*cursor<0) { /* *cursor can be equal to -1. This
+ happens when you click just after
+ the comma (and just before the
+ following space)
+ So we correct that.
+ */
*cursor = 0;
- g_warning("libbalsa_find_list_entry failed to compute the cursor.\n"
- "find a way to reproduce it and report it.");
}
return previous;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]