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