libyui-ncurses-pkg
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 #include <string>
47 #include <map>
48 #include <utility> // for STL std::pair
49 
50 #include <zypp/ui/Selectable.h>
51 
52 #include <yui/ncurses/NCPadWidget.h>
53 #include <yui/ncurses/NCTablePad.h>
54 #include <yui/ncurses/NCTable.h>
55 #include <yui/ncurses/NCTableItem.h>
56 
57 #include "NCPkgStrings.h"
58 #include "NCPkgStatusStrategy.h"
59 
60 
61 class NCPackageSelector;
62 
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( YItemIterator itemsBegin,
104  YItemIterator itemsEnd ) override
105  {
106  if ( _header[ sortCol() ] == NCPkgStrings::PkgSize() )
107  {
108  std::sort( itemsBegin, itemsEnd, CompareSize() );
109  }
110  else if ( _header[ sortCol() ] == NCPkgStrings::PkgName() )
111  {
112  std::sort( itemsBegin, itemsEnd, CompareName( sortCol() ) );
113  }
114  else
115  {
116  std::sort( itemsBegin, itemsEnd, Compare( sortCol() ) );
117  }
118 
119  if ( reverse() )
120  std::reverse( itemsBegin, itemsEnd );
121  }
122 
123 private:
124 
125  std::vector<std::string> _header;
126 
127 
131  static std::wstring cellContent( YItem * item, int col )
132  {
133  std::wstring empty;
134 
135  if ( ! item )
136  return empty;
137 
138  YTableItem * tableItem = dynamic_cast<YTableItem *>( item );
139 
140  if ( ! tableItem )
141  return empty;
142 
143  YTableCell * tableCell = tableItem->cell( col );
144 
145  if ( ! tableCell )
146  return empty;
147 
148  return NCstring( tableCell->label() ).str();
149  }
150 
151 
152  class CompareSize
153  {
154  public:
155  CompareSize()
156  {}
157 
158  bool operator() ( YItem * item1, YItem * item2 ) const
159  {
160  YTableItem * tableItem1 = dynamic_cast<YTableItem *>( item1 );
161  YTableItem * tableItem2 = dynamic_cast<YTableItem *>( item2 );
162 
163  if ( ! tableItem1 ) return true;
164  if ( ! tableItem2 ) return true;
165 
166  const NCPkgTableTag * tag1 = static_cast<const NCPkgTableTag *>( tableItem1->cell(0) );
167  const NCPkgTableTag * tag2 = static_cast<const NCPkgTableTag *>( tableItem2->cell(0) );
168 
169  return tag1->getDataPointer()->installSize() <
170  tag2->getDataPointer()->installSize();
171  }
172  };
173 
174 
175  class CompareName
176  {
177  public:
178  CompareName( int uiCol )
179  : _uiCol( uiCol )
180  {}
181 
182  bool operator() ( YItem * item1, YItem * item2 ) const
183  {
184  std::wstring w1 = cellContent( item1, _uiCol );
185  std::wstring w2 = cellContent( item2, _uiCol );
186 
187  // It is safe to use collate unaware wscasecmp() here because package names
188  // are 7 bit ASCII only. Better yet, we don't even want this to be sorted
189  // by locale specific rules: "ch" for example would be sorted after "h" in
190  // Czech which in the context of package names (which are English) would be
191  // plain wrong.
192  int result = wcscasecmp( w1.data(), w2.data() );
193 
194  return result < 0;
195  }
196 
197  private:
198  const int _uiCol;
199  };
200 
201 
202  class Compare
203  {
204  public:
205  Compare( int uiCol )
206  : _uiCol( uiCol )
207  {}
208 
209  bool operator() ( YItem * item1, YItem * item2 ) const
210  {
211  std::wstring w1 = cellContent( item1, _uiCol );
212  std::wstring w2 = cellContent( item2, _uiCol );
213 
214  int result = wcscoll ( w1.data(), w2.data() );
215 
216  return result < 0;
217  }
218 
219  private:
220  const int _uiCol;
221  };
222 };
223 
224 
232 class NCPkgTable : public NCTable
233 {
234 public:
235 
236  enum NCPkgTableType
237  {
238  T_Packages,
239  T_Availables,
240  T_Patches,
241  T_Update,
242  T_PatchPkgs,
243  T_Selections,
244  T_Languages,
245  T_MultiVersion,
246  T_Unknown
247  };
248 
249  enum NCPkgTableListAction
250  {
251  A_Install,
252  A_Delete,
253  A_Keep,
254  A_UpdateNewer,
255  A_Update,
256  A_Unknown
257  };
258 
259  enum NCPkgTableListType
260  {
261  L_Changes,
262  L_Installed,
263  L_Unknown
264  };
265 
266  enum NCPkgTableInfoType
267  {
268  I_Descr,
269  I_Technical,
270  I_Versions,
271  I_Files,
272  I_Deps,
273  I_PatchDescr,
274  I_PatchPkgs
275  };
276 
277 private:
278 
279  NCPkgTable & operator=( const NCPkgTable & );
280  NCPkgTable ( const NCPkgTable & );
281 
282  NCPackageSelector * packager; // connection to the PackageSelector,
283 
284  NCPkgStatusStrategy * statusStrategy; // particular methods to get the status
285 
286  NCPkgTableType tableType; // the type (e.g. table of packages, patches)
287  bool haveInstalledVersion; // for T_Packages and T_Update
288 
289  // returns the first column of line with 'index' (the tag)
290  NCPkgTableTag * getTag ( const int & index );
291 
292  NCPkgTableInfoType visibleInfo;
293 
294  std::vector<std::string> header; // the table header
295 
296 
297 public:
298 
302  NCPkgTable( YWidget * parent, YTableHeader * tableHeader );
303 
304  virtual ~NCPkgTable();
305 
306 
315  virtual void addLine( ZyppStatus status,
316  const std::vector<std::string> & elements,
317  ZyppObj objPtr,
318  ZyppSel slbPtr );
319 
323  void drawList() { sortItems( 1 ); return DrawPad(); }
324 
328  virtual void itemsCleared();
329 
336  NClabel getCellContents( int index, int colnum );
337 
344  virtual NCursesEvent wHandleInput( wint_t key );
345 
351  void setPackager( NCPackageSelector * pkg ) { packager = pkg; }
352 
362  bool changeStatus( ZyppStatus newstat,
363  const ZyppSel & slbPtr,
364  ZyppObj objPtr,
365  bool singleChange );
366 
367  bool changeObjStatus( int key );
368 
369  bool changeListObjStatus( NCPkgTableListAction key );
370 
371  bool cycleObjStatus();
372 
377  bool updateTable();
378 
384  ZyppStatus getStatus( int index );
385 
386 #ifdef FIXME
387 
392  bool SourceInstall( bool install );
393 #endif
394 
403  bool setTableType( NCPkgTableType type, NCPkgStatusStrategy * strategy )
404  {
405  if ( !strategy )
406  return false;
407 
408  delete statusStrategy;
409  statusStrategy = strategy;
410  tableType = type;
411 
412  return true;
413  }
414 
415  NCPkgTableType getTableType() { return tableType; }
416 
422  ZyppObj getDataPointer( int index );
423 
429  ZyppSel getSelPointer( int index );
430 
435  unsigned int getNumLines() { return myPad()->Lines(); }
436 
441  void fillHeader();
442 
449  bool createListEntry ( ZyppPkg pkgPtr, ZyppSel slbPtr );
450 
456  bool createPatchEntry ( ZyppPatch pkgPtr, ZyppSel slbPtr );
457 
463  bool createInfoEntry ( std::string text );
464 
469  bool showInformation();
470 
475  bool confirmRetracted( ZyppObj pkg, ZyppSel sel );
476 
477  void setVisibleInfo( NCPkgTableInfoType info) { visibleInfo = info; }
478 
479  NCPkgTableInfoType VisibleInfo() { return visibleInfo; }
480 
481  bool fillAvailableList ( ZyppSel slb );
482  bool fillSummaryList ( NCPkgTableListType type );
483 
484  void updateInfo( ZyppObj pkgPtr, ZyppSel slbPtr, NCPkgTableInfoType mode );
485 
486 };
487 
489 
490 #endif // NCPkgTable_h
virtual NCursesEvent wHandleInput(wint_t key)
Definition: NCPkgTable.cc:766
bool showInformation()
Definition: NCPkgTable.cc:728
bool createPatchEntry(ZyppPatch pkgPtr, ZyppSel slbPtr)
Definition: NCPkgTable.cc:693
Definition: NCPkgTable.h:232
virtual void addLine(ZyppStatus status, const std::vector< std::string > &elements, ZyppObj objPtr, ZyppSel slbPtr)
Definition: NCPkgTable.cc:144
ZyppObj getDataPointer(int index)
Definition: NCPkgTable.cc:835
void fillHeader()
Definition: NCPkgTable.cc:406
Definition: NCPkgStatusStrategy.h:52
bool createListEntry(ZyppPkg pkgPtr, ZyppSel slbPtr)
Definition: NCPkgTable.cc:513
Definition: NCPkgTable.h:68
bool createInfoEntry(std::string text)
Definition: NCPkgTable.cc:678
ZyppStatus getStatus(int index)
Definition: NCPkgTable.cc:825
ZyppSel getSelPointer(int index)
Definition: NCPkgTable.cc:845
bool updateTable()
Definition: NCPkgTable.cc:333
bool setTableType(NCPkgTableType type, NCPkgStatusStrategy *strategy)
Definition: NCPkgTable.h:403
unsigned int getNumLines()
Definition: NCPkgTable.h:435
Definition: NCPkgTable.h:95
Definition: NCPackageSelector.h:109
virtual void itemsCleared()
Definition: NCPkgTable.cc:162
bool confirmRetracted(ZyppObj pkg, ZyppSel sel)
Definition: NCPkgTable.cc:1195
NClabel getCellContents(int index, int colnum)
void drawList()
Definition: NCPkgTable.h:323
void setPackager(NCPackageSelector *pkg)
Definition: NCPkgTable.h:351
bool changeStatus(ZyppStatus newstat, const ZyppSel &slbPtr, ZyppObj objPtr, bool singleChange)
Definition: NCPkgTable.cc:171