libyui-ncurses-pkg  2.48.2
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  pkgList.sort( sortByName );
248 
249  // fill the package table
250  std::list<ZyppSel>::iterator listIt;
251  ZyppPkg pkgPtr;
252 
253 
254  for ( listIt = pkgList.begin(); listIt != pkgList.end(); ++listIt )
255  {
256  ZyppSel selectable = *listIt;
257 
258  // Multiple instances of this package may or may not be in the same
259  // RPM group, so let's check both the installed version (if there
260  // is any) and the candidate version.
261  //
262  // Make sure we emit only one filterMatch() signal if both exist
263  // and both are in the same RPM group. We don't want multiple list
264  // entries for the same package!
265 
266  bool match =
267  checkPackage( selectable->candidateObj(), selectable, rpmGroup ) ||
268  checkPackage( selectable->installedObj(), selectable, rpmGroup );
269 
270  // If there is neither an installed nor a candidate package, check
271  // any other instance.
272 
273  if ( ! match &&
274  ! selectable->installedObj() &&
275  ! selectable->candidateObj() )
276  checkPackage( selectable->theObj(), selectable, rpmGroup );
277 
278  }
279 
280  // show the package list
281  packageList->setCurrentItem( 0 );
282  packageList->drawList();
283  packageList->showInformation();
284 
285  yuiMilestone() << "Filling package list" << endl;
286 
287  if ( ! label.empty() )
288  {
289  YLabel *packageLabel = packager->PackageLabel();
290  // show the selected filter label
291  if ( packageLabel )
292  {
293  packageLabel->setText( label );
294  }
295  }
296 
297  return true;
298 
299 }
300 
301 ///////////////////////////////////////////////////////////////////
302 //
303 //
304 // METHOD NAME : NCPkgFilterRPMGroups::addItem
305 // METHOD TYPE : void
306 //
307 // DESCRIPTION :
308 //
309 void NCPkgFilterRPMGroups::addItem( YTreeItem * newItem )
310 {
311  if ( !filterTree )
312  {
313  yuiError() << "ERROR: rpm groups tree not available" << endl;
314  return;
315  }
316 
317  filterTree->addItem( newItem );
318 
319 }
320 
321 /////////////////////////////////////////////////////////////////////
322 ////
323 ////
324 //// METHOD NAME : NCPopup::wHandleInput
325 //// METHOD TYPE : NCursesEvent
326 ////
327 //// DESCRIPTION :
328 ////
329 NCursesEvent NCPkgFilterRPMGroups::wHandleInput( wint_t ch )
330 {
331  return NCTree::wHandleInput( ch );
332 }
333 
334 ///////////////////////////////////////////////////////////////////
335 //
336 // cloneTree
337 //
338 // Adds all tree items got from YPkgRpmGroupTagsFilterView to
339 // the filter popup tree
340 //
341 void NCPkgFilterRPMGroups::cloneTree( YStringTreeItem * parentOrig, NCRpmGroupItem * parentClone )
342 {
343  YStringTreeItem * child = parentOrig->firstChild();
344  NCRpmGroupItem * clone;
345 
346  while ( child )
347  {
348  yuiDebug() << "Rpm group (translated): " << child->value().translation() << endl;
349 
350  if ( parentClone )
351  {
352  // YTreeItems which have a parent will automatically register
353  // this item with the parent item.
354  clone = new NCRpmGroupItem( parentClone, child->value().translation(), child );
355  }
356  else
357  {
358  clone = new NCRpmGroupItem( child->value().translation(), child );
359  // use addItem() only for the toplevel items
360  addItem( clone );
361  }
362 
363  cloneTree( child, clone );
364  child = child->next();
365  }
366 }
367 
bool showInformation()
Show the corresponding information (e.g.
Definition: NCPkgTable.cc:762
The package table class.
Definition: NCPkgTable.h:175
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:263