]> git.deb.at Git - pkg/abook.git/blob - edit.c
Don't handle extra_column/extra_alternative as numbers in abookrc
[pkg/abook.git] / edit.c
1
2 /*
3  * $Id$
4  *
5  * by JH <jheinonen@users.sourceforge.net>
6  *
7  * Copyright (C) Jaakko Heinonen
8  */
9
10 #include <string.h>
11 #include <stdlib.h>
12 #include "abook_curses.h"
13 #include "ui.h"
14 #include "abook.h"
15 #include "database.h"
16 #include "list.h"
17 #include "edit.h"
18 #include "misc.h"
19 #ifdef HAVE_CONFIG_H
20 #       include "config.h"
21 #endif
22
23 /*
24  * some extern variables
25  */
26
27 extern struct abook_field abook_fields[];
28
29 extern list_item *database;
30 extern int curitem;
31 extern int items;
32
33 WINDOW *editw;
34
35 static void
36 editor_tab(int tab)
37 {
38         int i;
39         char *tab_names[] = {
40                 "/ CONTACT \\",
41                 "/ ADDRESS \\",
42                 "/  PHONE  \\",
43                 "/  OTHER  \\"
44         };
45
46         mvwhline(editw, TABLINE+1, 0, UI_HLINE_CHAR, EDITW_COLS);
47
48         for(i=0; i < TABS; i++)
49                 mvwaddstr(editw, TABLINE, 16 * i + 3, tab_names[i]);
50
51         mvwaddstr(editw, TABLINE+1, 16 * tab + 2, "/           \\");
52 }
53
54 void
55 get_first_email(char *str, int item)
56 {
57         char *tmp;
58
59         if(database[item][EMAIL] == NULL) {
60                 *str = 0;
61                 return;
62         }
63
64         strncpy(str, database[item][EMAIL], MAX_EMAIL_LEN);
65         if( (tmp = strchr(str, ',')) )
66                 *tmp=0;
67         else
68                 str[MAX_EMAIL_LEN-1] = 0;
69 }
70
71 static void
72 roll_emails(int item)
73 {
74         char tmp[MAX_EMAILSTR_LEN];
75         char *p;
76
77         strcpy(tmp, database[item][EMAIL]);
78
79         if( !(p = strchr(tmp, ',')) )
80                 return;
81         else
82                 *p=0;
83
84         strcpy(database[item][EMAIL], p+1);
85         strcat(database[item][EMAIL], ",");
86         strcat(database[item][EMAIL], tmp);     
87 }
88
89 static void
90 init_editor()
91 {
92         clear();
93         editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
94
95         refresh_statusline();
96 }
97
98 /*
99  * we have to introduce edit_undo here
100  */
101 static void edit_undo(int item, int mode);
102
103 enum {
104         BACKUP_ITEM,
105         RESTORE_ITEM,
106         CLEAR_UNDO
107 };
108
109
110 static void
111 close_editor()
112 {
113         edit_undo(-1, CLEAR_UNDO);
114         delwin(editw);
115         refresh_screen();
116 }
117
118 static void
119 print_editor_header(int item)
120 {
121         char *header;
122         char email[MAX_EMAIL_LEN];
123         int i, x, len;
124         
125         if( (header = (char *)malloc(EDITW_COLS)) == NULL )
126                 return;
127
128         get_first_email(email, item);
129         
130         if( *database[item][EMAIL] )
131                 snprintf(header, EDITW_COLS, "%s <%s>",
132                                 database[item][NAME],
133                                 email);
134         else
135                 snprintf(header, EDITW_COLS, "%s", database[item][NAME]);
136
137         len = strlen(header);
138         x = (EDITW_COLS - len) / 2;
139         mvwaddstr(editw, 0, x, header);
140         for(i = x; i < x + len; i++)
141                 mvwaddch(editw, 1, i, '^');
142         free(header);
143 }
144
145 static void
146 editor_print_data(int tab, int item)
147 {
148         int i, j;
149         int y, x;
150
151         for(i = 0, j = 1; i < ITEM_FIELDS; i++) {
152                 if(abook_fields[i].tab != tab)
153                         continue;
154
155                 if(i==EMAIL) { /* special field */
156                         int k;
157                         char emails[MAX_EMAILS][MAX_EMAIL_LEN];
158                         split_emailstr(item, emails);
159                         getyx(editw, y, x);
160                         mvwaddstr(editw, y+1, TAB_START_X, "E-mail addresses:");
161                         for(k = 0; k < MAX_EMAILS; k++) {
162                                 getyx(editw, y, x);
163                                 mvwprintw(editw, y+1, TAB_START_X,
164                                 "%c -", '2' + k);
165                                 mvwprintw(editw, y +1, TAB_COLON_POS,
166                                                 ": %s", emails[k]);
167                         }
168                         continue;
169                 }
170                                 
171                 if(j > 1) {
172                         getyx(editw, y, x); y++;
173                 } else
174                         y = TAB_START_Y;
175
176                 mvwprintw(editw, y, TAB_START_X, "%d - %s",
177                                 j,
178                                 abook_fields[i].name);
179                 mvwaddch(editw, y, TAB_COLON_POS, ':');
180                 mvwaddstr(editw, y, TAB_COLON_POS + 2,
181                                 safe_str(database[item][i]));
182
183                 j++;
184         }
185 }
186
187 /*
188  * function: change_field
189  * 
190  * parameters:
191  *  (char *msg)
192  *   message to display as a prompt
193  *  (char **field)
194  *   a pointer to pointer which will point a new string. if the latter
195  *   pointer != NULL it will be freed (if user doesn't cancel)
196  * 
197  * returns (int)
198  *  a nonzero value if user has cancelled and zero if user has typed a
199  *  valid string
200  */
201
202 static int
203 change_field(char *msg, char **field)
204 {
205         char tmp[MAX_FIELD_LEN];
206         int max_len = MAX_FIELD_LEN;
207         int ret;
208         
209         if( !strncmp("E-mail", msg, 6) )
210                 max_len = MAX_EMAIL_LEN;
211         
212         statusline_addstr(msg);
213         if( (ret = statusline_getnstr( tmp, max_len - 1, 0 ) ? 1:0 ) ) {
214                 my_free(*field);
215                 if( *tmp )
216                         *field = strdup(tmp);
217         }
218
219         clear_statusline();
220         refresh_statusline();
221
222         return !ret;
223 }
224
225 static void
226 change_name_field(char **field)
227 {
228         char *tmp;
229
230         tmp = strdup(*field);
231         change_field("Name: ", field);
232
233         if( *field == NULL || ! **field ) {
234                 my_free(*field);
235                 *field = strdup(tmp);
236         }
237
238         my_free(tmp);
239 }
240
241 static void
242 fix_email_str(char *str)
243 {
244         for(; *str; str++ )
245                 *str = *str == ',' ? '_' : *str;
246 }
247
248 static void
249 edit_emails(char c, int item)
250 {
251         char *field = NULL;
252         char emails[MAX_EMAILS][MAX_EMAIL_LEN];
253         char tmp[MAX_EMAILSTR_LEN] = "";
254         int i, len;
255
256         split_emailstr(item, emails);
257
258         if(change_field("E-mail: ", &field)) {
259 #ifdef DEBUG
260                 fprintf(stderr, "change_field = TRUE\n");
261 #endif
262                 return; /* user cancelled ( C-g ) */
263         }
264         if(field) {
265                 strncpy(emails[c - '2'], field, MAX_EMAIL_LEN);
266                 fix_email_str(emails[c - '2']);
267         } else
268                 *emails[c - '2'] = 0;
269         
270         my_free(database[item][EMAIL]);
271
272         for(i = 0; i < MAX_EMAILS; i++) {
273                 if( *emails[i] ) {
274                         strcat(tmp, emails[i]);
275                         strcat(tmp, ",");
276                 }
277         }
278
279         len = strlen(tmp);
280         if(tmp[len -1] == ',')
281                 tmp[len-1] =0;
282
283         database[item][EMAIL] = strdup(tmp);
284 }
285
286 static int
287 edit_field(int tab, char c, int item)
288 {
289         int i, j;
290         int n = c - '1' + 1;
291         char *str;
292
293         if(n < 1 || n > MAX_TAB_LINES)
294                 return 0;
295
296         edit_undo(item, BACKUP_ITEM);
297
298         if(tab == TAB_CONTACT) {
299                 switch(c) {
300                         case '1': change_name_field(&database[item][NAME]);
301                                   break;
302                         case '2':
303                         case '3':
304                         case '4':
305                         case '5': edit_emails(c, item); break;
306                         default: return 0;
307                 }
308                 return 1;
309         }
310
311         for(i=0, j=0; i< ITEM_FIELDS; i++) {
312                 if(abook_fields[i].tab == tab)
313                         j++;
314                 if(j==n)
315                         break;
316         }
317
318         if(j!=n)
319                 return 0;
320
321         str = mkstr("%s: ", abook_fields[i].name);
322         change_field(str, &database[item][i]);
323
324         free(str);
325
326         return 1;
327 }
328
329 static void
330 edit_undo(int item, int mode)
331 {
332         int i;
333         static list_item *backup = NULL;
334
335         switch(mode) {
336                 case CLEAR_UNDO:
337                         if(backup) {
338                                 free_list_item(backup[0]);
339                                 my_free(backup);
340                         }
341                         break;
342                 case BACKUP_ITEM:
343                         if(backup) {
344                                 free_list_item(backup[0]);
345                                 my_free(backup);
346                         }
347                         backup = abook_malloc(sizeof(list_item));
348                         for(i = 0; i < ITEM_FIELDS; i++)
349                                 backup[0][i] = safe_strdup(database[item][i]);
350                         break;
351                 case RESTORE_ITEM:
352                         if(backup) {
353                                 free_list_item(database[item]);
354                                 itemcpy(database[item], backup[0]);
355                                 my_free(backup);
356                         }
357                         break;
358         }
359 }
360
361 static int
362 edit_loop(int item)
363 {
364         static int tab = 0; /* first tab */
365         int c;
366         
367         werase(editw);
368         headerline(EDITOR_HELPLINE);
369         refresh_statusline();
370         print_editor_header(item);
371         editor_tab(tab);
372         editor_print_data(tab, item);
373         wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
374
375         refresh();
376         wrefresh(editw);
377
378         switch( (c = getch()) ) {
379                 case 'c': tab = TAB_CONTACT; break;
380                 case 'a': tab = TAB_ADDRESS; break;
381                 case 'p': tab = TAB_PHONE; break;
382                 case 'o': tab = TAB_OTHER; break;
383                 case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1;
384                                break;
385                 case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1;
386                                 break;
387                 case '<':
388                 case 'k': if(is_valid_item(item-1)) item--; break;
389                 case '>':
390                 case 'j': if(is_valid_item(item+1)) item++; break;
391                 case 'r': roll_emails(item); break;
392                 case '?': display_help(HELP_EDITOR); break;
393                 case 'u': edit_undo(item, RESTORE_ITEM); break;
394                 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
395                 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
396                 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
397                 default:  return edit_field(tab, c, item) ? item : -1;
398         }
399
400         return item;
401 }
402
403 void
404 edit_item(int item)
405 {
406         if( item < 0 ) {
407                 if( curitem < 0 )
408                         return;
409                 else
410                         item = curitem;
411         }
412
413         init_editor();
414
415         while( (item = edit_loop(item)) >= 0 )
416                 curitem = item; /* hmm, this is not very clean way to go */
417
418         close_editor();
419 }
420
421 void
422 add_item()
423 {
424         char *field = NULL;
425         list_item item;
426
427         change_field("Name: ", &field);
428
429         if( field == NULL )
430                 return;
431
432         memset(item, 0, sizeof(item));
433
434         item[NAME] = field;
435
436         add_item2database(item);
437
438         curitem = LAST_ITEM;
439
440         edit_item(LAST_ITEM);
441 }
442