libyui-ncurses-pkg  2.50.7
NCPkgFilterRepo.cc
1 /****************************************************************************
2 |
3 | Copyright (c) [2002-2011] Novell, Inc.
4 | All Rights Reserved.
5 |
6 | This program is free software; you can redistribute it and/or
7 | modify it under the terms of version 2 of the GNU General Public License as
8 | published by the Free Software Foundation.
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
16 | along with this program; if not, contact Novell, Inc.
17 |
18 | To contact Novell about this file by physical or electronic mail,
19 | you may find current contact information at www.novell.com
20 |
21 |***************************************************************************/
22 
23 
24 /*---------------------------------------------------------------------\
25 | |
26 | __ __ ____ _____ ____ |
27 | \ \ / /_ _/ ___|_ _|___ \ |
28 | \ V / _` \___ \ | | __) | |
29 | | | (_| |___) || | / __/ |
30 | |_|\__,_|____/ |_| |_____| |
31 | |
32 | core system |
33 | (C) SuSE GmbH |
34 \----------------------------------------------------------------------/
35 
36  File: NCPkgFilterRepo.cc
37 
38  Author: Bubli <kmachalkova@suse.cz>
39 
40 /-*/
41 #define YUILogComponent "ncurses-pkg"
42 #include <YUILog.h>
43 
44 #include "NCPkgFilterRepo.h"
45 
46 #include "YDialog.h"
47 #include "NCLayoutBox.h"
48 #include "NCSpacing.h"
49 #include "NCPackageSelector.h"
50 
51 #include "NCZypp.h"
52 
53 using std::endl;
54 
55 /*
56  Textdomain "ncurses-pkg"
57 */
58 
59 ///////////////////////////////////////////////////////////////////
60 //
61 //
62 // METHOD NAME : NCPkgRepoTag::NCPkgRepoTag
63 // METHOD TYPE : Constructor
64 //
65 // DESCRIPTION :
66 //
67 
68 NCPkgRepoTag::NCPkgRepoTag ( ZyppRepo repoPtr)
69  : YTableCell(std::string(" "))
70  , repo (repoPtr)
71 {
72 
73 }
74 
75 ///////////////////////////////////////////////////////////////////
76 //
77 //
78 // METHOD NAME : NCPkgRepoTable::NCPkgRepoTable
79 // METHOD TYPE : Constructor
80 //
81 // DESCRIPTION :
82 //
83 
84 NCPkgRepoTable::NCPkgRepoTable( YWidget *parent, YTableHeader *tableHeader, NCPackageSelector *pkg )
85  :NCTable( parent, tableHeader )
86  , packager(pkg)
87 {
88  fillHeader();
89  fillRepoList();
90 }
91 
92 ///////////////////////////////////////////////////////////////////
93 //
94 //
95 // METHOD NAME : NCPkgRepoTable::fillHeader
96 // METHOD TYPE : void
97 //
98 // DESCRIPTION : Fill header of repositories table (name + URL)
99 //
100 
101 void NCPkgRepoTable::fillHeader()
102 {
103  std::vector <std::string> header;
104 
105  header.reserve(2);
106  header.push_back( "L" );
107  header.push_back( "L" + NCPkgStrings::PkgName() );
108 
109  setHeader( header);
110 }
111 
112 ///////////////////////////////////////////////////////////////////
113 //
114 //
115 // METHOD NAME : NCPkgRepoTable::addLine
116 // METHOD TYPE : void
117 //
118 // DESCRIPTION : Add one line (with tag) to the repository table
119 //
120 
121 void NCPkgRepoTable::addLine ( ZyppRepo r, const std::vector <std::string> & cols )
122 {
123  // use default ctor, add cell in the next step
124  YTableItem *tabItem = new YTableItem();
125 
126  // place tag (with repo reference) to the 0th column
127  tabItem->addCell( new NCPkgRepoTag ( r ) );
128 
129  // and append the rest (name, URL and stuff)
130  for ( const std::string& s: cols )
131  tabItem->addCell(s);
132 
133  // this is NCTable::addItem( tabItem );
134  // it actually appends the line to the table
135  addItem( tabItem );
136 
137 
138 }
139 
140 ///////////////////////////////////////////////////////////////////
141 //
142 //
143 // METHOD NAME : NCPkgRepoTable::getTag
144 // METHOD TYPE : NCPkgRepoTag *
145 //
146 // DESCRIPTION : Get tag of repository table line on current index,
147 // ( contains repository reference)
148 //
149 
151 {
152  NCTableLine *line = myPad()->ModifyLine( index );
153  if ( !line )
154  {
155  return 0;
156  }
157 
158  YTableItem *it = dynamic_cast<YTableItem*> (line->origItem() );
159 
160  // get actual repo tag from 0th column of the table
161  YTableCell *tcell = it->cell(0);
162  NCPkgRepoTag *tag = static_cast<NCPkgRepoTag *>( tcell );
163 
164  return tag;
165 }
166 
167 ///////////////////////////////////////////////////////////////////
168 //
169 //
170 // METHOD NAME : NCPkgRepoTable::getRepo
171 // METHOD TYPE : ZyppRepo
172 //
173 // DESCRIPTION : Get repository reference from selected line's tag
174 //
175 
176 ZyppRepo NCPkgRepoTable::getRepo( int index )
177 {
178  NCPkgRepoTag *t = getTag( index );
179 
180  if ( !t )
181  {
182  return ZyppRepo();
183  }
184  else
185  {
186  return t->getRepo();
187  }
188 }
189 
190 std::string NCPkgRepoTable::showDescription( ZyppRepo r)
191 {
192  std::string ret = "";
193 
194  if ( r.isSystemRepo())
195  ret = _( "<b>@System</b>: local RPM database" );
196  else
197  {
198  std::string label = _( "<b>Repository URL:</b>" );
199  zypp::Url srcUrl;
200  if ( ! r.info().baseUrlsEmpty() )
201  srcUrl = *(r).info().baseUrlsBegin();
202 
203  ret = label + srcUrl.asString();
204  }
205  return ret;
206 }
207 
208 /////////////////////////////////////////////////////////////////////
209 ////
210 ////
211 //// METHOD NAME : NCPkgFilterRepo::fillRepoList
212 //// METHOD TYPE : bool
213 ////
214 //// DESCRIPTION : Add items to the repository list (assoc.
215 //// product name, if any, and URL)
216 ////
217 //
219 {
220  yuiMilestone() << "Filling repository list" << endl;
221 
222  std::vector <std::string> oneLine;
223 
224  // iterate through all repositories
225  for ( ZyppRepositoryIterator it = ZyppRepositoriesBegin();
226  it != ZyppRepositoriesEnd();
227  ++it)
228  {
229  oneLine.clear();
230 
231  // let's find some product for this repository
232  // but not now :) bug #296782
233  // ZyppProduct product = findProductForRepo( (*it) );
234  // if ( product )
235  //{
236  // name = product->summary();
237  //}
238  std::string name = (*it).info().name();
239 
240  oneLine.push_back( name );
241  addLine( (*it), oneLine);
242  }
243 
244  return true;
245 }
246 
247 bool NCPkgRepoTable::showRepoPackages()
248 {
249  int index = getCurrentItem();
250  ZyppRepo repo = getRepo( index );
251 
252  yuiMilestone() << "Selected repository " << repo.info().alias().c_str() << endl;
253  yuiMilestone() << "Collecting packages in selected repository" << endl;
254 
255  NCPkgTable *pkgList = packager->PackageList();
256  // clean the pkg table first
257  pkgList->itemsCleared();
258 
259  zypp::PoolQuery q;
260  q.addRepo( repo.info().alias() );
261  q.addKind( zypp::ResKind::package );
262 
263  for ( zypp::PoolQuery::Selectable_iterator it = q.selectableBegin();
264  it != q.selectableEnd(); it++)
265  {
266  ZyppPkg pkg = tryCastToZyppPkg( (*it)->theObj() );
267  pkgList->createListEntry ( pkg, *it);
268  }
269 
270  packager->FilterDescription()->setText( showDescription( repo ) );
271 
272  pkgList->setCurrentItem( 0 );
273  pkgList->drawList();
274  pkgList->showInformation();
275 
276  return true;
277 }
278 
279 ///////////////////////////////////////////////////////////////////
280 //
281 //
282 // METHOD NAME : NCPkgFilterRepo::findProductForRepo
283 // METHOD TYPE : ZyppProduct
284 //
285 // DESCRIPTION : Find single zypp::Product for this repository
286 // (null product if multiple products found)
287 //
288 
289 ZyppProduct NCPkgRepoTable::findProductForRepo( ZyppRepo repo)
290 {
291 
292  ZyppProduct product;
293 
294  zypp::ResPool::byKind_iterator beg = zypp::ResPool::instance().byKindBegin( zypp::ResKind::product);
295  zypp::ResPool::byKind_iterator end = zypp::ResPool::instance().byKindEnd( zypp::ResKind::product);
296 
297  while( beg != end && !product )
298  {
299  // Single product - most common case
300  if ( beg->resolvable()->repoInfo().alias() == repo.info().alias() )
301  product = zypp::asKind<zypp::Product>( beg->resolvable() );
302  beg++;
303  }
304 
305  while ( beg != end )
306  {
307  if ( beg->resolvable()->repoInfo().alias() == repo.info().alias() )
308  {
309  // Aw, multiple product found, we don't want those
310  yuiWarning() << "Multiple products in repository " <<
311  repo.info().alias().c_str() << endl;
312  ZyppProduct null;
313  return null;
314  }
315 
316  beg++;
317  }
318 
319  if ( !product )
320  {
321  // bad luck, nothing found
322  yuiMilestone() << "No product in repository " <<
323  repo.info().alias().c_str() << endl;
324  }
325 
326  return product;
327 }
328 
329 ///////////////////////////////////////////////////////////////////
330 //
331 //
332 // METHOD NAME : NCPkgFilterRepo::wHandleInput
333 // METHOD TYPE : NCursesEvent
334 //
335 // DESCRIPTION : default boring handle-input
336 //
337 
338 NCursesEvent NCPkgRepoTable::wHandleInput( wint_t ch )
339 {
340  NCursesEvent ret = NCursesEvent::none;
341  handleInput( ch );
342 
343  switch ( ch )
344  {
345  case KEY_UP:
346  case KEY_DOWN:
347  case KEY_NPAGE:
348  case KEY_PPAGE:
349  case KEY_END:
350  case KEY_HOME:
351  ret = NCursesEvent::handled;
352  showRepoPackages();
353  break;
354 
355  default:
356  ret = NCTable::wHandleInput( ch );
357  break;
358  }
359 
360  return ret;
361 }
362 
bool showInformation()
Show the corresponding information (e.g.
Definition: NCPkgTable.cc:731
ZyppProduct findProductForRepo(ZyppRepo repo)
Find single zypp::Product for this repository (null product if multiple products found) ...
The package table class.
Definition: NCPkgTable.h:205
STL namespace.
ZyppRepo getRepo(int index)
Get repository reference from selected line&#39;s tag.
NCPkgRepoTag * getTag(const int &index)
Get tag of repository table line on current index, ( contains repository reference) ...
bool createListEntry(ZyppPkg pkgPtr, ZyppSel slbPtr)
Creates a line in the package table.
Definition: NCPkgTable.cc:516
NCPkgRepoTag(ZyppRepo repo)
A helper class to hold a reference to zypp::Repository for each repository table line (actually it&#39;s ...
bool fillRepoList()
Add items to the repository list (assoc.
virtual void itemsCleared()
Clears the package list.
Definition: NCPkgTable.cc:159
virtual void addLine(ZyppRepo r, const std::vector< std::string > &cols)
Add one line (with tag) to the repositories table.
void drawList()
Draws the package list (has to be called after the loop with addLine() calls)
Definition: NCPkgTable.h:296