libyui-ncurses-pkg  2.50.10
NCPkgTable.h
1 /****************************************************************************
2 |
3 | Copyright (c) [2002-2011] Novell, Inc.
4 | Copyright (C) 2020 SUSE LLC
5 | All Rights Reserved.
6 |
7 | This program is free software; you can redistribute it and/or
8 | modify it under the terms of version 2 of the GNU General Public License as
9 | published by the Free Software Foundation.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program; if not, contact Novell, Inc.
18 |
19 | To contact Novell about this file by physical or electronic mail,
20 | you may find current contact information at www.novell.com
21 |
22 |***************************************************************************/
23 
24 
25 /*---------------------------------------------------------------------\
26 | |
27 | __ __ ____ _____ ____ |
28 | \ \ / /_ _/ ___|_ _|___ \ |
29 | \ V / _` \___ \ | | __) | |
30 | | | (_| |___) || | / __/ |
31 | |_|\__,_|____/ |_| |_____| |
32 | |
33 | core system |
34 | (C) SuSE GmbH |
35 \----------------------------------------------------------------------/
36 
37  File: NCPkgTable.h
38 
39  Author: Gabriele Strattner <gs@suse.de>
40 
41 /-*/
42 #ifndef NCPkgTable_h
43 #define NCPkgTable_h
44 
45 #include <iosfwd>
46 
47 #include "NCPadWidget.h"
48 #include "NCTablePad.h"
49 #include "NCTable.h"
50 #include "NCTableItem.h"
51 #include "NCPkgStrings.h"
52 
53 #include <map>
54 #include <string>
55 #include <utility> // for STL std::pair
56 
57 #include <zypp/ui/Selectable.h>
58 
59 #include "NCPkgStatusStrategy.h"
60 
61 
62 class NCPackageSelector;
63 
64 /**
65  * This class is used for the first column of the package table
66  * which contains the status information of the package (installed,
67  * not installed, to be deleted and so on).
68  **/
69 class NCPkgTableTag : public YTableCell
70 {
71 private:
72 
73  ZyppStatus status;
74  ZyppObj dataPointer;
75  // cannot get at it from dataPointer
76  ZyppSel selPointer;
77 
78 public:
79 
80  NCPkgTableTag( ZyppObj pkgPtr,
81  ZyppSel selPtr,
82  ZyppStatus stat = S_NoInst );
83 
84  ~NCPkgTableTag() {}
85 
86  void setStatus( ZyppStatus stat ) { status = stat; }
87  ZyppStatus getStatus() const { return status; }
88  // returns the corresponding std::string value to given package status
89  std::string statusToString( ZyppStatus stat ) const;
90 
91  ZyppObj getDataPointer() const { return dataPointer; }
92  ZyppSel getSelPointer() const { return selPointer; }
93 };
94 
95 
96 class NCPkgTableSort : public NCTableSortStrategyBase
97 {
98 public:
99 
100  NCPkgTableSort( const std::vector<std::string> & head )
101  : _header( head )
102  {}
103 
104  virtual void sort( YItemIterator itemsBegin,
105  YItemIterator itemsEnd ) override
106  {
107  if ( _header[ sortCol() ] == NCPkgStrings::PkgSize() )
108  {
109  std::sort( itemsBegin, itemsEnd, CompareSize() );
110  }
111  else if ( _header[ sortCol() ] == NCPkgStrings::PkgName() )
112  {
113  std::sort( itemsBegin, itemsEnd, CompareName( sortCol() ) );
114  }
115  else
116  {
117  std::sort( itemsBegin, itemsEnd, Compare( sortCol() ) );
118  }
119 
120  if ( reverse() )
121  std::reverse( itemsBegin, itemsEnd );
122  }
123 
124 private:
125 
126  std::vector<std::string> _header;
127 
128 
129  /**
130  * Return the content of column no. 'col' for an item.
131  **/
132  static std::wstring cellContent( YItem * item, int col )
133  {
134  std::wstring empty;
135 
136  if ( ! item )
137  return empty;
138 
139  YTableItem * tableItem = dynamic_cast<YTableItem *>( item );
140 
141  if ( ! tableItem )
142  return empty;
143 
144  YTableCell * tableCell = tableItem->cell( col );
145 
146  if ( ! tableCell )
147  return empty;
148 
149  return NCstring( tableCell->label() ).str();
150  }
151 
152 
153  class CompareSize
154  {
155  public:
156  CompareSize()
157  {}
158 
159  bool operator() ( YItem * item1, YItem * item2 ) const
160  {
161  YTableItem * tableItem1 = dynamic_cast<YTableItem *>( item1 );
162  YTableItem * tableItem2 = dynamic_cast<YTableItem *>( item2 );
163 
164  if ( ! tableItem1 ) return true;
165  if ( ! tableItem2 ) return true;
166 
167  const NCPkgTableTag * tag1 = static_cast<const NCPkgTableTag *>( tableItem1->cell(0) );
168  const NCPkgTableTag * tag2 = static_cast<const NCPkgTableTag *>( tableItem2->cell(0) );
169 
170  return tag1->getDataPointer()->installSize() <
171  tag2->getDataPointer()->installSize();
172  }
173  };
174 
175 
176  class CompareName
177  {
178  public:
179  CompareName( int uiCol )
180  : _uiCol( uiCol )
181  {}
182 
183  bool operator() ( YItem * item1, YItem * item2 ) const
184  {
185  std::wstring w1 = cellContent( item1, _uiCol );
186  std::wstring w2 = cellContent( item2, _uiCol );
187 
188  // It is safe to use collate unaware wscasecmp() here because package names
189  // are 7 bit ASCII only. Better yet, we don't even want this to be sorted
190  // by locale specific rules: "ch" for example would be sorted after "h" in
191  // Czech which in the context of package names (which are English) would be
192  // plain wrong.
193  int result = wcscasecmp( w1.data(), w2.data() );
194 
195  return result < 0;
196  }
197 
198  private:
199  const int _uiCol;
200  };
201 
202 
203  class Compare
204  {
205  public:
206  Compare( int uiCol )
207  : _uiCol( uiCol )
208  {}
209 
210  bool operator() ( YItem * item1, YItem * item2 ) const
211  {
212  std::wstring w1 = cellContent( item1, _uiCol );
213  std::wstring w2 = cellContent( item2, _uiCol );
214 
215  int result = wcscoll ( w1.data(), w2.data() );
216 
217  return result < 0;
218  }
219 
220  private:
221  const int _uiCol;
222  };
223 };
224 
225 
226 /**
227  * The package table class. Provides methods to fill the table,
228  * set the status info and so on.
229  * Has a connection to the PackageSelector which is used to do
230  * changes which affect other widgets.
231  *
232  **/
233 class NCPkgTable : public NCTable
234 {
235 public:
236 
237  enum NCPkgTableType
238  {
239  T_Packages,
240  T_Availables,
241  T_Patches,
242  T_Update,
243  T_PatchPkgs,
244  T_Selections,
245  T_Languages,
246  T_MultiVersion,
247  T_Unknown
248  };
249 
250  enum NCPkgTableListAction
251  {
252  A_Install,
253  A_Delete,
254  A_Keep,
255  A_UpdateNewer,
256  A_Update,
257  A_Unknown
258  };
259 
260  enum NCPkgTableListType
261  {
262  L_Changes,
263  L_Installed,
264  L_Unknown
265  };
266 
267  enum NCPkgTableInfoType
268  {
269  I_Descr,
270  I_Technical,
271  I_Versions,
272  I_Files,
273  I_Deps,
274  I_PatchDescr,
275  I_PatchPkgs
276  };
277 
278 private:
279 
280  NCPkgTable & operator=( const NCPkgTable & );
281  NCPkgTable ( const NCPkgTable & );
282 
283  NCPackageSelector * packager; // connection to the PackageSelector,
284 
285  NCPkgStatusStrategy * statusStrategy; // particular methods to get the status
286 
287  NCPkgTableType tableType; // the type (e.g. table of packages, patches)
288  bool haveInstalledVersion; // for T_Packages and T_Update
289 
290  // returns the first column of line with 'index' (the tag)
291  NCPkgTableTag * getTag ( const int & index );
292 
293  NCPkgTableInfoType visibleInfo;
294 
295  std::vector<std::string> header; // the table header
296 
297 
298 public:
299 
300  /**
301  * Constructor
302  */
303  NCPkgTable( YWidget * parent, YTableHeader * tableHeader );
304 
305  virtual ~NCPkgTable();
306 
307 
308  /**
309  * This method is called to add a line to the package list.
310  * @param status The package status (first column of the table)
311  * @param elements A std::vector<std::string> containing the package data
312  * @param objPtr The pointer to the packagemanager object
313  * @param objPtr The pointer to the selectable object
314  * @return void
315  */
316  virtual void addLine( ZyppStatus status,
317  const std::vector<std::string> & elements,
318  ZyppObj objPtr,
319  ZyppSel slbPtr );
320 
321  /**
322  * Draws the package list (has to be called after the loop with addLine() calls)
323  */
324  void drawList() { sortItems( 1 ); return DrawPad(); }
325 
326  /**
327  * Clears the package list
328  */
329  virtual void itemsCleared();
330 
331  /**
332  * Returns the contents of a certain cell in table
333  * @param index The table line
334  * @param column The column
335  * @eturn NClabel
336  */
337  NClabel getCellContents( int index, int colnum );
338 
339  /**
340  * Handles the events concerning the package table (e.g. scroll the list,
341  * change the package status, ...)
342  * @param key The key which is pressed
343  * @return NCursesEvent
344  */
345  virtual NCursesEvent wHandleInput( wint_t key );
346 
347  /**
348  * Sets the member variable PackageSelector *packager
349  * @param pkg The PackageSelector pointer
350  * @return void
351  */
352  void setPackager( NCPackageSelector * pkg ) { packager = pkg; }
353 
354  /**
355  * Informs the package manager about the status change of
356  * the currently selected package and updates the states
357  * of all packages in the list
358  * @param newstat The new status
359  * @param slbPtr The pointer to the object to change
360  * @param objPtr is candidatePtr or what the user selected instead of it.
361  * @return bool
362  */
363  bool changeStatus( ZyppStatus newstat,
364  const ZyppSel & slbPtr,
365  ZyppObj objPtr,
366  bool singleChange );
367 
368  bool changeObjStatus( int key );
369 
370  bool changeListObjStatus( NCPkgTableListAction key );
371 
372  bool cycleObjStatus();
373 
374  /**
375  * Set the status information if status has changed
376  * @return bool
377  */
378  bool updateTable();
379 
380  /**
381  * Gets the currently displayed package status.
382  * @param index The index in package table (the line)
383  * @return ZyppStatus
384  */
385  ZyppStatus getStatus( int index );
386 
387 #ifdef FIXME
388  /**
389  * Toggles the installation of the source package.
390  * @param install
391  * @return bool
392  */
393  bool SourceInstall( bool install );
394 #endif
395 
396  /**
397  * Sets the type of the table and the status strategy (which means call particular methods
398  * to set/get the status for different zypp::ResObjects (zypp::Patch, zypp::Package or available zypp::Package)
399  * @param type The type (see enum NCPkgTableType)
400  * @param strategy The certain strategy (available strategies see NCPkgStatusStrategy.h).
401  * Has to be allocated with new - is deleted by NCPkgTable.
402  * @return bool
403  */
404  bool setTableType( NCPkgTableType type, NCPkgStatusStrategy * strategy )
405  {
406  if ( !strategy )
407  return false;
408 
409  delete statusStrategy;
410  statusStrategy = strategy;
411  tableType = type;
412 
413  return true;
414  }
415 
416  NCPkgTableType getTableType() { return tableType; }
417 
418  /**
419  * Gets the data pointer of a certain package.
420  * @param index The index in package table (the line)
421  * @return ZyppObj
422  */
423  ZyppObj getDataPointer( int index );
424 
425  /**
426  * Gets the selectable pointer of a certain package.
427  * @param index The index in package table (the line)
428  * @return ZyppSel
429  */
430  ZyppSel getSelPointer( int index );
431 
432  /**
433  * Returns the number of lines in the table (the table size)
434  * @return unsigned int
435  */
436  unsigned int getNumLines() { return myPad()->Lines(); }
437 
438  /**
439  * Fills the header of the table
440  * @return void
441  */
442  void fillHeader();
443 
444  /**
445  * Creates a line in the package table.
446  * @param pkgPtr The package pointer
447  * @param slbPtr The selectable pointer
448  * @return bool
449  */
450  bool createListEntry ( ZyppPkg pkgPtr, ZyppSel slbPtr );
451 
452  /**
453  * Creates a line in the YOU patch table.
454  * @param pkgPtr The YOU patch pointer
455  * @return bool
456  */
457  bool createPatchEntry ( ZyppPatch pkgPtr, ZyppSel slbPtr );
458 
459  /**
460  * Creates a line in the table shwing an info text.
461  * @param text The information
462  * @return bool
463  */
464  bool createInfoEntry ( std::string text );
465 
466  /**
467  * Show the corresponding information (e.g. the package description).
468  * @return bool
469  */
470  bool showInformation();
471 
472  /**
473  * Ask the user for confirmation of installing a retracted package.
474  * This returns 'true' if the user confirmed, 'false' if not.
475  **/
476  bool confirmRetracted( ZyppObj pkg, ZyppSel sel );
477 
478  void setVisibleInfo( NCPkgTableInfoType info) { visibleInfo = info; }
479 
480  NCPkgTableInfoType VisibleInfo() { return visibleInfo; }
481 
482  bool fillAvailableList ( ZyppSel slb );
483  bool fillSummaryList ( NCPkgTableListType type );
484 
485  void updateInfo( ZyppObj pkgPtr, ZyppSel slbPtr, NCPkgTableInfoType mode );
486 
487 };
488 
489 ///////////////////////////////////////////////////////////////////
490 
491 #endif // NCPkgTable_h
NCPkgTable::getStatus
ZyppStatus getStatus(int index)
Gets the currently displayed package status.
Definition: NCPkgTable.cc:821
NCPkgTable::getSelPointer
ZyppSel getSelPointer(int index)
Gets the selectable pointer of a certain package.
Definition: NCPkgTable.cc:841
NCPkgTable::getNumLines
unsigned int getNumLines()
Returns the number of lines in the table (the table size)
Definition: NCPkgTable.h:436
NCPkgTable::createListEntry
bool createListEntry(ZyppPkg pkgPtr, ZyppSel slbPtr)
Creates a line in the package table.
Definition: NCPkgTable.cc:509
NCPkgTable::confirmRetracted
bool confirmRetracted(ZyppObj pkg, ZyppSel sel)
Ask the user for confirmation of installing a retracted package.
Definition: NCPkgTable.cc:1191
NCPkgTable::drawList
void drawList()
Draws the package list (has to be called after the loop with addLine() calls)
Definition: NCPkgTable.h:324
NCPkgTableTag
This class is used for the first column of the package table which contains the status information of...
Definition: NCPkgTable.h:70
NCPkgTable::createInfoEntry
bool createInfoEntry(std::string text)
Creates a line in the table shwing an info text.
Definition: NCPkgTable.cc:674
NCPkgTable::updateTable
bool updateTable()
Set the status information if status has changed.
Definition: NCPkgTable.cc:329
NCPkgTable::setTableType
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:404
NCPkgTable::getCellContents
NClabel getCellContents(int index, int colnum)
Returns the contents of a certain cell in table.
NCPkgTableSort
Definition: NCPkgTable.h:97
NCPkgTable::itemsCleared
virtual void itemsCleared()
Clears the package list.
Definition: NCPkgTable.cc:158
NCPkgTable::setPackager
void setPackager(NCPackageSelector *pkg)
Sets the member variable PackageSelector *packager.
Definition: NCPkgTable.h:352
NCPkgTable::wHandleInput
virtual NCursesEvent wHandleInput(wint_t key)
Handles the events concerning the package table (e.g.
Definition: NCPkgTable.cc:762
NCPkgTable::changeStatus
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:167
NCPkgTable::showInformation
bool showInformation()
Show the corresponding information (e.g.
Definition: NCPkgTable.cc:724
NCPkgTable::createPatchEntry
bool createPatchEntry(ZyppPatch pkgPtr, ZyppSel slbPtr)
Creates a line in the YOU patch table.
Definition: NCPkgTable.cc:689
NCPkgTable::getDataPointer
ZyppObj getDataPointer(int index)
Gets the data pointer of a certain package.
Definition: NCPkgTable.cc:831
NCPkgTable
The package table class.
Definition: NCPkgTable.h:234
NCPkgTable::addLine
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
NCPkgTable::fillHeader
void fillHeader()
Fills the header of the table.
Definition: NCPkgTable.cc:402
NCPackageSelector
Definition: NCPackageSelector.h:113
NCPkgStatusStrategy
Definition: NCPkgStatusStrategy.h:53