pkgcmds.cc 8.6 KB

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