libyui-qt-pkg  2.47.2
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
280 YQPkgRepoListItem::singleProduct( ZyppRepo zyppRepo )
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 
YQPkgRepoList::addRepo
void addRepo(ZyppRepo repo)
Add a repository to the list.
Definition: YQPkgRepoList.cc:185
YQPkgRepoList::~YQPkgRepoList
virtual ~YQPkgRepoList()
Destructor.
Definition: YQPkgRepoList.cc:98
YQPkgRepoList::filterIfVisible
void filterIfVisible()
Same as filter(), but only if this widget is currently visible.
Definition: YQPkgRepoList.cc:129
YQPkgRepoList::filterMatch
void filterMatch(ZyppSel selectable, ZyppPkg pkg)
Emitted during filtering for each pkg that matches the filter and the candidate package comes from th...
YQPkgRepoListItem::zyppRepo
ZyppRepo zyppRepo() const
Returns the ZYPP repository this item corresponds to.
Definition: YQPkgRepoList.h:181
YQPkgRepoList::fillList
void fillList()
Fill the list.
Definition: YQPkgRepoList.cc:105
YQPkgRepoListItem::YQPkgRepoListItem
YQPkgRepoListItem(YQPkgRepoList *parentList, ZyppRepo repo)
Constructor.
Definition: YQPkgRepoList.cc:203
YQPkgRepoList::filter
void filter()
Filter according to the view's rules and current selection.
Definition: YQPkgRepoList.cc:137
YQPkgRepoList::YQPkgRepoList
YQPkgRepoList(QWidget *parent)
Constructor.
Definition: YQPkgRepoList.cc:64
YQPkgRepoListItem::singleProduct
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...
Definition: YQPkgRepoList.cc:279
YQPkgRepoListItem::~YQPkgRepoListItem
virtual ~YQPkgRepoListItem()
Destructor.
Definition: YQPkgRepoList.cc:272
YQPkgRepoList::filterStart
void filterStart()
Emitted when the filtering starts.
YQPkgRepoListItem
Definition: YQPkgRepoList.h:164
YQPkgRepoList::countEnabledRepositories
static int countEnabledRepositories()
Returns the number of enabled repositories.
Definition: YQPkgRepoList.cc:122
YQPkgRepoList::filterFinished
void filterFinished()
Emitted when filtering is finished.
YQPkgRepoList
Display a list of libzypp repositories.
Definition: YQPkgRepoList.h:57
YQPkgRepoList::selection
YQPkgRepoListItem * selection() const
Returns the currently selected item or 0 if there is none.
Definition: YQPkgRepoList.cc:192
YQPkgFilters::singleProductFilter
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