libyui-ncurses-pkg  2.50.7
NCPkgFilterService.cc
1 /****************************************************************************
2 |
3 | Copyright (c) [2002-2011] Novell, Inc.
4 | Copyright (c) [2018] SUSE LLC
5 | All Rights Reserved.
6 |
7 | This program is free software; you can redistribute it and/or
8 | modify it under the terms of version 2 of the GNU General Public License as
9 | published by the Free Software Foundation.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program; if not, contact SUSE.
18 |
19 | To contact SUSE about this file by physical or electronic mail,
20 | you may find current contact information at www.suse.com
21 |
22 |***************************************************************************/
23 
24 #define YUILogComponent "ncurses-pkg"
25 #include <YUILog.h>
26 
27 #include <zypp/ServiceInfo.h>
28 #include <boost/algorithm/string.hpp>
29 
30 #include "NCPkgFilterService.h"
31 
32 #include "YDialog.h"
33 #include "NCLayoutBox.h"
34 #include "NCSpacing.h"
35 #include "NCPackageSelector.h"
36 
37 #include "NCZypp.h"
38 
39 using std::endl;
40 
41 /*
42  Textdomain "ncurses-pkg"
43 */
44 
45 ///////////////////////////////////////////////////////////////////
46 //
47 //
48 // METHOD NAME : NCPkgServiceTag::NCPkgServiceTag
49 // METHOD TYPE : Constructor
50 //
51 // DESCRIPTION :
52 //
53 
54 NCPkgServiceTag::NCPkgServiceTag ( ZyppService servicePtr)
55  : YTableCell(std::string(" "))
56  , service (servicePtr)
57 {
58 
59 }
60 
61 ///////////////////////////////////////////////////////////////////
62 //
63 //
64 // METHOD NAME : NCPkgServiceTable::NCPkgServiceTable
65 // METHOD TYPE : Constructor
66 //
67 // DESCRIPTION :
68 //
69 
70 NCPkgServiceTable::NCPkgServiceTable( YWidget *parent, YTableHeader *tableHeader, NCPackageSelector *pkg )
71  :NCTable( parent, tableHeader )
72  , packager(pkg)
73  , repo_manager(new zypp::RepoManager())
74 {
75  fillHeader();
77 }
78 
80 {
81  bool ret = std::any_of(ZyppRepositoriesBegin(), ZyppRepositoriesEnd(), [](const zypp::Repository& repo) {
82  // if the repository does not belong to any service then the service name is empty
83  return !repo.info().service().empty();
84  });
85 
86  yuiMilestone() << "Found a libzypp service: " << ret << endl;
87  return ret;
88 }
89 
90 ///////////////////////////////////////////////////////////////////
91 //
92 //
93 // METHOD NAME : NCPkgServiceTable::fillHeader
94 // METHOD TYPE : void
95 //
96 // DESCRIPTION : Fill header of servicesitories table (name + URL)
97 //
98 
99 void NCPkgServiceTable::fillHeader()
100 {
101  std::vector <std::string> header;
102 
103  header.reserve(2);
104  header.push_back( "L" );
105  header.push_back( "L" + NCPkgStrings::PkgName() );
106 
107  setHeader( header);
108 }
109 
110 ///////////////////////////////////////////////////////////////////
111 //
112 //
113 // METHOD NAME : NCPkgServiceTable::addLine
114 // METHOD TYPE : void
115 //
116 // DESCRIPTION : Add one line (with tag) to the service table
117 //
118 
119 void NCPkgServiceTable::addLine ( ZyppService svc, const std::vector <std::string> & cols )
120 {
121  // use default ctor, add cell in the next step
122  YTableItem *tabItem = new YTableItem();
123 
124  // place tag (with service reference) to the 0th column
125  tabItem->addCell( new NCPkgServiceTag( svc ) );
126 
127  // and append the rest (name, URL and stuff)
128  for ( const std::string& s: cols )
129  tabItem->addCell(s);
130 
131  // this is NCTable::addItem( tabItem );
132  // it actually appends the line to the table
133  addItem( tabItem );
134 }
135 
136 ///////////////////////////////////////////////////////////////////
137 //
138 //
139 // METHOD NAME : NCPkgServiceTable::getTag
140 // METHOD TYPE : NCPkgServiceTag *
141 //
142 // DESCRIPTION : Get tag of service table line on current index,
143 // (contains service reference)
144 //
145 
147 {
148  NCTableLine *line = myPad()->ModifyLine( index );
149  if ( !line )
150  {
151  return nullptr;
152  }
153 
154  YTableItem *it = line->origItem();
155 
156  // get actual service tag from 0th column of the table
157  YTableCell *tcell = it->cell(0);
158  NCPkgServiceTag *tag = static_cast<NCPkgServiceTag *>( tcell );
159 
160  return tag;
161 }
162 
163 ///////////////////////////////////////////////////////////////////
164 //
165 //
166 // METHOD NAME : NCPkgServiceTable::getService
167 // METHOD TYPE : ZyppService
168 //
169 // DESCRIPTION : Get service reference from selected line's tag
170 //
171 
172 ZyppService NCPkgServiceTable::getService( int index )
173 {
174  NCPkgServiceTag *t = getTag( index );
175 
176  return t ? t->getService() : ZyppService();
177 }
178 
179 static std::string html_escape(const std::string& s)
180 {
181  std::string escaped = boost::replace_all_copy(s, "&", "&amp;");
182  boost::replace_all(escaped, "<", "&lt;");
183  boost::replace_all(escaped, ">", "&gt;");
184  return escaped;
185 }
186 
187 std::string NCPkgServiceTable::getDescription(ZyppService svc)
188 {
189  zypp::ServiceInfo si = repo_manager->getService(svc);
190 
191  std::string label = _( "<b>Service URL:</b>" );
192  std::string ret = label + html_escape(si.url().asString());
193  return ret;
194 }
195 
196 /////////////////////////////////////////////////////////////////////
197 ////
198 ////
199 //// METHOD NAME : NCPkgFilterService::fillServiceList
200 //// METHOD TYPE : bool
201 ////
202 //// DESCRIPTION : Add items to the service list (assoc.
203 //// product name, if any, and URL)
204 ////
205 //
207 {
208  yuiMilestone() << "Filling service list" << endl;
209 
210  std::set<std::string> seen_services;
211 
212  std::for_each(ZyppRepositoriesBegin(), ZyppRepositoriesEnd(), [&](const zypp::Repository& repo)
213  {
214  const std::string &service_name(repo.info().service());
215  if (!service_name.empty())
216  {
217  if (seen_services.find(service_name) == seen_services.end())
218  {
219  seen_services.insert(service_name);
220 
221  std::vector <std::string> oneLine;
222  oneLine.push_back( service_name );
223  addLine( service_name, oneLine);
224  }
225  }
226  });
227 
228  return true;
229 }
230 
232 {
233  int index = getCurrentItem();
234  ZyppService service = getService( index );
235 
236  yuiMilestone() << "Selected service " << service << endl;
237  yuiMilestone() << "Collecting packages in selected service" << endl;
238 
239  NCPkgTable *pkgList = packager->PackageList();
240  // clean the pkg table first
241  pkgList->itemsCleared();
242 
243  zypp::PoolQuery query;
244  query.addKind( zypp::ResKind::package );
245 
246  std::for_each(ZyppRepositoriesBegin(), ZyppRepositoriesEnd(), [&](const zypp::Repository& repo)
247  {
248  if (service == repo.info().service())
249  {
250  yuiMilestone() << "Adding repo filter: " << repo.info().alias() << endl;
251  query.addRepo( repo.info().alias() );
252  }
253  });
254 
255  for ( zypp::PoolQuery::Selectable_iterator it = query.selectableBegin();
256  it != query.selectableEnd();
257  it++)
258  {
259  ZyppPkg pkg = tryCastToZyppPkg( (*it)->theObj() );
260  pkgList->createListEntry ( pkg, *it);
261  }
262 
263  packager->FilterDescription()->setText( getDescription( service ) );
264 
265  pkgList->setCurrentItem( 0 );
266  pkgList->drawList();
267  pkgList->showInformation();
268 }
269 
270 ///////////////////////////////////////////////////////////////////
271 //
272 //
273 // METHOD NAME : NCPkgFilterService::wHandleInput
274 // METHOD TYPE : NCursesEvent
275 //
276 // DESCRIPTION : default boring handle-input
277 //
278 
279 NCursesEvent NCPkgServiceTable::wHandleInput( wint_t ch )
280 {
281  NCursesEvent ret = NCursesEvent::none;
282  handleInput( ch );
283 
284  switch ( ch )
285  {
286  case KEY_UP:
287  case KEY_DOWN:
288  case KEY_NPAGE:
289  case KEY_PPAGE:
290  case KEY_END:
291  case KEY_HOME:
292  ret = NCursesEvent::handled;
294  break;
295 
296  default:
297  ret = NCTable::wHandleInput( ch );
298  break;
299  }
300 
301  return ret;
302 }
303 
bool showInformation()
Show the corresponding information (e.g.
Definition: NCPkgTable.cc:731
NCPkgServiceTag(ZyppService service)
A helper class to hold a reference to zypp::Service for each service table line (actually it&#39;s a dumm...
NCPkgServiceTag * getTag(int index)
Get tag of service table line on current index, (contains service reference)
The package table class.
Definition: NCPkgTable.h:205
virtual void addLine(ZyppService r, const std::vector< std::string > &cols)
Add one line (with tag) to the services table.
std::string getDescription(ZyppService r)
static bool any_service()
STL namespace.
bool createListEntry(ZyppPkg pkgPtr, ZyppSel slbPtr)
Creates a line in the package table.
Definition: NCPkgTable.cc:516
bool fillServiceList()
Add items to the service list (assoc.
ZyppService getService(int index)
Get service reference from selected line&#39;s tag.
virtual void itemsCleared()
Clears the package list.
Definition: NCPkgTable.cc:159
void showServicePackages()
Make the Package List show the packages for the currently selected service.
void drawList()
Draws the package list (has to be called after the loop with addLine() calls)
Definition: NCPkgTable.h:296