fix to compile
[platform/upstream/libzypp.git] / zypp / PoolQuery.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/PoolQuery.cc
10  *
11 */
12 #include <iostream>
13 #include <list>
14 #include <vector>
15 #include <algorithm>
16
17 #include "zypp/base/Logger.h"
18 #include "zypp/base/PtrTypes.h"
19 #include "zypp/base/DefaultIntegral.h"
20 #include "zypp/PoolQuery.h"
21 #include "zypp/base/UserRequestException.h"
22
23 #include "zypp/sat/Pool.h"
24 #include "zypp/sat/Solvable.h"
25
26 extern "C"
27 {
28 #include "satsolver/repo.h"
29 }
30
31 using namespace std;
32
33 ///////////////////////////////////////////////////////////////////
34 namespace zypp
35 { /////////////////////////////////////////////////////////////////
36
37   struct PoolQuery::Impl
38   {
39
40     Impl()
41       : _flags( 0 | SEARCH_NOCASE | SEARCH_SUBSTRING )
42       , _status_flags(ALL)
43     {}
44
45     ~Impl()
46     {
47       //MIL << std::endl;
48     }
49
50   public:
51
52     static int repo_search_cb(void *cbdata, ::Solvable *s, ::Repodata *data, ::Repokey *key, ::KeyValue *kv)
53     {
54       //int  SEARCH_NEXT_KEY      =  1;
55       //int  SEARCH_NEXT_SOLVABLE =  2;
56       int  SEARCH_STOP = 3;
57
58       PoolQuery *me = (PoolQuery*) cbdata;
59
60       bool r = false;
61
62       sat::Solvable solvable(s - sat::Pool::instance().get()->solvables);
63
64       // now filter by kind here (we cant do it before)
65       if ( ! me->_pimpl->_kinds.empty() )
66       {
67         // the user wants to filter by kind.
68         if ( find( me->_pimpl->_kinds.begin(),
69                    me->_pimpl->_kinds.end(),
70                    solvable.kind() )
71              == me->_pimpl->_kinds.end() )
72         {
73           // we did not find the kind in the list
74           // so this is not a result.
75           return SEARCH_NEXT_SOLVABLE;
76         }
77       }
78
79       if (me->_pimpl->_fnc)
80         r = me->_pimpl->_fnc( makeResObject(solvable) );
81       
82       if (!r)
83         return SEARCH_STOP;
84       return SEARCH_NEXT_SOLVABLE;
85     }
86
87     vector<string> _repos;
88     vector<string> _names;
89     vector<Resolvable::Kind> _kinds;
90     int _flags;
91     int _status_flags;
92     mutable PoolQuery::ProcessResolvable _fnc;
93   private:
94     friend Impl * rwcowClone<Impl>( const Impl * rhs );
95     /** clone for RWCOW_pointer */
96     Impl * clone() const
97     { return new Impl( *this ); }
98   };
99   ///////////////////////////////////////////////////////////////////
100
101   /** \relates PoolQuery::Impl Stream output */
102   inline std::ostream & operator<<( std::ostream & str, const PoolQuery::Impl & obj )
103   {
104     return str << "PoolQuery::Impl";
105   }
106
107   ///////////////////////////////////////////////////////////////////
108   //
109   //    CLASS NAME : PoolQuery
110   //
111   ///////////////////////////////////////////////////////////////////
112
113   PoolQuery::PoolQuery()
114     : _pimpl(new Impl())
115   {}
116
117   PoolQuery::~PoolQuery()
118   {}
119
120   void PoolQuery::addKind(const Resolvable::Kind &kind)
121   { _pimpl->_kinds.push_back(kind); }
122
123   void PoolQuery::setCaseSensitive(const bool value)
124   {
125     if (value)
126       _pimpl->_flags = (_pimpl->_flags &  ~SEARCH_NOCASE);
127     else
128       _pimpl->_flags = (_pimpl->_flags | SEARCH_NOCASE);
129   }
130
131   void PoolQuery::setMatchExact(const bool value)
132   {
133     if (value)
134     {
135       _pimpl->_flags = (_pimpl->_flags | SEARCH_STRING);
136       _pimpl->_flags = (_pimpl->_flags &  ~SEARCH_REGEX);
137       _pimpl->_flags = (_pimpl->_flags &  ~SEARCH_SUBSTRING);
138       _pimpl->_flags = (_pimpl->_flags &  ~SEARCH_GLOB);
139     }
140     else
141     {
142       _pimpl->_flags = (_pimpl->_flags & ~SEARCH_STRING);
143     }
144   }
145
146   void PoolQuery::setFlags(int flags)
147   { _pimpl->_flags = flags; }
148
149   void PoolQuery::setInstalledOnly()
150   {
151     _pimpl->_status_flags = (_pimpl->_status_flags | INSTALLED_ONLY);
152   }
153
154   void PoolQuery::setUninstalledOnly()
155   {
156     _pimpl->_status_flags = (_pimpl->_status_flags | UNINSTALLED_ONLY);
157   }
158
159   void PoolQuery::setStatusFilterFlags( int flags )
160   {
161     _pimpl->_status_flags = (_pimpl->_status_flags | flags);
162   }
163   
164   void PoolQuery::execute(const string &term, ProcessResolvable fnc) const
165   {
166     _pimpl->_fnc = fnc;
167
168     sat::Pool pool(sat::Pool::instance());
169     for ( sat::Pool::RepoIterator itr = pool.reposBegin();
170           itr != pool.reposEnd();
171           ++itr )
172     {
173       // filter by installed uninstalled
174       if ( ( _pimpl->_status_flags & INSTALLED_ONLY ) && (itr->name() != sat::Pool::instance().systemRepoName()) )
175         continue;
176
177       if ( ( _pimpl->_status_flags & UNINSTALLED_ONLY ) && (itr->name() == sat::Pool::instance().systemRepoName()) )
178         continue;
179
180       // is this repo in users repos?
181       bool included = ( find(_pimpl->_repos.begin(), _pimpl->_repos.end(), itr->name()) != _pimpl->_repos.end() );
182
183       // only look in user repos filter if the filter is not empty
184       // in this case we search in all
185       if ( _pimpl->_repos.empty() || included  )
186       {
187         repo_search( itr->get(), 0, 0, term.c_str(), _pimpl->_flags, Impl::repo_search_cb, (void*) (this));
188       }
189
190     }
191   }
192   
193   /******************************************************************
194   **
195   **    FUNCTION NAME : operator<<
196   **    FUNCTION TYPE : ostream &
197   */
198   ostream & operator<<( ostream & str, const PoolQuery & obj )
199   {
200     return str;
201   }
202
203   /////////////////////////////////////////////////////////////////
204 } // namespace zypp
205 ///////////////////////////////////////////////////////////////////
206