]> git.deb.at Git - pkg/abook.git/blob - edit.c
Imported Upstream version 0.4.16
[pkg/abook.git] / edit.c
1
2 /*
3  * $Id: edit.c,v 1.18 2001/10/12 09:32:51 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 "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         
124         if( (header = (char *)malloc(EDITW_COLS)) == NULL )
125                 return;
126
127         get_first_email(email, item);
128         
129         if( *database[item][EMAIL] )
130                 snprintf(header, EDITW_COLS, "%s <%s>",
131                                 database[item][NAME],
132                                 email);
133         else
134                 snprintf(header, EDITW_COLS, "%s", database[item][NAME]);
135
136         mvwaddstr(editw, 0, (EDITW_COLS - strlen(header)) / 2,
137                         header);
138
139         free(header);
140 }
141
142 static void
143 editor_print_data(int tab, int item)
144 {
145         int i, j;
146         int y, x;
147
148         for(i = 0, j = 1; i < ITEM_FIELDS; i++) {
149                 if(abook_fields[i].tab != tab)
150                         continue;
151
152                 if(i==EMAIL) { /* special field */
153                         int k;
154                         char emails[MAX_EMAILS][MAX_EMAIL_LEN];
155                         split_emailstr(item, emails);
156                         getyx(editw, y, x);
157                         mvwaddstr(editw, y+1, FIELDS_START_X,
158                                         "E-mail addresses:");
159                         for(k = 0; k < MAX_EMAILS; k++) {
160                                 getyx(editw, y, x);
161                                 mvwprintw(editw, y+1, FIELDS_START_X,
162                                 "%c -", '2' + k);
163                                 mvwprintw(editw, y +1, TAB_COLON_POS,
164                                                 ": %s", emails[k]);
165                         }
166                         continue;
167                 }
168                                 
169                 if(j > 1) {
170                         getyx(editw, y, x); y++;
171                 } else
172                         y = FIELDS_START_Y;
173
174                 mvwprintw(editw, y, FIELDS_START_X, "%d - %s",
175                                 j,
176                                 abook_fields[i].name);
177                 mvwaddch(editw, y, TAB_COLON_POS, ':');
178                 mvwaddstr(editw, y, TAB_COLON_POS + 2,
179                                 safe_str(database[item][i]));
180
181                 j++;
182         }
183 }
184
185 /*
186  * function: change_field
187  * 
188  * parameters:
189  *  (char *msg)
190  *   message to display as a prompt
191  *  (char **field)
192  *   a pointer to pointer which will point a new string. if the latter
193  *   pointer != NULL it will be freed (if user doesn't cancel)
194  * 
195  * returns (int)
196  *  a nonzero value if user has cancelled and zero if user has typed a
197  *  valid string
198  */
199
200 static int
201 change_field(char *msg, char **field)
202 {
203         char tmp[MAX_FIELD_LEN];
204         int max_len = MAX_FIELD_LEN;
205         int ret;
206         
207         if( !strncmp("E-mail", msg, 6) )
208                 max_len = MAX_EMAIL_LEN;
209         
210         statusline_addstr(msg);
211         if( (ret = statusline_getnstr( tmp, max_len - 1, 0 ) ? 1:0 ) ) {
212                 my_free(*field);
213                 if( *tmp )
214                         *field = strdup(tmp);
215         }
216
217         clear_statusline();
218         refresh_statusline();
219
220         return !ret;
221 }
222
223 static void
224 change_name_field(char **field)
225 {
226         char *tmp;
227
228         tmp = strdup(*field);
229         change_field("Name: ", field);
230
231         if( *field == NULL || ! **field ) {
232                 my_free(*field);
233                 *field = strdup(tmp);
234         }
235
236         my_free(tmp);
237 }
238
239 static void
240 fix_email_str(char *str)
241 {
242         for(; *str; str++ )
243                 *str = *str == ',' ? '_' : *str;
244 }
245
246 static void
247 edit_emails(char c, int item)
248 {
249         char *field = NULL;
250         char emails[MAX_EMAILS][MAX_EMAIL_LEN];
251         char tmp[MAX_EMAILSTR_LEN] = "";
252         int i, len;
253
254         split_emailstr(item, emails);
255
256         if(change_field("E-mail: ", &field)) {
257 #ifdef DEBUG
258                 fprintf(stderr, "change_field = TRUE\n");
259 #endif
260                 return; /* user cancelled ( C-g ) */
261         }
262         if(field) {
263                 strncpy(emails[c - '2'], field, MAX_EMAIL_LEN);
264                 fix_email_str(emails[c - '2']);
265         } else
266                 *emails[c - '2'] = 0;
267         
268         my_free(database[item][EMAIL]);
269
270         for(i = 0; i < MAX_EMAILS; i++) {
271                 if( *emails[i] ) {
272                         strcat(tmp, emails[i]);
273                         strcat(tmp, ",");
274                 }
275         }
276
277         len = strlen(tmp);
278         if(tmp[len -1] == ',')
279                 tmp[len-1] =0;
280
281         database[item][EMAIL] = strdup(tmp);
282 }
283
284 static int
285 edit_field(int tab, char c, int item)
286 {
287         int i, j;
288         int n = c - '1' + 1;
289         char *str;
290
291         if(n < 1 || n > MAX_TAB_FIELDS)
292                 return 0;
293
294         edit_undo(item, BACKUP_ITEM);
295
296         if(tab == TAB_CONTACT) {
297                 switch(c) {
298                         case '1': change_name_field(&database[item][NAME]);
299                                   break;
300                         case '2':
301                         case '3':
302                         case '4':
303                         case '5': edit_emails(c, item); break;
304                         default: return 0;
305                 }
306                 return 1;
307         }
308
309         for(i=0, j=0; i< ITEM_FIELDS; i++) {
310                 if(abook_fields[i].tab == tab)
311                         j++;
312                 if(j==n)
313                         break;
314         }
315
316         if(j!=n)
317                 return 0;
318
319         str = mkstr("%s: ", abook_fields[i].name);
320         change_field(str, &database[item][i]);
321
322         free(str);
323
324         return 1;
325 }
326
327 static void
328 edit_undo(int item, int mode)
329 {
330         int i;
331         static list_item *backup = NULL;
332
333         switch(mode) {
334                 case CLEAR_UNDO:
335                         if(backup) {
336                                 free_list_item(backup[0]);
337                                 my_free(backup);
338                         }
339                         break;
340                 case BACKUP_ITEM:
341                         if(backup) {
342                                 free_list_item(backup[0]);
343                                 my_free(backup);
344                         }
345                         backup = abook_malloc(sizeof(list_item));
346                         for(i = 0; i < ITEM_FIELDS; i++)
347                                 backup[0][i] = safe_strdup(database[item][i]);
348                         break;
349                 case RESTORE_ITEM:
350                         if(backup) {
351                                 free_list_item(database[item]);
352                                 itemcpy(database[item], backup[0]);
353                                 my_free(backup);
354                         }
355                         break;
356         }
357 }
358
359 static int
360 edit_loop(int item)
361 {
362         static int tab = 0; /* first tab */
363         int c;
364         
365         werase(editw);
366         headerline(EDITOR_HELPLINE);
367         refresh_statusline();
368         print_editor_header(item);
369         editor_tab(tab);
370         editor_print_data(tab, item);
371         wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
372
373         refresh();
374         wrefresh(editw);
375
376         switch( (c = getch()) ) {
377                 case 'c': tab = TAB_CONTACT; break;
378                 case 'a': tab = TAB_ADDRESS; break;
379                 case 'p': tab = TAB_PHONE; break;
380                 case 'o': tab = TAB_OTHER; break;
381                 case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1;
382                                break;
383                 case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1;
384                                 break;
385                 case '<':
386                 case 'k': if(is_valid_item(item-1)) item--; break;
387                 case '>':
388                 case 'j': if(is_valid_item(item+1)) item++; break;
389                 case 'r': roll_emails(item); break;
390                 case '?': display_help(HELP_EDITOR); break;
391                 case 'u': edit_undo(item, RESTORE_ITEM); break;
392                 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
393                 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
394                 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
395                 default:  return edit_field(tab, c, item) ? item : -1;
396         }
397
398         return item;
399 }
400
401 void
402 edit_item(int item)
403 {
404         if( item < 0 ) {
405                 if( curitem < 0 )
406                         return;
407                 else
408                         item = curitem;
409         }
410
411         init_editor();
412
413         while( (item = edit_loop(item)) >= 0 )
414                 curitem = item; /* hmm, this is not very clean way to go */
415
416         close_editor();
417 }
418
419 void
420 add_item()
421 {
422         char *field = NULL;
423         list_item item;
424
425         change_field("Name: ", &field);
426
427         if( field == NULL )
428                 return;
429
430         memset(item, 0, sizeof(item));
431
432         item[NAME] = field;
433
434         add_item2database(item);
435
436         curitem = LAST_ITEM;
437
438         edit_item(LAST_ITEM);
439 }
440