- by kind filter reenabled
[platform/upstream/libzypp.git] / zypp / PoolQuery.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PoolQuery.h
10  *
11 */
12 #ifndef ZYPP_POOLQUERY_H
13 #define ZYPP_POOLQUERY_H
14
15 #include "zypp/ui/Selectable.h"
16 #include "zypp/sat/SolvAttr.h"
17
18 #include "zypp/base/Function.h"
19
20 extern "C"
21 {
22 struct _Dataiterator;
23 }
24
25 ///////////////////////////////////////////////////////////////////
26 namespace zypp
27 { /////////////////////////////////////////////////////////////////
28
29   ///////////////////////////////////////////////////////////////////
30   //
31   //  CLASS NAME : PoolQuery
32   //
33   /**
34    * Meta-data query API. Returns solvables of specified kinds from specified
35    * repositories with attributes matching the specified search strings.
36    *
37    * TODO: details, examples.
38    */
39   class PoolQuery
40   {
41   public:
42     typedef std::vector<std::string>                           StrContainer;
43     typedef std::set<Resolvable::Kind>                         Kinds;
44     typedef std::map<sat::SolvAttr, StrContainer>              AttrMap;
45     typedef std::map<sat::SolvAttr, std::string>               CompiledAttrMap;
46     typedef unsigned int size_type;
47
48   public:
49     typedef function<bool( const sat::Solvable & )> ProcessResolvable;
50
51     PoolQuery();
52     ~PoolQuery();
53
54     /** Query result accessers. */
55     //@{
56
57     class ResultIterator;
58
59     /** */
60     ResultIterator begin();
61     /** */
62     ResultIterator end();
63     /** */
64     bool empty();
65     /** */
66     size_type size();
67     //@}
68
69     /**
70      * executes the query with the current settings
71      * results are yielded on the callback passed on
72      * construction
73      */
74     void execute(ProcessResolvable fnc);
75
76     /**
77      * Filter by selectable kind.
78      *
79      * By default, all kinds will be returned. If addKind() is used,
80      * only the specified kinds will be returned (multiple kinds will be ORed).
81      *
82      * Pass ResTraits<T>::kind to this method, where T is one of the
83      * \ref Resolvable child classes (e.g. ResTraits<Pattern>::kind).
84      */
85     void addKind(const Resolvable::Kind &kind);
86
87     /**
88      * Filter by repo.
89      *
90      * By default, all repos will be returned. If addRepo() is used,
91      * only the specified repo will be returned (multiple repos will be ORed).
92      */
93     void addRepo(const std::string &repoalias);
94
95     /** Installed status filter setters. */
96     //@{
97
98     /**
99      * Filter by status (installed uninstalled)
100      */
101     enum StatusFilter {
102       ALL = 0, // both install filter and uninstall filter bits are 0
103       INSTALLED_ONLY = 1,
104       UNINSTALLED_ONLY = 2
105     };
106     void setInstalledOnly();
107     void setUninstalledOnly();
108     void setStatusFilterFlags( int flags );
109
110     //@}
111
112     /**
113      * 
114      */
115     void addString(const std::string & value);
116
117     /**
118      * Filter by the \a value of any available solvable attribute.
119      *
120      * \note Solvables of a kind not supporting the specified attribute will
121      * <b>not</b> be returned.
122      * \todo check the above
123      *
124      * \param attr Attribute identfier. Use sat::Solvattr::* constants
125      * \param value What to search for.
126      */
127     void addAttribute(const sat::SolvAttr & attr, const std::string & value = "");
128
129     /**
130      * Filter by Selectable status.
131      *
132      * This should cover also plain 'is installed' and 'not installed' statuses.
133      *
134      * \param status Selectable status (zypp::ui::Status enum)
135      */
136     //void addStatus(const Status status);
137
138     /**
139      * Add dependency filter.
140      *
141      * \param dtype   depenedcy type
142      * \param name    depenency name
143      * \param edition edition for a versioned dependency
144      * \param rel     operand for a versioned dependency
145      *
146      * \todo maybe a isRegexp bool as in addName() for the name parameter would
147      *       be handy here as well.
148      * \todo add more addDependecy() variants
149      *//*
150     void addDependency(const Dep & dtype,
151                        const std::string & name,
152                        const Edition & edition = Edition(),
153                        const Rel & rel = Rel::EQ);
154 */
155
156
157     /** \name Text Matching Options */
158     //@{
159     /**
160      * Turn case sentitivity on or off (unsets or sets \ref SEARCH_NOCASE flag).
161      * PoolQuery defaults to case insensitive search unless this method
162      * is used.
163      *
164      * \param value Whether to turn the case sensitivity on (default) or off.
165      */
166     void setCaseSensitive(const bool value = true);
167
168     /** Set to match exact string instead of substring.*/
169     void setMatchExact();
170     /** Set to substring (the default). */
171     void setMatchSubstring();
172     /** Set to match globs. */
173     void setMatchGlob();
174     /** Set to use the query strings as regexes */
175     void setMatchRegex();
176     /** Set to match words (uses regex) */
177     void setMatchWord();
178     //void setLocale(const Locale & locale);
179     //@}
180
181
182     /**
183      * Require that all of the values set by addString, addAttribute, addDep
184      * match the values of respective attributes. 
185      */
186     void setRequireAll(const bool require_all = true);
187
188     /** \name getters */
189     //@{
190
191     /** Search strings added via addString() */
192     const StrContainer & strings() const;
193     /**
194      * Map (map<SolvAttr, StrContainer>) of attribute values added via
195      * addAttribute(), addDep in string form */
196     const AttrMap & attributes() const;
197
198     const Kinds & kinds() const;
199
200     const StrContainer & repos() const;
201
202     bool caseSensitive() const;
203
204     bool matchExact() const;
205     bool matchSubstring() const;
206     bool matchGlob() const;
207     bool matchRegex() const;
208     /**
209      * Returns currently used string matching type.
210      * \see satsolver/repo.h
211      */
212     int  matchType() const;
213
214     bool matchWord() const;
215
216     bool requireAll() const;
217
218     //@}
219
220     /**
221      * Reads from stream query. Attributes is sepated by delim. Query is
222      * separated by two delim.
223      *
224      * \param str input stream which contains query
225      * \param delim delimeter for attributes
226      * \return true if non-empty query is recovered
227      *
228      * \see readPoolQueriesFromFile
229      */
230     bool recover( std::istream &str, char delim = '\n' );
231
232     /**
233      * Writes a machine-readable string representation of the query to stream.
234      * Use \a delim as attribute delimiter.
235      *
236      * \param str output stream to write to
237      * \param delim delimiter for attributes
238      *
239      * \see writePoolQueriesToFile
240      */
241     void serialize( std::ostream &str, char delim = '\n' ) const;
242
243     /** Return a human-readable description of the query */
244     std::string asString() const;
245
246
247     // low level API
248
249     /**
250      * Free function to set the satsolver repo search
251      * flags.
252      *
253      * \see SEARCH_STRINGMASK
254      * \see SEARCH_STRING
255      * \see SEARCH_SUBSTRING
256      * \see SEARCH_GLOB
257      * \see SEARCH_REGEX
258      * \see SEARCH_NOCASE
259      * \see SEARCH_NO_STORAGE_SOLVABLE
260      */
261     void setFlags(int flags);
262
263     class Impl;
264   private:
265     /** Pointer to implementation */
266     RW_pointer<Impl> _pimpl;
267   };
268   ///////////////////////////////////////////////////////////////////
269
270   /** \relates PoolQuery Stream output. */
271   std::ostream & operator<<( std::ostream & str, const PoolQuery & obj );
272
273   bool equal(const PoolQuery& a, const PoolQuery& b);
274   bool operator==(const PoolQuery& a, const PoolQuery& b);
275
276   ///////////////////////////////////////////////////////////////////
277   //
278   //  CLASS NAME : PoolQuery::ResultIterator
279   //
280   /**
281    * 
282    */
283   class PoolQuery::ResultIterator : public boost::iterator_adaptor<
284     ResultIterator                     // Derived
285     , ::_Dataiterator *                // Base
286     , sat::Solvable                    // Value
287     , boost::forward_traversal_tag     // CategoryOrTraversal
288     , sat::Solvable                    // Reference
289   >
290   {
291   public:
292     ResultIterator()
293     : ResultIterator::iterator_adaptor_(0), _has_next(true),
294       _attrs(CompiledAttrMap()), _do_matching(false), _pool((sat::Pool::instance()))
295     { _rdit = 0; _sid = 0; }
296
297   private:
298     friend class boost::iterator_core_access;
299     friend class PoolQuery;
300
301     ResultIterator(Impl * pqimpl);
302
303     sat::Solvable dereference() const
304     {
305       return _sid ? sat::Solvable(_sid) : sat::Solvable::noSolvable;
306     }
307
308     void increment();
309
310     bool matchSolvable();
311
312     template <class OtherDerived, class OtherIterator, class V, class C, class R, class D>
313       bool equal( const boost::iterator_adaptor<OtherDerived, OtherIterator, V, C, R, D> & rhs ) const
314     {
315       if (!rhs.base() && !base())
316         return true;
317       if (!rhs.base() || !base())
318         return false;
319       /*if (rhs.base()->solvid == base()->solvid)
320         return true;*/
321       return true;
322     }
323
324   private:
325     //! \todo clean up this mess
326     ::_Dataiterator * _rdit;
327     PoolQuery::Impl * _pqimpl;
328     /*SolvableId*/ int _sid;
329     bool _has_next;
330     const CompiledAttrMap & _attrs;
331     bool _do_matching;
332     sat::Pool _pool;
333   };
334   ///////////////////////////////////////////////////////////////////
335
336
337   /////////////////////////////////////////////////////////////////
338 } // namespace zypp
339 ///////////////////////////////////////////////////////////////////
340
341 #endif // ZYPP_POOLQUERY_H