pkgcmds.cc 8.6 KB

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