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