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