]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/pkg/YQPkgSelDescriptionView.cc
don't create QObjects in ycp thread - make timeouts work
[duncan/yast2-qt4.git] / src / pkg / YQPkgSelDescriptionView.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgSelDescriptionView.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 <unistd.h>
23 #include <QRegExp>
24
25 #include "YQPkgSelDescriptionView.h"
26 #include "YQUI.h"
27 #include "YQi18n.h"
28 #include "utf8.h"
29
30 using std::list;
31 using std::string;
32
33
34 YQPkgSelDescriptionView::YQPkgSelDescriptionView( QWidget * parent )
35     : YQPkgDescriptionView( parent )
36 {
37 }
38
39
40 YQPkgSelDescriptionView::~YQPkgSelDescriptionView()
41 {
42     // NOP
43 }
44
45
46 void
47 YQPkgSelDescriptionView::showDetails( ZyppSel selectable )
48 {
49     _selectable = selectable;
50
51     if ( ! selectable )
52     {
53         clear();
54         return;
55     }
56
57     QString html_text = htmlHeading( selectable );
58
59     QString description = fromUTF8( selectable->theObj()->description() );
60
61     if ( ! description.contains( "<!-- DT:Rich -->" ) )
62         description = simpleHtmlParagraphs( description );
63
64     html_text += description;
65
66     setHtml( html_text );
67     //FIXME ensureVisible( 0, 0 );      // Otherwise hyperlinks will be centered
68 }
69
70
71
72 QString
73 YQPkgSelDescriptionView::htmlHeading( ZyppSel selectable )
74 {
75     ZyppObj             zyppObj         = selectable->theObj();
76     ZyppSelection       selection       = tryCastToZyppSelection( zyppObj );
77     ZyppPattern         pattern         = tryCastToZyppPattern  ( zyppObj );
78
79     if ( ! selection && ! pattern )
80         return YQPkgGenericDetailsView::htmlHeading( selectable );
81
82     QString summary = fromUTF8( zyppObj->summary() );
83     bool useBigFont = ( summary.length() <= 40 );
84
85     if ( summary.isEmpty() )                    // No summary?
86         summary = fromUTF8( zyppObj->name() );  // Use name instead (internal only normally)
87
88     QString icon = pattern ? pattern->icon().asString().c_str() : "";
89
90     if ( icon.isEmpty() )
91     {
92         icon = zyppObj->name().c_str();
93         icon.replace( ' ', '_' );
94     }
95
96     if ( ! icon.isEmpty() )
97     {
98         if ( icon.startsWith( "./" ) )
99             icon.replace( QRegExp( "^\\./" ), "" );
100             
101         if ( ! icon.endsWith( ".png", Qt::CaseInsensitive ) &&
102              ! icon.endsWith( ".jpg", Qt::CaseInsensitive )   )
103             icon += ".png";
104
105         QString origIconName = icon;
106
107         if ( ! icon.contains( "/" ) )           // no path at all
108         {
109             // Look in icon directories:
110             //
111             //   /usr/share/YaST2/theme/current/icons/32x32/apps/
112             //   /usr/share/YaST2/theme/current/icons/48x48/apps/
113
114             QString iconBaseName = icon;
115             icon = findIcon( QString( THEMEDIR ) + "/icons/32x32/apps/" + iconBaseName );
116
117             if ( icon.isEmpty() )
118                 icon = findIcon( QString( THEMEDIR ) + "/icons/48x48/apps/" + iconBaseName );
119         }
120         else if ( ! icon.startsWith( "/" ) )    // relative path
121         {
122             // Use path relative to theme directory:
123             //
124             //   /usr/share/YaST2/theme/current/ + icon
125
126             icon = findIcon( QString( THEMEDIR ) + "/" + icon );
127         }
128
129         if ( pattern && icon.isEmpty() )
130             y2warning( "No icon for pattern %s - icon name: %s",
131                        zyppObj->name().c_str(), qPrintable(origIconName) );
132     }
133
134
135     QString html = "<table width=100%";
136
137     if ( ! YQUI::ui()->usingVisionImpairedPalette() )
138         html += " bgcolor=#C8C8F8";     // or #E0E0F8 (very light blueish grey)
139
140     html += "><tr><td>"
141         + ( useBigFont ? QString( "<h2>" ) : QString( "<b>" ) )
142         + summary
143         + ( useBigFont ? QString( "</h2>" ) : QString( "</b>" ) )
144         + "</td></tr>"
145         + "</table>";
146
147     if ( ! icon.isEmpty() )
148     {
149         html = QString( "<table width=100%><tr>" )
150             + "<td><img src=\"" + icon + "\"></td>"
151             + "<td width=100%>" + html + "</td>"
152             + "</tr></table>";
153     }
154
155     return html;
156 }
157
158
159
160 QString
161 YQPkgSelDescriptionView::findIcon( const QString & icon ) const
162 {
163     if ( access( qPrintable(icon), R_OK ) == 0 )
164     {
165         y2debug( "Found icon %s", qPrintable(icon) );
166         return icon;
167     }
168     else
169     {
170         y2debug( "No icon %s", qPrintable(icon) );
171         return "";
172     }
173 }
174
175
176 #include "YQPkgSelDescriptionView.moc"