libyui-ncurses-pkg  2.46.1
 All Classes Functions
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 ( unsigned i = 1; i < cols.size() + 1; ++i ) {
131  tabItem->addCell( cols[ i-1 ]);
132  }
133 
134  // this is NCTable::addItem( tabItem );
135  //it actually appends the line to the table
136  addItem( tabItem );
137 
138 
139 }
140 
141 ///////////////////////////////////////////////////////////////////
142 //
143 //
144 // METHOD NAME : NCPkgRepoTable::getTag
145 // METHOD TYPE : NCPkgRepoTag *
146 //
147 // DESCRIPTION : Get tag of repository table line on current index,
148 // ( contains repository reference)
149 //
150 
152 {
153  NCTableLine *line = myPad()->ModifyLine( index );
154  if ( !line )
155  {
156  return 0;
157  }
158 
159  YTableItem *it = dynamic_cast<YTableItem*> (line->origItem() );
160 
161  //get actual repo tag from 0th column of the table
162  YTableCell *tcell = it->cell(0);
163  NCPkgRepoTag *tag = static_cast<NCPkgRepoTag *>( tcell );
164 
165  return tag;
166 }
167 
168 ///////////////////////////////////////////////////////////////////
169 //
170 //
171 // METHOD NAME : NCPkgRepoTable::getRepo
172 // METHOD TYPE : ZyppRepo
173 //
174 // DESCRIPTION : Get repository reference from selected line's tag
175 //
176 
177 ZyppRepo NCPkgRepoTable::getRepo( int index )
178 {
179  NCPkgRepoTag *t = getTag( index );
180 
181  if ( !t )
182  {
183  return ZyppRepo( );
184  }
185  else
186  {
187  return t->getRepo();
188  }
189 }
190 
191 std::string NCPkgRepoTable::showDescription( ZyppRepo r)
192 {
193  std::string ret = "";
194 
195  if ( r.isSystemRepo())
196  ret = _( "<b>@System</b>: local RPM database" );
197  else
198  {
199  std::string label = _( "<b>Repository URL:</b>" );
200  zypp::Url srcUrl;
201  if ( ! r.info().baseUrlsEmpty() )
202  srcUrl = *(r).info().baseUrlsBegin();
203 
204  ret = label + srcUrl.asString();
205  }
206  return ret;
207 }
208 
209 /////////////////////////////////////////////////////////////////////
210 ////
211 ////
212 //// METHOD NAME : NCPkgFilterRepo::fillRepoList
213 //// METHOD TYPE : bool
214 ////
215 //// DESCRIPTION : Add items to the repository list (assoc.
216 //// product name, if any, and URL)
217 ////
218 //
220 {
221  yuiMilestone() << "Filling repository list" << endl;
222 
223  std::vector <std::string> oneLine;
224 
225  //iterate through all repositories
226  for ( ZyppRepositoryIterator it = ZyppRepositoriesBegin();
227  it != ZyppRepositoriesEnd();
228  ++it)
229  {
230  oneLine.clear();
231 
232  // let's find some product for this repository
233  // but not now :) bug #296782
234  //ZyppProduct product = findProductForRepo( (*it) );
235  //if ( product )
236  //{
237  // name = product->summary();
238  //}
239  std::string name = (*it).info().name();
240 
241  oneLine.push_back( name );
242  addLine( (*it), oneLine);
243  }
244 
245  return true;
246 }
247 
248 bool NCPkgRepoTable::showRepoPackages( )
249 {
250  int index = getCurrentItem();
251  ZyppRepo repo = getRepo( index );
252 
253  yuiMilestone() << "Selected repository " << repo.info().alias().c_str() << endl;
254  yuiMilestone() << "Collecting packages in selected repository" << endl;
255 
256  NCPkgTable *pkgList = packager->PackageList();
257  //clean the pkg table first
258  pkgList->itemsCleared ();
259 
260  zypp::PoolQuery q;
261  q.addRepo( repo.info().alias() );
262  q.addKind( zypp::ResKind::package );
263 
264  for( zypp::PoolQuery::Selectable_iterator it = q.selectableBegin();
265  it != q.selectableEnd(); it++)
266  {
267  ZyppPkg pkg = tryCastToZyppPkg( (*it)->theObj() );
268  pkgList->createListEntry ( pkg, *it);
269  }
270 
271  packager->FilterDescription()->setText( showDescription( repo ) );
272 
273  pkgList->setCurrentItem( 0 );
274  pkgList->drawList();
275  pkgList->showInformation();
276 
277  return true;
278 }
279 
280 ///////////////////////////////////////////////////////////////////
281 //
282 //
283 // METHOD NAME : NCPkgFilterRepo::findProductForRepo
284 // METHOD TYPE : ZyppProduct
285 //
286 // DESCRIPTION : Find single zypp::Product for this repository
287 // (null product if multiple products found)
288 //
289 
290 ZyppProduct NCPkgRepoTable::findProductForRepo( ZyppRepo repo)
291 {
292 
293  ZyppProduct product;
294 
295  zypp::ResPool::byKind_iterator beg = zypp::ResPool::instance().byKindBegin( zypp::ResKind::product);
296  zypp::ResPool::byKind_iterator end = zypp::ResPool::instance().byKindEnd( zypp::ResKind::product);
297 
298  while( beg != end && !product )
299  {
300  //Single product - most common case
301  if ( beg->resolvable()->repoInfo().alias() == repo.info().alias() )
302  product = zypp::asKind<zypp::Product>( beg->resolvable() );
303  beg++;
304  }
305 
306  while ( beg != end )
307  {
308  if ( beg->resolvable()->repoInfo().alias() == repo.info().alias() )
309  {
310  //Aw, multiple product found, we don't want those
311  yuiWarning() << "Multiple products in repository " <<
312  repo.info().alias().c_str() << endl;
313  ZyppProduct null;
314  return null;
315  }
316 
317  beg++;
318  }
319 
320  if ( !product )
321  {
322  //bad luck, nothing found
323  yuiMilestone() << "No product in repository " <<
324  repo.info().alias().c_str() << endl;
325  }
326 
327  return product;
328 }
329 
330 ///////////////////////////////////////////////////////////////////
331 //
332 //
333 // METHOD NAME : NCPkgFilterRepo::wHandleInput
334 // METHOD TYPE : NCursesEvent
335 //
336 // DESCRIPTION : default boring handle-input
337 //
338 
339 NCursesEvent NCPkgRepoTable::wHandleInput( wint_t ch )
340 {
341  NCursesEvent ret = NCursesEvent::none;
342  handleInput( ch );
343 
344  switch ( ch )
345  {
346  case KEY_UP:
347  case KEY_DOWN:
348  case KEY_NPAGE:
349  case KEY_PPAGE:
350  case KEY_END:
351  case KEY_HOME: {
352  ret = NCursesEvent::handled;
353  showRepoPackages();
354  break;
355  }
356 
357  default:
358  ret = NCTable::wHandleInput( ch ) ;
359  }
360 
361  return ret;
362 }
363 
bool showInformation()
Show the corresponding information (e.g.
Definition: NCPkgTable.cc:762
ZyppProduct findProductForRepo(ZyppRepo repo)
Find single zypp::Product for this repository (null product if multiple products found) ...
The package table class.
Definition: NCPkgTable.h:175
STL namespace.
ZyppRepo getRepo(int index)
Get repository reference from selected line'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:550
NCPkgRepoTag(ZyppRepo repo)
A helper class to hold a reference to zypp::Repository for each repository table line (actually it's ...
bool fillRepoList()
Add items to the repository list (assoc.
virtual void itemsCleared()
Clears the package list.
Definition: NCPkgTable.cc:184
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:263