pkgcmds.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. int packagelist::affectedmatches(struct pkginfo *pkg, struct pkginfo *comparewith) {
  34. switch (statsortorder) {
  35. case sso_avail:
  36. if (comparewith->clientdata->ssavail != pkg->clientdata->ssavail) return 0;
  37. break;
  38. case sso_state:
  39. if (comparewith->clientdata->ssstate != pkg->clientdata->ssstate) return 0;
  40. break;
  41. case sso_unsorted:
  42. break;
  43. default:
  44. internerr("unknown statsortorder in affectedmatches");
  45. }
  46. if (comparewith->priority != pkginfo::pri_unset &&
  47. (comparewith->priority != pkg->priority ||
  48. comparewith->priority == pkginfo::pri_other &&
  49. strcasecmp(comparewith->otherpriority,pkg->otherpriority)))
  50. return 0;
  51. if (comparewith->section &&
  52. strcasecmp(comparewith->section,
  53. pkg->section ?
  54. pkg->section : ""))
  55. return 0;
  56. return 1;
  57. }
  58. void packagelist::affectedrange(int *startp, int *endp) {
  59. if (table[cursorline]->pkg->name) {
  60. *startp= cursorline;
  61. *endp= cursorline+1;
  62. return;
  63. }
  64. int index;
  65. for (index= cursorline; index < nitems && !table[index]->pkg->name; index++);
  66. if (index >= nitems) {
  67. *startp= *endp= cursorline;
  68. return;
  69. }
  70. *startp= index;
  71. while (index < nitems && affectedmatches(table[index]->pkg,table[cursorline]->pkg))
  72. index++;
  73. *endp= index;
  74. }
  75. void packagelist::movecursorafter(int ncursor) {
  76. if (ncursor >= nitems) ncursor= nitems-1;
  77. topofscreen += ncursor-cursorline;
  78. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  79. if (topofscreen < 0) topofscreen= 0;
  80. setcursor(ncursor);
  81. refreshlist(); redrawthisstate();
  82. }
  83. pkginfo::pkgwant packagelist::reallywant(pkginfo::pkgwant nwarg,
  84. struct perpackagestate *pkgstate) {
  85. if (nwarg != pkginfo::want_sentinel) return nwarg;
  86. pkginfo::pkgstatus status= pkgstate->pkg->status;
  87. if (status == pkginfo::stat_notinstalled) return pkginfo::want_purge;
  88. if (status == pkginfo::stat_configfiles) return pkginfo::want_deinstall;
  89. return pkginfo::want_install;
  90. }
  91. void packagelist::setwant(pkginfo::pkgwant nwarg) {
  92. int index, top, bot;
  93. pkginfo::pkgwant nw;
  94. if (!readwrite) { beep(); return; }
  95. if (recursive) {
  96. redrawitemsrange(cursorline,cursorline+1);
  97. table[cursorline]->selected= reallywant(nwarg,table[cursorline]);
  98. redraw1item(cursorline);
  99. top= cursorline;
  100. bot= cursorline+1;
  101. } else {
  102. packagelist *sub= new packagelist(bindings,0);
  103. affectedrange(&top,&bot);
  104. for (index= top; index < bot; index++) {
  105. if (!table[index]->pkg->name) continue;
  106. nw= reallywant(nwarg,table[index]);
  107. if (table[index]->selected == nw ||
  108. (table[index]->selected == pkginfo::want_purge &&
  109. nw == pkginfo::want_deinstall))
  110. continue;
  111. sub->add(table[index]->pkg,nw);
  112. }
  113. repeatedlydisplay(sub,dp_may,this);
  114. for (index=top; index < bot; index++)
  115. redraw1item(index);
  116. }
  117. movecursorafter(bot);
  118. }
  119. void packagelist::kd_select() { setwant(pkginfo::want_install); }
  120. void packagelist::kd_hold() { setwant(pkginfo::want_hold); }
  121. void packagelist::kd_deselect() { setwant(pkginfo::want_deinstall); }
  122. void packagelist::kd_unhold() { setwant(pkginfo::want_sentinel); }
  123. void packagelist::kd_purge() { setwant(pkginfo::want_purge); }
  124. int would_like_to_install(pkginfo::pkgwant wantvalue, pkginfo *pkg) {
  125. /* Returns: 1 for yes, 0 for no, -1 for if they want to preserve an error condition. */
  126. if (debug) fprintf(debug,"would_like_to_install(%d, %s) status %d\n",
  127. wantvalue,pkg->name,pkg->status);
  128. if (wantvalue == pkginfo::want_install) return 1;
  129. if (wantvalue != pkginfo::want_hold) return 0;
  130. if (pkg->status == pkginfo::stat_installed) return 1;
  131. if (pkg->status == pkginfo::stat_notinstalled ||
  132. pkg->status == pkginfo::stat_configfiles) return 0;
  133. return -1;
  134. }
  135. const char *packagelist::itemname(int index) {
  136. return table[index]->pkg->name;
  137. }
  138. void packagelist::kd_swapstatorder() {
  139. if (sortorder == so_unsorted) return;
  140. switch (statsortorder) {
  141. case sso_avail: statsortorder= sso_state; break;
  142. case sso_state: statsortorder= sso_unsorted; break;
  143. case sso_unsorted: statsortorder= sso_avail; break;
  144. default: internerr("unknown statsort in kd_swapstatorder");
  145. }
  146. resortredisplay();
  147. }
  148. void packagelist::kd_swaporder() {
  149. switch (sortorder) {
  150. case so_priority: sortorder= so_section; break;
  151. case so_section: sortorder= so_alpha; break;
  152. case so_alpha: sortorder= so_priority; break;
  153. case so_unsorted: return;
  154. default: internerr("unknown sort in kd_swaporder");
  155. }
  156. resortredisplay();
  157. }
  158. void packagelist::resortredisplay() {
  159. const char *oldname= table[cursorline]->pkg->name;
  160. sortmakeheads();
  161. int newcursor;
  162. newcursor= 0;
  163. if (oldname) {
  164. int index;
  165. for (index=0; index<nitems; index++) {
  166. if (table[index]->pkg->name && !strcasecmp(oldname,table[index]->pkg->name)) {
  167. newcursor= index;
  168. break;
  169. }
  170. }
  171. }
  172. topofscreen= newcursor-1;
  173. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  174. if (topofscreen < 0) topofscreen= 0;
  175. setwidths();
  176. redrawtitle();
  177. redrawcolheads();
  178. ldrawnstart= ldrawnend= -1;
  179. cursorline= -1;
  180. setcursor(newcursor);
  181. refreshlist();
  182. }
  183. void packagelist::kd_versiondisplay() {
  184. switch (versiondisplayopt) {
  185. case vdo_both: versiondisplayopt= vdo_none; break;
  186. case vdo_none: versiondisplayopt= vdo_available; break;
  187. case vdo_available: versiondisplayopt= vdo_both; break;
  188. default: internerr("unknown versiondisplayopt in kd_versiondisplay");
  189. }
  190. setwidths();
  191. leftofscreen= 0;
  192. ldrawnstart= ldrawnend= -1;
  193. redrawtitle();
  194. redrawcolheads();
  195. redrawitemsrange(topofscreen,lesserint(topofscreen+list_height,nitems));
  196. refreshlist();
  197. }
  198. void packagelist::kd_verbose() {
  199. verbose= !verbose;
  200. setwidths();
  201. leftofscreen= 0;
  202. ldrawnstart= ldrawnend= -1;
  203. redrawtitle();
  204. redrawcolheads();
  205. redrawitemsrange(topofscreen,lesserint(topofscreen+list_height,nitems));
  206. refreshlist();
  207. }
  208. void packagelist::kd_quit_noop() { }
  209. void packagelist::kd_revert_abort() {
  210. int index;
  211. for (index=0; index<nitems; index++) {
  212. if (table[index]->pkg->name)
  213. table[index]->selected= table[index]->original;
  214. ldrawnstart= ldrawnend= -1;
  215. }
  216. refreshlist(); redrawthisstate();
  217. }
  218. void packagelist::kd_revertdirect() {
  219. int index;
  220. for (index=0; index<nitems; index++) {
  221. if (table[index]->pkg->name)
  222. table[index]->selected= table[index]->direct;
  223. ldrawnstart= ldrawnend= -1;
  224. }
  225. refreshlist(); redrawthisstate();
  226. }
  227. void packagelist::kd_revertsuggest() {
  228. int index;
  229. for (index=0; index<nitems; index++) {
  230. if (table[index]->pkg->name)
  231. table[index]->selected= table[index]->suggested;
  232. ldrawnstart= ldrawnend= -1;
  233. }
  234. refreshlist(); redrawthisstate();
  235. }
  236. /* fixme: configurable purge/deselect */
  237. void packagelist::kd_toggleinfo() {
  238. showinfo= (showinfo+2) % 3;
  239. setheights();
  240. if (cursorline >= topofscreen+list_height) topofscreen += list_height;
  241. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  242. if (topofscreen < 0) topofscreen= 0;
  243. infotopofscreen= 0;
  244. redraw1item(cursorline);
  245. refreshlist();
  246. redrawthisstate();
  247. redrawinfo();
  248. }
  249. void packagelist::kd_info() {
  250. if (!showinfo) {
  251. showinfo= 2; kd_toggleinfo();
  252. } else {
  253. currentinfo++;
  254. infotopofscreen=0;
  255. redrawinfo();
  256. }
  257. }