]> git.deb.at Git - pkg/abook.git/blobdiff - edit.c
*** empty log message ***
[pkg/abook.git] / edit.c
diff --git a/edit.c b/edit.c
index 0d14512d789706b68c7075adcaba717af230711c..c4f17afbdc41336b202336e3968a6aeaae22319a 100644 (file)
--- a/edit.c
+++ b/edit.c
@@ -2,20 +2,23 @@
 /*
  * $Id$
  *
- * by JH <jheinonen@bigfoot.com>
+ * by JH <jheinonen@users.sourceforge.net>
  *
  * Copyright (C) Jaakko Heinonen
  */
 
 #include <string.h>
 #include <stdlib.h>
+#include <assert.h>
 #include "abook_curses.h"
 #include "ui.h"
 #include "abook.h"
 #include "database.h"
+#include "gettext.h"
 #include "list.h"
 #include "edit.h"
 #include "misc.h"
+#include "xmalloc.h"
 #ifdef HAVE_CONFIG_H
 #      include "config.h"
 #endif
@@ -33,22 +36,47 @@ extern int items;
 WINDOW *editw;
 
 static void
-editor_tab(int tab)
+editor_tab(const int tab)
 {
-       int i;
-       char *tab_names[] = {
-               "/ CONTACT \\",
-               "/ ADDRESS \\",
-               "/  PHONE  \\",
-               "/  OTHER  \\"
+       int i, j;
+       int x_pos = 2; /* current x pos */
+       static char *tab_names[] = {
+               N_("CONTACT"),
+               N_("ADDRESS"),
+               N_(" PHONE "),
+               N_(" OTHER "),
+               N_("CUSTOM ")
        };
 
-       mvwhline(editw, TABLINE+1, 0, UI_HLINE_CHAR, EDITW_COLS);
+       mvwhline(editw, TABLINE + 1, 0, UI_HLINE_CHAR, EDITW_COLS);
+
+       for(i = 0; i < TABS; i++) {
+               int width = strwidth(gettext(tab_names[i])) + 5;
 
-       for(i=0; i < TABS; i++)
-               mvwaddstr(editw, TABLINE, 16 * i + 3, tab_names[i]);
+               if(x_pos + width + 1 > EDITW_COLS) {
+                       statusline_msg(_("Tab name too wide for screen"));
+                       break;
+               }
 
-       mvwaddstr(editw, TABLINE+1, 16 * tab + 2, "/           \\");
+               mvwaddch(editw,  TABLINE + 1, x_pos,  UI_TEE_CHAR);
+               mvwaddch(editw,  TABLINE + 1, x_pos + width - 2, UI_TEE_CHAR);
+
+               mvwaddch(editw,  TABLINE, x_pos,  UI_ULCORNER_CHAR);
+               mvwaddch(editw,  TABLINE, x_pos + 1,  UI_LBOXLINE_CHAR);
+               mvwaddstr(editw, TABLINE, x_pos + 2,  gettext(tab_names[i]));
+               mvwaddch(editw,  TABLINE, x_pos + width - 3, UI_RBOXLINE_CHAR);
+               mvwaddch(editw,  TABLINE, x_pos + width - 2, UI_URCORNER_CHAR);
+
+               if(i == tab) {
+                       mvwaddch(editw,  TABLINE + 1, x_pos, UI_LRCORNER_CHAR);
+                       for(j = 0; j < width - 3; j++)
+                               mvwaddstr(editw,
+                                       TABLINE + 1, x_pos + j + 1, " ");
+                       mvwaddch(editw,  TABLINE + 1, x_pos + width - 2,
+                               UI_LLCORNER_CHAR);
+               }
+               x_pos += width;
+       }
 }
 
 void
@@ -63,7 +91,7 @@ get_first_email(char *str, int item)
 
        strncpy(str, database[item][EMAIL], MAX_EMAIL_LEN);
        if( (tmp = strchr(str, ',')) )
-               *tmp=0;
+               *tmp = 0;
        else
                str[MAX_EMAIL_LEN-1] = 0;
 }
@@ -79,11 +107,11 @@ roll_emails(int item)
        if( !(p = strchr(tmp, ',')) )
                return;
        else
