pkgcmds.cc 8.3 KB

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