]> git.deb.at Git - pkg/abook.git/blob - edit.c
- whitespace changes
[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);
186                         y++;
187                 } else
188                         y = FIELDS_START_Y;
189
190                 mvwprintw(editw, y, FIELDS_START_X, "%d - %s",
191                                 j,
192                                 abook_fields[i].name);
193                 mvwaddch(editw, y, TAB_COLON_POS, ':');
194                 mvwaddstr(editw, y, TAB_COLON_POS + 2,
195                                 safe_str(database[item][i]));
196
197                 j++;
198         }
199 }
200
201 /*
202  * function: change_field
203  *
204  * parameters:
205  *  (char *msg)
206  *   message to display as a prompt
207  *  (char **field)
208  *   a pointer to a pointer which will point a new string. if the latter
209  *   pointer != NULL it will be freed (if user doesn't cancel)
210  *
211  * returns (int)
212  *  a nonzero value if user has cancelled and zero if user has typed a
213  *  valid string
214  */
215
216 static int
217 change_field(char *msg, char **field)
218 {
219         int max_len = MAX_FIELD_LEN;
220         char *old;
221         int ret = 0;
222
223         if(!strncmp("E-mail", msg, 6))
224                 max_len = MAX_EMAIL_LEN;
225
226         old = *field;
227
228         *field = ui_readline(msg, old, max_len - 1, 0);
229
230         if(*field) {
231                 xfree(old);
232                 if(!**field)
233                         xfree(*field);
234         } else {
235                 *field = old;
236                 ret = 1;
237         }
238
239         clear_statusline();
240         refresh_statusline();
241
242         return ret;
243 }
244
245 static void
246 change_name_field(char **field)
247 {
248         char *tmp;
249
250         tmp = strdup(*field);
251         change_field("Name: ", field);
252
253         if(*field == NULL || ! **field) {
254                 xfree(*field);
255                 *field = strdup(tmp);
256         }
257
258         xfree(tmp);
259 }
260
261 static void
262 fix_email_str(char *str)
263 {
264         for(; *str; str++)
265                 *str = *str == ',' ? '_' : *str;
266 }
267
268 static void
269 edit_emails(char c, int item)
270 {
271         char *field;
272         char emails[MAX_EMAILS][MAX_EMAIL_LEN];
273         char tmp[MAX_EMAILSTR_LEN] = "";
274         int i, len;
275         int email_num = c - '2';
276
277         split_emailstr(item, emails);
278         field = strdup(emails[email_num]);
279
280         if(change_field("E-mail: ", &field))
281                 return; /* user cancelled ( C-g ) */
282
283         if(field) {
284                 strncpy(emails[email_num], field, MAX_EMAIL_LEN);
285                 fix_email_str(emails[email_num]);
286         } else
287                 *emails[email_num] = 0;
288
289         xfree(database[item][EMAIL]);
290
291         for(i = 0; i < MAX_EMAILS; i++) {
292                 if( *emails[i] ) {
293                         strcat(tmp, emails[i]);
294                         strcat(tmp, ",");
295                 }
296         }
297
298         len = strlen(tmp);
299         if(tmp[len -1] == ',')
300                 tmp[len-1] =0;
301
302         database[item][EMAIL] = strdup(tmp);
303 }
304
305 static int
306 edit_field(int tab, char c, int item)
307 {
308         int i, j;
309         int n = c - '1' + 1;
310         char *str;
311
312         if(n < 1 || n > MAX_TAB_FIELDS)
313                 return 0;
314
315         edit_undo(item, BACKUP_ITEM);
316
317         if(tab == TAB_CONTACT) {
318                 switch(c) {
319                         case '1': change_name_field(&database[item][NAME]);
320                                   break;
321                         case '2':
322                         case '3':
323                         case '4':
324                         case '5': edit_emails(c, item); break;
325                         default: return 0;
326                 }
327                 return 1;
328         }
329
330         for(i = 0, j = 0; i< ITEM_FIELDS; i++) {
331                 if(abook_fields[i].tab == tab)
332                         j++;
333                 if(j==n)
334                         break;
335         }
336
337         if(j != n)
338                 return 0;
339
340         str = mkstr("%s: ", abook_fields[i].name);
341         change_field(str, &database[item][i]);
342
343         free(str);
344
345         return 1;
346 }
347
348 static void
349 edit_undo(int item, int mode)
350 {
351         int i;
352         static list_item *backup = NULL;
353
354         switch(mode) {
355                 case CLEAR_UNDO:
356                         if(backup) {
357                                 free_list_item(backup[0]);
358                                 xfree(backup);
359                         }
360                         break;
361                 case BACKUP_ITEM:
362                         if(backup) {
363                                 free_list_item(backup[0]);
364                                 xfree(backup);
365                         }
366                         backup = xmalloc(sizeof(list_item));
367                         for(i = 0; i < ITEM_FIELDS; i++)
368                                 backup[0][i] = safe_strdup(database[item][i]);
369                         break;
370                 case RESTORE_ITEM:
371                         if(backup) {
372                                 free_list_item(database[item]);
373                                 itemcpy(database[item], backup[0]);
374                                 xfree(backup);
375                         }
376                         break;
377                 default:
378                         assert(0);
379         }
380 }
381
382 static int
383 edit_loop(int item)
384 {
385         static int tab = 0; /* first tab */
386         int c;
387
388         werase(editw);
389         headerline(EDITOR_HELPLINE);
390         refresh_statusline();
391         print_editor_header(item);
392         editor_tab(tab);
393         editor_print_data(tab, item);
394         wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
395
396         refresh();
397         wrefresh(editw);
398
399         switch((c = getch())) {
400                 case 'c': tab = TAB_CONTACT; break;
401                 case 'a': tab = TAB_ADDRESS; break;
402                 case 'p': tab = TAB_PHONE; break;
403                 case 'o': tab = TAB_OTHER; break;
404                 case 'C': tab = TAB_CUSTOM; break;
405                 case 'h':
406                 case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1;
407                                break;
408                 case 'l':
409                 case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1;
410                                 break;
411                 case KEY_UP:
412                 case '<':
413                 case 'k': if(is_valid_item(item-1)) item--; break;
414                 case KEY_DOWN:
415                 case '>':
416                 case 'j': if(is_valid_item(item + 1)) item++; break;
417                 case 'r': roll_emails(item); break;
418                 case '?': display_help(HELP_EDITOR); break;
419                 case 'u': edit_undo(item, RESTORE_ITEM); break;
420                 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
421                 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
422                 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
423                 default:  return edit_field(tab, c, item) ? item : -1;
424         }
425
426         return item;
427 }
428
429 void
430 edit_item(int item)
431 {
432         if( item < 0 ) {
433                 if( curitem < 0 )
434                         return;
435                 else
436                         item = curitem;
437         }
438
439         init_editor();
440
441         while((item = edit_loop(item)) >= 0)
442                 curitem = item; /* hmm, this is not very clean way to go */
443
444         close_editor();
445 }
446
447 void
448 add_item()
449 {
450         char *field = NULL;
451         list_item item;
452
453         change_field("Name: ", &field);
454
455         if( field == NULL )
456                 return;
457
458         memset(item, 0, sizeof(item));
459
460         item[NAME] = field;
461
462         add_item2database(item);
463
464         curitem = LAST_ITEM;
465
466         edit_item(LAST_ITEM);
467 }
468