aeb1f0e74e10ac4c3e95bf76df9fb21a9d08d19d
[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(PoolQuery::ProcessResolvable fnc)
41       : _flags( 0 | SEARCH_NOCASE | SEARCH_SUBSTRING )
42       , _fnc(fnc)
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       //#define SEARCH_NEXT_KEY         1
55       //#define SEARCH_NEXT_SOLVABLE    2
56       //#define SEACH_STOP              3
57
58       PoolQuery *me = (PoolQuery*) cbdata;
59       bool r = me->_pimpl->_fnc(  makeResObject(sat::Solvable(s - sat::Pool::instance().get()->solvables)));
60       
61       if (!r)
62         return SEARCH_STOP;
63       return SEARCH_NEXT_SOLVABLE;
64     }
65
66     vector<string> _repos;
67     vector<string> _names;
68     vector<Resolvable::Kind> _kinds;
69     int _flags;
70     PoolQuery::ProcessResolvable _fnc;
71   private:
72     friend Impl * rwcowClone<Impl>( const Impl * rhs );
73     /** clone for RWCOW_pointer */
74     Impl * clone() const
75     { return new Impl( *this ); }
76   };
77   ///////////////////////////////////////////////////////////////////
78
79   /** \relates PoolQuery::Impl Stream output */
80   inline std::ostream & operator<<( std::ostream & str, const PoolQuery::Impl & obj )
81   {
82     return str << "PoolQuery::Impl";
83   }
84
85   ///////////////////////////////////////////////////////////////////
86   //
87   //    CLASS NAME : PoolQuery
88   //
89   ///////////////////////////////////////////////////////////////////
90
91   PoolQuery::PoolQuery( PoolQuery::ProcessResolvable fnc )
92     : _pimpl(new Impl(fnc))
93   {}
94
95   PoolQuery::~PoolQuery()
96   {}
97
98   void PoolQuery::addKind(const Resolvable::Kind &kind)
99   { _pimpl->_kinds.push_back(kind); }
100
101   void PoolQuery::setCaseSensitive(const bool value)
102   {
103     if (value)
104       _pimpl->_flags = (_pimpl->_flags &  ~SEARCH_NOCASE);
105     else
106       _pimpl->_flags = (_pimpl->_flags | SEARCH_NOCASE);
107   }
108
109   void PoolQuery::setFlags(int flags)
110   { _pimpl->_flags = flags; }
111
112   
113   void PoolQuery::execute(const string &term) const
114   {
115     
116     sat::Pool pool(sat::Pool::instance());
117     for ( sat::Pool::RepoIterator itr = pool.reposBegin();
118           itr != pool.reposEnd();
119           ++itr )
120     {
121       cout << "repo: " << itr->name() << endl;
122       // is this repo in users repos?
123       bool included = ( find(_pimpl->_repos.begin(), _pimpl->_repos.end(), itr->name()) != _pimpl->_repos.end() );
124       // only look in user repos filter if the filter is not empty
125       // in this case we search in all
126       if ( _pimpl->_repos.empty() || included  )
127       {
128         // now search in all names
129         //for ( vector<string>::const_iterator itn = _pimpl->_names.begin();
130         //      itn != _pimpl->_names.end();
131         //      ++itn )
132         //{
133           repo_search( itr->get(), 0, 0, term.c_str(), _pimpl->_flags, Impl::repo_search_cb, (void*) (this));
134         //}
135       }
136
137     }
138   }
139   
140   /******************************************************************
141   **
142   **    FUNCTION NAME : operator<<
143   **    FUNCTION TYPE : ostream &
144   */
145   ostream & operator<<( ostream & str, const PoolQuery & obj )
146   {
147     return str;
148   }
149
150   /////////////////////////////////////////////////////////////////
151 } // namespace zypp
152 ///////////////////////////////////////////////////////////////////
153