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