libyui-ncurses-pkg  2.48.9
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 << std::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 
132  // this is NCTable::addItem( tabItem );
133  //it actually appends the line to the table
134  addItem( tabItem );
135 
136 
137 }
138 
139 ///////////////////////////////////////////////////////////////////
140 //
141 //
142 // METHOD NAME : NCPkgServiceTable::getTag
143 // METHOD TYPE : NCPkgServiceTag *
144 //
145 // DESCRIPTION : Get tag of service table line on current index,
146 // (contains service reference)
147 //
148 
150 {
151  NCTableLine *line = myPad()->ModifyLine( index );
152  if ( !line )
153  {
154  return nullptr;
155  }
156 
157  YTableItem *it = line->origItem();
158 
159  //get actual service tag from 0th column of the table
160  YTableCell *tcell = it->cell(0);
161  NCPkgServiceTag *tag = static_cast<NCPkgServiceTag *>( tcell );
162 
163  return tag;
164 }
165 
166 ///////////////////////////////////////////////////////////////////
167 //
168 //
169 // METHOD NAME : NCPkgServiceTable::getService
170 // METHOD TYPE : ZyppService
171 //
172 // DESCRIPTION : Get service reference from selected line's tag
173 //
174 
175 ZyppService NCPkgServiceTable::getService( int index )
176 {
177  NCPkgServiceTag *t = getTag( index );
178 
179  return t ? t->getService() : ZyppService();
180 }
181 
182 static std::string html_escape(const std::string& s)
183 {
184  std::string escaped = boost::replace_all_copy(s, "&", "&amp;");
185  boost::replace_all(escaped, "<", "&lt;");
186  boost::replace_all(escaped, ">", "&gt;");
187  return escaped;
188 }
189 
190 std::string NCPkgServiceTable::getDescription(ZyppService svc)
191 {
192  zypp::ServiceInfo si = repo_manager->getService(svc);
193 
194  std::string label = _( "<b>Service URL:</b>" );
195  std::string ret = label + html_escape(si.url().asString());
196  return ret;
197 }
198 
199 /////////////////////////////////////////////////////////////////////
200 ////
201 ////
202 //// METHOD NAME : NCPkgFilterService::fillServiceList
203 //// METHOD TYPE : bool
204 ////
205 //// DESCRIPTION : Add items to the service list (assoc.
206 //// product name, if any, and URL)
207 ////
208 //
210 {
211  yuiMilestone() << "Filling service list" << endl;
212 
213  std::set<std::string> seen_services;
214 
215  std::for_each(ZyppRepositoriesBegin(), ZyppRepositoriesEnd(), [&](const zypp::Repository& repo)
216  {
217  const std::string &service_name(repo.info().service());
218  if (!service_name.empty())
219  {
220  if (seen_services.find(service_name) == seen_services.end())
221  {
222  seen_services.insert(service_name);
223 
224  std::vector <std::string> oneLine;
225  oneLine.push_back( service_name );
226  addLine( service_name, oneLine);
227  }
228  }
229  });
230 
231  return true;
232 }
233 
235 {
236  int index = getCurrentItem();
237  ZyppService service = getService( index );
238 
239  yuiMilestone() << "Selected service " << service << endl;
240  yuiMilestone() << "Collecting packages in selected service" << endl;
241 
242  NCPkgTable *pkgList = packager->PackageList();
243  //clean the pkg table first
244  pkgList->itemsCleared ();
245 
246  zypp::PoolQuery query;
247  query.addKind( zypp::ResKind::package );
248 
249  std::for_each(ZyppRepositoriesBegin(), ZyppRepositoriesEnd(), [&](const zypp::Repository& repo)
250  {
251  if (service == repo.info().service())
252  {
253  yuiMilestone() << "Adding repo filter: " << repo.info().alias() << std::endl;
254  query.addRepo( repo.info().alias() );
255  }
256  });
257 
258  for( zypp::PoolQuery::Selectable_iterator it = query.selectableBegin();
259  it != query.selectableEnd(); it++)
260  {
261  ZyppPkg pkg = tryCastToZyppPkg( (*it)->theObj() );
262  pkgList->createListEntry ( pkg, *it);
263  }
264 
265  packager->FilterDescription()->setText( getDescription( service ) );
266 
267  pkgList->setCurrentItem( 0 );
268  pkgList->drawList();
269  pkgList->showInformation();
270 }
271 
272 ///////////////////////////////////////////////////////////////////
273 //
274 //
275 // METHOD NAME : NCPkgFilterService::wHandleInput
276 // METHOD TYPE : NCursesEvent
277 //
278 // DESCRIPTION : default boring handle-input
279 //
280 
281 NCursesEvent NCPkgServiceTable::wHandleInput( wint_t ch )
282 {
283  NCursesEvent ret = NCursesEvent::none;
284  handleInput( ch );
285 
286  switch ( ch )
287  {
288  case KEY_UP:
289  case KEY_DOWN:
290  case KEY_NPAGE:
291  case KEY_PPAGE:
292  case KEY_END:
293  case KEY_HOME: {
294  ret = NCursesEvent::handled;
296  break;
297  }
298 
299  default:
300  ret = NCTable::wHandleInput( ch ) ;
301  }
302 
303  return ret;
304 }
305 
bool showInformation()
Show the corresponding information (e.g.
Definition: NCPkgTable.cc:762
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:207
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:550
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:184
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:295