Source Code (Use browser search to find items of interest.)

Class Index

kioslave'SearchRequest (./kdebase/kioslave/ldap/kldap.h:504)

  class SearchRequest : public Request
  {
  public:

    /**
     * Constructor. Initializes a search request.
     *
     * Creates a new search request object. 
     *
     * @param c The connection to use.
     * @param m The mode for the request.
     *
     */
    SearchRequest(Connection &c, RunMode m=Asynchronous);

    /**
     * Constructor. Initializes from a LDAP url.
     *
     * Takes a LDAP url and creates a matching search request.
     *
     * LDAP urls are defined in RFC 2255.
     *
     * @param c The connection to use.
     * @param url The LDAP url. 
     * @param m The mode for the request.
     *
     */
    SearchRequest(Connection &c, const char *_url, RunMode m=Asynchronous);

    /// Derived method from Request.
    virtual bool execute();

    /**
     * Performs a search.
     *
     * This method is a just a shortcut for:
     * 
     * setBase(base); setFilter(filter); execute();
     *
     * @param base The base DN to start the search.
     * @param filter The filter to use.
     *
     * @return TRUE, if the search was started, else FALSE.
     *
     */
    bool search(QString base, QString filter="(objectClass=*)");

    /**
     * Set the base DN for the search.
     *
     * Sets the distinguished name from which the search will be started.
     *
     * @param base the base dn for the search.
     *
     */
    void setBase(QString base) { _base=base; };

    /**
     * Returns the base DN for the search.
     *
     * @return the base DN for the search.
     *
     */
    QString base() { return _base; };

    /** 
     * Set the filter for the search.
     *
     * This method sets a filter expression for the 
     * search. The search will only return entries matching
     * the filter.
     *
     * The default filter is "(objectClass=*)".
     *
     * @param filter The filter to use.
     *
     */
    void setFilter(QString filter) { _filter=filter; };

    /// Returns the filter for the search.
    QString filter() { return _filter; };

    /**
     * Set the attributes to return.
     *
     * This method sets the list of attributes to return
     * for each entry found in the search.
     * 
     * @param list The list of attributes to return.
     *
     */
    void setAttributes(const QStrList &list) { _attributes=list; };

    /// Returns the list of attributes to return for each entry.
    QStrList &attributes() { return _attributes; };

    /**
     * Set the scope of the search.
     *
     * There are three possibele values for the scope of the search:
     *
     * LDAP_SCOPE_BASE: Only search in the base DN.
     * LDAP_SCOPE_ONELEVEL: Search in the children of the base DN.
     * LDAP_SCOPE_SUBTREE: Search the tree originating in the base DN.
     *
     * The default is LDAP_SCOPE_SUBTREE.
     *
     * @param s The scope to use.
     *
     */
    void setScope(int s) { _scope=s; };

    /// Returns the scope of the search.
    int scope() { return _scope; };

    /**
     * Returns the first entry found.
     *
     * After finishing the search, this method returns the first
     * entry found.
     *
     * @return The first entry found.
     */
    Entry first();

    /**
     * Returns the next entry found.
     *
     * @return The next entry found.
     *
     */
    Entry next();

    /**
     * Indicate if more entries are available.
     *
     * @return TRUE, if there are more entries, else FALSE.
     *
     */
    bool end();

    /**
     * Return the search result as an LDIF string.
     *
     * @return the search result as LDIF string.
     */
    QString asLDIF();

  private:

    QString  _base, _filter;
    QStrList _attributes;
    int      _scope, _attrsonly;

    LDAPMessage *entry;
    
  };

};


kioslave'SearchRequest::SearchRequest() (./kdebase/kioslave/ldap/kldap.cpp:199)

SearchRequest::SearchRequest(Connection &c, RunMode m)
  : Request(c,m), _base(""), _filter("(objectClass=*)"), _scope(LDAP_SCOPE_SUBTREE),
    _attrsonly(0), entry(0)
{
  expected = LDAP_RES_SEARCH_RESULT;

  // try to connect
  if (!c.isConnected())
    c.connect();
  _handle = c.handle();
}



kioslave'SearchRequest::SearchRequest() (./kdebase/kioslave/ldap/kldap.cpp:212)

