Logo Search packages:      
Sourcecode: packagesearch version File versions

aptsearchplugin.cpp

#include <qwidget.h>
#include <qtimer.h>
#include <qlineedit.h>
#include <qstatusbar.h>
#include <qcheckbox.h>

#include <helpers.h>
#include <iprovider.h>

#include "aptsearchplugin.h"

#include "aptsearchpluginshortinputwidget.h"



namespace NPlugin
{

AptSearchPlugin::AptSearchPlugin(NApt::IAptSearch* pAptSearch) :
      _title("Apt-Search Plugin"),
      _briefDescription("Performs a full text search"),
      _description("This plugin can be used to search the packages for expressions."),
      _pAptSearch(pAptSearch)
{
      _pShortInputWidget = 0;
      _pStatusBar = 0;
      
      _pDelayTimer = new QTimer(this, "delayTimer");
      _delayTime=1000;
      connect(_pDelayTimer, SIGNAL(timeout()), SLOT(evaluateSearch()));
}

AptSearchPlugin::~AptSearchPlugin()
{
      delete _pShortInputWidget;
      delete _pDelayTimer;
}


/////////////////////////////////////////////////////
// Plugin Interface
/////////////////////////////////////////////////////

void AptSearchPlugin::init(IProvider* pProvider)
{
      _pProvider = pProvider;
      QMainWindow* pWindow = _pProvider->mainWindow();
      _pShortInputWidget = new AptSearchPluginShortInputWidget(pWindow, "AptSearchShortInputWIdget");
      _pShortInputWidget->show();
      _pStatusBar = pWindow->statusBar();
      _pHandleMaker = &pProvider->handleMaker();
      connect(
            _pShortInputWidget->_pAptSearchTextInput, 
            SIGNAL(textChanged(const QString&)),
            SLOT(onInputTextChanged(const QString&))
      );
      connect(
            _pShortInputWidget->_pAptSearchTextInput, 
            SIGNAL(returnPressed()),
            SLOT(evaluateSearch())
      );
      connect(
            _pShortInputWidget->_pSearchDescriptionsCheck,
            SIGNAL(toggled(bool)), 
            SLOT(evaluateSearch())
      );
}

/////////////////////////////////////////////////////
// Search Plugin Interface
/////////////////////////////////////////////////////

QWidget* AptSearchPlugin::shortInputAndFeedbackWidget() const
{
      return _pShortInputWidget;
}

void AptSearchPlugin::clearSearch()
{
      _pShortInputWidget->_pAptSearchTextInput->clear();
      _pDelayTimer->stop();
      evaluateSearch();
}


bool AptSearchPlugin::isInactive() const
{
      return _includePatterns.isEmpty() && _excludePatterns.isEmpty();
}

/////////////////////////////////////////////////////
// Helper Methods
/////////////////////////////////////////////////////

void AptSearchPlugin::onInputTextChanged(const QString&)
{
      _pStatusBar->message( tr("delayed evaluation - waiting for further input") );
      _pDelayTimer->start(_delayTime,true);
}

void AptSearchPlugin::evaluateSearch()
{
      // stop the delay timer in case that this evaluateSearch() was triggered by
      // another event
      _pDelayTimer->stop();
      _pProvider->reportBusy(this, tr("Performing full text search on Package Database"));
      _searchResult.clear();
      parseSearchExpression(_pShortInputWidget->_pAptSearchTextInput->text());
      if ( !isInactive() )    // if the search is not empty
      {
            QStringList patterns = this->searchPatterns();
            _pAptSearch->search(_searchResult, *_pHandleMaker, _includePatterns, _excludePatterns,  
                        _pShortInputWidget->_pSearchDescriptionsCheck->isChecked());
      }
      _pProvider->reportReady(this);
      emit searchChanged(this);
}

QStringList AptSearchPlugin::searchPatterns()
{
      return _includePatterns;
}

void AptSearchPlugin::parseSearchExpression(const QString& expression)
{
      _includePatterns.clear();
      _excludePatterns.clear();
      if (expression.isEmpty())
            return;
      QStringList patterns = QStringList::split('"',expression, true);
      
      // status flags
      // invariant:   !(minus && plus)
      bool plus = false;
      bool minus = false;
      bool inGroup = (expression == '"');
      for (QStringList::iterator it = patterns.begin(); it != patterns.end(); ++it )
      {
            if (!inGroup)
            {
                  QStringList newPatterns = QStringList::split(' ',(*it), true);
                  for (QStringList::iterator jt = newPatterns.begin(); jt != newPatterns.end(); ++jt )
                  {
                        QString p = (*jt);
                        if (p.isEmpty())
                              continue;
                        if (p[0] == '+')
                        {
                              plus = true;
                              minus = false;
                              p = p.mid(1);
                        }
                        else if (p[0] == '-')
                        {
                              minus = true;
                              plus = false;
                              p = p.mid(1);
                        }
                        if (p.isEmpty())
                              continue;
                        if (!minus)
                        {
                              _includePatterns.push_back(p);
                              plus = false;
                        }
                        else
                        {
                              _excludePatterns.push_back(p);
                              minus = false;
                        }
                  }
            }
            else
            {
                  if ((*it).isEmpty())
                        continue;
                  if (!minus)
                  {
                        _includePatterns.push_back(*it);
                        plus = false;
                  }
                  else
                  {
                        _excludePatterns.push_back(*it);
                        minus = false;
                  }
            }
            inGroup = !inGroup;
      }
}



}     // namespace NPlugin

Generated by  Doxygen 1.6.0   Back to index