-               *p=0;
+               *p = 0;
 
        strcpy(database[item][EMAIL], p+1);
        strcat(database[item][EMAIL], ",");
-       strcat(database[item][EMAIL], tmp);     
+       strcat(database[item][EMAIL], tmp);
 }
 
 static void
@@ -95,17 +123,50 @@ init_editor()
        refresh_statusline();
 }
 
-/*
- * we have to introduce edit_undo here
- */
-static void edit_undo(int item, int mode);
-
 enum {
        BACKUP_ITEM,
        RESTORE_ITEM,
        CLEAR_UNDO
 };
 
+static void
+edit_undo(int item, int mode)
+{
+       int i;
+       static list_item *backup = NULL;
+
+       switch(mode) {
+               case CLEAR_UNDO:
+                       if(backup) {
+                               free_list_item(backup[0]);
+                               xfree(backup);
+                       }
+                       break;
+               case BACKUP_ITEM:
+                       if(backup) {
+                               free_list_item(backup[0]);
+                               xfree(backup);
+                       }
+                       backup = xmalloc(sizeof(list_item));
+                       for(i = 0; i < ITEM_FIELDS; i++)
+                               if(database[item][i] == NULL)
+                                       backup[0][i] = NULL;
+                               else
+                                       backup[0][i] =
+                                               xstrdup(database[item][i]);
+                       break;
+               case RESTORE_ITEM:
+                       if(backup) {
+                               free_list_item(database[item]);
+                               itemcpy(database[item], backup[0]);
+                               xfree(backup);
+                       }
+                       break;
+               default:
+                       assert(0);
+       }
+}
+
 
 static void
 close_editor()
@@ -120,55 +181,64 @@ print_editor_header(int item)
 {
        char *header;
        char email[MAX_EMAIL_LEN];
-       int i, x, len;
-       
-       if( (header = (char *)malloc(EDITW_COLS)) == NULL )
+
+       if((header = xmalloc(EDITW_COLS)) == NULL)
                return;
 
        get_first_email(email, item);
-       
-       if( *database[item][EMAIL] )
+
+       if(*database[item][EMAIL])
                snprintf(header, EDITW_COLS, "%s <%s>",
                                database[item][NAME],
-                               database[item][EMAIL]);
+                               email);
        else
                snprintf(header, EDITW_COLS, "%s", database[item][NAME]);
 
