summaryrefslogtreecommitdiff
path: root/src/pkg_view.cc
blob: 271fe9452c2bc25c86022373ec278b532f8306f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// pkg_view.cc
//
//  Copyright 2000-2005, 2007 Daniel Burrows
//
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License as
//   published by the Free Software Foundation; either version 2 of
//   the License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//   General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; see the file COPYING.  If not, write to
//   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//   Boston, MA 02111-1307, USA.

#include "pkg_view.h"

#include "aptitude.h"

#include "desc_parse.h"
#include "edit_pkg_hier.h"
#include "menu_redirect.h"
#include "pkg_columnizer.h"
#include "reason_fragment.h"
#include "trust.h"
#include "ui.h"

#include <cmdline/cmdline_why.h>

#include <vscreen/fragment.h>
#include <cwidget/widgets/label.h>
#include <cwidget/widgets/multiplex.h>
#include <cwidget/widgets/scrollbar.h>
#include <cwidget/widgets/table.h>
#include <cwidget/widgets/text_layout.h>
#include <vscreen/vscreen_widget.h>
#include <vscreen/config/keybindings.h>
#include <vscreen/transcode.h>

#include <generic/apt/apt.h>
#include <generic/apt/config_signal.h>
#include <generic/apt/matchers.h>

#include <apt-pkg/error.h>
#include <apt-pkg/pkgrecords.h>

#include <sigc++/adaptors/bind.h>
#include <sigc++/functors/mem_fun.h>
#include <sigc++/functors/ptr_fun.h>

#include <ctype.h>

#include <string>

using namespace std;

class pkg_handling_label:public vs_label
{
  column_definition_list *columns;

  bool have_pkg;
  pkgCache::PkgIterator pkg;
  pkgCache::VerIterator ver;

  void zap_package()
  {
    have_pkg=false;
  }

protected:
  pkg_handling_label(column_definition_list *_columns)
    :vs_label(" "), columns(_columns), have_pkg(false)
  {
    cache_closed.connect(sigc::mem_fun(*this, &pkg_handling_label::zap_package));
  }

public:
  static ref_ptr<pkg_handling_label>
  create(column_definition_list *columns)
  {
    ref_ptr<pkg_handling_label> rval(new pkg_handling_label(columns));
    rval->decref();
    return rval;
  }

  ~pkg_handling_label() {delete columns;}

  size size_request() {return size(1,1);}

  void set_columns(column_definition_list *_columns)
  {
    delete columns;
    columns=_columns;
    vscreen_update();
  }

  void do_columnify(const pkgCache::PkgIterator &_pkg,
		    const pkgCache::VerIterator &_ver)
  {
    pkg=_pkg;
    ver=_ver;

    have_pkg=!pkg.end();

    vscreen_update();
  }

  void paint(const style &st)
  {
    vs_widget_ref tmpref(this);

    if(apt_cache_file)
      {
	// Needed to initialize translated widths and stuff.
	pkg_item::pkg_columnizer::setup_columns();

	if(!have_pkg)
	  {
	    pkg=pkgCache::PkgIterator();
	    // Reinitialize it all the time to avoid the "magic autochanging
	    // pointers" bug.
	    ver=pkgCache::VerIterator(*apt_cache_file);
	  }

	empty_column_parameters p;
	set_text(pkg_item::pkg_columnizer(pkg, ver, *columns, 0).layout_columns(getmaxx(), p));
      }
    else
      set_text("");

    vs_label::paint(st);
  }
};

typedef ref_ptr<pkg_handling_label> pkg_handling_label_ref;

static void do_set_column_format(string key, string the_default,
				 pkg_handling_label &lBare)
{
  pkg_handling_label_ref l(&lBare);

  string format=aptcfg->Find(key, the_default.c_str());
  wstring wformat;

  column_definition_list *columns=NULL;

  if(!transcode(format.c_str(), wformat))
    _error->Errno("iconv", _("Couldn't transcode column definition"));
  else
    columns=parse_columns(wformat,
			  pkg_item::pkg_columnizer::parse_column_type,
			  pkg_item::pkg_columnizer::defaults);

  if(!columns)
    _error->Error(_("Couldn't parse column definition"));
  else
    l->set_columns(columns);
}

