libyui-ncurses-pkg  2.48.5
NCPkgFilterRPMGroups.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: NCPkgFilterRPMGroups.cc
37 
38  Author: Gabriele Strattner <gs@suse.de>
39 
40 /-*/
41 #define YUILogComponent "ncurses-pkg"
42 #include <YUILog.h>
43 
44 #include "NCPkgFilterRPMGroups.h"
45 
46 #include "NCTree.h"
47 #include "YMenuButton.h"
48 #include "YDialog.h"
49 #include "YTreeItem.h"
50 #include "NCLayoutBox.h"
51 #include "NCPkgStrings.h"
52 #include "NCPackageSelector.h"
53 
54 #include "NCZypp.h"
55 
56 using std::endl;
57 
58 ///////////////////////////////////////////////////////////////////
59 //
60 //
61 // CLASS NAME : NCRpmGroupItem
62 //
63 // DESCRIPTION : class derived from YTreeItem with additional
64 // property to store the original rpm group item
65 //
66 //
67 class NCRpmGroupItem : public YTreeItem {
68 
69 private:
70  YStringTreeItem * rpmGroupItem;
71 
72 public:
73 
74  NCRpmGroupItem( YTreeItem * parent,
75  const std::string & label,
76  YStringTreeItem * origItem )
77  : YTreeItem( parent, label ),
78  rpmGroupItem( origItem )
79  {
80 
81  }
82 
83  NCRpmGroupItem( const std::string & label,
84  YStringTreeItem * origItem )
85  : YTreeItem( label ),
86  rpmGroupItem( origItem )
87  {
88 
89  }
90 
91  YStringTreeItem * getOrigItem() const { return rpmGroupItem; }
92 
93 };
94 
95 
96 ///////////////////////////////////////////////////////////////////
97 //
98 //
99 // METHOD NAME : NCPkgFilterRPMGroups::NCPkgFilterRPMGroups
100 // METHOD TYPE : Constructor
101 //
102 // DESCRIPTION :
103 //
104 
105 NCPkgFilterRPMGroups::NCPkgFilterRPMGroups( YWidget *parent, std::string label, NCPackageSelector * pkg )
106  : NCTree( parent, label )
107  , filterTree( this )
108  , packager ( pkg )
109  , _rpmGroupsTree (0)
110 {
111  // create the layout (the NCTree)
112  setNotify(true);
113 
114  _rpmGroupsTree = new YRpmGroupsTree ();
115 
116  ZyppPoolIterator b = zyppPkgBegin ();
117  ZyppPoolIterator e = zyppPkgEnd ();
118  ZyppPoolIterator i;
119 
120  for ( i = b; i != e; ++i )
121  {
122  ZyppPkg zyppPkg = tryCastToZyppPkg( (*i)->theObj() );
123  if ( zyppPkg )
124  {
125  _rpmGroupsTree->addRpmGroup (zyppPkg->group ());
126  yuiDebug() << "Adding group: " << zyppPkg->group() << endl;
127  }
128  }
129 
130  if (_rpmGroupsTree )
131  {
132  // clone the tree (fill the NCTree)
133  cloneTree( _rpmGroupsTree->root(), 0 );
134  }
135 }
136 
137 ///////////////////////////////////////////////////////////////////
138 //
139 //
140 // METHOD NAME : NCPkgFilterRPMGroups::~NCPkgFilterRPMGroups
141 // METHOD TYPE : Destructor
142 //
143 // DESCRIPTION :
144 //
145 NCPkgFilterRPMGroups::~NCPkgFilterRPMGroups()
146 {
147 }
148 
149 ///////////////////////////////////////////////////////////////////
150 //
151 //
152 // METHOD NAME : NCPkgFilterRPMGroups::showFilterPopup
153 // METHOD TYPE : void
154 //
155 // DESCRIPTION :
156 //
157 bool NCPkgFilterRPMGroups::handleEvent( )
158 {
159 
160  YStringTreeItem * origItem;
161  const YTreeItem * item = filterTree->getCurrentItem();
162 
163  if ( item )
164  {
165  const NCRpmGroupItem * rpmGroupItem = dynamic_cast<const NCRpmGroupItem *>(item);
166 
167  if ( rpmGroupItem )
168  {
169  // get the original rpm group item (YStringTreeItem)
170  origItem = rpmGroupItem->getOrigItem();
171 
172  if ( origItem )
173  {
174  std::string label = origItem->value().translation();
175 
176  // fill the package list
177  showRPMGroupPackages( label, origItem );
178 
179  yuiMilestone() << "Selected RPM group: " << label << endl;
180  }
181  }
182  }
183  else
184  {
185  yuiError() << "Current item not valid" << endl;
186  }
187 
188  return true;
189 }
190 
191 YStringTreeItem * NCPkgFilterRPMGroups::getDefaultRpmGroup()
192 {
193  if ( _rpmGroupsTree &&
194  _rpmGroupsTree->root() )
195  return _rpmGroupsTree->root()->firstChild();
196  else
197  return 0;
198 }
199 
200 
201 bool NCPkgFilterRPMGroups::checkPackage( ZyppObj opkg, ZyppSel slb,
202  YStringTreeItem * rpmGroup )
203 {
204  ZyppPkg pkg = tryCastToZyppPkg (opkg);
205  if ( ! pkg || ! rpmGroup )
206  return false;
207 
208  NCPkgTable * packageList = packager->PackageList();
209 
210  if ( !packageList )
211  {
212  yuiError() << "Widget is not a valid NCPkgTable widget" << endl;
213  return false;
214  }
215 
216  std::string group_str = _rpmGroupsTree->rpmGroup (rpmGroup);
217  yuiDebug() << group_str << endl;
218  // is the requested rpm group a prefix of this package's group?
219  if ( pkg->group ().find (group_str) == 0 )
220  {
221  yuiDebug() << slb->name() << endl;
222  packageList->createListEntry( pkg, slb );
223 
224  return true;
225  }
226  else
227  {
228  return false;
229  }
230 }
231 
232 bool NCPkgFilterRPMGroups::showRPMGroupPackages ( const std::string & label, YStringTreeItem *rpmGroup )
233 {
234  NCPkgTable * packageList = packager->PackageList();
235 
236  if ( !packageList )
237  {
238  yuiError() << "No valid NCPkgTable widget" << endl;
239  return false;
240  }
241 
242  // clear the package table
243  packageList->itemsCleared ();
244 
245  // get the package list and sort it
246  std::list<ZyppSel> pkgList( zyppPkgBegin (), zyppPkgEnd () );
247 
248  // fill the package table
249  std::list<ZyppSel>::iterator listIt;
250  ZyppPkg pkgPtr;
251 
252 
253  for ( listIt = pkgList.begin(); listIt != pkgList.end(); ++listIt )
254  {
255  ZyppSel selectable = *listIt;
256 
257  // Multiple instances of this package may or may not be in the same
258  // RPM group, so let's check both the installed version (if there
259  // is any) and the candidate version.
260  //
261  // Make sure we emit only one filterMatch() signal if both exist
262  // and both are in the same RPM group. We don't want multiple list
263  // entries for the same package!
264 
265  bool match =
266  checkPackage( selectable->candidateObj(), selectable, rpmGroup ) ||
267  checkPackage( selectable->installedObj(), selectable, rpmGroup );
268 
269  // If there is neither an installed nor a candidate package, check
270  // any other instance.
271 
272  if ( ! match &&
273  ! selectable->installedObj() &&
274  ! selectable->candidateObj() )
275  checkPackage( selectable->theObj(), selectable, rpmGroup );
276 
277  }
278 
279  // show the package list
280  packageList->setCurrentItem( 0 );
281  packageList->drawList();
282  packageList->showInformation();
283 
284  yuiMilestone() << "Filling package list" << endl;
285 
286  if ( ! label.empty() )
287  {
288  YLabel *packageLabel = packager->PackageLabel();
289  // show the selected filter label
290  if ( packageLabel )
291  {
292  packageLabel->setText( label );
293  }
294  }
295 
296  return true;
297 
298 }
299 
300 ///////////////////////////////////////////////////////////////////
301 //
302 //
303 // METHOD NAME : NCPkgFilterRPMGroups::addItem
304 // METHOD TYPE : void
305 //
306 // DESCRIPTION :
307 //
308 void NCPkgFilterRPMGroups::addItem( YTreeItem * newItem )
309 {
310  if ( !filterTree )
311  {
312  yuiError() << "ERROR: rpm groups tree not available" << endl;
313  return;
314  }
315 
316  filterTree->addItem( newItem );
317 
318 }
319 
320 /////////////////////////////////////////////////////////////////////
321 ////
322 ////
323 //// METHOD NAME : NCPopup::wHandleInput
324 //// METHOD TYPE : NCursesEvent
325 ////
326 //// DESCRIPTION :
327 ////
328 NCursesEvent NCPkgFilterRPMGroups::wHandleInput( wint_t ch )
329 {
330  return NCTree::wHandleInput( ch );
331 }
332 
333 ///////////////////////////////////////////////////////////////////
334 //
335 // cloneTree
336 //
337 // Adds all tree items got from YPkgRpmGroupTagsFilterView to
338 // the filter popup tree
339 //
340 void NCPkgFilterRPMGroups::cloneTree( YStringTreeItem * parentOrig, NCRpmGroupItem * parentClone )
341 {
342  YStringTreeItem * child = parentOrig->firstChild();
343  NCRpmGroupItem * clone;
344 
345  while ( child )
346  {
347  yuiDebug() << "Rpm group (translated): " << child->value().translation() << endl;
348 
349  if ( parentClone )
350  {
351  // YTreeItems which have a parent will automatically register
352  // this item with the parent item.
353  clone = new NCRpmGroupItem( parentClone, child->value().translation(), child );
354  }
355  else
356  {
357  clone = new NCRpmGroupItem( child->value().translation(), child );
358  // use addItem() only for the toplevel items
359  addItem( clone );
360  }
361 
362  cloneTree( child, clone );
363  child = child->next();
364  }
365 }
366 
bool showInformation()
Show the corresponding information (e.g.
Definition: NCPkgTable.cc:762
The package table class.
Definition: NCPkgTable.h:207
bool createListEntry(ZyppPkg pkgPtr, ZyppSel slbPtr)
Creates a line in the package table.
Definition: NCPkgTable.cc:550
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:295