-       len = strlen(header);
-       x = (EDITW_COLS - len) / 2;
-       mvwaddstr(editw, 0, x, header);
-       for(i = x; i < x + len; i++)
-               mvwaddch(editw,1, i, '^');
+       mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2,
+                       header);
+
        free(header);
 }
 
 static void
 editor_print_data(int tab, int item)
 {
-       const int pos_x = EDITW_COLS > 70 ? 8:4;
-       const int start_y = 4;
        int i, j;
+       int y, x;
 
        for(i = 0, j = 1; i < ITEM_FIELDS; i++) {
                if(abook_fields[i].tab != tab)
                        continue;
 
-               if(i==EMAIL) { /* special field */
+               if(i == EMAIL) { /* special field */
                        int k;
                        char emails[MAX_EMAILS][MAX_EMAIL_LEN];
                        split_emailstr(item, emails);
-                       mvwaddstr(editw, 6, pos_x, "E-mail addresses:");
-                       for(k=0; k < MAX_EMAILS; k++)
-                               mvwprintw(editw, 7 + k, pos_x,
-                               "%c -\t\t%s", '2' + k, emails[k] );
+                       getyx(editw, y, x);
+                       mvwaddstr(editw, y+1, FIELDS_START_X,
+                                       _("E-mail addresses:"));
+                       for(k = 0; k < MAX_EMAILS; k++) {
+                               getyx(editw, y, x);
+                               mvwprintw(editw, y+1, FIELDS_START_X,
+                               "%c -", '2' + k);
+                               mvwprintw(editw, y +1, TAB_COLON_POS,
+                                               ": %s", emails[k]);
+                       }
                        continue;
                }
-                               
-               mvwprintw(editw, start_y + j, pos_x, "%d - %s",
+
+               if(j > 1) {
+                       getyx(editw, y, x);
+                       y++;
+               } else
+                       y = FIELDS_START_Y;
+
+               mvwprintw(editw, y, FIELDS_START_X, "%d - %s",
                                j,
-                               abook_fields[i].name);
-               mvwaddch(editw, start_y + j, 28, ':');
-               mvwaddstr(editw, start_y + j, 30, safe_str(database[item][i]));
+                               gettext(abook_fields[i].name));
+               mvwaddch(editw, y, TAB_COLON_POS, ':');
+               mvwaddstr(editw, y, TAB_COLON_POS + 2,
+                               safe_str(database[item][i]));
 
                j++;
        }
@@ -176,14 +246,14 @@ editor_print_data(int tab, int item)
 
 /*
  * function: change_field
- * 
+ *
  * parameters:
  *  (char *msg)
  *   message to display as a prompt
  *  (char **field)
- *   a pointer to pointer which will point a new string. if the latter
+ *   a pointer to pointer which will point a new string. if the latter
  *   pointer != NULL it will be freed (if user doesn't cancel)
- * 
+ *
  * returns (int)
  *  a nonzero value if user has cancelled and zero if user has typed a
  *  valid string
@@ -192,24 +262,30 @@ editor_print_data(int tab, int item)
 static int
 change_field(char *msg, char **field)
 {
-       char tmp[MAX_FIELD_LEN];
        int max_len = MAX_FIELD_LEN;
-       int ret;
-       
-       if( !strncmp("E-mail", msg, 6) )
+       char *old;
+       int ret = 0;
+
+       if(!strncmp("E-mail", msg, 6))
                max_len = MAX_EMAIL_LEN;
-       
-       statusline_addstr(msg);
-       if( (ret = statusline_getnstr( tmp, max_len - 1, 0 ) ? 1:0 ) ) {
-               my_free(*field);
-               if( *tmp )
-                       *field = strdup(tmp);
+
+       old = *field;
+
+       *field = ui_readline(msg, old, max_len - 1, 0);
+
+       if(*field) {
+               xfree(old);
+               if(!**field)
+                       xfree(*field);
+       } else {
+               *field = old;
+               ret = 1;
        }
 
        clear_statusline();
        refresh_statusline();
 
-       return !ret;
+       return ret;
 }
 
 static void
@@ -217,49 +293,48 @@ change_name_field(char **field)
 {
        char *tmp;
 
-       tmp = strdup(*field);
+       tmp = xstrdup(*field);
        change_field("Name: ", field);
 
-       if( *field == NULL || ! **field ) {
-               my_free(*field);
-               *field = strdup(tmp);
+       if(*field == NULL || ! **field) {
+               xfree(*field);
+               *field = xstrdup(tmp);
        }
 
-       my_free(tmp);
+       xfree(tmp);
 }
 
 static void
 fix_email_str(char *str)
 {
-       for(; *str; str++ )
+       for(; *str; str++)
                *str = *str == ',' ? '_' : *str;
 }
 
 static void
 edit_emails(char c, int item)
 {
-       char *field = NULL;
-       char emails[4][MAX_EMAIL_LEN];
+       char *field;
+       char emails[MAX_EMAILS][MAX_EMAIL_LEN];
        char tmp[MAX_EMAILSTR_LEN] = "";
        int i, len;
+       int email_num = c - '2';
 
        split_emailstr(item, emails);
+       field = xstrdup(emails[email_num]);
 
-       if(change_field("E-mail: ", &field)) {
-#ifdef DEBUG
-               fprintf(stderr, "change_field = TRUE\n");
-#endif
+       if(change_field("E-mail: ", &field))
                return; /* user cancelled ( C-g ) */
-       }
+
        if(field) {
-               strncpy(emails[c - '2'], field, MAX_EMAIL_LEN);
-               fix_email_str(emails[c - '2']);
+               strncpy(emails[email_num], field, MAX_EMAIL_LEN);
+               fix_email_str(emails[email_num]);
        } else
-               *emails[c - '2'] = 0;
-       
-       my_free(database[item][EMAIL]);
+               *emails[email_num] = 0;
+
+       xfree(database[item][EMAIL]);
 
-       for(i=0; i<4; i++) {
+       for(i = 0; i < MAX_EMAILS; i++) {
                if( *emails[i] ) {
                        strcat(tmp, emails[i]);
                        strcat(tmp, ",");
@@ -270,7 +345,7 @@ edit_emails(char c, int item)
        if(tmp[len -1] == ',')
                tmp[len-1] =0;
 
-       database[item][EMAIL] = strdup(tmp);
+       database[item][EMAIL] = xstrdup(tmp);
 }
 
 static int
@@ -280,7 +355,7 @@ edit_field(int tab, char c, int item)
        int n = c - '1' + 1;
        char *str;
 
-       if(n < 1 || n > 6)
+       if(n < 1 || n > MAX_TAB_FIELDS)
                return 0;
 
        edit_undo(item, BACKUP_ITEM);
@@ -298,17 +373,17 @@ edit_field(int tab, char c, int item)
                return 1;
        }
 
-       for(i=0, j=0; i< ITEM_FIELDS; i++) {
+       for(i = 0, j = 0; i< ITEM_FIELDS; i++) {
                if(abook_fields[i].tab == tab)
                        j++;
                if(j==n)
                        break;
        }
 
-       if(j!=n)
+       if(j != n)
                return 0;
 
-       str = mkstr("%s: ", abook_fields[i].name);
+       str = strdup_printf("%s: ", gettext(abook_fields[i].name));
        change_field(str, &database[item][i]);
 
        free(str);
@@ -316,46 +391,14 @@ edit_field(int tab, char c, int item)
        return 1;
 }
 
-static void
-edit_undo(int item, int mode)
-{
-       int i;
-       static list_item *backup = NULL;
-
-       switch(mode) {
-               case CLEAR_UNDO:
-                       if(backup) {
-                               free_list_item(backup[0]);
-                               my_free(backup);
-                       }
-                       break;
-               case BACKUP_ITEM:
-                       if(backup) {
-                               free_list_item(backup[0]);
-                               my_free(backup);
-                       }
-                       backup = abook_malloc(sizeof(list_item));
-                       for(i = 0; i < ITEM_FIELDS; i++)
-                               backup[0][i] = safe_strdup(database[item][i]);
-                       break;
-               case RESTORE_ITEM:
-                       if(backup) {
-                               free_list_item(database[item]);
-                               itemcpy(database[item], backup[0]);
-                               my_free(backup);
-                       }
-                       break;
-       }
-}
-
 static int
 edit_loop(int item)
 {
        static int tab = 0; /* first tab */
        int c;
-       
+
        werase(editw);
-       headerline(EDITOR_HELPLINE);
+       headerline(gettext(EDITOR_HELPLINE));
        refresh_statusline();
        print_editor_header(item);
        editor_tab(tab);
@@ -365,24 +408,29 @@ edit_loop(int item)
        refresh();
        wrefresh(editw);
 
-       switch( (c = getch()) ) {
+       switch((c = getch())) {
                case 'c': tab = TAB_CONTACT; break;
                case 'a': tab = TAB_ADDRESS; break;
                case 'p': tab = TAB_PHONE; break;
                case 'o': tab = TAB_OTHER; break;
+               case 'C': tab = TAB_CUSTOM; break;
+               case 'h':
                case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1;
                               break;
+               case 'l':
                case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1;
                                break;
+               case KEY_UP:
                case '<':
                case 'k': if(is_valid_item(item-1)) item--; break;
+               case KEY_DOWN:
                case '>':
-               case 'j': if(is_valid_item(item+1)) item++; break;
+               case 'j': if(is_valid_item(item + 1)) item++; break;
                case 'r': roll_emails(item); break;
                case '?': display_help(HELP_EDITOR); break;
                case 'u': edit_undo(item, RESTORE_ITEM); break;
-               case 'm': launch_mutt(item); break;
-               case 'v': launch_wwwbrowser(item); break;
+               case 'm': launch_mutt(item); clearok(stdscr, 1); break;
+               case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
                case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
                default:  return edit_field(tab, c, item) ? item : -1;
        }
@@ -402,7 +450,7 @@ edit_item(int item)
 
        init_editor();
 
-       while( (item = edit_loop(item)) >= 0 )
+       while((item = edit_loop(item)) >= 0)
                curitem = item; /* hmm, this is not very clean way to go */
 
        close_editor();