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