]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/pkg/YQPkgVersionsView.cc
- Don't crash on warnings (where is that NDEBUG defined?)
[duncan/yast2-qt4.git] / src / pkg / YQPkgVersionsView.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgVersionsView.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17   Textdomain "packages-qt"
18
19 /-*/
20
21 #define y2log_component "qt-pkg"
22
23 #include <YQZypp.h>
24 #include <zypp/Repository.h>
25 #include <ycp/y2log.h>
26 #include <QTabWidget>
27 #include <QRegExp>
28 #include <QHeaderView>
29
30 #include "YQPkgVersionsView.h"
31 #include "YQPkgRepoList.h"
32 #include "YQIconPool.h"
33 #include "YQi18n.h"
34 #include "utf8.h"
35
36
37 YQPkgVersionsView::YQPkgVersionsView( QWidget * parent, bool userCanSwitch )
38     : QY2ListView( parent )
39 {
40     _selectable         = 0;
41     _parentTab          = dynamic_cast<QTabWidget *> (parent);
42     _userCanSwitch      = userCanSwitch;
43
44     _versionCol = -42;
45     _archCol    = -42;
46     _productCol = -42;
47     _urlCol     = -42;
48     _repoCol    = -42;
49     _statusCol  = -42;
50     _nameCol    = -42;
51     _summaryCol = -42;
52
53     QStringList headers;
54     int numCol = 0;
55     headers << _( "Version"     );      _versionCol     = numCol++;
56     headers << _( "Arch."       );      _archCol        = numCol++;
57     headers << _( "Product"     );      _productCol     = numCol++;
58     headers << _( "Repository"  );      _repoCol        = numCol++;
59     headers << _( "URL"         );      _urlCol         = numCol++;
60
61     setHeaderLabels(headers);
62     _statusCol  = _productCol;
63
64     _nameCol    = _versionCol;
65     _summaryCol = _repoCol;
66
67     setSortByInsertionSequence( true );
68
69     // saveColumnWidths();      // Minimize column widths
70     // header()->hide();
71
72
73     if ( _parentTab )
74     {
75         connect( parent, SIGNAL( currentChanged(QWidget *) ),
76                  this,   SLOT  ( reload        (QWidget *) ) );
77     }
78
79     connect( this,      SIGNAL( currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem * ) ),
80              this,      SLOT  ( checkForChangedCandidate() ) );
81 }
82
83
84 YQPkgVersionsView::~YQPkgVersionsView()
85 {
86     // NOP
87 }
88
89
90 void
91 YQPkgVersionsView::reload( QWidget * newCurrent )
92 {
93     if ( newCurrent == this )
94         showDetailsIfVisible( _selectable );
95 }
96
97
98 void
99 YQPkgVersionsView::showDetailsIfVisible( ZyppSel selectable )
100 {
101     _selectable = selectable;
102
103     if ( _parentTab )           // Is this view embedded into a tab widget?
104     {
105         if ( _parentTab->currentWidget() == this )  // Is this page the topmost?
106             showDetails( selectable );
107     }
108     else        // No tab parent - simply show data unconditionally.
109     {
110         showDetails( selectable );
111     }
112 }
113
114
115 void
116 YQPkgVersionsView::showDetails( ZyppSel selectable )
117 {
118     _selectable = selectable;
119     clear();
120
121     if ( ! selectable )
122         return;
123
124 #if FIXME
125     QY2CheckListItem * root = new QY2CheckListItem( this, selectable->theObj()->name().c_str() );
126     //FIXME add element
127     Q_CHECK_PTR( root );
128     root->setExpanded( true );
129
130     bool installedIsAvailable = false;
131
132     zypp::ui::Selectable::available_iterator it = selectable->availableBegin();
133
134     while ( it != selectable->availableEnd() )
135     {
136         new YQPkgVersion( this, root, selectable, *it, _userCanSwitch );
137
138         if ( selectable->installedObj() &&
139              selectable->installedObj()->edition() == (*it)->edition() &&
140              selectable->installedObj()->arch()    == (*it)->arch()      )
141             // FIXME: In future releases, also the vendor will make a difference
142             installedIsAvailable = true;
143
144 #if 0
145         // DEBUG
146         new YQPkgVersion( this, root, selectable, *it, _userCanSwitch );
147         new YQPkgVersion( this, root, selectable, *it, _userCanSwitch );
148         new YQPkgVersion( this, root, selectable, *it, _userCanSwitch );
149         // DEBUG
150 #endif
151         ++it;
152     }
153
154     if ( selectable->hasInstalledObj() && ! installedIsAvailable )
155         new YQPkgVersion( this, root, selectable, selectable->installedObj(), false );
156
157 #endif
158 }
159
160
161 void
162 YQPkgVersionsView::checkForChangedCandidate()
163 {
164     QTreeWidgetItemIterator iter(this);
165     QTreeWidgetItem *first = *iter;
166
167     if ( ! first || ! _selectable )
168         return;
169
170     
171     QTreeWidgetItemIterator iter_c(first);
172     
173     while ( *iter_c )
174     {
175         YQPkgVersion * versionItem = dynamic_cast<YQPkgVersion *> (*iter_c);
176
177         if ( versionItem && (versionItem->checkState(0) == Qt::Checked) )
178         {
179             ZyppObj newCandidate = versionItem->zyppObj();
180
181             if ( newCandidate != _selectable->candidateObj() )
182             {
183                 y2milestone( "Candidate changed" );
184
185                 // Change status of selectable
186
187                 ZyppStatus status = _selectable->status();
188
189                 if ( _selectable->installedObj() &&
190                      _selectable->installedObj()->arch()    == newCandidate->arch() &&
191                      _selectable->installedObj()->edition() == newCandidate->edition() )
192                 {
193                     // Switch back to the original instance -
194                     // the version that was previously installed
195                     status = S_KeepInstalled;
196                 }
197                 else
198                 {
199                     switch ( status )
200                     {
201                         case S_KeepInstalled:
202                         case S_Protected:
203                         case S_AutoDel:
204                         case S_AutoUpdate:
205                         case S_Del:
206                         case S_Update:
207
208                             status = S_Update;
209                             break;
210
211                         case S_NoInst:
212                         case S_Taboo:
213                         case S_Install:
214                         case S_AutoInstall:
215                             status = S_Install;
216                             break;
217                     }
218                 }
219
220                 _selectable->set_status( status );
221
222
223                 // Set candidate
224
225                 _selectable->setCandidate( newCandidate );
226                 emit candidateChanged( newCandidate );
227                 return;
228             }
229         }
230
231         ++iter_c;
232     }
233 }
234
235
236 QSize
237 YQPkgVersionsView::minimumSizeHint() const
238 {
239     return QSize( 0, 0 );
240 }
241
242
243
244
245
246
247 YQPkgVersion::YQPkgVersion( YQPkgVersionsView * pkgVersionList,
248                             QY2CheckListItem *  parent,
249                             ZyppSel             selectable,
250                             ZyppObj             zyppObj,
251                             bool                enabled )
252     : QY2CheckListItem( parent, "" /*
253                         enabled ?
254                         Q3CheckListItem::RadioButton :
255                         Q3CheckListItem::Controller */ )        // cheap way to make it read-only
256     , _pkgVersionList( pkgVersionList )
257     , _selectable( selectable )
258     , _zyppObj( zyppObj )
259 {
260     // FIXME setOn( _zyppObj == _selectable->candidateObj() );
261
262     if ( versionCol() >= 0 )    setText( versionCol(), zyppObj->edition().asString().c_str() );
263     if ( archCol()    >= 0 )    setText( archCol(),    zyppObj->arch().asString().c_str() );
264     if ( repoCol() >= 0 )       setText( repoCol(), fromUTF8(zyppObj->repository().info().alias()) );
265     if ( productCol() >= 0 )
266     {
267         ZyppProduct product = YQPkgRepoListItem::singleProduct( zyppObj->repository() );
268
269         if ( product )
270             setText( productCol(), QString::fromStdString( product->summary() ) );
271     }
272     if ( urlCol() >= 0 )
273     {
274         zypp::Url repoUrl;
275
276         if ( ! zyppObj->repository().info().baseUrlsEmpty() )
277             repoUrl = *zyppObj->repository().info().baseUrlsBegin();
278
279         setText( urlCol(), repoUrl.asString().c_str() );
280     }
281
282     if ( _selectable->hasInstalledObj() )
283     {
284         if ( _zyppObj->edition() == _selectable->installedObj()->edition() &&
285              _zyppObj->arch()    == _selectable->installedObj()->arch()      )
286         {
287             // FIXME setPixmap( statusCol(), YQIconPool::pkgKeepInstalled() );
288             setBackgroundColor( QColor( 0xF0, 0xF0, 0xF0 ) );   // light grey
289             setTextColor( QColor( 0, 0x90, 0 ) );               // green
290         }
291     }
292 }
293
294
295 YQPkgVersion::~YQPkgVersion()
296 {
297     // NOP
298 }
299
300
301 QString
302 YQPkgVersion::toolTip(int)
303 {
304     QString tip;
305
306     if ( zyppObj() == selectable()->installedObj() )
307         tip = _( "This version is installed in your system." );
308
309     return tip;
310 }
311
312
313 bool YQPkgVersion::operator< ( const QTreeWidgetItem & otherListViewItem ) const
314 {
315     const YQPkgVersion * other = dynamic_cast<const YQPkgVersion *> (&otherListViewItem);
316
317     if ( other )
318     {
319         return ( this->zyppObj()->edition() < other->zyppObj()->edition() );
320     }
321
322     // Fallback: Use parent class method
323     return QY2CheckListItem::operator<( otherListViewItem );
324 }
325
326
327
328 #include "YQPkgVersionsView.moc"