summaryrefslogtreecommitdiff
path: root/src/desc_render.cc
blob: c0bb4c5fb68c5e84bec72e9e72f067b051dd15cd (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
// desc_parse.cc
//
//  Copyright 2004-2008, 2010 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.
//
//  Parses a description into a cw::fragment.

#include "desc_render.h"

#include "aptitude.h"
#include "ui.h"

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

#include <cwidget/fragment.h>
#include <cwidget/generic/util/transcode.h>
#include <cwidget/config/colors.h>

using namespace std;

namespace cw = cwidget;

namespace aptitude
{
  namespace
  {
    cw::fragment *make_desc_fragment(const std::vector<description_element_ref> &elements,
				     int level)
    {
      std::vector<cw::fragment *> fragments;

      for(std::vector<description_element_ref>::const_iterator it = elements.begin();
	  it != elements.end(); ++it)
	{
	  const description_element_ref &elt(*it);

	  switch(elt->get_type())
	    {
	    case description_element::blank_line:
	      fragments.push_back(cw::newline_fragment());
	      break;
	    case description_element::paragraph:
	      fragments.push_back(wrapbox(cw::text_fragment(elt->get_string())));
	      break;
	    case description_element::literal:
	      fragments.push_back(cw::hardwrapbox(cw::text_fragment(elt->get_string())));
	      break;
	    case description_element::bullet_list:
	      {
		wstring bullet;
		bullet.push_back(L"*+-"[level%3]);

		cw::fragment *item_contents(make_desc_fragment(elt->get_elements(),
							       level + 1));

		fragments.push_back(cw::style_fragment(cw::text_fragment(bullet),
						       cw::get_style("Bullet")));
		fragments.push_back(cw::indentbox(1,
						  (level + 1) * 2,
						  item_contents));
	      }
	      break;
	    }
	}

      return cw::sequence_fragment(fragments);
    }
  }

  cw::fragment *make_desc_fragment(const std::vector<description_element_ref> &elements)
  {
    return make_desc_fragment(elements, 0);
  }
}

cw::fragment *make_desc_fragment(const wstring &desc)
{
  std::vector<aptitude::description_element_ref> elements;
  aptitude::parse_desc(desc, elements);

  return aptitude::make_desc_fragment(elements);
}


cw::fragment *make_tags_fragment(const pkgCache::PkgIterator &pkg)
{
  if(pkg.end())
    return NULL;

  using aptitude::apt::get_fullname;
  using aptitude::apt::get_tags;
  using aptitude::apt::tag;

  const set<tag> s(get_tags(pkg));

  vector<cw::fragment *> rval;
  if(s.empty() == false)
    {
      vector<cw::fragment *> tags;
      for(set<tag>::const_iterator i = s.begin(); i != s.end(); ++i)
	{
	  std::string name(get_fullname(*i));

	  tags.push_back(cw::text_fragment(name));
	}

      rval.push_back(dropbox(cw::fragf("%B%s: %b", _("Tags")),
                             wrapbox(cw::join_fragments(tags, L", "))));
    }

  typedef aptitudeDepCache::user_tag user_tag;
  const set<user_tag> &user_tags((*apt_cache_file)->get_ext_state(pkg).user_tags);
  if(!user_tags.empty())
    {
      vector<cw::fragment *> tags;
      for(set<user_tag>::const_iterator it = user_tags.begin();
	  it != user_tags.end(); ++it)
	{
	  tags.push_back(cw::text_fragment((*apt_cache_file)->deref_user_tag(*it)));
	}

      rval.push_back(dropbox(cw::fragf("%B%s: %b", _("User Tags")),
			     wrapbox(cw::join_fragments(tags, L", "))));
    }

  if(!rval.empty())
    return cw::join_fragments(rval, L"\n");
  else
    return NULL;
}