libyui-qt-pkg  2.47.5
YQPkgDescriptionView.cc
1 /**************************************************************************
2 Copyright (C) 2000 - 2010 Novell, Inc.
3 All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
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 along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 
19 **************************************************************************/
20 
21 
22 /*---------------------------------------------------------------------\
23 | |
24 | __ __ ____ _____ ____ |
25 | \ \ / /_ _/ ___|_ _|___ \ |
26 | \ V / _` \___ \ | | __) | |
27 | | | (_| |___) || | / __/ |
28 | |_|\__,_|____/ |_| |_____| |
29 | |
30 | core system |
31 | (C) SuSE GmbH |
32 \----------------------------------------------------------------------/
33 
34  File: YQPkgDescriptionView.cc
35 
36  Author: Stefan Hundhammer <sh@suse.de>
37 
38  Textdomain "qt-pkg"
39 
40 /-*/
41 
42 #define YUILogComponent "qt-pkg"
43 #include "YUILog.h"
44 #include <QRegExp>
45 #include <QFile>
46 #include <QFileInfo>
47 #include <QList>
48 #include <QSettings>
49 #include "zypp/VendorSupportOptions.h"
50 #include "YQPkgDescriptionView.h"
51 #include "YQPkgDescriptionDialog.h"
52 #include "YQi18n.h"
53 #include "utf8.h"
54 #include "YQUI.h"
55 #include <qbuffer.h>
56 
57 #define DESKTOP_TRANSLATIONS "desktop_translations"
58 #define DESKTOPFILEDIR "\\/share\\/applications\\/.*\\.desktop$" // RegExp
59 
60 
61 
62 using std::list;
63 using std::endl;
64 using std::string;
65 using namespace zypp;
66 
67 YQPkgDescriptionView::YQPkgDescriptionView( QWidget * parent, bool showSupportability )
68  : YQPkgGenericDetailsView( parent )
69  , _showSupportability ( showSupportability )
70 {
71  //FIXME setMimeSourceFactory( 0 );
72  initLang();
73 }
74 
75 
77 {
78  // NOP
79 }
80 
81 
82 void
83 YQPkgDescriptionView::showDetails( ZyppSel selectable )
84 {
85  _selectable = selectable;
86 
87  if ( ! selectable )
88  {
89  clear();
90  return;
91  }
92 
93  QString html_text = htmlStart();
94 
95  html_text += htmlHeading( selectable );
96 
97  QString description = fromUTF8( selectable->theObj()->description() );
98 
99  if ( ! description.contains( "<!-- DT:Rich -->" ) )
100  description = simpleHtmlParagraphs( description );
101 
102  html_text += ( "<p>" + description + "</p>");
103 
104  // if the object is a patch, show the problem references too
105  Patch::constPtr patch = asKind<Patch>(selectable->theObj());
106  if ( patch )
107  {
108  html_text += "<p>";
109  html_text += _("References:");
110  html_text += "</p>";
111  html_text += "<ul>";
112 
113  for ( Patch::ReferenceIterator rit = patch->referencesBegin();
114  rit != patch->referencesEnd();
115  ++rit )
116  {
117  html_text += QString( "<li>%1 (%2) : %3</li>" )
118  .arg( rit.id().c_str() )
119  .arg( rit.type().c_str() )
120  .arg( rit.title().c_str() );
121  }
122  html_text += "</ul>";
123  }
124 
125  // if it is a package, show the support information
126  Package::constPtr package = asKind<Package>(selectable->theObj());
127  if ( _showSupportability && package )
128  {
129  html_text += "<p>";
130  // Translators: %1 contains the support level like "Level 3", "unsupported" or "unknown"
131  html_text += _("Supportability: %1").arg( fromUTF8(asUserString(package->vendorSupport()).c_str() ));
132  html_text += "</p>";
133  }
134 
135  // show application names and icons from desktop files if available
136  ZyppPkg installed = tryCastToZyppPkg( selectable->installedObj() );
137  if ( installed )
138  {
139  // ma@: It might be worth passing Package::FileList directly
140  // instead of copying _all_ filenames into a list first.
141  // Package::FileList is a query, so it does not eat much memory.
142  zypp::Package::FileList f( installed->filelist() );
143  std::list<std::string> tmp( f.begin(), f.end() );
144  html_text += applicationIconList( tmp );
145  }
146 
147  html_text += htmlEnd();
148  setHtml( html_text );
149  //FIXME ensureVisible( 0, 0 ); // Otherwise hyperlinks will be centered
150 }
151 
152 
153 
155 {
156  bool foundAuthorsList = false;
157  QString html_text = "<p>";
158 
159  QStringList lines = text.trimmed().split( '\n', QString::KeepEmptyParts );
160  QStringList::const_iterator it = lines.begin();
161 
162  while ( it != lines.end() )
163  {
164  QString line = htmlEscape( *it ).trimmed();
165 
166  if ( line.startsWith("* ") || line.startsWith("- ") || line.startsWith("# ") )
167  {
168  line = "<li>" + line + "</li>";
169  }
170 
171  if ( line.startsWith( "Authors:" ) )
172  {
173  line = "<p><b>" + line + "</b><ul>";
174  foundAuthorsList = true;
175  }
176 
177  if ( foundAuthorsList )
178  {
179  if ( ! line.startsWith( "-----" ) && ! line.isEmpty() )
180  html_text += "<li>" + line + "</li>";
181  }
182  else
183  {
184  if ( line.isEmpty() )
185  html_text += "</p><p>";
186  else
187  html_text += " " + line;
188  }
189 
190 
191 
192 
193  ++it;
194  }
195 
196  if ( foundAuthorsList )
197  html_text += "</ul>";
198 
199  html_text += "</p>";
200 
201  return html_text;
202 }
203 
204 
205 void
207 {
208  if ( url.scheme() == "pkg" )
209  {
210  QString pkgName = url.authority();
211  yuiMilestone() << "Hyperlinking to package \"" << pkgName << "\"" << endl;
213  }
214  else
215  {
216  yuiError() << "Protocol not supported - can't follow hyperlink \""
217  << url.toString() << "\"" << endl;
218  }
219 }
220 
221 
222 void
224 {
225  showLink( url );
226 }
227 
228 
229 QString
230 YQPkgDescriptionView::applicationIconList( const list<string> & fileList ) const
231 {
232  QString html = "";
233  QMap<QString, QString> desktopEntries;
234 
235  QStringList desktopFiles = findDesktopFiles( fileList );
236 
237  if ( desktopFiles.size() == 0 )
238  return QString();
239 
240  // headline for a list of application icons that belong to a selected package
241 
242  for ( int i = 0; i < desktopFiles.size(); ++i )
243  {
244  desktopEntries = readDesktopFile( desktopFiles[i] );
245 
246  QIcon icon = YQUI::ui()->loadIcon( desktopEntries["Icon"].toStdString() );
247 
248  if ( ! icon.isNull() )
249  {
250  QPixmap pixmap = icon.pixmap(32);
251  QByteArray byteArray;
252  QBuffer buffer(&byteArray);
253  pixmap.save(&buffer, "PNG");
254  html += "<tr><td valign='middle' align='center'>";
255  html += QString("<td><img src=\"data:image/png;base64,") + byteArray.toBase64() + QString( "\">" );
256  html += "</td><td valign='middle' align='left'>";
257  html += "<b>" + desktopEntries["Name"] + "</b>";
258  html += "</td></tr>";
259  }
260  }
261 
262  if ( ! html.isEmpty() )
263  {
264  html = _("This package contains: ")
265  + "<table border='0'>"
266  + html
267  + "</table>";
268  }
269 
270  return "<p>" + html + "</p>";
271 }
272 
273 
274 QMap<QString, QString>
275 YQPkgDescriptionView::readDesktopFile( const QString & fileName ) const
276 {
277  QMap<QString, QString> desktopEntries;
278  QString name, genericName;
279 
280  QSettings file( fileName, QSettings::IniFormat );
281  file.setIniCodec( "UTF-8");
282  file.beginGroup( "Desktop Entry" );
283  desktopEntries["Icon"] = file.value( "Icon" ).toString();
284  desktopEntries["Exec"] = file.value( "Exec" ).toString();
285 
286  // translate Name
287  name = file.value( QString( "Name[%1]" ).arg( langWithCountry ) ).toString();
288 
289  if ( name.isEmpty() )
290  name= file.value( QString( "Name[%1]" ).arg( lang ) ).toString() ;
291 
292  if ( name.isEmpty() )
293  {
294  QFileInfo fileInfo (fileName);
295  QString msgid = QString( "Name(%1)" ).arg( fileInfo.fileName() );
296  msgid += ": ";
297  msgid += file.value( QString( "Name" )).toString();
298  name = QString::fromUtf8( dgettext( DESKTOP_TRANSLATIONS, msgid.toLatin1() ) );
299 
300  if ( name == msgid )
301  name = "";
302  }
303  if ( name.isEmpty() )
304  name= file.value( QString( "Name" ) ).toString() ;
305  desktopEntries["Name"] = name;
306 
307  file.endGroup();
308 
309  return desktopEntries;
310 }
311 
312 
313 QStringList
314 YQPkgDescriptionView::findDesktopFiles( const list<string> & fileList ) const
315 {
316  QStringList desktopFiles;
317 
318  for ( list<string>::const_iterator it = fileList.begin();
319  it != fileList.end(); ++it )
320  {
321  QString line = fromUTF8( *it );
322 
323  if ( line.contains( QRegExp( DESKTOPFILEDIR ) ) )
324  desktopFiles << line;
325  }
326 
327  return desktopFiles;
328 }
329 
330 
332 {
333  const char *lang_cstr = getenv( "LANG" );
334 
335  if ( lang_cstr )
336  {
337  langWithCountry = lang_cstr;
338  langWithCountry.replace( QRegExp( "[@\\.].*$" ), "" ); // remove .utf8 / @euro etc.
339 
340  lang = langWithCountry;
341  lang.replace( QRegExp( "_.*$" ), "" ); // remove _DE etc.
342  }
343 }
344 
345 
QMap< QString, QString > readDesktopFile(const QString &fileName) const
Extract name, icon and exec attributes from a desktop file.
QString simpleHtmlParagraphs(QString text)
Format a multi-line text into paragraphs.
YQPkgDescriptionView(QWidget *parent, bool showSupportability=true)
Constructor.
static QString htmlHeading(ZyppSel selectable, bool showVersion=false)
Returns a uniform heading in HTML format for the specified selectable: name and summary or name...
virtual void setSource(const QUrl &name)
Get the document pointed to by a hyperlink.
QString applicationIconList(const list< string > &fileList) const
Return html text that contains a list of application icons.
QStringList findDesktopFiles(const list< string > &fileList) const
Search for all desktop files in a file list.
Abstract base class for details views.
void initLang()
Initialize the language code (lang).
static QString htmlEscape(const QString &plainText)
Escapes characters special to HTML in a ( plain text ) string, such as: &#39;<&#39; -> &#39;<&#39; &#39;>&#39; -> &#39;>&#39; &#39;&&#39; -> ...
static void showDescriptionDialog(const QString &pkgName)
Static convenience method: Post a description dialog for pkg &#39;pkgName&#39;.
void showLink(const QUrl &url)
Show information for a hyperlinked object, e.g., a "pkg:somepkg" link to another package.
virtual ~YQPkgDescriptionView()
Destructor.
static QString htmlStart()
starts the html tag and set the style
virtual void showDetails(ZyppSel selectable)
Show details for the specified package: In this case the package description.