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