PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms 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
Possible License(s): GPL-2.0
  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. #include <ui.h>
  21. #include <pwman.h>
  22. WINDOW *list = NULL;
  23. int lines = 0;
  24. //int curitem = 0;
  25. int
  26. uilist_init()
  27. {
  28. char str[80];
  29. list = newwin(LIST_LINES, COLS, LIST_TOP, 0);
  30. scrollok(list, TRUE);
  31. }
  32. /*
  33. int
  34. resize_list()
  35. {
  36. wresize(list, LIST_LINES, COLS);
  37. }
  38. */
  39. int
  40. uilist_free()
  41. {
  42. delwin(list);
  43. list = NULL;
  44. }
  45. int
  46. uilist_highlight_line(int line)
  47. {
  48. scrollok(list, FALSE);
  49. int i;
  50. wmove(list, line, 0);
  51. waddch(list, '>');
  52. for(i = 1; i < COLS; i++)
  53. waddch(list, ' ');
  54. scrollok(list, TRUE);
  55. }
  56. PWList *
  57. uilist_get_highlighted_sublist()
  58. {
  59. PWList *iter;
  60. int i = -1;
  61. if(!current_pw_sublist){ return NULL; }
  62. if(current_pw_sublist->parent){ i++; }
  63. for(iter = current_pw_sublist->sublists; iter != NULL; iter = iter->next){
  64. i++;
  65. if(i == current_pw_sublist->current_item){
  66. break;
  67. }
  68. }
  69. return iter;
  70. }
  71. Pw *
  72. uilist_get_highlighted_item()
  73. {
  74. Pw *iter;
  75. PWList *listiter;
  76. int i = -1;
  77. if(current_pw_sublist->parent){ i++; }
  78. for(listiter = current_pw_sublist->sublists; listiter != NULL; listiter = listiter->next){
  79. i++;
  80. }
  81. for(iter = current_pw_sublist->list; iter != NULL; iter = iter->next){
  82. if( filter_apply(iter, options->filter) ){
  83. i++;
  84. }
  85. if( i == current_pw_sublist->current_item ){
  86. debug("get_highlighted_item: found %d, break now", i);
  87. return iter;
  88. }
  89. }
  90. /* fprintf(stderr, "%d.", curitem);
  91. for(iter = pwlist; (iter != NULL) && i <= curitem; iter = iter->next){
  92. if( apply_filter(iter, options->filter) ){
  93. i++;
  94. }
  95. }*/
  96. debug("get_highlighted_item: nothing found, return NULL");
  97. return NULL;
  98. }
  99. LIST_ITEM_TYPE
  100. uilist_get_highlighted_type()
  101. {
  102. Pw *iter;
  103. PWList *listiter;
  104. int i = -1;
  105. if(current_pw_sublist->parent){
  106. if(current_pw_sublist->current_item == 0){
  107. return PW_UPLEVEL;
  108. }
  109. i++;
  110. }
  111. for(listiter = current_pw_sublist->sublists; listiter != NULL; listiter = listiter->next){
  112. i++;
  113. if(i == current_pw_sublist->current_item){
  114. return PW_SUBLIST;
  115. }
  116. }
  117. for(iter = current_pw_sublist->list; iter != NULL; iter = iter->next){
  118. if( filter_apply(iter, options->filter) ){
  119. i++;
  120. }
  121. if( i == current_pw_sublist->current_item ){
  122. return PW_ITEM;
  123. }
  124. }
  125. return PW_NULL;
  126. }
  127. int
  128. uilist_refresh()
  129. {
  130. Pw *iter;
  131. PWList *listiter;
  132. int i = 0;
  133. static int first_list_item = 0;
  134. int num_shown = 0;
  135. debug("refresh_list: refreshing list");
  136. if(list == NULL){
  137. uilist_init();
  138. }
  139. if(current_pw_sublist == NULL){
  140. return -1;
  141. }
  142. uilist_clear();;
  143. lines = 0;
  144. uilist_headerline();
  145. // Ensure we don't end up off the screen
  146. if(current_pw_sublist->current_item < 0){
  147. current_pw_sublist->current_item = 0;
  148. }
  149. if(current_pw_sublist->current_item < first_list_item){
  150. first_list_item = current_pw_sublist->current_item;
  151. } else if((current_pw_sublist->current_item > LAST_LIST_ITEM)){
  152. first_list_item = current_pw_sublist->current_item - (LIST_LINES-1);
  153. }
  154. // If we aren't at the top level, off the "Up One Level" item
  155. if(current_pw_sublist->parent){
  156. if((i >= first_list_item) && (i <= LAST_LIST_ITEM)){
  157. if(lines == current_pw_sublist->current_item){
  158. uilist_highlight_line(num_shown);
  159. }
  160. mvwprintw(list, num_shown, NAMEPOS, "<Up One Level - \"%s\">", current_pw_sublist->parent->name);
  161. wattrset(list, A_NORMAL);
  162. wstandend(list);
  163. num_shown++;
  164. }
  165. i++;
  166. lines++;
  167. }
  168. // Draw our sublists
  169. for(listiter = current_pw_sublist->sublists; listiter != NULL; listiter = listiter->next){
  170. if((i >= first_list_item) && (i <= LAST_LIST_ITEM)){
  171. if(lines == current_pw_sublist->current_item){
  172. uilist_highlight_line(num_shown);
  173. }
  174. mvwprintw(list, num_shown, NAMEPOS, "%s ->", listiter->name);
  175. wattrset(list, A_NORMAL);
  176. wstandend(list);
  177. num_shown++;
  178. }
  179. i++;
  180. lines++;
  181. }
  182. // Draw our entries, if the filter says it's ok
  183. for(iter = current_pw_sublist->list; (iter != NULL); iter = iter->next){
  184. /*
  185. * if line satifies filter criteria increment i and lines
  186. */
  187. if( filter_apply(iter, options->filter) ){
  188. if((i >= first_list_item) && (i <= LAST_LIST_ITEM)){
  189. if(lines == current_pw_sublist->current_item){
  190. uilist_highlight_line(num_shown);
  191. }
  192. wattrset(list, COLOR_PAIR(1));
  193. mvwaddnstr(list, num_shown, NAMEPOS, iter->name, NAMELEN);
  194. mvwaddnstr(list, num_shown, HOSTPOS, iter->host, HOSTLEN);
  195. mvwaddnstr(list, num_shown, USERPOS, iter->user, USERLEN);
  196. wstandend(list);
  197. num_shown++;
  198. }
  199. i++;
  200. lines++;
  201. }
  202. }
  203. wrefresh(list);
  204. hide_cursor();
  205. // Is the cursor off the screen, after moving up or down the tree?
  206. // (Don't trigger this if we have no entries yet)
  207. if(current_pw_sublist->current_item) {
  208. if((lines-1) < current_pw_sublist->current_item) {
  209. // Just adjust, then redraw
  210. current_pw_sublist->current_item = lines-1;
  211. uilist_refresh();
  212. }
  213. }
  214. // If we have filtering turned on, then warn the user of that
  215. if(options->filter) {
  216. filter_alert(options->filter);
  217. }
  218. debug("refresh_list: done refreshing list");
  219. }
  220. int
  221. uilist_clear()
  222. {
  223. int i;
  224. werase(list);
  225. for(i = 0; i < COLS; i++){
  226. mvaddch(2, i, ' ');
  227. }
  228. }
  229. int
  230. uilist_headerline()
  231. {
  232. int i;
  233. show_cursor();
  234. attrset(A_BOLD);
  235. mvaddnstr(2, NAMEPOS, "Name", NAMELEN);
  236. mvaddnstr(2, HOSTPOS, "Host", HOSTLEN);
  237. mvaddnstr(2, USERPOS, "Username", USERLEN);
  238. attrset(A_NORMAL);
  239. hide_cursor();
  240. }
  241. int
  242. uilist_page_up()
  243. {
  244. current_pw_sublist->current_item -= (LIST_LINES - 1);
  245. if(current_pw_sublist->current_item < 1){
  246. current_pw_sublist->current_item = 0;
  247. }
  248. uilist_refresh();
  249. }
  250. int
  251. uilist_page_down()
  252. {
  253. current_pw_sublist->current_item += (LIST_LINES - 1);
  254. if(current_pw_sublist->current_item >= (lines - 1)){
  255. current_pw_sublist->current_item = lines -1;
  256. }
  257. uilist_refresh();
  258. }
  259. int
  260. uilist_up()
  261. {
  262. if(current_pw_sublist->current_item < 1){
  263. return;
  264. }
  265. current_pw_sublist->current_item--;
  266. uilist_refresh();
  267. }
  268. int
  269. uilist_down()
  270. {
  271. if(current_pw_sublist->current_item >= (lines-1)){
  272. return;
  273. }
  274. current_pw_sublist->current_item++;
  275. uilist_refresh();
  276. }