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