]> git.deb.at Git - pkg/abook.git/blob - list.c
Don't handle extra_column/extra_alternative as numbers in abookrc
[pkg/abook.git] / list.c
1
2 /*
3  * $Id$
4  *
5  * by JH <jheinonen@users.sourceforge.net>
6  *
7  * Copyright (C) Jaakko Heinonen
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include "abook.h"
13 #include "ui.h"
14 #include "database.h"
15 #include "edit.h"
16 #include "list.h"
17 #include "misc.h"
18 #include "options.h"
19
20 #define MIN_EXTRA_COLUMN        ADDRESS /* 2 */
21 #define MAX_EXTRA_COLUMN        LAST_FIELD
22
23 int curitem = -1;
24 int first_list_item = -1;
25 char *selected = NULL;
26
27 int extra_column = -1;
28 int extra_alternative = -1;
29
30 extern int items;
31 extern list_item *database;
32 extern struct abook_field abook_fields[];
33
34 WINDOW *list = NULL;
35
36 void
37 init_list()
38 {
39         int i;
40         char *e_column_str = options_get_str("extra_column");
41         char *e_alternative_str = options_get_str("extra_alternative");
42
43         list = newwin(LIST_LINES, LIST_COLS, LIST_TOP, 0);
44         scrollok(list, TRUE);
45
46         /*
47          * init extra_column and extra alternative
48          */
49
50         if(e_column_str && *e_column_str) {
51                 for(i = 0; i < ITEM_FIELDS; i++) {
52                         if(!strcasecmp(e_column_str, abook_fields[i].key)) {
53                                 extra_column = i;
54                                 break;
55                         }
56                 }
57                 if(extra_column < MIN_EXTRA_COLUMN ||
58                                 extra_column > MAX_EXTRA_COLUMN) {
59                         extra_column = -1;
60                 }
61         }
62
63         if(e_alternative_str && *e_alternative_str) {
64                 for(i = 0; i < ITEM_FIELDS; i++) {
65                         if(!strcasecmp(e_alternative_str,
66                                                 abook_fields[i].key)) {
67                                 extra_alternative = i;
68                                 break;
69                         }
70                 }
71                 if(extra_alternative < MIN_EXTRA_COLUMN ||
72                                 extra_alternative > MAX_EXTRA_COLUMN) {
73                         extra_alternative = -1;
74                 }
75         }
76
77 }
78
79 void
80 close_list()
81 {
82         delwin(list);
83         list = NULL;
84 }
85
86 void
87 refresh_list()
88 {
89         int i, line;
90         
91         werase(list);
92
93         ui_print_number_of_items();
94         
95         if( items < 1 ) {
96                 refresh();
97                 wrefresh(list);
98                 return;
99         }
100         
101         if(curitem < 0)
102                 curitem = 0;
103
104         if(first_list_item < 0)
105                 first_list_item = 0;
106
107         if(curitem < first_list_item)
108                 first_list_item = curitem;
109         else if(curitem > LAST_LIST_ITEM)
110                 first_list_item = max(curitem - LIST_LINES + 1, 0);
111
112         for( line = 0, i = first_list_item ; i <= LAST_LIST_ITEM && i < items;
113                         line++, i++ ) {
114
115                 if(i == curitem)
116                         highlight_line(list, line);
117                 
118                 print_list_line(i, line);
119
120                 wstandend(list);
121         }
122
123         wrefresh(list);
124 }
125
126 void
127 print_list_line(int i, int line)
128 {
129         int extra = extra_column;
130         char tmp[MAX_EMAILSTR_LEN];
131         int real_emaillen = (extra_column > 0 || extra_alternative > 0) ?
132                 EMAILLEN : COLS - EMAILPOS;
133
134         scrollok(list, FALSE);
135
136         if( selected[i] )
137                 mvwaddch(list, line, 0, '*' );
138         
139         mvwaddnstr(list, line, NAMEPOS, database[i][NAME], NAMELEN);
140         if( options_get_int( "show_all_emails"  ) )
141                 mvwaddnstr(list, line, EMAILPOS, database[i][EMAIL],
142                                 real_emaillen);
143         else {
144                 get_first_email(tmp, i);
145                 mvwaddnstr(list, line, EMAILPOS, tmp, real_emaillen);
146         }
147
148         if(extra < 0 || !database[i][extra])
149                 extra = extra_alternative;
150         if(extra >= 0)
151                 mvwaddnstr(list, line, EXTRAPOS,
152                                 safe_str(database[i][extra]),
153                                 EXTRALEN);
154
155         scrollok(list, TRUE);
156 }
157         
158
159 void
160 list_headerline()
161 {
162         attrset(A_BOLD);
163         mvaddstr(2, NAMEPOS, abook_fields[NAME].name);
164         mvaddstr(2, EMAILPOS, abook_fields[EMAIL].name);
165         if(extra_column > 0)
166                 mvaddnstr(2, EXTRAPOS, abook_fields[extra_column].name,
167                                 COLS-EXTRAPOS);
168         attrset(A_NORMAL);
169 }
170
171 void
172 scroll_up()
173 {
174         if( curitem < 1 )
175                 return;
176
177         curitem--;
178
179         refresh_list();
180 }
181
182 void
183 scroll_down()
184 {
185         if( curitem > items - 2 )
186                 return;
187
188         curitem++;
189
190         refresh_list();
191 }
192
193
194 void
195 page_up()
196 {
197         if( curitem < 1 )
198                 return;
199         
200         curitem = curitem == first_list_item ?
201                 ((curitem -= LIST_LINES) < 0 ? 0 : curitem) : first_list_item;
202         
203         refresh_list();
204 }
205
206 void
207 page_down()
208 {
209         if( curitem > items - 2 )
210                 return;
211
212         curitem = curitem == LAST_LIST_ITEM ?
213                 ((curitem += LIST_LINES) > LAST_ITEM ? LAST_ITEM : curitem) :
214                 min(LAST_LIST_ITEM, LAST_ITEM);
215
216         refresh_list();
217 }
218
219
220 void
221 select_none()
222 {
223         memset( selected, 0, items );
224 }
225
226 void
227 select_all()
228 {
229         memset( selected, 1, items );
230 }
231
232 void
233 move_curitem(int direction)
234 {
235         list_item tmp;
236
237         if( curitem < 0 || curitem > LAST_ITEM )
238                 return;
239
240         itemcpy(tmp, database[curitem]);
241
242         switch(direction) {
243                 case MOVE_ITEM_UP:
244                         if( curitem < 1 )
245                                 return;
246                         itemcpy(database[curitem], database[curitem - 1]);
247                         itemcpy(database[curitem-1], tmp);
248                         scroll_up();
249                         break;
250
251                 case MOVE_ITEM_DOWN:
252                         if( curitem >= LAST_ITEM )
253                                 return;
254                         itemcpy(database[curitem], database[curitem + 1]);
255                         itemcpy(database[curitem+1], tmp);
256                         scroll_down();
257                         break;
258         }
259 }
260
261 void
262 goto_home()
263 {
264         if(items > 0)
265                 curitem = 0;
266         
267         refresh_list();
268 }
269
270 void
271 goto_end()
272 {
273         if(items > 0)
274                 curitem = LAST_ITEM;
275
276         refresh_list();
277 }
278
279
280 void
281 highlight_line(WINDOW *win, int line)
282 {
283         wstandout(win);
284         
285 #ifdef mvwchgat
286         mvwchgat(win, line, 0, -1,  A_STANDOUT, 0, NULL);
287 #else
288         /*
289          * buggy function: FIXME
290          */
291         scrollok(win, FALSE);
292         {
293                 int i;
294                 wmove(win, line, 0);
295                 for(i = 0; i < COLS; i++)
296                         waddch(win, ' ');
297         /*wattrset(win, 0);*/
298         }
299         scrollok(win, TRUE);
300 #endif
301 }
302
303 int
304 selected_items()
305 {
306         int i, n = 0;
307
308         for(i = 0; i < items; i++)
309                 if(selected[i])
310                         n++;
311
312         return n;
313 }
314
315 void
316 invert_selection()
317 {
318         int i;
319
320         if( items < 1 )
321                 return;
322
323         for(i = 0; i < items; i++)
324                 selected[i] = !selected[i];
325 }
326
327 int
328 list_current_item()
329 {
330         return curitem;
331 }
332
333 int
334 list_is_empty()
335 {
336         return items < 1;
337 }
338
339