SearchRequest::SearchRequest(Connection &c, const char *_url, RunMode m)
  : Request(c,m), _base(""), _filter("(objectClass=*"), _scope(LDAP_SCOPE_SUBTREE),
    _attrsonly(0), entry(0)
{
  Url url(_url);

  expected = LDAP_RES_SEARCH_RESULT;

  // close the connection if not to the right host/port
  if (url.host() != c.host() || url.port() != c.port())
    {
      if (c.isConnected())
	c.disconnect();
      c.setHost(url.host());
      c.setPort(url.port());
    }

  // try to connect
  if (!c.isConnected())
    c.connect();
  _handle = c.handle();
  
  // TODO: authenticate via the basename extension!!!!!

  // set the search criteria
  setBase(url.dn());
  setScope(url.scope());
  setFilter(url.filter());
  setAttributes(url.attributes());
}



kioslave'SearchRequest::execute() (./kdebase/kioslave/ldap/kldap.cpp:244)

bool SearchRequest::execute()
{
  if (!handle())
    return FALSE;

  // call the inherited method
  Request::execute();

  debug << "search request: " << _base << " " << _scope << " " << _filter << endl;

  // Honour the attributes to return
  char **attrs = 0;
  int count = _attributes.count();
  if (count > 0)
    {
      attrs = static_cast<char**>(malloc((count+1) * sizeof(char*)));
      for (int i=0; i<count; i++)
	  attrs[i] = strdup(_attributes.at(i));
      attrs[count] = 0;
    }  
  
  // if async, just issue the request
  if (mode == Asynchronous)
    {
      // start searching
      id = ldap_search(handle(), const_cast<char*>(_base.ascii()), _scope, 
		       const_cast<char*>(_filter.ascii()), attrs, _attrsonly);

      // free the attributes list again
      if (count > 0)
	{
	  for (int i=0; i<count; i++)
	    free(attrs[i]);
	  free(attrs);
	}
      
      // check for an error
      if (id == -1)
	{
	  id = 0;
	  running = FALSE;
	  return check(handle()->ld_errno);
	}
      return TRUE;
    }

  int retval;

  // call the right version
  if (useTimeout())
    {
      retval = ldap_search_st(handle(), const_cast<char*>(_base.ascii()), _scope, 
			      const_cast<char*>(_filter.ascii()), attrs, 
			      _attrsonly, &_timeout, &req_result);
    }
  else
    {
      retval = ldap_search_s(handle(), const_cast<char*>(_base.ascii()), _scope, 
			     const_cast<char*>(_filter.ascii()), attrs,
			     _attrsonly, &req_result);
    }

  // free the attributes list again
  if (count > 0)
    {
      for (int i=0; i<count; i++)
	free(attrs[i]);
      free(attrs);
    }
  
  running = FALSE;

  return check(retval);
}



kioslave'SearchRequest::search() (./kdebase/kioslave/ldap/kldap.cpp:320)

bool SearchRequest::search(QString base, QString filter)
{
  debug << "search: base=" << base << " filter=" << filter;

  setBase(base);
  setFilter(filter);

  bool retval = execute();

  debug << (retval ? ": Success" : "Failed") << endl;
  
  return retval;
}



kioslave'SearchRequest::first() (./kdebase/kioslave/ldap/kldap.cpp:404)

Entry SearchRequest::first()
{
  entry = ldap_first_entry(handle(), req_result);
  return Entry(handle(), entry);
}



kioslave'SearchRequest::next() (./kdebase/kioslave/ldap/kldap.cpp:411)

Entry SearchRequest::next()
{
  entry = ldap_next_entry(handle(), entry);
  return Entry(handle(), entry);
}



kioslave'SearchRequest::end() (./kdebase/kioslave/ldap/kldap.cpp:418)

bool SearchRequest::end()
{
  return entry == 0;
}



kioslave'SearchRequest::asLDIF() (./kdebase/kioslave/ldap/kldap.cpp:424)

QString SearchRequest::asLDIF()
{
  QString result;
  QTextOStream os(&result);
  BerElement *entry;
  char       *name;
  char       **vals;

  LDAPMessage *item = ldap_first_entry(handle(), req_result);
  while (item)
    {
      // print the dn
      os << "dn: " <<  ldap_get_dn(handle(), item) << endl;

      // iterate over the attributes    
      name = ldap_first_attribute(handle(), item, &entry);
      while (name != 0)
	{
	  // print the values
	  vals = ldap_get_values(handle(), item, name);
	  if (vals) 
	    for (int i=0; vals[i] != 0; i++)
		os << name << ": " << vals[i] << endl;
	  ldap_value_free(vals);
	  
	  // next attribute
	  name = ldap_next_attribute(handle(), item, entry);
	}

      // next entry
      os << endl;
      item = ldap_next_entry(handle(), item);
    }

  return result;
}