pkgcmds.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * pkgcmds.cc - package list keyboard commands
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2008-2014 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <dpkg/dpkg.h>
  26. #include <dpkg/dpkg-db.h>
  27. #include "dselect.h"
  28. #include "pkglist.h"
  29. bool
  30. packagelist::affectedmatches(struct pkginfo *pkg, struct pkginfo *comparewith) {
  31. switch (statsortorder) {
  32. case sso_avail:
  33. if (comparewith->clientdata->ssavail != pkg->clientdata->ssavail)
  34. return false;
  35. break;
  36. case sso_state:
  37. if (comparewith->clientdata->ssstate != pkg->clientdata->ssstate)
  38. return false;
  39. break;
  40. case sso_unsorted:
  41. break;
  42. default:
  43. internerr("unknown statsortorder %d", statsortorder);
  44. }
  45. if (comparewith->priority != PKG_PRIO_UNSET &&
  46. (comparewith->priority != pkg->priority ||
  47. (comparewith->priority == PKG_PRIO_OTHER &&
  48. strcasecmp(comparewith->otherpriority, pkg->otherpriority))))
  49. return false;
  50. if (comparewith->section &&
  51. strcasecmp(comparewith->section,
  52. pkg->section ?
  53. pkg->section : ""))
  54. return false;
  55. return true;
  56. }
  57. void packagelist::affectedrange(int *startp, int *endp) {
  58. if (table[cursorline]->pkg->set->name) {
  59. *startp= cursorline;
  60. *endp= cursorline+1;
  61. return;
  62. }
  63. int index = cursorline;
  64. while (index < nitems && !table[index]->pkg->set->name)
  65. 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. pkgwant
  84. packagelist::reallywant(pkgwant nwarg, struct perpackagestate *pkgstate)
  85. {
  86. if (nwarg != PKG_WANT_SENTINEL)
  87. return nwarg;
  88. pkgstatus status = pkgstate->pkg->status;
  89. if (status == PKG_STAT_NOTINSTALLED)
  90. return PKG_WANT_PURGE;
  91. if (status == PKG_STAT_CONFIGFILES)
  92. return PKG_WANT_DEINSTALL;
  93. return PKG_WANT_INSTALL;
  94. }
  95. void
  96. packagelist::setwant(pkgwant nwarg)
  97. {
  98. int index, top, bot;
  99. pkgwant nw;
  100. if (modstatdb_get_status() == msdbrw_readonly) {
  101. beep();
  102. return;
  103. }
  104. if (recursive) {
  105. redrawitemsrange(cursorline,cursorline+1);
  106. table[cursorline]->selected= reallywant(nwarg,table[cursorline]);
  107. redraw1item(cursorline);
  108. top= cursorline;
  109. bot= cursorline+1;
  110. } else {
  111. packagelist *sub = new packagelist(bindings, nullptr);
  112. affectedrange(&top,&bot);
  113. for (index= top; index < bot; index++) {
  114. if (!table[index]->pkg->set->name)
  115. continue;
  116. nw= reallywant(nwarg,table[index]);
  117. if (table[index]->selected == nw ||
  118. (table[index]->selected == PKG_WANT_PURGE &&
  119. nw == PKG_WANT_DEINSTALL))
  120. continue;
  121. sub->add(table[index]->pkg,nw);
  122. }
  123. repeatedlydisplay(sub,dp_may,this);
  124. for (index=top; index < bot; index++)
  125. redraw1item(index);
  126. }
  127. movecursorafter(bot);
  128. }
  129. bool manual_install = false;
  130. void packagelist::kd_select() {
  131. manual_install = true;
  132. setwant(PKG_WANT_INSTALL);
  133. manual_install = false;
  134. }
  135. void packagelist::kd_hold() { setwant(PKG_WANT_HOLD); }
  136. void packagelist::kd_deselect() { setwant(PKG_WANT_DEINSTALL); }
  137. void packagelist::kd_unhold() { setwant(PKG_WANT_SENTINEL); }
  138. void packagelist::kd_purge() { setwant(PKG_WANT_PURGE); }
  139. int
  140. would_like_to_install(pkgwant wantvalue, pkginfo *pkg)
  141. {
  142. /* Returns: 1 for yes, 0 for no, -1 for if they want to preserve an error condition. */
  143. debug(dbg_general, "would_like_to_install(%d, %s) status %d",
  144. wantvalue, pkg_name(pkg, pnaw_always), pkg->status);
  145. if (wantvalue == PKG_WANT_INSTALL)
  146. return 1;
  147. if (wantvalue != PKG_WANT_HOLD)
  148. return 0;
  149. if (pkg->status == PKG_STAT_INSTALLED)
  150. return 1;
  151. if (pkg->status == PKG_STAT_NOTINSTALLED ||
  152. pkg->status == PKG_STAT_CONFIGFILES)
  153. return 0;
  154. return -1;
  155. }
  156. const char *packagelist::itemname(int index) {
  157. return table[index]->pkg->set->name;
  158. }
  159. void packagelist::kd_swapstatorder() {
  160. if (sortorder == so_unsorted) return;
  161. switch (statsortorder) {
  162. case sso_avail: statsortorder= sso_state; break;
  163. case sso_state: statsortorder= sso_unsorted; break;
  164. case sso_unsorted: statsortorder= sso_avail; break;
  165. default:
  166. internerr("unknown statsort %d", statsortorder);
  167. }
  168. resortredisplay();
  169. }
  170. void packagelist::kd_swaporder() {
  171. switch (sortorder) {
  172. case so_priority: sortorder= so_section; break;
  173. case so_section: sortorder= so_alpha; break;
  174. case so_alpha: sortorder= so_priority; break;
  175. case so_unsorted: return;
  176. default:
  177. internerr("unknown sort %d", sortorder);
  178. }
  179. resortredisplay();
  180. }
  181. void packagelist::resortredisplay() {
  182. const char *oldname = table[cursorline]->pkg->set->name;
  183. sortmakeheads();
  184. int newcursor;
  185. newcursor= 0;
  186. if (oldname) {
  187. int index;
  188. for (index=0; index<nitems; index++) {
  189. if (table[index]->pkg->set->name &&
  190. strcasecmp(oldname, table[index]->pkg->set->name) == 0) {
  191. newcursor= index;
  192. break;
  193. }
  194. }
  195. }
  196. topofscreen= newcursor-1;
  197. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  198. if (topofscreen < 0) topofscreen= 0;
  199. setwidths();
  200. redrawtitle();
  201. redrawcolheads();
  202. ldrawnstart= ldrawnend= -1;
  203. cursorline= -1;
  204. setcursor(newcursor);
  205. refreshlist();
  206. }
  207. void
  208. packagelist::kd_archdisplay()
  209. {
  210. switch (archdisplayopt) {
  211. case ado_both:
  212. archdisplayopt = ado_none;
  213. break;
  214. case ado_none:
  215. archdisplayopt = ado_available;
  216. break;
  217. case ado_available:
  218. archdisplayopt = ado_both;
  219. break;
  220. default:
  221. internerr("unknown archdisplayopt %d", archdisplayopt);
  222. }
  223. setwidths();
  224. leftofscreen = 0;
  225. ldrawnstart = ldrawnend = -1;
  226. redrawtitle();
  227. redrawcolheads();
  228. redrawitemsrange(topofscreen, min(topofscreen + list_height, nitems));
  229. refreshlist();
  230. }
  231. void packagelist::kd_versiondisplay() {
  232. switch (versiondisplayopt) {
  233. case vdo_both: versiondisplayopt= vdo_none; break;
  234. case vdo_none: versiondisplayopt= vdo_available; break;
  235. case vdo_available: versiondisplayopt= vdo_both; break;
  236. default:
  237. internerr("unknown versiondisplayopt %d", versiondisplayopt);
  238. }
  239. setwidths();
  240. leftofscreen= 0;
  241. ldrawnstart= ldrawnend= -1;
  242. redrawtitle();
  243. redrawcolheads();
  244. redrawitemsrange(topofscreen, min(topofscreen + list_height, nitems));
  245. refreshlist();
  246. }
  247. void packagelist::kd_verbose() {
  248. verbose= !verbose;
  249. setwidths();
  250. leftofscreen= 0;
  251. ldrawnstart= ldrawnend= -1;
  252. redrawtitle();
  253. redrawcolheads();
  254. redrawitemsrange(topofscreen, min(topofscreen + list_height, nitems));
  255. refreshlist();
  256. }
  257. void packagelist::kd_quit_noop() { }
  258. void packagelist::kd_revert_abort() {
  259. int index;
  260. for (index=0; index<nitems; index++) {
  261. if (table[index]->pkg->set->name)
  262. table[index]->selected= table[index]->original;
  263. ldrawnstart= ldrawnend= -1;
  264. }
  265. refreshlist(); redrawthisstate();
  266. }
  267. void packagelist::kd_revertdirect() {
  268. int index;
  269. for (index=0; index<nitems; index++) {
  270. if (table[index]->pkg->set->name)
  271. table[index]->selected= table[index]->direct;
  272. ldrawnstart= ldrawnend= -1;
  273. }
  274. refreshlist(); redrawthisstate();
  275. }
  276. void packagelist::kd_revertsuggest() {
  277. int index;
  278. for (index=0; index<nitems; index++) {
  279. if (table[index]->pkg->set->name)
  280. table[index]->selected= table[index]->suggested;
  281. ldrawnstart= ldrawnend= -1;
  282. }
  283. refreshlist(); redrawthisstate();
  284. }
  285. void
  286. packagelist::kd_revertinstalled()
  287. {
  288. int i;
  289. for (i = 0; i < nitems; i++) {
  290. if (table[i]->pkg->set->name)
  291. table[i]->selected = reallywant(PKG_WANT_SENTINEL, table[i]);
  292. ldrawnstart = ldrawnend = -1;
  293. }
  294. refreshlist();
  295. redrawthisstate();
  296. }
  297. /* FIXME: configurable purge/deselect */
  298. void packagelist::kd_toggleinfo() {
  299. showinfo= (showinfo+2) % 3;
  300. setheights();
  301. if (cursorline >= topofscreen+list_height) topofscreen += list_height;
  302. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  303. if (topofscreen < 0) topofscreen= 0;
  304. infotopofscreen= 0;
  305. redraw1item(cursorline);
  306. refreshlist();
  307. redrawthisstate();
  308. redrawinfo();
  309. }
  310. void packagelist::kd_info() {
  311. if (!showinfo) {
  312. showinfo= 2; kd_toggleinfo();
  313. } else {
  314. currentinfo++;
  315. infotopofscreen=0;
  316. redrawinfo();
  317. }
  318. }