pkgcmds.cc 8.9 KB

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