pkgcmds.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * dselect - Debian GNU/Linux package maintenance user interface
  3. * pkgcmds.cc - package list keyboard commands
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with this; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <ncurses.h>
  24. #include <assert.h>
  25. #include <signal.h>
  26. extern "C" {
  27. #include "config.h"
  28. #include "dpkg.h"
  29. #include "dpkg-db.h"
  30. }
  31. #include "dselect.h"
  32. #include "pkglist.h"
  33. static int matches(struct pkginfo *pkg, struct pkginfo *comparewith) {
  34. if (comparewith->priority != pkginfo::pri_unset &&
  35. (comparewith->priority != pkg->priority ||
  36. comparewith->priority == pkginfo::pri_other &&
  37. strcasecmp(comparewith->otherpriority,pkg->otherpriority)))
  38. return 0;
  39. if (comparewith->section &&
  40. strcasecmp(comparewith->section,
  41. pkg->section ?
  42. pkg->section : ""))
  43. return 0;
  44. return 1;
  45. }
  46. void packagelist::affectedrange(int *startp, int *endp) {
  47. if (table[cursorline]->pkg->name) {
  48. *startp= cursorline;
  49. *endp= cursorline+1;
  50. return;
  51. }
  52. int index;
  53. for (index= cursorline; index < nitems && !table[index]->pkg->name; index++);
  54. if (index >= nitems) {
  55. *startp= *endp= cursorline;
  56. return;
  57. }
  58. *startp= index;
  59. while (index < nitems && matches(table[index]->pkg,table[cursorline]->pkg)) index++;
  60. *endp= index;
  61. }
  62. void packagelist::movecursorafter(int ncursor) {
  63. if (ncursor >= nitems) ncursor= nitems-1;
  64. topofscreen += ncursor-cursorline;
  65. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  66. if (topofscreen < 0) topofscreen= 0;
  67. setcursor(ncursor);
  68. refreshlist(); redrawthisstate();
  69. }
  70. pkginfo::pkgwant packagelist::reallywant(pkginfo::pkgwant nwarg,
  71. struct perpackagestate *pkgstate) {
  72. if (nwarg != pkginfo::want_sentinel) return nwarg;
  73. pkginfo::pkgstatus status= pkgstate->pkg->status;
  74. if (status == pkginfo::stat_notinstalled) return pkginfo::want_purge;
  75. if (status == pkginfo::stat_configfiles) return pkginfo::want_deinstall;
  76. return pkginfo::want_install;
  77. }
  78. void packagelist::setwant(pkginfo::pkgwant nwarg) {
  79. int index, top, bot;
  80. pkginfo::pkgwant nw;
  81. if (!readwrite) { beep(); return; }
  82. if (recursive) {
  83. redrawitemsrange(cursorline,cursorline+1);
  84. table[cursorline]->selected= reallywant(nwarg,table[cursorline]);
  85. redraw1item(cursorline);
  86. top= cursorline;
  87. bot= cursorline+1;
  88. } else {
  89. packagelist *sub= new packagelist(bindings,0);
  90. affectedrange(&top,&bot);
  91. for (index= top; index < bot; index++) {
  92. if (!table[index]->pkg->name) continue;
  93. nw= reallywant(nwarg,table[index]);
  94. if (table[index]->selected == nw ||
  95. (table[index]->selected == pkginfo::want_purge &&
  96. nw == pkginfo::want_deinstall))
  97. continue;
  98. sub->add(table[index]->pkg,nw);
  99. }
  100. repeatedlydisplay(sub,dp_may,this);
  101. for (index=top; index < bot; index++)
  102. redraw1item(index);
  103. }
  104. movecursorafter(bot);
  105. }
  106. void packagelist::kd_select() { setwant(pkginfo::want_install); }
  107. void packagelist::kd_hold() { setwant(pkginfo::want_hold); }
  108. void packagelist::kd_deselect() { setwant(pkginfo::want_deinstall); }
  109. void packagelist::kd_unhold() { setwant(pkginfo::want_sentinel); }
  110. void packagelist::kd_purge() { setwant(pkginfo::want_purge); }
  111. int would_like_to_install(pkginfo::pkgwant wantvalue, pkginfo *pkg) {
  112. /* Returns: 1 for yes, 0 for no, -1 for if they want to preserve an error condition. */
  113. if (debug) fprintf(debug,"would_like_to_install(%d, %s) status %d\n",
  114. wantvalue,pkg->name,pkg->status);
  115. if (wantvalue == pkginfo::want_install) return 1;
  116. if (wantvalue != pkginfo::want_hold) return 0;
  117. if (pkg->status == pkginfo::stat_installed) return 1;
  118. if (pkg->status == pkginfo::stat_notinstalled ||
  119. pkg->status == pkginfo::stat_configfiles) return 0;
  120. return -1;
  121. }
  122. const char *packagelist::itemname(int index) {
  123. return table[index]->pkg->name;
  124. }
  125. void packagelist::kd_swapstatorder() {
  126. if (sortorder == so_unsorted) return;
  127. switch (statsortorder) {
  128. case sso_avail: statsortorder= sso_state; break;
  129. case sso_state: statsortorder= sso_unsorted; break;
  130. case sso_unsorted: statsortorder= sso_avail; break;
  131. default: internerr("unknown statsort in kd_swapstatorder");
  132. }
  133. resortredisplay();
  134. }
  135. void packagelist::kd_swaporder() {
  136. switch (sortorder) {
  137. case so_priority: sortorder= so_section; break;
  138. case so_section: sortorder= so_alpha; break;
  139. case so_alpha: sortorder= so_priority; break;
  140. case so_unsorted: return;
  141. default: internerr("unknown sort in kd_swaporder");
  142. }
  143. resortredisplay();
  144. }
  145. void packagelist::resortredisplay() {
  146. const char *oldname= table[cursorline]->pkg->name;
  147. sortmakeheads();
  148. int newcursor;
  149. newcursor= 0;
  150. if (oldname) {
  151. int index;
  152. for (index=0; index<nitems; index++) {
  153. if (table[index]->pkg->name && !strcasecmp(oldname,table[index]->pkg->name)) {
  154. newcursor= index;
  155. break;
  156. }
  157. }
  158. }
  159. topofscreen= newcursor-1;
  160. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  161. if (topofscreen < 0) topofscreen= 0;
  162. setwidths();
  163. redrawtitle();
  164. redrawcolheads();
  165. ldrawnstart= ldrawnend= -1;
  166. cursorline= -1;
  167. setcursor(newcursor);
  168. refreshlist();
  169. }
  170. void packagelist::kd_versiondisplay() {
  171. switch (versiondisplayopt) {
  172. case vdo_both: versiondisplayopt= vdo_none; break;
  173. case vdo_none: versiondisplayopt= vdo_available; break;
  174. case vdo_available: versiondisplayopt= vdo_both; break;
  175. default: internerr("unknown versiondisplayopt in kd_versiondisplay");
  176. }
  177. setwidths();
  178. leftofscreen= 0;
  179. ldrawnstart= ldrawnend= -1;
  180. redrawtitle();
  181. redrawcolheads();
  182. redrawitemsrange(topofscreen,lesserint(topofscreen+list_height,nitems));
  183. refreshlist();
  184. }
  185. void packagelist::kd_verbose() {
  186. verbose= !verbose;
  187. setwidths();
  188. leftofscreen= 0;
  189. ldrawnstart= ldrawnend= -1;
  190. redrawtitle();
  191. redrawcolheads();
  192. redrawitemsrange(topofscreen,lesserint(topofscreen+list_height,nitems));
  193. refreshlist();
  194. }
  195. void packagelist::kd_quit_noop() { }
  196. void packagelist::kd_revert_abort() {
  197. int index;
  198. for (index=0; index<nitems; index++) {
  199. if (table[index]->pkg->name)
  200. table[index]->selected= table[index]->original;
  201. ldrawnstart= ldrawnend= -1;
  202. }
  203. refreshlist(); redrawthisstate();
  204. }
  205. void packagelist::kd_revertdirect() {
  206. int index;
  207. for (index=0; index<nitems; index++) {
  208. if (table[index]->pkg->name)
  209. table[index]->selected= table[index]->direct;
  210. ldrawnstart= ldrawnend= -1;
  211. }
  212. refreshlist(); redrawthisstate();
  213. }
  214. void packagelist::kd_revertsuggest() {
  215. int index;
  216. for (index=0; index<nitems; index++) {
  217. if (table[index]->pkg->name)
  218. table[index]->selected= table[index]->suggested;
  219. ldrawnstart= ldrawnend= -1;
  220. }
  221. refreshlist(); redrawthisstate();
  222. }
  223. /* fixme: configurable purge/deselect */
  224. void packagelist::kd_toggleinfo() {
  225. showinfo= (showinfo+2) % 3;
  226. setheights();
  227. if (cursorline >= topofscreen+list_height) topofscreen += list_height;
  228. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  229. if (topofscreen < 0) topofscreen= 0;
  230. infotopofscreen= 0;
  231. redraw1item(cursorline);
  232. refreshlist();
  233. redrawthisstate();
  234. redrawinfo();
  235. }
  236. void packagelist::kd_info() {
  237. if (!showinfo) {
  238. showinfo= 2; kd_toggleinfo();
  239. } else {
  240. currentinfo++;
  241. infotopofscreen=0;
  242. redrawinfo();
  243. }
  244. }