libyui-qt-pkg  2.45.15.2
YQPkgServiceList.cc
1 /**************************************************************************
2 Copyright (C) 2018 SUSE LLC
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 Textdomain "qt-pkg"
20 
21 /-*/
22 
23 #include <algorithm>
24 #include <set>
25 #include <string>
26 #include <QDateTime>
27 #include <QHeaderView>
28 #include <QString>
29 
30 #define YUILogComponent "qt-pkg"
31 #include "YUILog.h"
32 #include <zypp/PoolQuery.h>
33 #include <zypp/RepoManager.h>
34 #include <zypp/ServiceInfo.h>
35 
36 #include <QTreeWidget>
37 #include "YQPkgServiceList.h"
38 #include "YQPkgFilters.h"
39 #include "YQi18n.h"
40 #include "utf8.h"
41 
42 using std::string;
43 using std::list;
44 using std::endl;
45 using std::set;
46 using std::vector;
47 
48 
50  : QY2ListView( parent )
51 {
52  yuiDebug() << "Creating service list" << endl;
53 
54  QStringList headers;
55 
56  // TRANSLATORS: Column header for the service list
57  headers << _("Name");
58  _nameCol = 0;
59 
60  setHeaderLabels( headers );
61  header()->setSectionResizeMode( _nameCol, QHeaderView::Stretch );
62 
63  setSelectionMode( QAbstractItemView::ExtendedSelection ); // allow multi-selection with Ctrl-mouse
64 
65  connect( this, SIGNAL( itemSelectionChanged() ),
66  this, SLOT ( filterIfVisible()) );
67  setIconSize(QSize(32,32));
68  fillList();
69  setSortingEnabled( true );
70  sortByColumn( nameCol(), Qt::AscendingOrder );
71  selectSomething();
72 
73  yuiDebug() << "Creating service list done" << endl;
74 }
75 
77 {
78  // NOP
79 }
80 
81 void
83 {
84  clear();
85  yuiDebug() << "Filling service list" << endl;
86 
87  std::set<std::string> added_services;
88  zypp::RepoManager repo_manager;
89 
90  std::for_each(ZyppRepositoriesBegin(), ZyppRepositoriesEnd(), [&](const zypp::Repository& repo) {
91  const std::string &service_name(repo.info().service());
92  if (!service_name.empty())
93  {
94  bool found = std::any_of(added_services.begin(), added_services.end(), [&](const std::string& name) {
95  return service_name == name;
96  });
97 
98  if (!found)
99  {
100  addService(service_name, repo_manager);
101  added_services.insert(service_name);
102  }
103  }
104  });
105 
106  yuiDebug() << "Service list filled" << endl;
107 }
108 
109 void
111 {
112  if ( isVisible() )
113  filter();
114 }
115 
116 void
118 {
119  emit filterStart();
120 
121  yuiMilestone() << "Collecting packages in selected services..." << endl;
122  QTime stopWatch;
123  stopWatch.start();
124 
125  //
126  // Collect all packages from repositories belonging to this service
127  //
128  QTreeWidgetItem * item;
129  QList<QTreeWidgetItem *> items = selectedItems();
130  QListIterator<QTreeWidgetItem *> it(items);
131 
132  while ( it.hasNext() )
133  {
134  item = it.next();
135  YQPkgServiceListItem * serviceItem = dynamic_cast<YQPkgServiceListItem *> (item);
136 
137  if ( serviceItem )
138  {
139  yuiMilestone() << "Selected service: " << serviceItem->zyppService() << endl;
140 
141  zypp::PoolQuery query;
142  std::for_each(ZyppRepositoriesBegin(), ZyppRepositoriesEnd(), [&](const zypp::Repository& repo) {
143  if (serviceItem->zyppService() == repo.info().service())
144  {
145  yuiMilestone() << "Adding repo filter: " << repo.info().alias() << endl;
146  query.addRepo( repo.info().alias() );
147  }
148  });
149  query.addKind(zypp::ResKind::package);
150 
151  std::for_each(query.selectableBegin(), query.selectableEnd(), [&](const zypp::ui::Selectable::Ptr &selectable) {
152  emit filterMatch( selectable, tryCastToZyppPkg( selectable->theObj() ) );
153  });
154  }
155  }
156 
157  yuiDebug() << "Packages sent to package list. Elapsed time: "
158  << stopWatch.elapsed() / 1000.0 << " sec"
159  << endl;
160 
161  emit filterFinished();
162 }
163 
164 void
165 YQPkgServiceList::addService( ZyppService service, const zypp::RepoManager &mgr )
166 {
167  new YQPkgServiceListItem( this, service, mgr );
168 }
169 
170 
173 {
174  QTreeWidgetItem * item = currentItem();
175  return dynamic_cast<YQPkgServiceListItem *> (item);
176 }
177 
179  ZyppService service, const zypp::RepoManager &mgr )
180  : QY2ListViewItem( parentList )
181  , _serviceList( parentList )
182  , _zyppService( service )
183 {
184 
185  zypp::ServiceInfo srvinfo = mgr.getService(service);
186  _zyppServiceName = srvinfo.name();
187  QString service_name(fromUTF8(_zyppServiceName));
188 
189  if ( nameCol() >= 0 && !service.empty() )
190  {
191  setText( nameCol(), service_name);
192  }
193 
194  QString infoToolTip("<p><b>" + service_name.toHtmlEscaped() + "</b></p>");
195 
196  // TRANSLATORS: Tooltip item, followed by service URL
197  infoToolTip += "<p><b>" + _("URL: ") + "</b>" + fromUTF8(srvinfo.url().asString()).toHtmlEscaped() + "</p>";
198 
199  ZyppProduct product = singleProduct( _zyppService );
200  if ( product )
201  {
202  // TRANSLATORS: Tooltip item, followed by product name
203  infoToolTip += ("<p><b>" + _("Product: ") + "</b>"
204  + fromUTF8(product->summary()).toHtmlEscaped() + "</p>");
205  }
206 
207  // TRANSLATORS: Tooltip item, followed by the list of repositories inluded in the libzypp service
208  infoToolTip += "<p><b>" + _("Repositories:") + "</b><ul>";
209  std::for_each(ZyppRepositoriesBegin(), ZyppRepositoriesEnd(), [&](const zypp::Repository& repo) {
210  if (service == repo.info().service())
211  infoToolTip += "<li>" + fromUTF8(repo.name()).toHtmlEscaped() + "</li>";
212  });
213  infoToolTip += "</ul></p>";
214 
215  setToolTip( nameCol(), infoToolTip);
216 
217  QString iconPath;
218  QString iconName = "yast-update";
219 
220  if ( QIcon::hasThemeIcon(iconName) )
221  {
222  setIcon( 0, QIcon::fromTheme(iconName) );
223  }
224  else
225  setIcon( 0, QIcon( iconPath.sprintf("/usr/share/icons/hicolor/48x48/apps/%s.png", iconName.toUtf8().data()) ));
226 }
227 
229 {
230  // NOP
231 }
232 
233 ZyppProduct
234 YQPkgServiceListItem::singleProduct( ZyppService zyppService )
235 {
236  return YQPkgFilters::singleProductFilter([&](const zypp::PoolItem& item) {
237  // filter the products from the requested service
238  return item.resolvable()->repoInfo().service() == zyppService;
239  });
240 }
241 
242 bool
243 YQPkgServiceListItem::operator< ( const QTreeWidgetItem & other ) const
244 {
245  const YQPkgServiceListItem * otherItem = dynamic_cast<const YQPkgServiceListItem *>(&other);
246 
247  // case insensitive compare
248  return QString::compare(fromUTF8(zyppServiceName()), fromUTF8(otherItem->zyppServiceName()), Qt::CaseInsensitive) < 0;
249 }
250 
251 #include "YQPkgServiceList.moc"
void filterStart()
Emitted when the filtering starts.
void filter()
Filter according to the view&#39;s rules and current selection.
ZyppService zyppService() const
Returns the ZYPP service this item corresponds to (its alias)
virtual ~YQPkgServiceList()
Destructor.
std::string zyppServiceName() const
Returns the ZYPP service name this item corresponds to.
virtual ~YQPkgServiceListItem()
Destructor.
void filterIfVisible()
Same as filter(), but only if this widget is currently visible.
YQPkgServiceList(QWidget *parent)
Constructor.
void filterFinished()
Emitted when filtering is finished.
A widget to display a list of libzypp services.
static ZyppProduct singleProduct(ZyppService service)
Returns the product on a source if it has one single product or 0 if there are no or multiple product...
void fillList()
Fill the list.
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 addService(ZyppService service, const zypp::RepoManager &mgr)
Add a service to the list.
YQPkgServiceListItem * selection() const
Returns the currently selected item or 0 if there is none.
YQPkgServiceListItem(YQPkgServiceList *parentList, ZyppService service, const zypp::RepoManager &mgr)
Constructor.
void filterMatch(ZyppSel selectable, ZyppPkg pkg)
Emitted during filtering for each pkg that matches the filter and the candidate package comes from th...