libyui-gtk-pkg  2.42.9
 All Classes
yzyppwrapper.h
1 /********************************************************************
2  * YaST2-GTK - http://en.opensuse.org/YaST2-GTK *
3  ********************************************************************/
4 
5 /* Simplifies, unifies and extends libzypp's API.
6 
7  Several classes are available to wrap around common Zypp objects
8  in order to simplify or extend them. These methods can be interwined
9  with direct libzypp use, but you should call Ypp::notifySelModified()
10  to broadcast any change to a selectable.
11 
12  Use Ypp::QueryPool to iterate through the Selectable pool.
13  It extends zypp::PoolQuery by adding the possibility to add
14  custom criterias for filtering, to be apply endogenously.
15 
16  If you need to iterate through it several times, you can create
17  a Ypp::List out of it. This random-access list can be manually
18  manipulated with several available methods such as for sorting.
19  The list is ref-counted so you can easily and freely hold it at
20  several widgets.
21  In order to inspect several common properties of a group of packages,
22  pass a list to Ypp::ListProps.
23 
24  Usage example (unlock all packages):
25 
26  Ypp::PoolQuery query (Ypp::Selectable::PACKAGE_TYPE);
27  query.addCriteria (new Ypp::StatusMatch (Ypp::StatusMatch::IS_LOCKED));
28 
29  Ypp::List list (query);
30  list.unlock();
31 
32  You are advised to register an Ypp::Interface implementation as some
33  transactions are bound by user decisions. Call Ypp::init() and
34  Ypp::finish() when you begin or are done (respectively) using these
35  methods.
36  Use Ypp::addSelListener() to be notified of any 'selectable' change.
37 */
38 
39 #ifndef ZYPP_WRAPPER_H
40 #define ZYPP_WRAPPER_H
41 
42 #include <zypp/ZYppFactory.h>
43 #include <zypp/ResObject.h>
44 #include <zypp/ResKind.h>
45 #include <zypp/ResPoolProxy.h>
46 #include <zypp/PoolQuery.h>
47 #include <zypp/ui/Selectable.h>
48 #include <zypp/Patch.h>
49 #include <zypp/Package.h>
50 #include <zypp/Pattern.h>
51 #include <zypp/Product.h>
52 #include <zypp/Repository.h>
53 #include <zypp/RepoManager.h>
54 #include <zypp/sat/LocaleSupport.h>
55 #include <string>
56 #include <list>
57 #include "yzypptags.h"
58 
59 typedef zypp::ResPool _ZyppPool;
60 typedef zypp::ResPoolProxy ZyppPool;
61 inline ZyppPool zyppPool() { return zypp::getZYpp()->poolProxy(); }
62 inline _ZyppPool _zyppPool() { return zypp::getZYpp()->pool(); }
63 typedef zypp::ResObject::constPtr ZyppResObject;
64 typedef zypp::ResObject* ZyppResObjectPtr;
65 typedef zypp::ui::Selectable::Ptr ZyppSelectable;
66 typedef zypp::ui::Selectable* ZyppSelectablePtr;
67 typedef zypp::Package::constPtr ZyppPackage;
68 typedef zypp::Patch::constPtr ZyppPatch;
69 typedef zypp::Pattern::constPtr ZyppPattern;
70 typedef zypp::Repository ZyppRepository;
71 typedef zypp::PoolQuery ZyppQuery;
72 typedef zypp::sat::SolvAttr ZyppAttribute;
73 typedef zypp::ByteCount Size_t;
74 typedef zypp::DiskUsageCounter::MountPoint ZyppDu;
75 typedef zypp::DiskUsageCounter::MountPointSet ZyppDuSet;
76 
77 inline ZyppPackage castZyppPackage (ZyppResObject obj)
78 { return zypp::dynamic_pointer_cast <const zypp::Package> (obj); }
79 inline ZyppPatch castZyppPatch (ZyppResObject obj)
80 { return zypp::dynamic_pointer_cast <const zypp::Patch> (obj); }
81 inline ZyppPattern castZyppPattern (ZyppResObject obj)
82 { return zypp::dynamic_pointer_cast <const zypp::Pattern> (obj); }
83 
84 namespace Ypp
85 {
86  struct List;
87 
88  // Repositories setup
89 
90  struct Repository {
91  // merges zypp::Repository and zypp::RepoInfo -- the first are
92  // the actual repository structure (which includes only the
93  // enabled ones), the others are from the setup file.
94 
95  Repository (zypp::Repository repo);
96  Repository (zypp::RepoInfo repo);
97  std::string name();
98  std::string url();
99  bool enabled();
100  bool isOutdated();
101  bool isSystem();
102 
103  bool operator == (const Repository &other) const;
104 
105  ZyppRepository &zyppRepo() { return m_repo; }
106 
107  private:
108  ZyppRepository m_repo;
109  zypp::RepoInfo m_repo_info;
110  bool m_onlyInfo;
111  };
112 
113  void getRepositoryFromAlias (const std::string &alias,
114  std::string &name, std::string &url);
115 
116  // Selectable & complementory structs
117 
118  struct Version {
119  Version (ZyppResObject zobj);
120 
121  int type(); // Ypp::Selectable::Type
122 
123  std::string number();
124  std::string arch();
125  Repository repository();
126 
127  Size_t size();
128  Size_t downloadSize();
129 
130  bool isInstalled();
131  bool toModify();
132 
133  bool operator < (Version &other);
134  bool operator > (Version &other);
135  bool operator == (Version &other);
136 
137  ZyppResObject zyppObj() { return m_zobj; }
138 
139  private:
140  ZyppResObject m_zobj;
141  };
142 
143  struct Selectable {
144  enum Type {
145  PACKAGE, PATTERN, LANGUAGE, PATCH, ALL
146  };
147  static zypp::ResKind asKind (Type type);
148  static Type asType (zypp::ResKind kind);
149 
150  Selectable (ZyppSelectable sel);
151  Selectable (zypp::Locale locale);
152 
153  Type type();
154  std::string name();
155  std::string summary();
156  std::string description (bool as_html);
157 
158  bool visible();
159 
160  bool isInstalled();
161  bool hasUpgrade();
162  bool isLocked();
163 
164  bool toInstall();
165  bool toRemove();
166  bool toModify();
167  bool toModifyAuto();
168 
169  void install(); // installs candidate
170  void remove();
171  void undo();
172  void lock (bool lock);
173 
174  bool canRemove();
175  bool canLock();
176 
177  int totalVersions();
178  Version version (int n);
179 
180  bool hasCandidateVersion();
181  Version candidate();
182  void setCandidate (Version &version);
183 
184  bool hasInstalledVersion();
185  Version installed();
186  Version anyVersion();
187 
188  bool operator == (const Selectable &other) const;
189  bool operator != (const Selectable &other) const;
190 
191  ZyppSelectable zyppSel() { return m_sel; }
192  zypp::Locale zyppLocale() { return m_locale; }
193 
194  private:
195  Type m_type;
196  ZyppSelectable m_sel;
197  zypp::Locale m_locale;
198  };
199 
200  struct Collection {
201  Collection (Selectable &sel);
202  bool contains (Selectable &sel);
203  void stats (int *installed, int *total);
204 
205  Ypp::List *getContent(); // cached
206 
207  Selectable &asSelectable() { return m_sel; }
208 
209  private:
210  Selectable m_sel;
211  };
212 
213  struct Package {
214  Package (Selectable &sel);
215  int support();
216  static int supportTotal();
217  static std::string supportSummary (int support);
218  static std::string supportDescription (int support);
219 
220  std::string url();
221 
222  YPkgGroupEnum group();
223  std::string rpm_group();
224 
225  bool isCandidatePatch();
226  Selectable getCandidatePatch();
227 
228  private:
229  Selectable m_sel;
230  };
231 
232  struct Patch {
233  Patch (Selectable &sel);
234  int priority();
235  static int priorityTotal();
236  static const char *prioritySummary (int priority);
237  static const char *priorityIcon (int priority);
238 
239  private:
240  Selectable m_sel;
241  };
242 
243  struct SelListener {
244  // a selectable modification has ripple effects; we don't bother
245  // detecting them
246  virtual void selectableModified() = 0;
247  };
248 
249  void addSelListener (SelListener *listener);
250  void removeSelListener (SelListener *listener);
251 
252  // you shouldn't need to call notifySelModified() directly, but instead call
253  // e.g. runSolver()
254  void notifySelModified();
255 
256  struct Problem {
257  std::string description, details;
258  struct Solution {
259  std::string description, details;
260  bool apply;
261  void *impl;
262  };
263  Solution *getSolution (int nb);
264  void *impl;
265  };
266 
267  // runSolver() gets called automatically when you install/remove/... a
268  // a package using the Ypp::Selectable API
269  bool runSolver (bool force = false); // returns whether succesful
270  void setEnableSolver (bool enabled); // true by default
271  bool isSolverEnabled();
272  bool showPendingLicenses (Ypp::Selectable::Type type);
273 
274  // temporarily suspends run-solver while installing/removing a few packages at a time
275  // -- used by Ypp::List
276  void startTransactions();
277  bool finishTransactions(); // returns return of runSolver()
278 
279  struct Interface {
280  virtual bool showLicense (Selectable &sel, const std::string &license) = 0;
281  virtual bool showMessage (Selectable &sel, const std::string &message) = 0;
282  // resolveProblems = false to cancel the action that had that effect
283  virtual bool resolveProblems (const std::list <Problem *> &problems) = 0;
284  };
285 
286  void setInterface (Interface *interface);
287  Interface *getInterface();
288 
289  // Pool selectables
290 
291  struct Match {
292  virtual bool match (Selectable &sel) = 0;
293  virtual ~Match() {}
294  };
295 
296  struct StrMatch : public Match {
297  // exclusive match -- zypp only supports or'ed strings
298  // you can combine attributes (inclusive)
299  enum Attribute {
300  NAME = 0x1, SUMMARY = 0x2, DESCRIPTION = 0x4
301  }; // use regular zypp methods for other attributes
302  StrMatch (int attrbs);
303  void add (const std::string &str);
304  virtual bool match (Selectable &sel);
305 
306  virtual ~StrMatch();
307  struct Impl;
308  Impl *impl;
309  };
310 
311  struct StatusMatch : public Match {
312  enum Status {
313  IS_INSTALLED, NOT_INSTALLED, HAS_UPGRADE, IS_LOCKED, TO_MODIFY
314  };
315  StatusMatch (Status status);
316  virtual bool match (Selectable &sel);
317 
318  private: Status m_status;
319  };
320 
321  struct PKGroupMatch : public Match {
322  PKGroupMatch (YPkgGroupEnum group);
323  virtual bool match (Selectable &sel);
324 
325  private: YPkgGroupEnum m_group;
326  };
327 
328  struct RpmGroupMatch : public Match {
329  RpmGroupMatch (const std::string &group);
330  virtual bool match (Selectable &sel);
331 
332  private: std::string m_group;
333  };
334 
335  struct FromCollectionMatch : public Match {
337  virtual bool match (Selectable &sel);
338 
339  private: Collection m_collection;
340  };
341 
342  struct CollectionContainsMatch : public Match {
344  virtual bool match (Selectable &collection);
345 
346  private: Selectable m_contains;
347  };
348 
349  struct SupportMatch : public Match {
350  SupportMatch (int n) : m_n (n) {}
351  virtual bool match (Selectable &sel)
352  { return Package (sel).support() == m_n; }
353  private: int m_n;
354  }; // we should replace some of these stuff by generic properties
355 
356  struct PriorityMatch : public Match {
357  PriorityMatch (int n) : m_n (n) {}
358  virtual bool match (Selectable &sel)
359  { return Patch (sel).priority() == m_n; }
360  private: int m_n;
361  };
362 
363  struct Query {
364  virtual void addCriteria (Match *match) = 0; // to be free'd by Query
365  virtual bool hasNext() = 0;
366  virtual Selectable next() = 0;
367  virtual int guessSize() = 0; // only knows for sure after iterating
368  };
369 
370  struct PoolQuery : public Query {
371  PoolQuery (Selectable::Type type); // for languages, use LangQuery
372  ~PoolQuery();
373 
374  enum MatchType {
375  CONTAINS, EXACT, GLOB, REGEX
376  };
377  enum StringAttribute {
378  NAME, SUMMARY, DESCRIPTION, FILELIST, PROVIDES, REQUIRES,
379  };
380  void setStringMode (bool caseSensitive, MatchType type);
381  void addStringAttribute (StringAttribute attrb);
382  void addStringOr (const std::string &str);
383 
384  void addRepository (Repository &repository);
385 
386  virtual void addCriteria (Match *match); // exclusive
387 
388  virtual bool hasNext(); // only after setup
389  virtual Selectable next();
390  virtual int guessSize();
391 
392  ZyppQuery &zyppQuery();
393  Selectable::Type poolType();
394 
395  struct Impl;
396  Impl *impl;
397  private: // prevent copy
398  PoolQuery (const PoolQuery&); PoolQuery &operator= (const PoolQuery&);
399  };
400 
401  struct LangQuery : public Query {
402  LangQuery();
403  ~LangQuery();
404 
405  virtual void addCriteria (Match *match) {} /* not yet supported */
406  virtual bool hasNext();
407  virtual Selectable next();
408  virtual int guessSize();
409 
410  struct Impl;
411  Impl *impl;
412 
413  private: // prevent copy
414  LangQuery (const LangQuery&); LangQuery &operator= (const LangQuery&);
415  };
416 
417  // Aggregators
418 
419  struct List {
420  List (Query &query);
421  List (int reserve);
422  ~List();
423  List clone();
424 
425  Selectable &get (int index);
426  int size();
427 
428  int count (Match *match);
429  int find (const std::string &name);
430  int find (Selectable &sel);
431 
432  void reserve (int size);
433  void append (Selectable sel);
434 
435  void install();
436  void remove();
437  void lock (bool lock);
438  void undo();
439 
440  enum SortAttribute {
441  IS_INSTALLED_SORT, NAME_SORT, SIZE_SORT, REPOSITORY_SORT, SUPPORT_SORT
442  };
443  void sort (SortAttribute attrb, bool ascendent);
444  void reverse();
445 
446  bool operator == (const Ypp::List &other) const;
447  bool operator != (const Ypp::List &other) const;
448 
449  struct Impl;
450  Impl *impl;
451  List (const List&); // ref-counted
452  List &operator= (const List&);
453  };
454 
455  struct ListProps {
456  ListProps (List &list);
457  ~ListProps();
458 
459  bool isInstalled() const;
460  bool isNotInstalled() const;
461  bool hasUpgrade() const;
462  bool toModify() const;
463  bool isLocked() const;
464  bool isUnlocked() const;
465  bool canRemove() const;
466  bool canLock() const;
467 
468  int isInstalledNb() const;
469  int isNotInstalledNb() const;
470  int hasUpgradeNb() const;
471  int isLockedNb() const;
472  int toModifyNb() const;
473 
474  struct Impl;
475  Impl *impl;
476  private: // prevent copy
477  ListProps (const ListProps&); ListProps &operator= (const ListProps&);
478  };
479 
480  // Disk
481 
482  std::vector <std::string> getPartitionList();
483  const ZyppDu getPartition (const std::string &mount_point);
484 
485  // Misc utilities
486 
487  struct Busy {
488  // pass 0 as size to only show busy cursor
489  Busy (int size);
490  ~Busy();
491  void inc();
492 
493  struct Impl;
494  Impl *impl;
495  private: // prevent copy
496  Busy (const Busy&); Busy &operator= (const Busy&);
497  };
498 
499  struct BusyListener {
500  virtual void loading (float progress) = 0;
501  };
502 
503  void setBusyListener (BusyListener *listener);
504 
505  void init(); // ensures the floor is clean
506  void finish(); // ensures the floor is clean
507  bool isModified(); // anything changed?
508 };
509 
510 #endif /*ZYPP_WRAPPER_H*/
511