libyui-ncurses-pkg  2.48.2
 All Classes Functions
NCPkgFilterPattern.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: NCPkgFilterPattern.cc
37 
38  Author: Gabriele Strattner <gs@suse.de>
39 
40 /-*/
41 #define YUILogComponent "ncurses-pkg"
42 #include <YUILog.h>
43 #include <sstream>
44 #include <boost/format.hpp>
45 
46 #include "NCPkgFilterPattern.h"
47 
48 #include "YDialog.h"
49 #include "NCLayoutBox.h"
50 #include "NCSpacing.h"
51 #include "NCPkgStrings.h"
52 #include "NCPkgTable.h"
53 #include "NCPkgStatusStrategy.h"
54 #include <zypp/sat/LocaleSupport.h>
55 
56 #include "NCZypp.h"
57 #include "NCi18n.h"
58 
59 #ifdef FIXME
60 #define LOCALE Y2PM::getPreferredLocale()
61 #else
62 #define LOCALE
63 #endif
64 
65 using std::endl;
66 
67 /*
68  Textdomain "ncurses-pkg"
69 */
70 
71 struct paircmp
72 {
73  bool operator() (std::pair<std::string, std::string> p1, std::pair<std::string, std::string> p2)
74  {
75  if( p1.second != p2.second )
76  return p1.second < p2.second;
77  else
78  return ( p1.first < p2.first );
79  }
80 };
81 ///////////////////////////////////////////////////////////////////
82 //
83 //
84 // METHOD NAME : NCPkgFilterPattern::NCPkgFilterPattern
85 // METHOD TYPE : Constructor
86 //
87 // DESCRIPTION :
88 //
89 NCPkgFilterPattern::NCPkgFilterPattern( YWidget *parent, YTableHeader *header, NCPackageSelector * pkg )
90  : NCPkgTable( parent, header )
91  , packager( pkg )
92 {
93  createLayout( parent );
94  setNotify(true);
95  fillPatternList( );
96 }
97 
98 ///////////////////////////////////////////////////////////////////
99 //
100 //
101 // METHOD NAME : NCPkgFilterPattern::~NCPkgFilterPattern
102 // METHOD TYPE : Destructor
103 //
104 // DESCRIPTION :
105 //
106 NCPkgFilterPattern::~NCPkgFilterPattern()
107 {
108 
109 }
110 
111 ///////////////////////////////////////////////////////////////////
112 //
113 //
114 // METHOD NAME : NCPkgFilterPattern::createLayout
115 // METHOD TYPE : void
116 //
117 // DESCRIPTION :
118 //
119 void NCPkgFilterPattern::createLayout( YWidget *parent )
120 {
121 
122  setPackager( packager );
123 
124  // set status strategy
126 
127  setTableType( NCPkgTable::T_Selections, strat );
128 
129  fillHeader();
130 
131 }
132 
133 ///////////////////////////////////////////////////////////////////
134 //
135 // NCursesEvent & showSelectionPopup ()
136 //
137 //
139 {
140  int index = getCurrentItem();
141  ZyppObj objPtr = getDataPointer( index );
142  if ( objPtr )
143  {
144  // show the package list
145  std::set<std::string> packages;
146  ZyppPattern patPtr = tryCastToZyppPattern (objPtr);
147 
148  if (patPtr)
149  {
150  int total = 0;
151  int installed = 0;
152 
153  yuiMilestone() << "Show packages belonging to selected pattern: " << getCurrentLine() << endl;
154  NCPkgTable * packageList = packager->PackageList();
155 
156  if ( !packageList )
157  {
158  yuiError() << "Widget is not a valid NCPkgTable widget" << endl;
159  return;
160  }
161  packageList->itemsCleared ();
162 
163  zypp::Pattern::Contents related ( patPtr->contents() );
164  for ( zypp::Pattern::Contents::Selectable_iterator it = related.selectableBegin();
165  it != related.selectableEnd();
166  ++it )
167  {
168  ZyppPkg zyppPkg = tryCastToZyppPkg( (*it)->theObj() );
169  if ( zyppPkg )
170  {
171  packageList->createListEntry( zyppPkg, *it );
172  if ( (*it)->installedSize() > 0 )
173  {
174  ++installed;
175  }
176  ++total;
177  }
178  }
179  packager->FilterDescription()->setText ( showDescription( objPtr ) );
180 
181  std::ostringstream s;
182 
183  s << boost::format( _( "%d of %d package installed", "%d of %d packages installed", total )) % installed % total;
184 
185  packager->PatternLabel()->setLabel ( s.str() );
186 
187  packageList->setCurrentItem( 0 );
188  packageList->drawList();
189  packageList->showInformation();
190  }
191  }
192 }
193 
194 
195 //////////////////////////////////////////////////////////////////
196 //
197 // getCurrentLine()
198 //
199 // returns the currently selected list item (may be "really" selected
200 // or not)
201 //
202 std::string NCPkgFilterPattern::getCurrentLine( )
203 {
204 //if ( !sel )
205 // return "";
206 
207  int index = getCurrentItem();
208  ZyppObj selPtr = getDataPointer(index);
209 
210  return ( selPtr?selPtr->summary(LOCALE):"" );
211 }
212 std::string NCPkgFilterPattern::showDescription( ZyppObj objPtr )
213 {
214  ZyppPattern patPtr = tryCastToZyppPattern (objPtr);
215  return patPtr->description();
216 }
217 
218 ///////////////////////////////////////////////////////////////////
219 //
220 //
221 // METHOD NAME : NCPopup::wHandleInput
222 // METHOD TYPE : NCursesEvent
223 //
224 // DESCRIPTION :
225 //
226 NCursesEvent NCPkgFilterPattern::wHandleInput( wint_t ch )
227 {
228  NCursesEvent ret = NCursesEvent::none;
229 
230  // call handleInput of NCPad
231  handleInput( ch );
232 
233  switch ( ch )
234  {
235  case KEY_UP:
236  case KEY_DOWN:
237  case KEY_NPAGE:
238  case KEY_PPAGE:
239  case KEY_END:
240  case KEY_HOME: {
241  ret = NCursesEvent::handled;
242  break;
243  }
244 
245 
246  default:
247  ret = NCPkgTable::wHandleInput( ch ) ;
248  }
249 
251  return ret;
252 }
253 
254 ///////////////////////////////////////////////////////////////////
255 //
256 // OrderFuncPattern
257 //
258 bool orderPattern( ZyppSel slb1, ZyppSel slb2 )
259 {
260  ZyppPattern ptr1 = tryCastToZyppPattern (slb1->theObj());
261  ZyppPattern ptr2 = tryCastToZyppPattern (slb2->theObj());
262  if ( !ptr1 || !ptr2 )
263  return false;
264  else
265  {
266  if( ptr1->order() != ptr2->order() )
267  return ( ptr1->order() < ptr2->order() );
268  else
269  return ( ptr1->name() < ptr2->name() );
270  }
271 }
272 
273 ///////////////////////////////////////////////////////////////////
274 //
275 //
276 // METHOD NAME : NCPkgFilterPattern::fillContainerList
277 // METHOD TYPE : bool
278 //
279 // DESCRIPTION :
280 //
282 {
283 
284  ZyppPoolIterator i, b, e;
285  std::map<std::string, std::list<ZyppSel> > patterns;
286  std::map<std::string, std::list<ZyppSel> >::iterator mapIt;
287 
288  for ( i = zyppPatternsBegin () ; i != zyppPatternsEnd (); ++i )
289  {
290  ZyppObj resPtr = (*i)->theObj();
291  bool show;
292 
293  ZyppPattern patPtr = tryCastToZyppPattern (resPtr);
294  show = patPtr && patPtr->userVisible ();
295 
296  if (show)
297  {
298  std::string cat = patPtr->category();
299 
300  //fallback category
301  if ( cat.empty())
302  cat = "other";
303 
304  //create "category_name" : list <patterns> std::pair
305  std::map <std::string, std::list<ZyppSel> >::iterator item = patterns.find(cat);
306  if( item == patterns.end())
307  {
308  std::list <ZyppSel> slbList;
309  slbList.push_back( (*i) );
310  yuiMilestone() << "New category added: " << cat << endl;
311  patterns.insert( make_pair (cat,slbList) );
312  }
313  else
314  {
315  (*item).second.push_back( (*i));
316  }
317 
318  yuiMilestone() << resPtr->kind () <<": " << resPtr->name()
319  << ", initial status: " << (*i)->status() << ", order: " << patPtr->order() << endl;
320  }
321  }
322 
323  std::set < std::pair <std::string, std::string>, paircmp > pat_index;
324  std::set < std::pair <std::string, std::string>, paircmp >::iterator indexIt;
325 
326  //for each category
327  for ( mapIt = patterns.begin(); mapIt != patterns.end(); ++mapIt )
328  {
329  std::string name = (*mapIt).first;
330  //sort the patterns by their order #
331  (*mapIt).second.sort( orderPattern );
332  std::list<ZyppSel>::iterator it = (*mapIt).second.begin();
333 
334  ZyppPattern pat = tryCastToZyppPattern ((*it)->theObj());
335 
336  if (pat)
337  {
338  yuiDebug() << "Lowest #: "<< pat->order() << endl;
339  //create "category name" : "order #" std::pair in index structure
340  pat_index.insert( make_pair( name, pat->order()) );
341 
342  }
343  }
344 
345  std::list<ZyppSel>::iterator listIt;
346  std::vector<std::string> pkgLine;
347 
348  //now retrieve patterns in defined order
349  for( indexIt = pat_index.begin(); indexIt != pat_index.end(); ++indexIt)
350  {
351  std::string name = (*indexIt).first;
352  std::list<ZyppSel> slbList = patterns[name];
353 
354  for ( listIt = slbList.begin(); listIt != slbList.end(); ++listIt )
355  {
356  ZyppObj resPtr = (*listIt)->theObj();
357  pkgLine.clear();
358 
359  pkgLine.push_back( resPtr->summary(LOCALE) ); // the description
360 
361  addLine( (*listIt)->status(), // the status
362  pkgLine,
363  resPtr, // ZyppPattern
364  (*listIt) ); // ZyppSel
365  }
366  }
367 
368  return true;
369 }
virtual NCursesEvent wHandleInput(wint_t key)
Handles the events concerning the package table (e.g.
Definition: NCPkgTable.cc:800
bool showInformation()
Show the corresponding information (e.g.
Definition: NCPkgTable.cc:762
bool fillPatternList()
Fills the std::list with the available selections (and the status info)
The package table class.
Definition: NCPkgTable.h:175
virtual void addLine(ZyppStatus status, const std::vector< std::string > &elements, ZyppObj objPtr, ZyppSel slbPtr)
This method is called to add a line to the package list.
Definition: NCPkgTable.cc:156
ZyppObj getDataPointer(int index)
Gets the data pointer of a certain package.
Definition: NCPkgTable.cc:874
void fillHeader()
Fills the header of the table.
Definition: NCPkgTable.cc:448
void showPatternPackages()
Shows the popup with the add ons (package categories).
bool createListEntry(ZyppPkg pkgPtr, ZyppSel slbPtr)
Creates a line in the package table.
Definition: NCPkgTable.cc:550
bool setTableType(NCPkgTableType type, NCPkgStatusStrategy *strategy)
Sets the type of the table and the status strategy (which means call particular methods to set/get th...
Definition: NCPkgTable.h:352
virtual void itemsCleared()
Clears the package list.
Definition: NCPkgTable.cc:184
void drawList()
Draws the package list (has to be called after the loop with addLine() calls)
Definition: NCPkgTable.h:263
void setPackager(NCPackageSelector *pkg)
Sets the member variable PackageSelector *packager.
Definition: NCPkgTable.h:300
virtual NCursesEvent wHandleInput(wint_t ch)
Handles the events concerning the package table (e.g.