class pkg_why_widget:public vs_text_layout
{
protected:
  pkg_why_widget()
  {
  }
public:
  static ref_ptr<pkg_why_widget> create()
  {
    ref_ptr<pkg_why_widget> rval(new pkg_why_widget);
    rval->decref();
    return rval;
  }

  void set_package(const pkgCache::PkgIterator &pkg,
		   const pkgCache::VerIterator &ver)
  {
    if(pkg.end())
      {
	set_fragment(text_fragment(""));
	return;
      }

    std::vector<pkg_matcher *> search_leaves;
    search_leaves.push_back(parse_pattern("~i!~M"));
    try
      {
	bool success = false;
	set_fragment(do_why(search_leaves,
			    pkg,
			    false,
			    false,
			    success));
      }
    catch(...)
      {
	for(std::vector<pkg_matcher *>::const_iterator it
	      = search_leaves.begin(); it != search_leaves.end(); ++it)
	  delete *it;
      }

    for(std::vector<pkg_matcher *>::const_iterator it
	  = search_leaves.begin(); it != search_leaves.end(); ++it)
      delete *it;
  }
};
typedef ref_ptr<pkg_why_widget> pkg_why_widget_ref;

class pkg_description_widget:public vs_text_layout
{
protected:
  pkg_description_widget()
  {
  }
public:
  static ref_ptr<pkg_description_widget> create()
  {
    ref_ptr<pkg_description_widget> rval(new pkg_description_widget);
    rval->decref();
    return rval;
  }

  void set_package(const pkgCache::PkgIterator &pkg,
		   const pkgCache::VerIterator &ver)
  {
    // Check against pkg.end() to hack around #339533; if ver is a
    // default iterator, pkg.end() is true.
    wstring newdesc(pkg.end() ? L"" : get_long_description(ver, apt_package_records));

    fragment *frag=make_desc_fragment(newdesc);

#ifdef APT_HAS_HOMEPAGE
    fragment *homepage;
    if(!ver.end())
      {
	pkgRecords::Parser &rec=apt_package_records->Lookup(ver.FileList());
	string homepagestr = rec.Homepage();
	homepage = (homepagestr == ""
		    ? fragf("")
		    : dropbox(fragf("%B%s%b", _("Homepage: ")),
			      hardwrapbox(text_fragment(rec.Homepage()))));
      }
    else
      homepage = fragf("");
#else
    fragment *homepage = fragf("");
#endif

    fragment *tags=make_tags_fragment(pkg);
    if(tags != NULL)
      tags = fragf("%n%n%F", tags);
    else
      tags = fragf("");

    fragment *untrusted_frag;

    if(pkg.end() || ver.end())
      untrusted_frag=NULL;
    else
      untrusted_frag=make_untrusted_warning(ver);

    if(untrusted_frag == NULL)
      set_fragment(fragf("%F%F%F", frag, homepage, tags));
    else
      set_fragment(fragf("%F%n%F%F%F", untrusted_frag, frag,
			 homepage, tags));
  }
};

typedef ref_ptr<pkg_description_widget> pkg_description_widget_ref;

// In order to properly dispatch line-up/down events to the sub-widgets,
// we need a meta-widget that knows about them.
//
// Note: this is not the most efficient way of doing things, but
// (a) it would be very error-prone to reproduce the multiplex's
//     behavior for this special case, and
// (b) only one of these is created per package view, so the overhead
//     of a few redundant pointers is acceptable.  In my opinion.
//
// This is still rather gross, and a better way would be nice.
class info_area_multiplex:public vs_multiplex
{
  vs_hier_editor_ref editor;
  pkg_description_widget_ref description;
  vs_table_ref description_table;

  pkg_why_widget_ref why;
  vs_table_ref why_table;

  vs_text_layout_ref reasons;
  vs_table_ref reasons_table;

  pkgCache::PkgIterator lastPkg;
  pkgCache::VerIterator lastVer;
  wstring lastDesc;

