- move the callback from constructor to query()
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 8 Feb 2008 17:09:51 +0000 (17:09 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 8 Feb 2008 17:09:51 +0000 (17:09 +0000)
- add filter by installed & uninstalled

devel/devel.dmacvicar/testbed.cc
tests/zypp/PoolQuery_test.cc
zypp/PoolQuery.cc
zypp/PoolQuery.h

index 3fb5d1e..0adbaa2 100644 (file)
@@ -49,8 +49,8 @@ int main(int argc, char **argv)
 //         }
 //       }
 
-      PoolQuery query( &result_cb );
-      query.execute("kde");
+      PoolQuery query();
+      query.execute("kde", &result_cb);
       
 
     }
index 970cf7d..1c175e5 100644 (file)
@@ -16,6 +16,7 @@ using namespace boost::unit_test;
 bool result_cb( const ResObject::Ptr &r )
 {
   cout << r << endl;
+  return true;
 }
 
 void poolquery_simple_test()
@@ -27,9 +28,16 @@ void poolquery_simple_test()
     
   sat::Pool::instance().addRepoSolv(dir + "foo.solv");
 
-  PoolQuery query( &result_cb );
-  query.execute("kde");
+  PoolQuery query;
+  //query.setInstalledOnly();
+  query.execute("kde", &result_cb);
+
+  cout << "search done." << endl;
+
+  query.setMatchExact(true);
+  query.execute("kde", &result_cb);
   
+  cout << "search done." << endl;
 }
 
 test_suite*
index aeb1f0e..64a8256 100644 (file)
@@ -37,9 +37,9 @@ namespace zypp
   struct PoolQuery::Impl
   {
 
-    Impl(PoolQuery::ProcessResolvable fnc)
+    Impl()
       : _flags( 0 | SEARCH_NOCASE | SEARCH_SUBSTRING )
-      , _fnc(fnc)
+      , _status_flags(ALL)
     {}
 
     ~Impl()
@@ -56,7 +56,28 @@ namespace zypp
       //#define SEACH_STOP              3
 
       PoolQuery *me = (PoolQuery*) cbdata;
-      bool r = me->_pimpl->_fnc(  makeResObject(sat::Solvable(s - sat::Pool::instance().get()->solvables)));
+
+      bool r = false;
+
+      sat::Solvable solvable(s - sat::Pool::instance().get()->solvables);
+
+      // now filter by kind here (we cant do it before)
+      if ( ! me->_pimpl->_kinds.empty() )
+      {
+        // the user wants to filter by kind.
+        if ( find( me->_pimpl->_kinds.begin(),
+                   me->_pimpl->_kinds.end(),
+                   solvable.kind() )
+             == me->_pimpl->_kinds.end() )
+        {
+          // we did not find the kind in the list
+          // so this is not a result.
+          return SEARCH_NEXT_SOLVABLE;
+        }
+      }
+
+      if (me->_pimpl->_fnc)
+        r = me->_pimpl->_fnc( makeResObject(solvable) );
       
       if (!r)
         return SEARCH_STOP;
@@ -67,7 +88,8 @@ namespace zypp
     vector<string> _names;
     vector<Resolvable::Kind> _kinds;
     int _flags;
-    PoolQuery::ProcessResolvable _fnc;
+    int _status_flags;
+    mutable PoolQuery::ProcessResolvable _fnc;
   private:
     friend Impl * rwcowClone<Impl>( const Impl * rhs );
     /** clone for RWCOW_pointer */
@@ -88,8 +110,8 @@ namespace zypp
   //
   ///////////////////////////////////////////////////////////////////
 
-  PoolQuery::PoolQuery( PoolQuery::ProcessResolvable fnc )
-    : _pimpl(new Impl(fnc))
+  PoolQuery::PoolQuery()
+    : _pimpl(new Impl())
   {}
 
   PoolQuery::~PoolQuery()
@@ -106,32 +128,63 @@ namespace zypp
       _pimpl->_flags = (_pimpl->_flags | SEARCH_NOCASE);
   }
 
