PageRenderTime 36ms CodeModel.GetById 24ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/pwman/src/uilist.c

https://github.com/evaryont/src
C | 319 lines | 232 code | 43 blank | 44 comment | 61 complexity | f3f032378241fdaabb46fecc3232a877 MD5 | raw file
  1/*
  2 *  PWMan - password management application
  3 *
  4 *  Copyright (C) 2002  Ivan Kelly <ivan@ivankelly.net>
  5 *
  6 *  This program is free software; you can redistribute it and/or modify
  7 *  it under the terms of the GNU General Public License as published by
  8 *  the Free Software Foundation; either version 2 of the License, or
  9 *  (at your option) any later version.
 10 *
 11 *  This program is distributed in the hope that it will be useful,
 12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 *  GNU General Public License for more details.
 15 *
 16 *  You should have received a copy of the GNU General Public License
 17 *  along with this program; if not, write to the Free Software
 18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 19 */
 20
 21#include <ui.h>
 22#include <pwman.h>
 23
 24WINDOW *list = NULL;
 25int lines = 0;
 26//int curitem = 0;
 27
 28int
 29uilist_init()
 30{
 31	char str[80];
 32	list = newwin(LIST_LINES, COLS, LIST_TOP, 0);
 33	
 34	scrollok(list, TRUE);
 35}
 36/*
 37int
 38resize_list()
 39{
 40	wresize(list, LIST_LINES, COLS);
 41}
 42*/
 43
 44int
 45uilist_free()
 46{
 47	delwin(list);
 48	list = NULL;
 49}
 50
 51int 
 52uilist_highlight_line(int line)
 53{
 54	scrollok(list, FALSE);
 55	int i;
 56	wmove(list, line, 0);
 57	waddch(list, '>');
 58	for(i = 1; i < COLS; i++)
 59		waddch(list, ' ');
 60	scrollok(list, TRUE);
 61}
 62
 63PWList *
 64uilist_get_highlighted_sublist()
 65{
 66	PWList *iter;
 67	int i = -1;
 68
 69	if(!current_pw_sublist){ return NULL; }
 70	if(current_pw_sublist->parent){ i++; }
 71
 72	for(iter = current_pw_sublist->sublists; iter != NULL; iter = iter->next){
 73		i++;
 74		if(i == current_pw_sublist->current_item){
 75			break;
 76		}
 77	}
 78	return iter;
 79}
 80
 81Pw *
 82uilist_get_highlighted_item()
 83{	
 84	Pw *iter;
 85	PWList *listiter;
 86	int i = -1;
 87
 88	if(current_pw_sublist->parent){ i++; }
 89	
 90	for(listiter = current_pw_sublist->sublists; listiter != NULL; listiter = listiter->next){
 91		i++;
 92	}
 93		
 94	for(iter = current_pw_sublist->list; iter != NULL; iter = iter->next){
 95		if( filter_apply(iter, options->filter) ){
 96			i++;
 97		}
 98		if( i == current_pw_sublist->current_item ){
 99			debug("get_highlighted_item: found %d, break now", i);
100			return iter;
101		}
102	}
103/*	fprintf(stderr, "%d.", curitem);
104	for(iter = pwlist; (iter != NULL) && i <= curitem; iter = iter->next){
105		if( apply_filter(iter, options->filter) ){
106			i++;	
107		}	
108	}*/
109	debug("get_highlighted_item: nothing found, return NULL");
110	return NULL;
111}
112
113LIST_ITEM_TYPE
114uilist_get_highlighted_type()
115{
116	Pw *iter;
117	PWList *listiter;
118	int i = -1;
119
120	if(current_pw_sublist->parent){
121		if(current_pw_sublist->current_item == 0){
122			return PW_UPLEVEL;
123		}
124		i++;
125	}
126	for(listiter = current_pw_sublist->sublists; listiter != NULL; listiter = listiter->next){
127		i++;
128		if(i == current_pw_sublist->current_item){
129			return PW_SUBLIST;
130		}
131	}
132		
133	for(iter = current_pw_sublist->list; iter != NULL; iter = iter->next){
134		if( filter_apply(iter, options->filter) ){
135			i++;
136		}
137		if( i == current_pw_sublist->current_item ){
138			return PW_ITEM;
139		}
140	}
141	return PW_NULL;
142}
143
144int
145uilist_refresh()
146{
147	Pw *iter;
148	PWList *listiter;
149	int i = 0;
150	static int first_list_item = 0;
151	int num_shown = 0;
152
153	debug("refresh_list: refreshing list");
154	if(list == NULL){
155		uilist_init();
156	}
157	if(current_pw_sublist == NULL){
158		return -1;
159	}
160
161	uilist_clear();;
162	lines = 0;
163
164	uilist_headerline();
165
166	// Ensure we don't end up off the screen
167	if(current_pw_sublist->current_item < 0){
168		current_pw_sublist->current_item = 0;
169	}
170	if(current_pw_sublist->current_item < first_list_item){
171		first_list_item = current_pw_sublist->current_item;		
172	} else if((current_pw_sublist->current_item > LAST_LIST_ITEM)){
173		first_list_item = current_pw_sublist->current_item - (LIST_LINES-1);
174	}
175
176	// If we aren't at the top level, off the "Up One Level" item
177	if(current_pw_sublist->parent){
178		if((i >= first_list_item) && (i <= LAST_LIST_ITEM)){
179			if(lines == current_pw_sublist->current_item){
180				uilist_highlight_line(num_shown);
181			}
182			mvwprintw(list, num_shown, NAMEPOS, "<Up One Level - \"%s\">", current_pw_sublist->parent->name);
183			wattrset(list, A_NORMAL);
184			wstandend(list);
185			num_shown++;
186		}
187		i++; 
188		lines++;
189	}
190	// Draw our sublists
191	for(listiter = current_pw_sublist->sublists; listiter != NULL; listiter = listiter->next){
192		if((i >= first_list_item) && (i <= LAST_LIST_ITEM)){
193			if(lines == current_pw_sublist->current_item){
194				uilist_highlight_line(num_shown);
195			}
196			mvwprintw(list, num_shown, NAMEPOS, "%s ->", listiter->name);
197			wattrset(list, A_NORMAL);
198			wstandend(list);
199			num_shown++;
200		}
201		i++; 
202		lines++;
203	}
204	// Draw our entries, if the filter says it's ok
205	for(iter = current_pw_sublist->list; (iter != NULL); iter = iter->next){
206		/*
207		 * if line satifies filter criteria increment i and lines
208		 */
209		if( filter_apply(iter, options->filter) ){
210			if((i >= first_list_item) && (i <= LAST_LIST_ITEM)){
211				if(lines == current_pw_sublist->current_item){
212					uilist_highlight_line(num_shown);
213				}
214				wattrset(list, COLOR_PAIR(1));
215				mvwaddnstr(list, num_shown, NAMEPOS, iter->name, NAMELEN);
216				mvwaddnstr(list, num_shown, HOSTPOS, iter->host, HOSTLEN);
217				mvwaddnstr(list, num_shown, USERPOS, iter->user, USERLEN);
218				wstandend(list);
219				num_shown++;
220			}
221			i++;	
222			lines++;
223		}
224		
225	}
226
227	wrefresh(list);
228	hide_cursor();
229
230	// Is the cursor off the screen, after moving up or down the tree?
231	// (Don't trigger this if we have no entries yet)
232	if(current_pw_sublist->current_item) {
233		if((lines-1) < current_pw_sublist->current_item) {
234			// Just adjust, then redraw
235			current_pw_sublist->current_item = lines-1;
236			uilist_refresh();
237		}
238	}
239
240	// If we have filtering turned on, then warn the user of that
241	if(options->filter) {
242		filter_alert(options->filter);
243	}
244
245	debug("refresh_list: done refreshing list");
246}
247
248int
249uilist_clear()
250{
251	int i;
252	
253	werase(list);
254	for(i = 0; i < COLS; i++){
255		mvaddch(2, i, ' ');
256	}
257}
258
259int
260uilist_headerline()
261{
262	int i;
263
264	show_cursor();
265	attrset(A_BOLD);
266
267	mvaddnstr(2, NAMEPOS, "Name", NAMELEN);
268	mvaddnstr(2, HOSTPOS, "Host", HOSTLEN);
269	mvaddnstr(2, USERPOS, "Username", USERLEN);
270
271	attrset(A_NORMAL);
272	hide_cursor();
273}
274
275int 
276uilist_page_up()
277{
278	current_pw_sublist->current_item -= (LIST_LINES - 1);
279
280	if(current_pw_sublist->current_item < 1){
281		current_pw_sublist->current_item = 0;
282	}
283	uilist_refresh();
284}
285
286int
287uilist_page_down()
288{
289	current_pw_sublist->current_item += (LIST_LINES - 1);
290
291	if(current_pw_sublist->current_item >= (lines - 1)){
292		current_pw_sublist->current_item = lines -1;
293	}
294	uilist_refresh();
295}
296
297int
298uilist_up()
299{
300	if(current_pw_sublist->current_item < 1){
301		return;
302	}
303
304	current_pw_sublist->current_item--;
305	
306	uilist_refresh();
307}
308
309int
310uilist_down()
311{
312	if(current_pw_sublist->current_item >= (lines-1)){
313		return;
314	}
315
316	current_pw_sublist->current_item++;
317
318	uilist_refresh();
319}