]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/pkg/YQPkgVersionsView.cc
picking up branches/tmp/sh/qt4-port/, merging it with trunk
[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.h>
27 #include <qregexp.h>
28 #include <q3header.h>
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     int numCol = 0;
54     addColumn( _( "Version"     ) );    _versionCol     = numCol++;
55     addColumn( _( "Arch."       ) );    _archCol        = numCol++;
56     addColumn( _( "Product"     ) );    _productCol     = numCol++;
57     addColumn( _( "Repository"  ) );    _repoCol        = numCol++;
58     addColumn( _( "URL"         ) );    _urlCol         = numCol++;
59     _statusCol  = _productCol;
60
61     _nameCol    = _versionCol;
62     _summaryCol = _repoCol;
63
64     setSortByInsertionSequence( true );
65
66     // saveColumnWidths();      // Minimize column widths
67     // header()->hide();
68
69
70     if ( _parentTab )
71     {
72         connect( parent, SIGNAL( currentChanged(QWidget *) ),
73                  this,   SLOT  ( reload        (QWidget *) ) );
74     }
75
76     connect( this,      SIGNAL( selectionChanged        () ),
77              this,      SLOT  ( checkForChangedCandidate() ) );
78 }
79
80
81 YQPkgVersionsView::~YQPkgVersionsView()
82 {
83     // NOP
84 }
85
86
87 void
88 YQPkgVersionsView::reload( QWidget * newCurrent )
89 {
90     if ( newCurrent == this )
91         showDetailsIfVisible( _selectable );
92 }
93
94
95 void
96 YQPkgVersionsView::showDetailsIfVisible( ZyppSel selectable )
97 {
98     _selectable = selectable;
99
100     if ( _parentTab )           // Is this view embedded into a tab widget?
101     {
102         if ( _parentTab->currentPage() == this )  // Is this page the topmost?
103             showDetails( selectable );
104     }
105     else        // No tab parent - simply show data unconditionally.
106     {
107         showDetails( selectable );
108     }
109 }
110
111
112 void
113 YQPkgVersionsView::showDetails( ZyppSel selectable )
114 {
115     _selectable = selectable;
116     clear();
117
118     if ( ! selectable )
119         return;
120
121     QY2CheckListItem * root = new QY2CheckListItem( this, selectable->theObj()->name().c_str(),
122                                                     Q3CheckListItem::Controller );
123     Q_CHECK_PTR( root );
124     root->setOpen( true );
125
126     bool installedIsAvailable = false;
127
128     zypp::ui::Selectable::available_iterator it = selectable->availableBegin();
129
130     while ( it != selectable->availableEnd() )
131     {
132         new YQPkgVersion( this, root, selectable, *it, _userCanSwitch );
133
134         if ( selectable->installedObj() &&
135              selectable->installedObj()->edition() == (*it)->edition() &&
136              selectable->installedObj()->arch()    == (*it)->arch()      )
137             // FIXME: In future releases, also the vendor will make a difference
138             installedIsAvailable = true;
139
140 #if 0
141         // DEBUG
142         new YQPkgVersion( this, root, selectable, *it, _userCanSwitch );
143         new YQPkgVersion( this, root, selectable, *it, _userCanSwitch );
144         new YQPkgVersion( this, root, selectable, *it, _userCanSwitch );
145         // DEBUG
146 #endif
147         ++it;
148     }
149
150     if ( selectable->hasInstalledObj() && ! installedIsAvailable )
151         new YQPkgVersion( this, root, selectable, selectable->installedObj(), false );
152 }
153
154
155 void
156 YQPkgVersionsView::checkForChangedCandidate()
157 {
158     if ( ! firstChild() || ! _selectable )
159         return;
160
161     Q3ListViewItem * item = firstChild()->firstChild();
162
163     while ( item )
164     {
165         YQPkgVersion * versionItem = dynamic_cast<YQPkgVersion *> (item);
166
167         if ( versionItem && versionItem->isOn() )
168         {
169             ZyppObj newCandidate = versionItem->zyppObj();
170
171             if ( newCandidate != _selectable->candidateObj() )
172             {
173                 y2milestone( "Candidate changed" );
174
175                 // Change status of selectable
176
177                 ZyppStatus status = _selectable->status();
178
179                 if ( _selectable->installedObj() &&
180                      _selectable->installedObj()->arch()    == newCandidate->arch() &&
181                      _selectable->installedObj()->edition() == newCandidate->edition() )
182                 {
183                     // Switch back to the original instance -
184                     // the version that was previously installed
185                     status = S_KeepInstalled;
186                 }
187                 else
188                 {
189                     switch ( status )
190                     {
191                         case S_KeepInstalled:
192                         case S_Protected:
193                         case S_AutoDel:
194                         case S_AutoUpdate:
195                         case S_Del:
196                         case S_Update:
197
198                             status = S_Update;
199                             break;
200
201                         case S_NoInst:
202                         case S_Taboo:
203                         case S_Install:
204                         case S_AutoInstall:
205                             status = S_Install;
206                             break;
207                     }
208                 }
209
210                 _selectable->set_status( status );
211
212
213                 // Set candidate
214
215                 _selectable->setCandidate( newCandidate );
216                 emit candidateChanged( newCandidate );
217                 return;
218             }
219         }
220
221         item = item->nextSibling();
222     }
223 }
224
225
226 QSize
227 YQPkgVersionsView::minimumSizeHint() const
228 {
229     return QSize( 0, 0 );
230 }
231
232
233
234
235
236
237 YQPkgVersion::YQPkgVersion( YQPkgVersionsView * pkgVersionList,
238                             QY2CheckListItem *  parent,
239                             ZyppSel             selectable,
240                             ZyppObj             zyppObj,
241                             bool                enabled )
242     : QY2CheckListItem( parent, "",
243                         enabled ?
244                         Q3CheckListItem::RadioButton :
245                         Q3CheckListItem::Controller )   // cheap way to make it read-only
246     , _pkgVersionList( pkgVersionList )
247     , _selectable( selectable )
248     , _zyppObj( zyppObj )
249 {
250     setOn( _zyppObj == _selectable->candidateObj() );
251
252     if ( versionCol() >= 0 )    setText( versionCol(), zyppObj->edition().asString().c_str() );
253     if ( archCol()    >= 0 )    setText( archCol(),    zyppObj->arch().asString().c_str() );
254     if ( repoCol() >= 0 )       setText( repoCol(), fromUTF8(zyppObj->repository().info().alias()) );
255     if ( productCol() >= 0 )
256     {
257         ZyppProduct product = YQPkgRepoListItem::singleProduct( zyppObj->repository() );
258
259         if ( product )
260             setText( productCol(), QString::fromStdString( product->summary() ) );
261     }
262     if ( urlCol() >= 0 )
263     {
264         zypp::Url repoUrl;
265
266         if ( ! zyppObj->repository().info().baseUrlsEmpty() )
267             repoUrl = *zyppObj->repository().info().baseUrlsBegin();
268
269         setText( urlCol(), repoUrl.asString().c_str() );
270     }
271
272     if ( _selectable->hasInstalledObj() )
273     {
274         if ( _zyppObj->edition() == _selectable->installedObj()->edition() &&
275              _zyppObj->arch()    == _selectable->installedObj()->arch()      )
276         {
277             setPixmap( statusCol(), YQIconPool::pkgKeepInstalled() );
278             setBackgroundColor( QColor( 0xF0, 0xF0, 0xF0 ) );   // light grey
279             setTextColor( QColor( 0, 0x90, 0 ) );               // green
280         }
281     }
282 }
283
284
285 YQPkgVersion::~YQPkgVersion()
286 {
287     // NOP
288 }
289
290
291 QString
292 YQPkgVersion::toolTip(int)
293 {
294     QString tip;
295
296     if ( zyppObj() == selectable()->installedObj() )
297         tip = _( "This version is installed in your system." );
298
299     return tip;
300 }
301
302
303 /**
304  * Comparison function used for sorting the list.
305  * Returns:
306  * -1 if this <  other
307  *  0 if this == other
308  * +1 if this >  other
309  **/
310 int
311 YQPkgVersion::compare( Q3ListViewItem * otherListViewItem,
312                        int              col,
313                        bool             ascending ) const
314 {
315     YQPkgVersion * other = dynamic_cast<YQPkgVersion *> (otherListViewItem);
316
317     if ( other )
318     {
319         if ( this->zyppObj()->edition() < other->zyppObj()->edition() ) return -1;
320         if ( this->zyppObj()->edition() > other->zyppObj()->edition() ) return  1;
321         return 0;
322     }
323
324     // Fallback: Use parent class method
325     return QY2CheckListItem::compare( otherListViewItem, col, ascending );
326 }
327
328
329
330 #include "YQPkgVersionsView.moc"