libyui-ncurses-pkg  2.48.5.2
NCPkgTable.h
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: NCPkgTable.h
37 
38  Author: Gabriele Strattner <gs@suse.de>
39 
40 /-*/
41 #ifndef NCPkgTable_h
42 #define NCPkgTable_h
43 
44 #include <iosfwd>
45 
46 #include "NCPadWidget.h"
47 #include "NCTablePad.h"
48 #include "NCTable.h"
49 #include "NCPkgStrings.h"
50 
51 #include <map>
52 #include <string>
53 #include <utility> // for STL std::pair
54 
55 #include <zypp/ui/Selectable.h>
56 
57 #include "NCPkgStatusStrategy.h"
58 
59 
60 class NCPackageSelector;
61 
62 /**
63  * This class is used for the first column of the package table
64  * which contains the status information of the package (installed,
65  * not installed, to be deleted and so on).
66  *
67  **/
68 class NCPkgTableTag : public YTableCell {
69 
70  private:
71 
72  ZyppStatus status;
73  ZyppObj dataPointer;
74  // cannot get at it from dataPointer
75  ZyppSel selPointer;
76 
77  public:
78 
79  NCPkgTableTag( ZyppObj pkgPtr,
80  ZyppSel selPtr,
81  ZyppStatus stat = S_NoInst );
82 
83  ~NCPkgTableTag() {}
84 
85  void setStatus( ZyppStatus stat ) { status = stat; }
86  ZyppStatus getStatus() const { return status; }
87  // returns the corresponding std::string value to given package status
88  std::string statusToString( ZyppStatus stat ) const;
89 
90  ZyppObj getDataPointer() const { return dataPointer; }
91  ZyppSel getSelPointer() const { return selPointer; }
92 };
93 
94 
95 class NCPkgTableSort : public NCTableSortStrategyBase {
96 
97 public:
98 
99  NCPkgTableSort( const std::vector<std::string> & head )
100  : _header ( head )
101  { }
102 
103  virtual void sort (
104  std::vector<NCTableLine *>::iterator itemsBegin,
105  std::vector<NCTableLine *>::iterator itemsEnd,
106  int uiColumn
107  )
108  {
109  if ( _header[ uiColumn ] == NCPkgStrings::PkgSize() )
110  {
111  std::sort( itemsBegin, itemsEnd, CompareSize() );
112  }
113  else if ( _header[ uiColumn ] == NCPkgStrings::PkgName() )
114  {
115  std::sort( itemsBegin, itemsEnd, CompareName( uiColumn ) );
116  }
117  else
118  {
119  std::sort( itemsBegin, itemsEnd, Compare( uiColumn ) );
120  }
121  }
122 
123 private:
124  std::vector<std::string> _header;
125 
126  class CompareSize
127  {
128  public:
129  CompareSize ( )
130  {}
131 
132  bool operator() ( NCTableLine * first,
133  NCTableLine * second
134  ) const
135  {
136  YTableItem *firstItem = dynamic_cast<YTableItem*> (first->origItem() );
137  YTableItem *secondItem = dynamic_cast<YTableItem*> (second->origItem() );
138  NCPkgTableTag *firstTag = static_cast<NCPkgTableTag *>( firstItem->cell(0) );
139  NCPkgTableTag *secondTag = static_cast<NCPkgTableTag *>( secondItem->cell(0) );
140 
141  return firstTag->getDataPointer()->installSize() <
142  secondTag->getDataPointer()->installSize();
143  }
144 
145  };
146 
147  class CompareName
148  {
149  public:
150  CompareName( int uiCol)
151  : _uiCol(uiCol)
152  {}
153 
154  bool operator() ( NCTableLine * first,
155  NCTableLine * second
156  ) const
157  {
158  std::wstring w1 = first->GetCol( _uiCol )->Label().getText().begin()->str();
159  std::wstring w2 = second->GetCol( _uiCol )->Label().getText().begin()->str();
160 
161  // It is safe to use collate unaware wscasecmp() here because package names
162  // are 7 bit ASCII only. Better yet, we don't even want this to be sorted
163  // by locale specific rules: "ch" for example would be sorted after "h" in
164  // Czech which in the context of package names (which are English) would be
165  // plain wrong.
166  int result = wcscasecmp( w1.data(), w2.data() );
167 
168  return result < 0;
169  }
170 
171  private:
172  int _uiCol;
173  };
174 
175  class Compare
176  {
177  public:
178  Compare ( int uiCol)
179  : _uiCol (uiCol)
180  {}
181 
182  bool operator() ( NCTableLine * first,
183  NCTableLine * second
184  ) const
185  {
186  std::wstring w1 = first->GetCol( _uiCol )->Label().getText().begin()->str();
187  std::wstring w2 = second->GetCol( _uiCol )->Label().getText().begin()->str();
188  int result = wcscoll ( w1.data(), w2.data() );
189 
190  if ( result < 0 )
191  return true;
192  else
193  return false;
194  }
195  private:
196  int _uiCol;
197  };
198 };
199 
200 /**
201  * The package table class. Provides methods to fill the table,
202  * set the status info and so on.
203  * Has a connection to the PackageSelector which is used to do
204  * changes which affect other widgets.
205  *
206  **/
207 class NCPkgTable : public NCTable {
208 
209 public:
210  enum NCPkgTableType {
211  T_Packages,
212  T_Availables,
213  T_Patches,
214  T_Update,
215  T_PatchPkgs,
216  T_Selections,
217  T_Languages,
218  T_MultiVersion,
219  T_Unknown
220  };
221 
222  enum NCPkgTableListAction {
223  A_Install,
224  A_Delete,
225  A_Keep,
226  A_UpdateNewer,
227  A_Update,
228  A_Unknown
229  };
230 
231  enum NCPkgTableListType {
232  L_Changes,
233  L_Installed,
234  L_Unknown
235  };
236 
237  enum NCPkgTableInfoType {
238  I_Descr,
239  I_Technical,
240  I_Versions,
241  I_Files,
242  I_Deps,
243  I_PatchDescr,
244  I_PatchPkgs
245  };
246 
247 private:
248 
249  NCPkgTable & operator=( const NCPkgTable & );
250  NCPkgTable ( const NCPkgTable & );
251 
252  NCPackageSelector * packager; // connection to the PackageSelector,
253 
254  NCPkgStatusStrategy * statusStrategy; // particular methods to get the status
255 
256  NCPkgTableType tableType; // the type (e.g. table of packages, patches)
257  bool haveInstalledVersion; // for T_Packages and T_Update
258 
259  // returns the first column of line with 'index' (the tag)
260  NCPkgTableTag * getTag ( const int & index );
261 
262  NCPkgTableInfoType visibleInfo;
263 
264  std::vector<std::string> header; // the table header
265 
266 protected:
267 
268 
269 public:
270 
271  /**
272  * Constructor
273  */
274  NCPkgTable( YWidget * parent, YTableHeader * tableHeader );
275 
276  virtual ~NCPkgTable();
277 
278 
279  /**
280  * This method is called to add a line to the package list.
281  * @param status The package status (first column of the table)
282  * @param elements A std::vector<std::string> containing the package data
283  * @param objPtr The pointer to the packagemanager object
284  * @param objPtr The pointer to the selectable object
285  * @return void
286  */
287  virtual void addLine( ZyppStatus status,
288  const std::vector<std::string> & elements,
289  ZyppObj objPtr,
290  ZyppSel slbPtr );
291 
292  /**
293  * Draws the package list (has to be called after the loop with addLine() calls)
294  */
295  void drawList( ) { myPad()->setOrder(1); return DrawPad(); }
296 
297  /**
298  * Clears the package list
299  */
300  virtual void itemsCleared();
301 
302  /**
303  * Changes the contents of a certain cell in table
304  * @param index The table line
305  * @param column The column
306  * @param newtext The new text
307  * @eturn void
308  */
309  virtual void cellChanged( int index, int colnum, const std::string & newtext );
310 
311  /**
312  * Returns the contents of a certain cell in table
313  * @param index The table line
314  * @param column The column
315  * @eturn NClabel
316  */
317  NClabel getCellContents( int index, int colnum );
318 
319  /**
320  * Handles the events concerning the package table (e.g. scroll the list,
321  * change the package status, ...)
322  * @param key The key which is pressed
323  * @return NCursesEvent
324  */
325  virtual NCursesEvent wHandleInput( wint_t key );
326 
327  /**
328  * Sets the member variable PackageSelector *packager
329  * @param pkg The PackageSelector pointer
330  * @return void
331  */
332  void setPackager( NCPackageSelector * pkg ) { packager = pkg; }
333 
334  /**
335  * Informs the package manager about the status change of
336  * the currently selected package and updates the states
337  * of all packages in the list
338  * @param newstat The new status
339  * @param slbPtr The pointer to the object to change
340  * @param objPtr is candidatePtr or what the user selected instead of it.
341  * @return bool
342  */
343  bool changeStatus( ZyppStatus newstat,
344  const ZyppSel & slbPtr,
345  ZyppObj objPtr,
346  bool singleChange );
347 
348  bool changeObjStatus( int key );
349 
350  bool changeListObjStatus( NCPkgTableListAction key );
351 
352  bool toggleObjStatus( );
353 
354  /**
355  * Set the status information if status has changed
356  * @return bool
357  */
358  bool updateTable();
359 
360  /**
361  * Gets the currently displayed package status.
362  * @param index The index in package table (the line)
363  * @return ZyppStatus
364  */
365  ZyppStatus getStatus( int index );
366 
367 #ifdef FIXME
368  /**
369  * Toggles the installation of the source package.
370  * @param install
371  * @return bool
372  */
373  bool SourceInstall( bool install );
374 #endif
375 
376  /**
377  * Sets the type of the table and the status strategy (which means call particular methods
378  * to set/get the status for different zypp::ResObjects (zypp::Patch, zypp::Package or available zypp::Package)
379  * @param type The type (see enum NCPkgTableType)
380  * @param strategy The certain strategy (available strategies see NCPkgStatusStrategy.h).
381  * Has to be allocated with new - is deleted by NCPkgTable.
382  * @return bool
383  */
384  bool setTableType( NCPkgTableType type, NCPkgStatusStrategy * strategy ) {
385  if ( !strategy )
386  return false;
387 
388  delete statusStrategy;
389  statusStrategy = strategy;
390  tableType = type;
391 
392  return true;
393  }
394 
395  NCPkgTableType getTableType() { return tableType; }
396 
397  /**
398  * Gets the data pointer of a certain package.
399  * @param index The index in package table (the line)
400  * @return ZyppObj
401  */
402  ZyppObj getDataPointer( int index );
403 
404  /**
405  * Gets the selectable pointer of a certain package.
406  * @param index The index in package table (the line)
407  * @return ZyppSel
408  */
409  ZyppSel getSelPointer( int index );
410 
411  /**
412  * Returns the number of lines in the table (the table size)
413  * @return unsigned int
414  */
415  unsigned int getNumLines( ) { return myPad()->Lines(); }
416 
417  /**
418  * Fills the header of the table
419  * @return void
420  */
421  void fillHeader( );
422 
423  /**
424  * Creates a line in the package table.
425  * @param pkgPtr The package pointer
426  * @param slbPtr The selectable pointer
427  * @return bool
428  */
429  bool createListEntry ( ZyppPkg pkgPtr, ZyppSel slbPtr );
430 
431  /**
432  * Creates a line in the YOU patch table.
433  * @param pkgPtr The YOU patch pointer
434  * @return bool
435  */
436  bool createPatchEntry ( ZyppPatch pkgPtr, ZyppSel slbPtr );
437 
438  /**
439  * Creates a line in the table shwing an info text.
440  * @param text The information
441  * @return bool
442  */
443  bool createInfoEntry ( std::string text );
444 
445  /**
446  * Show the corresponding information (e.g. the package description).
447  * @return bool
448  */
449  bool showInformation ( );
450 
451  void setVisibleInfo( NCPkgTableInfoType info) { visibleInfo = info; }
452 
453  NCPkgTableInfoType VisibleInfo() { return visibleInfo ; }
454 
455  bool fillAvailableList ( ZyppSel slb );
456  bool fillSummaryList ( NCPkgTableListType type );
457 
458  void updateInfo( ZyppObj pkgPtr, ZyppSel slbPtr, NCPkgTableInfoType mode );
459 
460 };
461 
462 ///////////////////////////////////////////////////////////////////
463 
464 #endif // NCPkgTable_h
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 createPatchEntry(ZyppPatch pkgPtr, ZyppSel slbPtr)
Creates a line in the YOU patch table.
Definition: NCPkgTable.cc:723
virtual void cellChanged(int index, int colnum, const std::string &newtext)
Changes the contents of a certain cell in table.
Definition: NCPkgTable.cc:198
The package table class.
Definition: NCPkgTable.h:207
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
bool createListEntry(ZyppPkg pkgPtr, ZyppSel slbPtr)
Creates a line in the package table.
Definition: NCPkgTable.cc:550
This class is used for the first column of the package table which contains the status information of...
Definition: NCPkgTable.h:68
bool createInfoEntry(std::string text)
Creates a line in the table shwing an info text.
Definition: NCPkgTable.cc:704
ZyppStatus getStatus(int index)
Gets the currently displayed package status.
Definition: NCPkgTable.cc:864
ZyppSel getSelPointer(int index)
Gets the selectable pointer of a certain package.
Definition: NCPkgTable.cc:884
bool updateTable()
Set the status information if status has changed.
Definition: NCPkgTable.cc:369
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:384
unsigned int getNumLines()
Returns the number of lines in the table (the table size)
Definition: NCPkgTable.h:415
virtual void itemsCleared()
Clears the package list.
Definition: NCPkgTable.cc:184
NClabel getCellContents(int index, int colnum)
Returns the contents of a certain cell in table.
void drawList()
Draws the package list (has to be called after the loop with addLine() calls)
Definition: NCPkgTable.h:295
void setPackager(NCPackageSelector *pkg)
Sets the member variable PackageSelector *packager.
Definition: NCPkgTable.h:332
bool changeStatus(ZyppStatus newstat, const ZyppSel &slbPtr, ZyppObj objPtr, bool singleChange)
Informs the package manager about the status change of the currently selected package and updates the...
Definition: NCPkgTable.cc:212