libyui-ncurses-pkg  2.50.8
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 ( std::vector<NCTableLine *>::iterator itemsBegin,
104  std::vector<NCTableLine *>::iterator itemsEnd ) override
105  {
106  if ( _header[ getColumn() ] == NCPkgStrings::PkgSize() )
107  {
108  std::sort( itemsBegin, itemsEnd, CompareSize() );
109  }
110  else if ( _header[ getColumn() ] == NCPkgStrings::PkgName() )
111  {
112  std::sort( itemsBegin, itemsEnd, CompareName( getColumn() ) );
113  }
114  else
115  {
116  std::sort( itemsBegin, itemsEnd, Compare( getColumn() ) );
117  }
118 
119  if ( isReverse() )
120  std::reverse( itemsBegin, itemsEnd );
121  }
122 
123 private:
124  std::vector<std::string> _header;
125 
126 
127  class CompareSize
128  {
129  public:
130  CompareSize()
131  {}
132 
133  bool operator() ( const NCTableLine * first,
134  const NCTableLine * second ) const
135  {
136  const YTableItem *firstItem = dynamic_cast<const YTableItem*> (first->origItem() );
137  const YTableItem *secondItem = dynamic_cast<const YTableItem*> (second->origItem() );
138  const NCPkgTableTag *firstTag = static_cast<const NCPkgTableTag *>( firstItem->cell(0) );
139  const NCPkgTableTag *secondTag = static_cast<const 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() ( const NCTableLine * first,
155  const NCTableLine * second ) const
156  {
157  std::wstring w1 = first->GetCol( _uiCol )->Label().getText().begin()->str();
158  std::wstring w2 = second->GetCol( _uiCol )->Label().getText().begin()->str();
159 
160  // It is safe to use collate unaware wscasecmp() here because package names
161  // are 7 bit ASCII only. Better yet, we don't even want this to be sorted
162  // by locale specific rules: "ch" for example would be sorted after "h" in
163  // Czech which in the context of package names (which are English) would be
164  // plain wrong.
165  int result = wcscasecmp( w1.data(), w2.data() );
166 
167  return result < 0;
168  }
169 
170  private:
171  const int _uiCol;
172  };
173 
174 
175  class Compare
176  {
177  public:
178  Compare ( int uiCol)
179  : _uiCol (uiCol)
180  {}
181 
182  bool operator() ( const NCTableLine * first,
183  const NCTableLine * second ) const
184  {
185  std::wstring w1 = first->GetCol( _uiCol )->Label().getText().begin()->str();
186  std::wstring w2 = second->GetCol( _uiCol )->Label().getText().begin()->str();
187 
188  int result = wcscoll ( w1.data(), w2.data() );
189 
190  return result < 0;
191  }
192 
193  private:
194  const int _uiCol;
195  };
196 };
197 
198 /**
199  * The package table class. Provides methods to fill the table,
200  * set the status info and so on.
201  * Has a connection to the PackageSelector which is used to do
202  * changes which affect other widgets.
203  *
204  **/
205 class NCPkgTable : public NCTable
206 {
207 public:
208 
209  enum NCPkgTableType
210  {
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  {
224  A_Install,
225  A_Delete,
226  A_Keep,
227  A_UpdateNewer,
228  A_Update,
229  A_Unknown
230  };
231 
232  enum NCPkgTableListType
233  {
234  L_Changes,
235  L_Installed,
236  L_Unknown
237  };
238 
239  enum NCPkgTableInfoType
240  {
241  I_Descr,
242  I_Technical,
243  I_Versions,
244  I_Files,
245  I_Deps,
246  I_PatchDescr,
247  I_PatchPkgs
248  };
249 
250 private:
251 
252  NCPkgTable & operator=( const NCPkgTable & );
253  NCPkgTable ( const NCPkgTable & );
254 
255  NCPackageSelector * packager; // connection to the PackageSelector,
256 
257  NCPkgStatusStrategy * statusStrategy; // particular methods to get the status
258 
259  NCPkgTableType tableType; // the type (e.g. table of packages, patches)
260  bool haveInstalledVersion; // for T_Packages and T_Update
261 
262  // returns the first column of line with 'index' (the tag)
263  NCPkgTableTag * getTag ( const int & index );
264 
265  NCPkgTableInfoType visibleInfo;
266 
267  std::vector<std::string> header; // the table header
268 
269 
270 public:
271 
272  /**
273  * Constructor
274  */
275  NCPkgTable( YWidget * parent, YTableHeader * tableHeader );
276 
277  virtual ~NCPkgTable();
278 
279 
280  /**
281  * This method is called to add a line to the package list.
282  * @param status The package status (first column of the table)
283  * @param elements A std::vector<std::string> containing the package data
284  * @param objPtr The pointer to the packagemanager object
285  * @param objPtr The pointer to the selectable object
286  * @return void
287  */
288  virtual void addLine( ZyppStatus status,
289  const std::vector<std::string> & elements,
290  ZyppObj objPtr,
291  ZyppSel slbPtr );
292 
293  /**
294  * Draws the package list (has to be called after the loop with addLine() calls)
295  */
296  void drawList() { myPad()->setOrder(1); return DrawPad(); }
297 
298  /**
299  * Clears the package list
300  */
301  virtual void itemsCleared();
302 
303  /**
304  * Changes the contents of a certain cell in table
305  * @param index The table line
306  * @param column The column
307  * @param newtext The new text
308  * @eturn void
309  */
310  virtual void cellChanged( int index, int colnum, const std::string & newtext );
311 
312  /**
313  * Returns the contents of a certain cell in table
314  * @param index The table line
315  * @param column The column
316  * @eturn NClabel
317  */
318  NClabel getCellContents( int index, int colnum );
319 
320  /**
321  * Handles the events concerning the package table (e.g. scroll the list,
322  * change the package status, ...)
323  * @param key The key which is pressed
324  * @return NCursesEvent
325  */
326  virtual NCursesEvent wHandleInput( wint_t key );
327 
328  /**
329  * Sets the member variable PackageSelector *packager
330  * @param pkg The PackageSelector pointer
331  * @return void
332  */
333  void setPackager( NCPackageSelector * pkg ) { packager = pkg; }
334 
335  /**
336  * Informs the package manager about the status change of
337  * the currently selected package and updates the states
338  * of all packages in the list
339  * @param newstat The new status
340  * @param slbPtr The pointer to the object to change
341  * @param objPtr is candidatePtr or what the user selected instead of it.
342  * @return bool
343  */
344  bool changeStatus( ZyppStatus newstat,
345  const ZyppSel & slbPtr,
346  ZyppObj objPtr,
347  bool singleChange );
348 
349  bool changeObjStatus( int key );
350 
351  bool changeListObjStatus( NCPkgTableListAction key );
352 
353  bool cycleObjStatus();
354 
355  /**
356  * Set the status information if status has changed
357  * @return bool
358  */
359  bool updateTable();
360 
361  /**
362  * Gets the currently displayed package status.
363  * @param index The index in package table (the line)
364  * @return ZyppStatus
365  */
366  ZyppStatus getStatus( int index );
367 
368 #ifdef FIXME
369  /**
370  * Toggles the installation of the source package.
371  * @param install
372  * @return bool
373  */
374  bool SourceInstall( bool install );
375 #endif
376 
377  /**
378  * Sets the type of the table and the status strategy (which means call particular methods
379  * to set/get the status for different zypp::ResObjects (zypp::Patch, zypp::Package or available zypp::Package)
380  * @param type The type (see enum NCPkgTableType)
381  * @param strategy The certain strategy (available strategies see NCPkgStatusStrategy.h).
382  * Has to be allocated with new - is deleted by NCPkgTable.
383  * @return bool
384  */
385  bool setTableType( NCPkgTableType type, NCPkgStatusStrategy * strategy )
386  {
387  if ( !strategy )
388  return false;
389 
390  delete statusStrategy;
391  statusStrategy = strategy;
392  tableType = type;
393 
394  return true;
395  }
396 
397  NCPkgTableType getTableType() { return tableType; }
398 
399  /**
400  * Gets the data pointer of a certain package.
401  * @param index The index in package table (the line)
402  * @return ZyppObj
403  */
404  ZyppObj getDataPointer( int index );
405 
406  /**
407  * Gets the selectable pointer of a certain package.
408  * @param index The index in package table (the line)
409  * @return ZyppSel
410  */
411  ZyppSel getSelPointer( int index );
412 
413  /**
414  * Returns the number of lines in the table (the table size)
415  * @return unsigned int
416  */
417  unsigned int getNumLines() { return myPad()->Lines(); }
418 
419  /**
420  * Fills the header of the table
421  * @return void
422  */
423  void fillHeader();
424 
425  /**
426  * Creates a line in the package table.
427  * @param pkgPtr The package pointer
428  * @param slbPtr The selectable pointer
429  * @return bool
430  */
431  bool createListEntry ( ZyppPkg pkgPtr, ZyppSel slbPtr );
432 
433  /**
434  * Creates a line in the YOU patch table.
435  * @param pkgPtr The YOU patch pointer
436  * @return bool
437  */
438  bool createPatchEntry ( ZyppPatch pkgPtr, ZyppSel slbPtr );
439 
440  /**
441  * Creates a line in the table shwing an info text.
442  * @param text The information
443  * @return bool
444  */
445  bool createInfoEntry ( std::string text );
446 
447  /**
448  * Show the corresponding information (e.g. the package description).
449  * @return bool
450  */
451  bool showInformation();
452 
453  /**
454  * Ask the user for confirmation of installing a retracted package.
455  * This returns 'true' if the user confirmed, 'false' if not.
456  **/
457  bool confirmRetracted( ZyppObj pkg, ZyppSel sel );
458 
459  void setVisibleInfo( NCPkgTableInfoType info) { visibleInfo = info; }
460 
461  NCPkgTableInfoType VisibleInfo() { return visibleInfo; }
462 
463  bool fillAvailableList ( ZyppSel slb );
464  bool fillSummaryList ( NCPkgTableListType type );
465 
466  void updateInfo( ZyppObj pkgPtr, ZyppSel slbPtr, NCPkgTableInfoType mode );
467 
468 };
469 
470 ///////////////////////////////////////////////////////////////////
471 
472 #endif // NCPkgTable_h
virtual NCursesEvent wHandleInput(wint_t key)
Handles the events concerning the package table (e.g.
Definition: NCPkgTable.cc:763
bool showInformation()
Show the corresponding information (e.g.
Definition: NCPkgTable.cc:731
bool createPatchEntry(ZyppPatch pkgPtr, ZyppSel slbPtr)
Creates a line in the YOU patch table.
Definition: NCPkgTable.cc:696
virtual void cellChanged(int index, int colnum, const std::string &newtext)
Changes the contents of a certain cell in table.
Definition: NCPkgTable.cc:165
The package table class.
Definition: NCPkgTable.h:205
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:140
ZyppObj getDataPointer(int index)
Gets the data pointer of a certain package.
Definition: NCPkgTable.cc:832
void fillHeader()
Fills the header of the table.
Definition: NCPkgTable.cc:409
bool createListEntry(ZyppPkg pkgPtr, ZyppSel slbPtr)
Creates a line in the package table.
Definition: NCPkgTable.cc:516
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:681
ZyppStatus getStatus(int index)
Gets the currently displayed package status.
Definition: NCPkgTable.cc:822
ZyppSel getSelPointer(int index)
Gets the selectable pointer of a certain package.
Definition: NCPkgTable.cc:842
bool updateTable()
Set the status information if status has changed.
Definition: NCPkgTable.cc:336
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:385
unsigned int getNumLines()
Returns the number of lines in the table (the table size)
Definition: NCPkgTable.h:417
virtual void itemsCleared()
Clears the package list.
Definition: NCPkgTable.cc:159
bool confirmRetracted(ZyppObj pkg, ZyppSel sel)
Ask the user for confirmation of installing a retracted package.
Definition: NCPkgTable.cc:1194
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:296
void setPackager(NCPackageSelector *pkg)
Sets the member variable PackageSelector *packager.
Definition: NCPkgTable.h:333
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:174