+  void PoolQuery::setMatchExact(const bool value)
+  {
+    if (value)
+    {
+      _pimpl->_flags = (_pimpl->_flags | SEARCH_STRING);
+      _pimpl->_flags = (_pimpl->_flags &  ~SEARCH_REGEX);
+      _pimpl->_flags = (_pimpl->_flags &  ~SEARCH_SUBSTRING);
+      _pimpl->_flags = (_pimpl->_flags &  ~SEARCH_GLOB);
+    }
+    else
+    {
+      _pimpl->_flags = (_pimpl->_flags & ~SEARCH_STRING);
+    }
+  }
+
   void PoolQuery::setFlags(int flags)
   { _pimpl->_flags = flags; }
 
+  void PoolQuery::setInstalledOnly()
+  {
+    _pimpl->_status_flags = (_pimpl->_status_flags | INSTALLED_ONLY);
+  }
+
+  void PoolQuery::setUninstalledOnly()
+  {
+    _pimpl->_status_flags = (_pimpl->_status_flags | UNINSTALLED_ONLY);
+  }
+
+  void PoolQuery::setStatusFilterFlags( int flags )
+  {
+    _pimpl->_status_flags = (_pimpl->_status_flags | flags);
+  }
   
-  void PoolQuery::execute(const string &term) const
+  void PoolQuery::execute(const string &term, ProcessResolvable fnc) const
   {
-    
+    _pimpl->_fnc = fnc;
+
     sat::Pool pool(sat::Pool::instance());
     for ( sat::Pool::RepoIterator itr = pool.reposBegin();
           itr != pool.reposEnd();
           ++itr )
     {
-      cout << "repo: " << itr->name() << endl;
+      // filter by installed uninstalled
+      if ( ( _pimpl->_status_flags & INSTALLED_ONLY ) && (itr->name() != sat::Pool::instance().systemRepoName()) )
+        continue;
+
+      if ( ( _pimpl->_status_flags & UNINSTALLED_ONLY ) && (itr->name() == sat::Pool::instance().systemRepoName()) )
+        continue;
+
       // is this repo in users repos?
       bool included = ( find(_pimpl->_repos.begin(), _pimpl->_repos.end(), itr->name()) != _pimpl->_repos.end() );
+
       // only look in user repos filter if the filter is not empty
       // in this case we search in all
       if ( _pimpl->_repos.empty() || included  )
       {
-        // now search in all names
-        //for ( vector<string>::const_iterator itn = _pimpl->_names.begin();
-        //      itn != _pimpl->_names.end();
-        //      ++itn )
-        //{
-          repo_search( itr->get(), 0, 0, term.c_str(), _pimpl->_flags, Impl::repo_search_cb, (void*) (this));
-        //}
+        repo_search( itr->get(), 0, 0, term.c_str(), _pimpl->_flags, Impl::repo_search_cb, (void*) (this));
       }
 
     }
index 489d595..5481bdf 100644 (file)
@@ -36,7 +36,7 @@ namespace zypp
 
     typedef function<bool( const ResObject::Ptr & )> ProcessResolvable;
 
-    PoolQuery(PoolQuery::ProcessResolvable fnc);
+    PoolQuery();
     ~PoolQuery();
 
   public:
@@ -46,7 +46,7 @@ namespace zypp
      * results are yielded on the callback passed on
      * construction
      */
-    void execute(const std::string &term) const;
+    void execute(const std::string &term, PoolQuery::ProcessResolvable fnc) const;
 
     /**
      * Filter by selectable kind.
@@ -67,6 +67,17 @@ namespace zypp
      */
     //void addName(const std::string & name, bool isRegex = false);
 
+    /**
+     * Filter by status (installed uninstalled)
+     */
+    enum StatusFilter {
+      ALL = 1,
+      INSTALLED_ONLY = 2,
+      UNINSTALLED_ONLY = 4
+    };
+    void setInstalledOnly();
+    void setUninstalledOnly();
+    void setStatusFilterFlags( int flags );
 
     /**
      * Filter by the \a value of any available attribute of selectables.
@@ -119,6 +130,11 @@ namespace zypp
     void setCaseSensitive(const bool value = true);
 
     /**
+     * Set match exact string
+     */
+    void setMatchExact(const bool value = true);
+
+    /**
      * Free function to set the satsolver repo search
        flags.