]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/pkg/YQPkgRepoList.cc
- start fixing repository list and versions view
[duncan/yast2-qt4.git] / src / pkg / YQPkgRepoList.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgRepoList.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17   Textdomain "packages-qt"
18
19 /-*/
20
21 #include <algorithm>
22 #include <QDateTime>
23
24 #define y2log_component "qt-pkg"
25 #include <ycp/y2log.h>
26 #include <zypp/RepoManager.h>
27
28 #include <QTreeWidget>
29 #include "YQPkgRepoList.h"
30 #include "YQi18n.h"
31 #include "utf8.h"
32
33 using std::list;
34 using std::set;
35 using std::vector;
36
37
38
39 YQPkgRepoList::YQPkgRepoList( QWidget * parent )
40     : QY2ListView( parent )
41 {
42     y2debug( "Creating repository list" );
43
44     _nameCol    = -1;
45     _urlCol     = -1;
46
47     int numCol = 0;
48
49     QStringList headers;
50
51     // Column headers for repository list
52     headers <<  _( "Name");     _nameCol        = numCol++;
53     headers << _( "URL");       _urlCol         = numCol++;
54
55     setHeaderLabels(headers);
56
57     //setAllColumnsShowFocus( true );
58     //setSelectionMode( Q3ListView::Extended ); // allow multi-selection with Ctrl-mouse
59
60     connect( this,      SIGNAL( currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *) ),
61              this,      SLOT  ( filterIfVisible()) );
62
63     fillList();
64     selectSomething();
65
66     y2debug( "Creating repository list done" );
67 }
68
69
70 YQPkgRepoList::~YQPkgRepoList()
71 {
72     // NOP
73 }
74
75
76 void
77 YQPkgRepoList::fillList()
78 {
79     clear();
80     y2debug( "Filling repository list" );
81
82     for ( ZyppRepositoryIterator it = ZyppRepositoriesBegin();
83           it != ZyppRepositoriesEnd();
84           ++it )
85     {
86         addRepo( *it );
87     }
88
89     y2debug( "Inst repository filled" );
90 }
91
92
93 int
94 YQPkgRepoList::countEnabledRepositories()
95 {
96     return zyppPool().knownRepositoriesSize();
97 }
98
99
100 void
101 YQPkgRepoList::filterIfVisible()
102 {
103     if ( isVisible() )
104         filter();
105 }
106
107
108 void
109 YQPkgRepoList::filter()
110 {
111     emit filterStart();
112
113     y2milestone( "Collecting packages in selected repositories..." );
114     QTime stopWatch;
115     stopWatch.start();
116
117
118     //
119     // Collect all packages on this repository
120     //
121
122     set<ZyppSel> exactMatches;
123     set<ZyppSel> nearMatches;
124
125     
126
127     QTreeWidgetItem * item;
128     
129     QList<QTreeWidgetItem *> items = selectedItems();
130     QListIterator<QTreeWidgetItem *> it(items);
131
132     while ( it.hasNext() )
133     {
134       item = it.next();
135       YQPkgRepoListItem * repoItem = dynamic_cast<YQPkgRepoListItem *> (item);
136
137         if ( repoItem )
138         {
139             ZyppRepo currentRepo = repoItem->zyppRepo();
140
141             for ( ZyppPoolIterator sel_it = zyppPkgBegin();
142                   sel_it != zyppPkgEnd();
143                   ++sel_it )
144             {
145                 if ( (*sel_it)->candidateObj() &&
146                       (*sel_it)->candidateObj()->repository() == currentRepo )
147                 {
148                     exactMatches.insert( *sel_it );
149                 }
150                 else
151                 {
152                     zypp::ui::Selectable::available_iterator pkg_it = (*sel_it)->availableBegin();
153
154                     while ( pkg_it != (*sel_it)->availableEnd() )
155                     {
156                         if ( (*pkg_it)->repository() == currentRepo )
157                             nearMatches.insert( *sel_it );
158
159                         ++pkg_it;
160                     }
161                 }
162             }
163
164         }
165     }
166
167
168     //
169     // Send all exact matches to the list
170     // (emit a filterMatch signal for each one)
171     //
172
173     set<ZyppSel>::const_iterator sel_it = exactMatches.begin();
174
175     while ( sel_it != exactMatches.end() )
176     {
177         emit filterMatch( (*sel_it), tryCastToZyppPkg( (*sel_it)->theObj() ) );
178         nearMatches.erase( *sel_it );
179         ++sel_it;
180     }
181
182
183     //
184     // Send all near matches to the list
185     // (emit a filterNearMatch signal for each one)
186     //
187
188     sel_it = nearMatches.begin();
189
190     while ( sel_it != nearMatches.end() )
191     {
192         emit filterNearMatch( *sel_it, tryCastToZyppPkg( (*sel_it)->theObj() ) );
193         ++sel_it;
194     }
195
196     y2debug( "Packages sent to package list. Elapsed time: %f sec", stopWatch.elapsed() / 1000.0 );
197
198     emit filterFinished();
199 }
200
201
202 void
203 YQPkgRepoList::addRepo( ZyppRepo repo )
204 {
205     new YQPkgRepoListItem( this, repo );
206 }
207
208
209 YQPkgRepoListItem *
210 YQPkgRepoList::selection() const
211 {
212     QTreeWidgetItem * item = currentItem();
213
214     if ( ! item )
215         return 0;
216
217     return dynamic_cast<YQPkgRepoListItem *> (item);
218 }
219
220
221
222
223
224
225 YQPkgRepoListItem::YQPkgRepoListItem( YQPkgRepoList *   repoList,
226                                       ZyppRepo          repo    )
227     : QY2ListViewItem( repoList )
228     , _repoList( repoList )
229     , _zyppRepo( repo )
230 {
231     if ( nameCol() >= 0 )
232     {
233         string name = repo.info().name();
234
235 #if SHOW_SINGLE_PRODUCT
236         ZyppProduct product = singleProduct( _zyppRepo );
237
238         if ( product )  // only if the repository provides exactly one product
239         {               // (which is the most common case)
240             name = product->summary();
241         }
242 #endif
243
244         if ( ! name.empty() )
245             setText( nameCol(), fromUTF8( name ));
246     }
247
248     if ( urlCol() >= 0 )
249     {
250         zypp::Url repoUrl;
251
252         if ( ! repo.info().baseUrlsEmpty() )
253             repoUrl = *repo.info().baseUrlsBegin();
254
255         setText( urlCol(), repoUrl.asString().c_str() );
256     }
257 }
258
259
260
261 YQPkgRepoListItem::~YQPkgRepoListItem()
262 {
263     // NOP
264 }
265
266
267 ZyppProduct
268 YQPkgRepoListItem::singleProduct( ZyppRepo zyppRepo )
269 {
270     ZyppProduct product;
271
272     zypp::ResStore::iterator it = zyppRepo.resolvables().begin();
273
274     //
275     // Find the first product on this repository
276     //
277
278     while ( it != zyppRepo.resolvables().end() && ! product )
279     {
280         product = zypp::dynamic_pointer_cast<zypp::Product>( *it );
281         ++it;
282     }
283
284     //
285     // Check if there is another product on this repository
286     //
287
288     while ( it != zyppRepo.resolvables().end() )
289     {
290         if ( zypp::dynamic_pointer_cast<zypp::Product>( *it ) )
291         {
292             y2milestone( "Multiple products in repository %s",
293                          zyppRepo.info().alias().c_str() );
294             ZyppProduct null;
295             return null;
296         }
297
298         ++it;
299     }
300
301     if ( ! product )
302         y2milestone( "No product in repository %s",
303                      zyppRepo.info().alias().c_str() );
304
305     return product;
306 }
307
308
309
310 #include "YQPkgRepoList.moc"