libyui-ncurses-pkg  2.48.9
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  // the rpm group
211  text += NCPkgStrings::RpmGroup();
212  text += package->group ();
213  text += "<br>";
214 
215  // name of the source package
216  text += "<b>" + _( "Source Package: " ) + "</b>";
217  text += package->sourcePkgName();
218  text += "-";
219  text += package->sourcePkgEdition().asString();
220  text += "<br>";
221 
222  std::list<std::string> authors = package->authors(); // zypp::Package
223  if ( !authors.empty() )
224  {
225  std::string author_text;
226  text += NCPkgStrings::Authors();
227  //authors, in one line
228  author_text = createText( authors, true );
229  // escape html
230  boost::replace_all( author_text, "<", "&lt;" );
231  boost::replace_all( author_text, ">", "&gt;" );
232  text += author_text;
233  }
234  }
235 
236  setValue (text);
237 
238 }
239 
240 void NCPkgPackageDetails::fileList( ZyppSel slbPtr )
241 {
242  std::string text = "";
243  // the file list is available only for installed packages
244  ZyppPkg package = tryCastToZyppPkg (slbPtr->installedObj());
245 
246  if ( package )
247  {
248  text += commonHeader( slbPtr->theObj() );
249  text += NCPkgStrings::ListOfFiles();
250  // get the file list from the package manager/show the list
251  zypp::Package::FileList pkgfilelist( package->filelist() );
252  std::list<std::string> fileList( pkgfilelist.begin(), pkgfilelist.end() );
253  text += createText( fileList, false ) ;
254  }
255 
256  else
257  text = _( "<i>This information is available for installed packages only.</i>" );
258 
259  setValue(text);
260 }
261 
262 void NCPkgPackageDetails::dependencyList( ZyppObj pkgPtr, ZyppSel slbPtr )
263 {
264  std::string text = commonHeader( pkgPtr );
265  // show the relations, all of them except provides which is above
266  zypp::Dep deptypes[] = {
267  zypp::Dep::PROVIDES,
268  zypp::Dep::PREREQUIRES,
269  zypp::Dep::REQUIRES,
270  zypp::Dep::CONFLICTS,
271  zypp::Dep::OBSOLETES,
272  zypp::Dep::RECOMMENDS,
273  zypp::Dep::SUGGESTS,
274  zypp::Dep::ENHANCES,
275  zypp::Dep::SUPPLEMENTS,
276  };
277  for (size_t i = 0; i < sizeof (deptypes)/sizeof(deptypes[0]); ++i)
278  {
279  zypp::Dep deptype = deptypes[i];
280  zypp::Capabilities relations = pkgPtr->dep (deptype);
281  std::string relline = createRelLine (relations);
282  if (!relline.empty ())
283  {
284  // FIXME: translate
285  text += "<b>" + deptype.asString () + ": </b>"
286  + relline + "<br>";
287  }
288  }
289 
290  setValue (text);
291 
292 }
293 
294 std::string NCPkgPackageDetails::createHtmlText( std::string value )
295 {
296  yuiDebug() << "Description: " << value << endl;
297 
298  // check RichText tag
299  if ( value.find( std::string(DOCTYPETAG) ) != std::string::npos )
300  {
301  return value; // input is rich text
302  }
303  // escape html
304  boost::replace_all( value, "&", "&amp;" );
305  boost::replace_all( value, "<", "&lt;" );
306  boost::replace_all( value, ">", "&gt;" );
307 
308  NCstring input( value );
309  NCtext descr( input );
310  NCtext html_descr( NCstring("<p>") );
311  std::string description = "";
312  bool ul_begin = false;
313  bool ul_found = false;
314  std::list<NCstring>::const_iterator line;
315 
316  for ( line = descr.Text().begin(); line != descr.Text().end(); ++line )
317  {
318  NCstring curr_line( *line );
319 
320  if ( curr_line.Str().empty() )
321  {
322  if ( ul_found ) // empty line after list
323  {
324  html_descr.append( NCstring("</li></ul><p>") );
325  ul_found = false;
326  ul_begin = false;
327  }
328  else
329  {
330  html_descr.append( NCstring("</p><p>") );
331  }
332  }
333  else if ( curr_line.Str().substr(0,2) == "- "
334  || curr_line.Str().substr(0,2) == "* ") // list item found
335  {
336  ul_found = true;
337  if ( !ul_begin )
338  {
339  html_descr.append( NCstring("</p><ul><li>") );
340  ul_begin = true;
341  }
342  else
343  {
344  html_descr.append( NCstring("</li><li>") );
345  }
346  html_descr.append( NCstring(curr_line.Str().substr(2)) );
347  }
348  else if ( curr_line.Str().substr(0,2) == " " ) // white spaces at begin
349  {
350  // just append the line (is added to list item or to paragraph)
351  html_descr.append( NCstring( curr_line.Str() ) );
352  }
353  else
354  {
355  if ( ul_found ) // first line after list
356  {
357  html_descr.append( NCstring("</li></ul><p>") );
358  ul_found = false;
359  ul_begin = false;
360  }
361  html_descr.append( NCstring(" " + curr_line.Str()) );
362  }
363  }
364 
365  if ( ul_found )
366  html_descr.append( NCstring("</li></ul>") );
367  else
368  html_descr.append( NCstring("</p>") );
369 
370  // create description
371  for ( line = html_descr.Text().begin(); line != html_descr.Text().end(); ++line )
372  {
373  NCstring curr_line( *line );
374  description += curr_line.Str();
375  }
376  // reduce number of empty lines
377  boost::replace_all( description, "</p><p></p>", "</p>" );
378 
379  return description;
380 }
381 
382 bool NCPkgPackageDetails::patchDescription( ZyppObj objPtr, ZyppSel selectable )
383 {
384  ZyppPatch patchPtr = tryCastToZyppPatch( objPtr );
385 
386  if ( !patchPtr || !selectable )
387  {
388  yuiError() << "Patch not valid" << endl;
389  return false;
390  }
391 
392  std::string descr;
393  descr += "<p>";
394  descr += NCPkgStrings::Patch();
395  descr += selectable->name();
396  descr += "&nbsp;";
397  // the patch size is not available
398  // descr += NCPkgStrings::Size();
399  // descr += patchPtr->size().asString( 8 );
400  descr += "<b>";
401  descr += NCPkgStrings::PatchKind();
402  descr += ": </b>";
403  descr += patchPtr->category();
404  descr += "&nbsp;";
405  descr += NCPkgStrings::Version();
406  descr += patchPtr->edition().asString();
407  descr += "</p>";
408 
409  // get and format the patch description
410  std::string value = patchPtr->description();
411  std::string html_text = "";
412  const std::string htmlIdent(DOCTYPETAG);
413 
414  if ( value.find( htmlIdent ) != std::string::npos )
415  {
416  html_text = value; // HTML text
417  }
418  else
419  {
420  // escape html
421  boost::replace_all( value, "&", "&amp;" );
422  boost::replace_all( value, "<", "&lt;" );
423  boost::replace_all( value, ">", "&gt;" );
424  html_text = "<pre>" + value + "</pre>"; // add <pre> to preserve newlines and spaces
425  }
426 
427  descr += html_text;
428 
429  descr += _( "References:<br>" );
430  for ( zypp::Patch::ReferenceIterator rit = patchPtr->referencesBegin();
431  rit != patchPtr->referencesEnd();
432  ++rit )
433  {
434  descr += rit.id().c_str();
435  descr += "&nbsp;";
436  descr += "(";
437  descr += rit.type().c_str();
438  descr += "):";
439  descr += "&nbsp;";
440  descr += rit.title().c_str();
441  descr += "<br>";
442  }
443  // show the description
444  setValue( descr );
445 
446  return true;
447 }
static const std::string MediaNo()
bold text Media No.
static const std::string Authors()
bold text Authors: (richtext)
static const std::string RpmGroup()
bold text Package Group: (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)