  /** True if the package had breakage the last time we checked. */
  bool hadBreakage;
  /** If the view was autoswitched to breakage reasons, this is set
   *  to the widget we switched away from; otherwise, it is \b NULL.
   */
  vs_widget_ref autoswitch;

protected:
  info_area_multiplex(const vs_hier_editor_ref &_editor,
		      const pkg_description_widget_ref &_description,
		      const vs_table_ref &_description_table,
		      const pkg_why_widget_ref &_why,
		      const vs_table_ref &_why_table,
		      const vs_text_layout_ref &_reasons,
		      const vs_table_ref &_reasons_table)
    :vs_multiplex(false),
     editor(_editor),
     description(_description), description_table(_description_table),
     why(_why), why_table(_why_table),
     reasons(_reasons), reasons_table(_reasons_table),
     hadBreakage(false), autoswitch(NULL)
  {
    package_states_changed.connect(sigc::mem_fun(*this,
						 &info_area_multiplex::reset_package));
  }

public:
  static ref_ptr<info_area_multiplex>
  create(const vs_hier_editor_ref &editor,
	 const pkg_description_widget_ref &description,
	 const vs_table_ref &description_table,
	 const pkg_why_widget_ref &why,
	 const vs_table_ref &why_table,
	 const vs_text_layout_ref &reasons,
	 const vs_table_ref &reasons_table)
  {
    ref_ptr<info_area_multiplex>
      rval(new info_area_multiplex(editor, description, description_table,
				   why, why_table, reasons, reasons_table));
    rval->decref();
    return rval;
  }

  void line_up()
  {
    vs_widget_ref tmpref(this);

    vs_widget_ref w=visible_widget();

    if(w==description_table)
      description->line_up();
    else if(w == why_table)
      why->line_up();
    else if(w==reasons_table)
      reasons->line_up();
  }

  void line_down()
  {
    vs_widget_ref tmpref(this);

    vs_widget_ref w=visible_widget();

    if(w==description_table)
      description->line_down();
    else if(w == why_table)
      why->line_down();
    else if(w==reasons_table)
      reasons->line_down();
  }

  void set_package(const pkgCache::PkgIterator &pkg,
		   const pkgCache::VerIterator &ver)
  {
    vs_widget_ref tmpref(this);

    bool hasBreakage;

    description->set_package(pkg, ver);
    reasons->set_fragment(reason_fragment(pkg, hasBreakage));
    editor->set_package(pkg, ver);
    why->set_package(pkg, ver);

    // autoswitch if a package is newly broken, or if we have just
    // moved to a broken package.
    if(hasBreakage &&
       (!hadBreakage ||
	!(pkg==lastPkg && ver==lastVer)) &&
       aptcfg->FindB(PACKAGE "::UI::Auto-Show-Reasons", true))
      {
	// Don't clobber the autoswitch bread crumb if we were
	// autoswitched and are still autoswitched.
	if(!autoswitch.valid())
	  autoswitch=visible_widget();
	reasons_table->show();
      }

    // We always set the package anyway in case something changed,
    // but only scroll to the top in this case:
    if(pkg!=lastPkg || ver!=lastVer)
      {
	lastPkg=pkg;
	lastVer=ver;

	description->move_to_top();
	reasons->move_to_top();
      }
    // If we are autoswitched and unbroken, switch back.
    else if(!hasBreakage && autoswitch.valid())
      {
	autoswitch->show();
	autoswitch=NULL;
      }

    hadBreakage=hasBreakage;
  }

  /** Cycles the multiplex, taking autoswitch behavior into account. */
  void cycle()
  {
    vs_widget_ref tmpref(this);

    if(autoswitch.valid() && autoswitch!=visible_widget())
      {
	autoswitch->show();
	autoswitch=NULL;
      }
    else
      {
	autoswitch=NULL;
	cycle_forward();
      }
  }

  /** Re-updates the package views, given that the package state may
   *  have changed.
   */
  void reset_package()
  {
    set_package(lastPkg, lastVer);
  }

  /** Set the description directly, without reference to a package.
   *  Used when no package is selected.
   *
   *  \param s the new description
   */
  void set_description(const std::wstring &s)
  {
    vs_widget_ref tmpref(this);

    if(s!=lastDesc)
      {
	lastDesc=s;

	description->set_fragment(make_desc_fragment(s));
	reasons->set_fragment(sequence_fragment(make_desc_fragment(s),
						newline_fragment(),
						nopackage(),
						NULL));

	description->move_to_top();
	reasons->move_to_top();
      }
  }
};

typedef ref_ptr<info_area_multiplex> info_area_multiplex_ref;

