]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/pkg/YQPkgDescriptionView.cc
6b537ffe84f64c7838f0bc1b9ecee082315eb400
[duncan/yast2-qt4.git] / src / pkg / YQPkgDescriptionView.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgDescriptionView.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19 #define y2log_component "qt-pkg"
20 #include <ycp/y2log.h>
21
22 #include <qregexp.h>
23 #include "YQPkgDescriptionView.h"
24 #include "YQPkgDescriptionDialog.h"
25 #include "YQi18n.h"
26 #include "utf8.h"
27
28 using std::list;
29 using std::string;
30
31
32 YQPkgDescriptionView::YQPkgDescriptionView( QWidget * parent )
33     : YQPkgGenericDetailsView( parent )
34 {
35     setMimeSourceFactory( 0 );
36 }
37
38
39 YQPkgDescriptionView::~YQPkgDescriptionView()
40 {
41     // NOP
42 }
43
44
45 void
46 YQPkgDescriptionView::showDetails( ZyppSel selectable )
47 {
48     _selectable = selectable;
49
50     if ( ! selectable )
51     {
52         clear();
53         return;
54     }
55
56     QString html_text = htmlHeading( selectable );
57
58     QString description = fromUTF8( selectable->theObj()->description() );
59
60     if ( ! description.contains( "<!-- DT:Rich -->" ) )
61         description = simpleHtmlParagraphs( description );
62
63     html_text += description;
64
65
66     setTextFormat( Qt::RichText );
67     setText( html_text );
68     ensureVisible( 0, 0 );      // Otherwise hyperlinks will be centered
69 }
70
71
72
73 QString YQPkgDescriptionView::simpleHtmlParagraphs( QString text )
74 {
75     bool foundAuthorsList = false;
76     QString html_text = "<p>";
77
78     QStringList lines = QStringList::split( '\n', text.stripWhiteSpace(),
79                                             true ); // allowEmptyEntries
80     QValueList<QString>::const_iterator it = lines.begin();
81
82     while ( it != lines.end() )
83     {
84         QString line = htmlEscape( *it ).stripWhiteSpace();
85
86         if ( line.startsWith( "Authors:" ) )
87         {
88             line = "<p><b>" + line + "</b><ul>";
89             foundAuthorsList = true;
90         }
91
92         if ( foundAuthorsList )
93         {
94             if ( ! line.startsWith( "-----" ) && ! line.isEmpty() )
95                 html_text += "<li>" + line + "</li>";
96         }
97         else
98         {
99             if ( line.isEmpty() )
100                 html_text += "</p><p>";
101             else
102                 html_text += " " + line;
103         }
104
105         ++it;
106     }
107
108     if ( foundAuthorsList )
109         html_text += "</ul>";
110     
111     html_text += "</p>";
112
113     return html_text;
114 }
115
116
117 void
118 YQPkgDescriptionView::showLink( const QString & url )
119 {
120     if ( url.startsWith( "pkg:" ) )
121     {
122         QString pkgName = url;
123         pkgName.remove( QRegExp( "^pkg:/*" ) ); // Remove leading protocol and slashes
124         pkgName.remove( QRegExp( "/*$" ) );     // Remove trailing slashes
125         y2milestone( "Hyperlinking to package '%s'", (const char *) pkgName );
126         YQPkgDescriptionDialog::showDescriptionDialog( pkgName );
127     }
128     else
129     {
130         y2error( "Protocol not supported - can't follow hyperlink '%s'",
131                  (const char *) url );
132     }
133 }
134
135
136 void
137 YQPkgDescriptionView::setSource( const QString & url )
138 {
139     showLink( url );
140 }
141
142
143 #include "YQPkgDescriptionView.moc"