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