libyui-ncurses-pkg  2.50.7
NCPkgPackageDetails.cc
1 /****************************************************************************
2 |
3 | Copyright (c) [2002-2011] Novell, Inc.
4 | All Rights Reserved.
5 |
6 | This program is free software; you can redistribute it and/or
7 | modify it under the terms of version 2 of the GNU General Public License as
8 | published by the Free Software Foundation.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program; if not, contact Novell, Inc.
17 |
18 | To contact Novell about this file by physical or electronic mail,
19 | you may find current contact information at www.novell.com
20 |
21 |***************************************************************************/
22 
23 
24 /*---------------------------------------------------------------------\
25 | |
26 | __ __ ____ _____ ____ |
27 | \ \ / /_ _/ ___|_ _|___ \ |
28 | \ V / _` \___ \ | | __) | |
29 | | | (_| |___) || | / __/ |
30 | |_|\__,_|____/ |_| |_____| |
31 | |
32 | core system |
33 | (C) SuSE GmbH |
34 \----------------------------------------------------------------------/
35 
36  File: NCPkgPackageDetails.cc
37 
38 /-*/
39 #define YUILogComponent "ncurses-pkg"
40 #include <YUILog.h>
41 
42 #include "NCPkgTable.h"
43 #include "NCPkgPackageDetails.h"
44 #include "NCPackageSelector.h"
45 
46 #include <boost/algorithm/string.hpp>
47 
48 using std::endl;
49 
50 /*
51  Textdomain "ncurses-pkg"
52 */
53 
54 NCPkgPackageDetails::NCPkgPackageDetails ( YWidget *parent, std::string initial_text, NCPackageSelector *pkger)
55  : NCRichText (parent, initial_text)
56  , pkg (pkger)
57 {
58 
59 }
60 
61 std::string NCPkgPackageDetails::createRelLine( const zypp::Capabilities & info )
62 {
63  std::string text = "";
64  zypp::Capabilities::const_iterator
65  b = info.begin(),
66  e = info.end(),
67  it;
68  unsigned int i, n = info.size();
69 
70  for ( it = b, i = 0; it != e; ++it, ++i )
71  {
72  text = text + (*it).asString();
73  if ( i < n - 1 )
74  {
75  text = text + ", ";
76  }
77  }
78 
79  return text;
80 }
81 
82 std::string NCPkgPackageDetails::createText( std::list<std::string> info, bool oneline )
83 {
84  std::list<std::string>::iterator it;
85  std::string text = "";
86  unsigned int i;
87 
88  for ( i = 0, it = info.begin(); it != info.end() && i < 1000; ++it, i++ )
89  {
90  text += (*it);
91  if ( i < info.size()-1 )
92  {
93  if ( oneline && i < 999 )
94  {
95  if ( boost::ends_with( text, ":" ) )
96  text += " ";
97  else
98  text += ", ";
99  }
100  else
101  {
102  text += "<br>";
103  }
104  }
105  if ( i == 999 )
106  {
107  text += "...";
108  }
109  }
110 
111  return text;
112 }
113 
114 std::string NCPkgPackageDetails::commonHeader( ZyppObj pkgPtr )
115 {
116  std::string text = "";
117 
118  if ( !pkgPtr )
119  return text;
120 
121  text += "<h3>" + pkgPtr->name() + " - ";
122  text += pkgPtr->summary() + "</h3>";
123 
124  return text;
125 }
126 
127 void NCPkgPackageDetails::longDescription ( ZyppObj pkgPtr )
128 {
129  std::string text = "";
130 
131  if ( !pkgPtr )
132  return;
133 
134  // text += commonHeader( pkgPtr );
135  text += pkgPtr->description();
136 
137  // show the description
138  setValue( createHtmlText(text) );
139 }
140 
141 void NCPkgPackageDetails::technicalData( ZyppObj pkgPtr, ZyppSel slbPtr )
142 {
143  std::string instVersion = "";
144  std::string version = "";
145  std::string text = "";
146 
147  if ( !pkgPtr || !slbPtr )
148  return;
149 
150  text += commonHeader( pkgPtr );
151 
152  if ( slbPtr->hasBothObjects() )
153  {
154  ZyppObj io = slbPtr->installedObj();
155  instVersion = io->edition().version();
156  instVersion += "-";
157  instVersion += io->edition().release();
158  ZyppObj co = slbPtr->candidateObj();
159  version = co->edition().version();
160  version += "-";
161  version += co->edition().release();
162  }
163  else
164  {
165  version = pkgPtr->edition().version();
166  version += "-";
167  version += pkgPtr->edition().release();
168  }
169 
170  text += NCPkgStrings::Version();
171  text += version;
172  if ( instVersion != "" )
173  {
174  text += " ";
175  text += NCPkgStrings::InstVersion();
176  text += instVersion;
177  }
178  text += "<br>";
179 
180  // show the size
181  text += NCPkgStrings::Size();
182  text += pkgPtr->installSize().asString();
183  text += " ";
184 
185  ZyppPkg package = tryCastToZyppPkg( pkgPtr );
186  ZyppPkg candidate = tryCastToZyppPkg( slbPtr->candidateObj() );
187  ZyppPkg installed = tryCastToZyppPkg( slbPtr->installedObj() );
188 
189  if ( installed )
190  package = installed;
191  else if ( candidate )
192  package = candidate;
193 
194  if ( package )
195  {
196  // add the media nr
197  text += NCPkgStrings::MediaNo();
198  char num[5];
199  int medianr = package->mediaNr();
200  sprintf( num, "%d", medianr );
201  text += num;
202  text += "<br>";
203 
204  // the license
205  text += NCPkgStrings::License();
206  text += package->license();
207  text += " ";
208  text += "<br>";
209 
210  // name of the source package
211  text += "<b>" + _( "Source Package: " ) + "</b>";
212  text += package->sourcePkgName();
213  text += "-";
214  text += package->sourcePkgEdition().asString();
215  text += "<br>";
216 
217  std::list<std::string> authors = package->authors(); // zypp::Package
218  if ( !authors.empty() )
219  {
220  std::string author_text;
221  text += NCPkgStrings::Authors();
222  // authors, in one line
223  author_text = createText( authors, true );
224  // escape html
225  boost::replace_all( author_text, "<", "&lt;" );
226  boost::replace_all( author_text, ">", "&gt;" );
227  text += author_text;
228  }
229  }
230 
231  setValue (text);
232 
233 }
234 
235 void NCPkgPackageDetails::fileList( ZyppSel slbPtr )
236 {
237  std::string text = "";
238  // the file list is available only for installed packages
239  ZyppPkg package = tryCastToZyppPkg (slbPtr->installedObj());
240 
241  if ( package )
242  {
243  text += commonHeader( slbPtr->theObj() );
244  text += NCPkgStrings::ListOfFiles();
245  // get the file list from the package manager/show the list
246  zypp::Package::FileList pkgfilelist( package->filelist() );
247  std::list<std::string> fileList( pkgfilelist.begin(), pkgfilelist.end() );
248  text += createText( fileList, false );
249  }
250 
251  else
252  text = _( "<i>This information is available for installed packages only.</i>" );
253 
254  setValue(text);
255 }
256 
257 void NCPkgPackageDetails::dependencyList( ZyppObj pkgPtr, ZyppSel slbPtr )
258 {
259  std::string text = commonHeader( pkgPtr );
260  // show the relations, all of them except provides which is above
261  zypp::Dep deptypes[] = {
262  zypp::Dep::PROVIDES,
263  zypp::Dep::PREREQUIRES,
264  zypp::Dep::REQUIRES,
265  zypp::Dep::CONFLICTS,
266  zypp::Dep::OBSOLETES,
267  zypp::Dep::RECOMMENDS,
268  zypp::Dep::SUGGESTS,
269  zypp::Dep::ENHANCES,
270  zypp::Dep::SUPPLEMENTS,
271  };
272  for (size_t i = 0; i < sizeof (deptypes)/sizeof(deptypes[0]); ++i)
273  {
274  zypp::Dep deptype = deptypes[i];
275  zypp::Capabilities relations = pkgPtr->dep (deptype);
276  std::string relline = createRelLine (relations);
277  if (!relline.empty())
278  {
279  // FIXME: translate
280  text += "<b>" + deptype.asString() + ": </b>"
281  + relline + "<br>";
282  }
283  }
284 
285  setValue (text);
286 
287 }
288 
289 std::string NCPkgPackageDetails::createHtmlText( std::string value )
290 {
291  yuiDebug() << "Description: " << value << endl;
292 
293  // check RichText tag
294  if ( value.find( std::string(DOCTYPETAG) ) != std::string::npos )
295  {
296  return value; // input is rich text
297  }
298  // escape html
299  boost::replace_all( value, "&", "&amp;" );
300  boost::replace_all( value, "<", "&lt;" );
301  boost::replace_all( value, ">", "&gt;" );
302 
303  NCstring input( value );
304  NCtext descr( input );
305  NCtext html_descr( NCstring("<p>") );
306  std::string description = "";
307  bool ul_begin = false;
308  bool ul_found = false;
309  std::list<NCstring>::const_iterator line;
310 
311  for ( line = descr.Text().begin(); line != descr.Text().end(); ++line )
312  {
313  NCstring curr_line( *line );
314 
315  if ( curr_line.Str().empty() )
316  {
317  if ( ul_found ) // empty line after list
318  {
319  html_descr.append( NCstring("</li></ul><p>") );
320  ul_found = false;
321  ul_begin = false;
322  }
323  else
324  {
325  html_descr.append( NCstring("</p><p>") );
326  }
327  }
328  else if ( curr_line.Str().substr(0, 2) == "- "
329  || curr_line.Str().substr(0, 2) == "* ") // list item found
330  {
331  ul_found = true;
332  if ( !ul_begin )
333  {
334  html_descr.append( NCstring("</p><ul><li>") );
335  ul_begin = true;
336  }
337  else
338  {
339  html_descr.append( NCstring("</li><li>") );
340  }
341  html_descr.append( NCstring(curr_line.Str().substr(2)) );
342  }
343  else if ( curr_line.Str().substr(0, 2) == " " ) // white spaces at begin
344  {
345  // just append the line (is added to list item or to paragraph)
346  html_descr.append( NCstring( curr_line.Str() ) );
347  }
348  else
349  {
350  if ( ul_found ) // first line after list
351  {
352  html_descr.append( NCstring("</li></ul><p>") );
353  ul_found = false;
354  ul_begin = false;
355  }
356  html_descr.append( NCstring(" " + curr_line.Str()) );
357  }
358  }
359 
360  if ( ul_found )
361  html_descr.append( NCstring("</li></ul>") );
362  else
363  html_descr.append( NCstring("</p>") );
364 
365  // create description
366  for ( line = html_descr.Text().begin(); line != html_descr.Text().end(); ++line )
367  {
368  NCstring curr_line( *line );
369  description += curr_line.Str();
370  }
371  // reduce number of empty lines
372  boost::replace_all( description, "</p><p></p>", "</p>" );
373 
374  return description;
375 }
376 
377 bool NCPkgPackageDetails::patchDescription( ZyppObj objPtr, ZyppSel selectable )
378 {
379  ZyppPatch patchPtr = tryCastToZyppPatch( objPtr );
380 
381  if ( !patchPtr || !selectable )
382  {
383  yuiError() << "Patch not valid" << endl;
384  return false;
385  }
386 
387  std::string descr;
388  descr += "<p>";
389  descr += NCPkgStrings::Patch();
390  descr += selectable->name();
391  descr += "&nbsp;";
392  // the patch size is not available
393  // descr += NCPkgStrings::Size();
394  // descr += patchPtr->size().asString( 8 );
395  descr += "<b>";
396  descr += NCPkgStrings::PatchKind();
397  descr += ": </b>";
398  descr += patchPtr->category();
399  descr += "&nbsp;";
400  descr += NCPkgStrings::Version();
401  descr += patchPtr->edition().asString();
402  descr += "</p>";
403 
404  // get and format the patch description
405  std::string value = patchPtr->description();
406  std::string html_text = "";
407  const std::string htmlIdent(DOCTYPETAG);
408 
409  if ( value.find( htmlIdent ) != std::string::npos )
410  {
411  html_text = value; // HTML text
412  }
413  else
414  {
415  // escape html
416  boost::replace_all( value, "&", "&amp;" );
417  boost::replace_all( value, "<", "&lt;" );
418  boost::replace_all( value, ">", "&gt;" );
419  html_text = "<pre>" + value + "</pre>"; // add <pre> to preserve newlines and spaces
420  }
421 
422  descr += html_text;
423 
424  descr += _( "References:<br>" );
425  for ( zypp::Patch::ReferenceIterator rit = patchPtr->referencesBegin();
426  rit != patchPtr->referencesEnd();
427  ++rit )
428  {
429  descr += rit.id().c_str();
430  descr += "&nbsp;";
431  descr += "(";
432  descr += rit.type().c_str();
433  descr += "):";
434  descr += "&nbsp;";
435  descr += rit.title().c_str();
436  descr += "<br>";
437  }
438  // show the description
439  setValue( descr );
440 
441  return true;
442 }
static const std::string MediaNo()
bold text Media No.
static const std::string Authors()
bold text Authors: (richtext)
static const std::string Patch()
bold text Patch: (richtext)
static const std::string InstVersion()
bold text Installed version: (richtext)
static const std::string License()
bold text License: (richtext)
static const std::string Size()
bold text Size: (richtext)
static const std::string Version()
bold text Version: (richtext)