libyui-qt-pkg  2.47.5
YQPkgRepoList.cc
1 /**************************************************************************
2 Copyright (C) 2000 - 2010 Novell, Inc.
3 All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 
19 **************************************************************************/
20 
21 
22 /*---------------------------------------------------------------------\
23 | |
24 | __ __ ____ _____ ____ |
25 | \ \ / /_ _/ ___|_ _|___ \ |
26 | \ V / _` \___ \ | | __) | |
27 | | | (_| |___) || | / __/ |
28 | |_|\__,_|____/ |_| |_____| |
29 | |
30 | core system |
31 | (C) SuSE GmbH |
32 \----------------------------------------------------------------------/
33 
34  File: YQPkgRepoList.cc
35 
36  Author: Stefan Hundhammer <sh@suse.de>
37 
38  Textdomain "qt-pkg"
39 
40 /-*/
41 
42 #include <algorithm>
43 #include <QElapsedTimer>
44 #include <QHeaderView>
45 
46 #define YUILogComponent "qt-pkg"
47 #include "YUILog.h"
48 #include <zypp/RepoManager.h>
49 #include <zypp/PoolQuery.h>
50 
51 #include <QTreeWidget>
52 #include "YQPkgRepoList.h"
53 #include "YQPkgFilters.h"
54 #include "YQi18n.h"
55 #include "utf8.h"
56 #include "YQUI.h"
57 
58 using std::string;
59 using std::list;
60 using std::endl;
61 using std::set;
62 using std::vector;
63 
64 
65 YQPkgRepoList::YQPkgRepoList( QWidget * parent )
66  : QY2ListView( parent )
67 {
68  yuiDebug() << "Creating repository list" << endl;
69 
70  _nameCol = -1;
71  _urlCol = -1;
72 
73  int numCol = 0;
74 
75  QStringList headers;
76 
77  // Column headers for repository list
78  headers << _( "Name"); _nameCol = numCol++;
79  // headers << _( "URL"); _urlCol = numCol++;
80 
81  setHeaderLabels( headers );
82  header()->setSectionResizeMode( _nameCol, QHeaderView::Stretch );
83 
84  //setAllColumnsShowFocus( true );
85  setSelectionMode( QAbstractItemView::ExtendedSelection ); // allow multi-selection with Ctrl-mouse
86 
87  connect( this, SIGNAL( itemSelectionChanged() ),
88  this, SLOT ( filterIfVisible()) );
89  setIconSize(QSize(32,32));
90  fillList();
91  setSortingEnabled( true );
92  sortByColumn( nameCol(), Qt::AscendingOrder );
93  selectSomething();
94 
95  yuiDebug() << "Creating repository list done" << endl;
96 }
97 
98 
100 {
101  // NOP
102 }
103 
104 
105 void
107 {
108  clear();
109  yuiDebug() << "Filling repository list" << endl;
110 
111  for ( ZyppRepositoryIterator it = ZyppRepositoriesBegin();
112  it != ZyppRepositoriesEnd();
113  ++it )
114  {
115  addRepo( *it );
116  }
117 
118  yuiDebug() << "Inst repository filled" << endl;
119 }
120 
121 
122 int
124 {
125  return zyppPool().knownRepositoriesSize();
126 }
127 
128 
129 void
131 {
132  if ( isVisible() )
133  filter();
134 }
135 
136 
137 void
139 {
140  emit filterStart();
141 
142  yuiMilestone() << "Collecting packages in selected repositories..." << endl;
143  QElapsedTimer stopWatch;
144  stopWatch.start();
145 
146 
147  //
148  // Collect all packages on this repository
149  //
150 
151  QTreeWidgetItem * item;
152 
153  QList<QTreeWidgetItem *> items = selectedItems();
154  QListIterator<QTreeWidgetItem *> it(items);
155 
156  while ( it.hasNext() )
157  {
158  item = it.next();
159  YQPkgRepoListItem * repoItem = dynamic_cast<YQPkgRepoListItem *> (item);
160 
161  if ( repoItem )
162  {
163  ZyppRepo currentRepo = repoItem->zyppRepo();
164 
165  zypp::PoolQuery query;
166  query.addRepo( currentRepo.info().alias() );
167  query.addKind(zypp::ResKind::package);
168 
169  for( zypp::PoolQuery::Selectable_iterator it = query.selectableBegin();
170  it != query.selectableEnd(); it++)
171  {
172  emit filterMatch( *it, tryCastToZyppPkg( (*it)->theObj() ) );
173  }
174  }
175  }
176 
177  yuiDebug() << "Packages sent to package list. Elapsed time: "
178  << stopWatch.elapsed() / 1000.0 << " sec"
179  << endl;
180 
181  emit filterFinished();
182 }
183 
184 
185 void
186 YQPkgRepoList::addRepo( ZyppRepo repo )
187 {
188  new YQPkgRepoListItem( this, repo );
189 }
190 
191 
194 {
195  QTreeWidgetItem * item = currentItem();
196 
197  if ( ! item )
198  return 0;
199 
200  return dynamic_cast<YQPkgRepoListItem *> (item);
201 }
202 
203 
205  ZyppRepo repo )
206  : QY2ListViewItem( repoList )
207  , _repoList( repoList )
208  , _zyppRepo( repo )
209 {
210  if ( nameCol() >= 0 )
211  {
212  string name = repo.info().name();
213  if ( ! name.empty() )
214  {
215  setText( nameCol(), fromUTF8( name ));
216  }
217  }
218 
219  std::string infoToolTip;
220  infoToolTip += ("<b>" + repo.info().name() + "</b>");
221 
222  ZyppProduct product = singleProduct( _zyppRepo );
223  if ( product )
224  {
225  infoToolTip += ("<p>" + product->summary() + "</p>");
226  }
227 
228  if ( ! repo.info().baseUrlsEmpty() )
229  {
230  zypp::RepoInfo::urls_const_iterator it;
231  infoToolTip += "<ul>";
232 
233  for ( it = repo.info().baseUrlsBegin();
234  it != repo.info().baseUrlsEnd();
235  ++it )
236  {
237  infoToolTip += ("<li>" + (*it).asString() + "</li>");
238  }
239  infoToolTip += "</ul>";
240  }
241  setToolTip( nameCol(), fromUTF8(infoToolTip) );
242 
243  QString iconPath;
244  QString iconName = "applications-internet";
245 
246  if ( ! repo.info().baseUrlsEmpty() )
247  {
248  zypp::Url repoUrl = *repo.info().baseUrlsBegin();
249 
250  if ( urlCol() >= 0 )
251  {
252  setText( urlCol(), repoUrl.asString().c_str() );
253  }
254 
255  if (QString(repoUrl.asString().c_str()).contains("KDE") )
256  iconName = "kde";
257  if (QString(repoUrl.asString().c_str()).contains("GNOME") )
258  iconName = "gnome";
259  if (QString(repoUrl.asString().c_str()).contains("update") )
260  iconName = "applications-utilities";
261  if (QString(repoUrl.asString().c_str()).contains("home:") )
262  iconName = "preferences-desktop";
263  }
264 
265  if ( repo.isSystemRepo() )
266  iconName = "preferences-system";
267 
268  setIcon( 0, YQUI::ui()->loadIcon( iconName.toStdString() ) );
269 }
270 
271 
272 
274 {
275  // NOP
276 }
277 
278 
279 ZyppProduct
281 {
282  return YQPkgFilters::singleProductFilter([&](const zypp::PoolItem& item) {
283  // filter the products from the requested repository
284  return item.resolvable()->repoInfo().alias() == zyppRepo.info().alias();
285  });
286 }
287 
288 bool
289 YQPkgRepoListItem::operator< ( const QTreeWidgetItem & other ) const
290 {
291  const YQPkgRepoListItem * otherItem = dynamic_cast<const YQPkgRepoListItem *>(&other);
292 
293  return zyppRepo().info().name() < otherItem->zyppRepo().info().name();
294 }
295 
void fillList()
Fill the list.
void filter()
Filter according to the view&#39;s rules and current selection.
static int countEnabledRepositories()
Returns the number of enabled repositories.
YQPkgRepoListItem * selection() const
Returns the currently selected item or 0 if there is none.
ZyppRepo zyppRepo() const
Returns the ZYPP repository this item corresponds to.
YQPkgRepoList(QWidget *parent)
Constructor.
void filterStart()
Emitted when the filtering starts.
virtual ~YQPkgRepoList()
Destructor.
YQPkgRepoListItem(YQPkgRepoList *parentList, ZyppRepo repo)
Constructor.
void filterFinished()
Emitted when filtering is finished.
void filterMatch(ZyppSel selectable, ZyppPkg pkg)
Emitted during filtering for each pkg that matches the filter and the candidate package comes from th...
virtual ~YQPkgRepoListItem()
Destructor.
static ZyppProduct singleProduct(ZyppRepo repo)
Returns the product on a source if it has one single product or 0 if there are no or multiple product...
Display a list of libzypp repositories.
Definition: YQPkgRepoList.h:58
static ZyppProduct singleProductFilter(std::function< bool(const zypp::PoolItem &item)> filter)
Returns the product if the filter finds a single product or null product if there are no or multiple ...
Definition: YQPkgFilters.cc:35
void addRepo(ZyppRepo repo)
Add a repository to the list.
void filterIfVisible()
Same as filter(), but only if this widget is currently visible.