]> git.deb.at Git - pkg/abook.git/blob - edit.c
const -> static change
[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 #ifdef HAVE_CONFIG_H
21 #       include "config.h"
22 #endif
23
24 /*
25  * some extern variables
26  */
27
28 extern struct abook_field abook_fields[];
29
30 extern list_item *database;
31 extern int curitem;
32 extern int items;
33
34 WINDOW *editw;
35
36 static void
37 editor_tab(const int tab)
38 {
39         int i;
40         const int spacing = 12;
41         static char *tab_names[] = {
42                 "CONTACT",
43                 "ADDRESS",
44                 " PHONE ",
45                 " OTHER ",
46                 "CUSTOM "
47         };
48
49         mvwhline(editw, TABLINE+1, 0, UI_HLINE_CHAR, EDITW_COLS);
50         for(i=0; i < TABS; i++) {
51                 mvwaddch(editw,  TABLINE+1, spacing * i + 2,  UI_TEE_CHAR);
52                 mvwaddch(editw,  TABLINE+1, spacing * i + 12, UI_TEE_CHAR);
53         }
54
55         for(i=0; i < TABS; i++) {
56                 mvwaddch(editw,  TABLINE, spacing * i + 2,  UI_ULCORNER_CHAR);
57                 mvwaddch(editw,  TABLINE, spacing * i + 3,  UI_LBOXLINE_CHAR);
58                 mvwaddstr(editw, TABLINE, spacing * i + 4,  tab_names[i]);
59                 mvwaddch(editw,  TABLINE, spacing * i + 11, UI_RBOXLINE_CHAR);
60                 mvwaddch(editw,  TABLINE, spacing * i + 12, UI_URCORNER_CHAR);
61         }
62
63         mvwaddch(editw,  TABLINE+1, spacing * tab + 2, UI_LRCORNER_CHAR);
64         mvwaddstr(editw, TABLINE+1, spacing * tab + 3, "         ");
65         mvwaddch(editw,  TABLINE+1, spacing * tab + 12, UI_LLCORNER_CHAR);
66 }
67
68 void
69 get_first_email(char *str, int item)
70 {
71         char *tmp;
72
73         if(database[item][EMAIL] == NULL) {
74                 *str = 0;
75                 return;
76         }
77
78         strncpy(str, database[item][EMAIL], MAX_EMAIL_LEN);
79         if( (tmp = strchr(str, ',')) )
80                 *tmp=0;
81         else
82                 str[MAX_EMAIL_LEN-1] = 0;
83 }
84
85 static void
86 roll_emails(int item)
87 {
88         char tmp[MAX_EMAILSTR_LEN];
89         char *p;
90
91         strcpy(tmp, database[item][EMAIL]);
92
93         if( !(p = strchr(tmp, ',')) )
94                 return;
95         else
96                 *p=0;
97
98         strcpy(database[item][EMAIL], p+1);
99         strcat(database[item][EMAIL], ",");
100         strcat(database[item][EMAIL], tmp);     
101 }
102
103 static void
104 init_editor()
105 {
106         clear();
107         editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
108
109         refresh_statusline();
110 }
111
112 /*
113  * we have to introduce edit_undo here
114  */
115 static void edit_undo(int item, int mode);
116
117 enum {
118         BACKUP_ITEM,
119         RESTORE_ITEM,
120         CLEAR_UNDO
121 };
122
123
124 static void
125 close_editor()
126 {
127         edit_undo(-1, CLEAR_UNDO);
128         delwin(editw);
129         refresh_screen();
130 }
131
132 static void
133 print_editor_header(int item)
134 {
135         char *header;
136         char email[MAX_EMAIL_LEN];
137
138         if( (header = (char *)malloc(EDITW_COLS)) == NULL )
139                 return;
140
141         get_first_email(email, item);
142
143         if( *database[item][EMAIL] )
144                 snprintf(header, EDITW_COLS, "%s <%s>",
145                                 database[item][NAME],
146                                 email);
147         else
148                 snprintf(header, EDITW_COLS, "%s", database[item][NAME]);
149
150         fprintf(stderr, "%d\n", strwidth(header));
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); y++;
186                 } else
187                         y = FIELDS_START_Y;
188
189                 mvwprintw(editw, y, FIELDS_START_X, "%d - %s",
190                                 j,
191                                 abook_fields[i].name);
192                 mvwaddch(editw, y, TAB_COLON_POS, ':');
193                 mvwaddstr(editw, y, TAB_COLON_POS + 2,
194                                 safe_str(database[item][i]));
195
196                 j++;
197         }
198 }
199
200 /*
201  * function: change_field
202  *
203  * parameters:
204  *  (char *msg)
205  *   message to display as a prompt
206  *  (char **field)
207  *   a pointer to a pointer which will point a new string. if the latter
208  *   pointer != NULL it will be freed (if user doesn't cancel)
209  *
210  * returns (int)
211  *  a nonzero value if user has cancelled and zero if user has typed a
212  *  valid string
213  */
214
215 static int
216 change_field(char *msg, char **field)
217 {
218         int max_len = MAX_FIELD_LEN;
219         char *old;
220         int ret = 0;
221
222         if( !strncmp("E-mail", msg, 6) )
223                 max_len = MAX_EMAIL_LEN;
224
225         old = *field;
226
227         *field = ui_readline(msg, old, max_len - 1, 0);
228
229         if(*field) {
230                 free(old);
231                 if(!**field)
232                         my_free(*field);
233         } else {
234                 *field = old;
235                 ret = 1;
236         }
237
238         clear_statusline();
239         refresh_statusline();
240
241         return ret;
242 }
243
244 static void
245 change_name_field(char **field)
246 {
247         char *tmp;
248
249         tmp = strdup(*field);
250         change_field("Name: ", field);
251
252         if( *field == NULL || ! **field ) {
253                 my_free(*field);
254                 *field = strdup(tmp);
255         }
256
257         my_free(tmp);
258 }
259
260 static void
261 fix_email_str(char *str)
262 {
263         for(; *str; str++ )
264                 *str = *str == ',' ? '_' : *str;
265 }
266
267 static void
268 edit_emails(char c, int item)
269 {
270         char *field;
271         char emails[MAX_EMAILS][MAX_EMAIL_LEN];
272         char tmp[MAX_EMAILSTR_LEN] = "";
273         int i, len;
274         int email_num = c - '2';
275
276         split_emailstr(item, emails);
277         field = strdup(emails[email_num]);
278
279         if(change_field("E-mail: ", &field))
280                 return; /* user cancelled ( C-g ) */
281
282         if(field) {
283                 strncpy(emails[email_num], field, MAX_EMAIL_LEN);
284                 fix_email_str(emails[email_num]);
285         } else
286                 *emails[email_num] = 0;
287
288         my_free(database[item][EMAIL]);
289
290         for(i = 0; i < MAX_EMAILS; i++) {
291                 if( *emails[i] ) {
292                         strcat(tmp, emails[i]);
293                         strcat(tmp, ",");
294                 }
295         }
296
297         len = strlen(tmp);
298         if(tmp[len -1] == ',')
299                 tmp[len-1] =0;
300
301         database[item][EMAIL] = strdup(tmp);
302 }
303
304 static int
305 edit_field(int tab, char c, int item)
306 {
307         int i, j;
308         int n = c - '1' + 1;
309         char *str;
310
311         if(n < 1 || n > MAX_TAB_FIELDS)
312                 return 0;
313
314         edit_undo(item, BACKUP_ITEM);
315
316         if(tab == TAB_CONTACT) {
317                 switch(c) {
318                         case '1': change_name_field(&database[item][NAME]);
319                                   break;
320                         case '2':
321                         case '3':
322                         case '4':
323                         case '5': edit_emails(c, item); break;
324                         default: return 0;
325                 }
326                 return 1;
327         }
328
329         for(i=0, j=0; i< ITEM_FIELDS; i++) {
330                 if(abook_fields[i].tab == tab)
331                         j++;
332                 if(j==n)
333                         break;
334         }
335
336         if(j!=n)
337                 return 0;
338
339         str = mkstr("%s: ", abook_fields[i].name);
340         change_field(str, &database[item][i]);
341
342         free(str);
343
344         return 1;
345 }
346
347 static void
348 edit_undo(int item, int mode)
349 {
350         int i;
351         static list_item *backup = NULL;
352
353         switch(mode) {
354                 case CLEAR_UNDO:
355                         if(backup) {
356                                 free_list_item(backup[0]);
357                                 my_free(backup);
358                         }
359                         break;
360                 case BACKUP_ITEM:
361                         if(backup) {
362                                 free_list_item(backup[0]);
363                                 my_free(backup);
364                         }
365                         backup = (list_item *)abook_malloc(sizeof(list_item));
366                         for(i = 0; i < ITEM_FIELDS; i++)
367                                 backup[0][i] = safe_strdup(database[item][i]);
368                         break;
369                 case RESTORE_ITEM:
370                         if(backup) {
371                                 free_list_item(database[item]);
372                                 itemcpy(database[item], backup[0]);
373                                 my_free(backup);
374                         }
375                         break;
376                 default:
377                         assert(0);
378         }
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