libyui-qt-pkg  2.45.15.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 <QDateTime>
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 
57 using std::string;
58 using std::list;
59 using std::endl;
60 using std::set;
61 using std::vector;
62 
63 
64 YQPkgRepoList::YQPkgRepoList( QWidget * parent )
65  : QY2ListView( parent )
66 {
67  yuiDebug() << "Creating repository list" << endl;
68 
69  _nameCol = -1;
70  _urlCol = -1;
71 
72  int numCol = 0;
73 
74  QStringList headers;
75 
76  // Column headers for repository list
77  headers << _( "Name"); _nameCol = numCol++;
78  // headers << _( "URL"); _urlCol = numCol++;
79 
80  setHeaderLabels( headers );
81  header()->setSectionResizeMode( _nameCol, QHeaderView::Stretch );
82 
83  //setAllColumnsShowFocus( true );
84  setSelectionMode( QAbstractItemView::ExtendedSelection ); // allow multi-selection with Ctrl-mouse
85 
86  connect( this, SIGNAL( itemSelectionChanged() ),
87  this, SLOT ( filterIfVisible()) );
88  setIconSize(QSize(32,32));
89  fillList();
90  setSortingEnabled( true );
91  sortByColumn( nameCol(), Qt::AscendingOrder );
92  selectSomething();
93 
94  yuiDebug() << "Creating repository list done" << endl;
95 }
96 
97 
99 {
100  // NOP
101 }
102 
103 
104 void
106 {
107  clear();
108  yuiDebug() << "Filling repository list" << endl;
109 
110  for ( ZyppRepositoryIterator it = ZyppRepositoriesBegin();
111  it != ZyppRepositoriesEnd();
112  ++it )
113  {
114  addRepo( *it );
115  }
116 
117  yuiDebug() << "Inst repository filled" << endl;
118 }
119 
120 
121 int
123 {
124  return zyppPool().knownRepositoriesSize();
125 }
126 
127 
128 void
130 {
131  if ( isVisible() )
132  filter();
133 }
134 
135 
136 void
138 {
139  emit filterStart();
140 
141  yuiMilestone() << "Collecting packages in selected repositories..." << endl;
142  QTime stopWatch;
143  stopWatch.start();
144 
145 
146  //
147  // Collect all packages on this repository
148  //
149 
150  QTreeWidgetItem * item;
151 
152  QList<QTreeWidgetItem *> items = selectedItems();
153  QListIterator<QTreeWidgetItem *> it(items);
154 
155  while ( it.hasNext() )
156  {
157  item = it.next();
158  YQPkgRepoListItem * repoItem = dynamic_cast<YQPkgRepoListItem *> (item);
159 
160  if ( repoItem )
161  {
162  ZyppRepo currentRepo = repoItem->zyppRepo();
163 
164  zypp::PoolQuery query;
165  query.addRepo( currentRepo.info().alias() );
166  query.addKind(zypp::ResKind::package);
167 
168  for( zypp::PoolQuery::Selectable_iterator it = query.selectableBegin();
169  it != query.selectableEnd(); it++)
170  {
171  emit filterMatch( *it, tryCastToZyppPkg( (*it)->theObj() ) );
172  }
173  }
174  }
175 
176  yuiDebug() << "Packages sent to package list. Elapsed time: "
177  << stopWatch.elapsed() / 1000.0 << " sec"
178  << endl;
179 
180  emit filterFinished();
181 }
182 
183 
184 void
185 YQPkgRepoList::addRepo( ZyppRepo repo )
186 {
187  new YQPkgRepoListItem( this, repo );
188 }
189 
190 
193 {
194  QTreeWidgetItem * item = currentItem();
195 
196  if ( ! item )
197  return 0;
198 
199  return dynamic_cast<YQPkgRepoListItem *> (item);
200 }
201 
202 
204  ZyppRepo repo )
205  : QY2ListViewItem( repoList )
206  , _repoList( repoList )
207  , _zyppRepo( repo )
208 {
209  if ( nameCol() >= 0 )
210  {
211  string name = repo.info().name();
212  if ( ! name.empty() )
213  {
214  setText( nameCol(), fromUTF8( name ));
215  }
216  }
217 
218  std::string infoToolTip;
219  infoToolTip += ("<b>" + repo.info().name() + "</b>");
220 
221  ZyppProduct product = singleProduct( _zyppRepo );
222  if ( product )
223  {
224  infoToolTip += ("<p>" + product->summary() + "</p>");
225  }
226 
227  if ( ! repo.info().baseUrlsEmpty() )
228  {
229  zypp::RepoInfo::urls_const_iterator it;
230  infoToolTip += "<ul>";
231 
232  for ( it = repo.info().baseUrlsBegin();
233  it != repo.info().baseUrlsEnd();
234  ++it )
235  {
236  infoToolTip += ("<li>" + (*it).asString() + "</li>");
237  }
238  infoToolTip += "</ul>";
239  }
240  setToolTip( nameCol(), fromUTF8(infoToolTip) );
241 
242  QString iconPath;
243  QString iconName = "yast-sw_source";
244 
245  if ( ! repo.info().baseUrlsEmpty() )
246  {
247  zypp::Url repoUrl = *repo.info().baseUrlsBegin();
248 
249  if ( urlCol() >= 0 )
250  {
251  setText( urlCol(), repoUrl.asString().c_str() );
252  }
253 
254  if (QString(repoUrl.asString().c_str()).contains("KDE") )
255  iconName = "pattern-kde";
256  if (QString(repoUrl.asString().c_str()).contains("GNOME") )
257  iconName = "pattern-gnome";
258  if (QString(repoUrl.asString().c_str()).contains("update") )
259  iconName = "yast-update";
260  if (QString(repoUrl.asString().c_str()).contains("home:") )
261  iconName = "yast-users";
262  }
263 
264  if ( repo.isSystemRepo() )
265  iconName = "yast-host";
266 
267  if ( QIcon::hasThemeIcon(iconName) )
268  {
269  setIcon( 0, QIcon::fromTheme(iconName) );
270  }
271  else
272  setIcon( 0, QIcon( iconPath.sprintf("/usr/share/icons/hicolor/48x48/apps/%s.png", iconName.toUtf8().data()) ));
273 }
274 
275 
276 
278 {
279  // NOP
280 }
281 
282 
283 ZyppProduct
285 {
286  return YQPkgFilters::singleProductFilter([&](const zypp::PoolItem& item) {
287  // filter the products from the requested repository
288  return item.resolvable()->repoInfo().alias() == zyppRepo.info().alias();
289  });
290 }
291 
292 bool
293 YQPkgRepoListItem::operator< ( const QTreeWidgetItem & other ) const
294 {
295  const YQPkgRepoListItem * otherItem = dynamic_cast<const YQPkgRepoListItem *>(&other);
296 
297  return zyppRepo().info().name() < otherItem->zyppRepo().info().name();
298 }
299 
300 #include "YQPkgRepoList.moc"
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.