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