vs_widget_ref make_package_view(list<package_view_item> &format,
				const vs_widget_ref &mainwidget,
				menu_redirect *menu_handler,
				pkg_signal *sig, desc_signal *desc_sig,
				bool show_reason_first)
{
  bool found_mainwidget=false;

  vs_table_ref rval=vs_table::create();

  eassert(mainwidget.valid());

  for(list<package_view_item>::iterator i=format.begin();
      i!=format.end();
      i++)
    {
      switch(i->type)
	{
	case PACKAGE_VIEW_MAINWIDGET:
	  if(found_mainwidget)
	    _error->Error(_("make_package_view: error in arguments -- two main widgets??"));
	  else
	    i->widget=mainwidget;
	  break;
	case PACKAGE_VIEW_STATIC:
	  if(!i->columns)
	    _error->Error(_("make_package_view: error in arguments -- bad column list for static item"));
	  else
	    {
	      pkg_handling_label_ref l=pkg_handling_label::create(i->columns);
	      i->widget=l;

	      if(sig)
		sig->connect(sigc::mem_fun(*l.unsafe_get_ref(), &pkg_handling_label::do_columnify));

	      if(!i->columns_cfg.empty())
		aptcfg->connect(i->columns_cfg,
				sigc::bind(sigc::ptr_fun(do_set_column_format),
					   i->columns_cfg,
					   i->columns_cfg_default,
					   l.weak_ref()));
	    }
	  break;
	case PACKAGE_VIEW_DESCRIPTION:
	  {
	    vs_hier_editor_ref e=vs_hier_editor::create();
	    pkg_description_widget_ref w=pkg_description_widget::create();
	    pkg_why_widget_ref why = pkg_why_widget::create();
	    vs_text_layout_ref l=vs_text_layout::create();

	    vs_table_ref wt=vs_table::create();
	    vs_table_ref lt=vs_table::create();
	    vs_table_ref why_table = vs_table::create();
	    info_area_multiplex_ref m=info_area_multiplex::create(e,
								  w, wt,
								  why, why_table,
								  l, lt);
	    vs_scrollbar_ref ws=vs_scrollbar::create(vs_scrollbar::VERTICAL);
	    vs_scrollbar_ref ls=vs_scrollbar::create(vs_scrollbar::VERTICAL);
	    vs_scrollbar_ref why_scrollbar = vs_scrollbar::create(vs_scrollbar::VERTICAL);

	    w->location_changed.connect(sigc::mem_fun(*ws.unsafe_get_ref(), &vs_scrollbar::set_slider));
	    l->location_changed.connect(sigc::mem_fun(*ls.unsafe_get_ref(), &vs_scrollbar::set_slider));
	    why->location_changed.connect(sigc::mem_fun(*why_scrollbar.unsafe_get_ref(), &vs_scrollbar::set_slider));

	    wt->add_widget_opts(w, 0, 0, 1, 1, vs_table::EXPAND | vs_table::FILL | vs_table::SHRINK, vs_table::EXPAND | vs_table::FILL | vs_table::SHRINK);
	    wt->add_widget_opts(ws, 0, 1, 1, 1, vs_table::ALIGN_RIGHT, vs_table::ALIGN_CENTER | vs_table::FILL);

	    lt->add_widget_opts(l, 0, 0, 1, 1, vs_table::EXPAND | vs_table::FILL | vs_table::SHRINK, vs_table::EXPAND | vs_table::FILL | vs_table::SHRINK);
	    lt->add_widget_opts(ls, 0, 1, 1, 1, vs_table::ALIGN_RIGHT, vs_table::EXPAND | vs_table::ALIGN_CENTER | vs_table::FILL);

	    why_table->add_widget_opts(why, 0, 0, 1, 1, vs_table::EXPAND | vs_table::FILL | vs_table::SHRINK, vs_table::EXPAND | vs_table::FILL | vs_table::SHRINK);
	    why_table->add_widget_opts(why_scrollbar, 0, 1, 1, 1, vs_table::ALIGN_RIGHT, vs_table::EXPAND | vs_table::ALIGN_CENTER | vs_table::FILL);

	    // HACK: speaks for itself
	    vs_tree_ref thetree=mainwidget.dyn_downcast<vs_tree>();

	    i->widget=m;

	    // Set up a null reason to start with.  (the package
	    // signal is only called if an actual package is
	    // highlighted)
	    l->set_fragment(nopackage());

	    if(sig)
	      sig->connect(sigc::mem_fun(*m.unsafe_get_ref(), &info_area_multiplex::set_package));

	    if(desc_sig)
	      desc_sig->connect(sigc::mem_fun(*m.unsafe_get_ref(), &info_area_multiplex::set_description));

	    mainwidget->connect_key("DescriptionDown", &global_bindings,
				    sigc::mem_fun(*m.unsafe_get_ref(),
						  &info_area_multiplex::line_down));
	    mainwidget->connect_key("DescriptionUp", &global_bindings,
				    sigc::mem_fun(*m.unsafe_get_ref(),
						  &info_area_multiplex::line_up));
	    mainwidget->connect_key("DescriptionCycle", &global_bindings,
				    sigc::mem_fun(*m.unsafe_get_ref(),
						  &info_area_multiplex::cycle));
	    mainwidget->connect_key("EditHier", &global_bindings,
				    sigc::mem_fun(*e.unsafe_get_ref(),
						  &vscreen_widget::show));
	    mainwidget->connect_key("EditHier", &global_bindings,
				    sigc::mem_fun(*m.unsafe_get_ref(),
						  &vscreen_widget::show));
	    mainwidget->connect_key("EditHier", &global_bindings,
				    sigc::bind(sigc::mem_fun(*rval.unsafe_get_ref(), &vs_table::focus_widget_bare),
					       m.weak_ref()));

	    e->hidden_sig.connect(sigc::bind(sigc::mem_fun(*rval.unsafe_get_ref(), &vs_table::focus_widget_bare),
					     mainwidget.weak_ref()));

	    if(thetree.valid())
	      e->commit_changes.connect(sigc::mem_fun(*thetree.unsafe_get_ref(), &vs_tree::line_down));

	    m->add_visible_widget(e, false);
	    m->add_visible_widget(wt, true);
	    m->add_visible_widget(lt, true);
	    m->add_visible_widget(why_table, true);

	    if(show_reason_first)
	      lt->show();
	    else
	      wt->show();

	    // FIXME: this is a grotesque hack.
	    if(mainwidget->get_visible())
	      rval->focus_widget(mainwidget);
	  }
	  break;
	default:
	  _error->Error(_("make_package_view: bad argument!"));
	  break;
	}

      if(i->widget.valid())
	{
	  // If we have a main widget or a description widget, the
	  // size of the widget in question will be ignored.
	  int xopts=i->xopts, yopts=i->yopts;

	  if(i->type == PACKAGE_VIEW_MAINWIDGET ||
	     i->type == PACKAGE_VIEW_DESCRIPTION)
	    {
	      xopts|=vs_table::IGNORE_SIZE_REQUEST;
	      yopts|=vs_table::IGNORE_SIZE_REQUEST;
	    }

	  rval->add_widget_opts(i->widget, i->row, i->col, i->h, i->w,
				xopts, yopts);

	  i->widget->set_bg_style(i->st);

	  if(i->popupdownkey.size()>0)
	    rval->connect_key(i->popupdownkey,
			      &global_bindings,
			      sigc::mem_fun(*i->widget.unsafe_get_ref(),
					    &vscreen_widget::toggle_visible));

	  if(i->visible)
	    i->widget->show();
	}
    }

  // Slow, but the list is (hopefully) << 10 elements or so.
  for(list<package_view_item>::iterator i=format.begin();
      i!=format.end();
      i++)
    if(i->popupdownlinked.size()>0)
      for(list<package_view_item>::iterator j=format.begin();
	  j!=format.end();
	  j++)
	{
	  if(!strcasecmp(j->name.c_str(), i->popupdownlinked.c_str()))
	    {
	      // Having to make two connections is annoying.
	      j->widget->shown_sig.connect(sigc::mem_fun(*i->widget.unsafe_get_ref(), &vscreen_widget::show));
	      j->widget->hidden_sig.connect(sigc::mem_fun(*i->widget.unsafe_get_ref(), &vscreen_widget::hide));
	      break;
	    }
	}

  if(!mainwidget.valid())
    _error->Error(_("make_package_view: no main widget found"));

  if(menu_handler)
    create_menu_bindings(menu_handler, rval);

  return rval;
}