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