/* This file is part of the KDE project
* Copyright (C) 2002 Simon MacMullen
* Copyright (C) 2007 Matthias Lechner
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#define STRIGI_IMPORT_API
#include <strigi/streamthroughanalyzer.h>
#include <strigi/analyzerplugin.h>
#include <strigi/fieldtypes.h>
#include <strigi/analysisresult.h>
#include <strigi/cnstr.h>
// Qt includes
#include <QDir>
#include <QFileInfo>
#include <QString>
using namespace Strigi;
using namespace std;
class FolderThroughAnalyzerFactory;
class FolderThroughAnalyzer : public StreamThroughAnalyzer {
private:
const FolderThroughAnalyzerFactory* factory;
AnalysisResult* idx;
void setIndexable( AnalysisResult *i ) {
idx = i;
}
jstreams::InputStream* connectInputStream( jstreams::InputStream *in );
bool isReadyWithStream() { return true; }
public:
FolderThroughAnalyzer( const FolderThroughAnalyzerFactory* f ) : factory( f ) {}
};
class FolderThroughAnalyzerFactory : public StreamThroughAnalyzerFactory {
private:
const char* getName() const {
return "FolderThroughAnalyzer";
}
StreamThroughAnalyzer* newInstance() const {
return new FolderThroughAnalyzer(this);
}
void registerFields( FieldRegister& );
static const cnstr itemsFieldName;
static const cnstr sizeFieldName;
public:
const RegisteredField* itemsField;
const RegisteredField* sizeField;
};
const cnstr FolderThroughAnalyzerFactory::itemsFieldName( "items" );
const cnstr FolderThroughAnalyzerFactory::sizeFieldName( "size" );
void FolderThroughAnalyzerFactory::registerFields( FieldRegister& reg ) {
itemsField = reg.registerField( itemsFieldName, FieldRegister::integerType, 1, 0);
sizeField = reg.registerField( sizeFieldName, FieldRegister::integerType, 1, 0 );
}
jstreams::InputStream* FolderThroughAnalyzer::connectInputStream( jstreams::InputStream* in ) {
const string& stdStrPath = idx->path();
const QString path = stdStrPath.c_str();
if( path.isEmpty() )
return in;
QDir dir;
if( !dir.cd(path) )
return in;
QFileInfoList infoList = dir.entryInfoList();
QFileInfo *currentFileInfo = 0;
int items = 0;
int bytes = 0;
for( int i=0; i<infoList.size(); i++ ) {
*currentFileInfo = infoList.at(i);
if ( !currentFileInfo->fileName().startsWith( "." ) )
items++;
bytes += currentFileInfo->size();
}
idx->setField( factory->itemsField, items );
idx->setField( factory->sizeField, bytes );
return in;
}
class Factory : public AnalyzerFactoryFactory {
public:
list<StreamThroughAnalyzerFactory*>
getStreamThroughAnalyzerFactories() const {
list<StreamThroughAnalyzerFactory*> af;
af.push_back(new FolderThroughAnalyzerFactory());
return af;
}
};
STRIGI_ANALYZER_FACTORY(Factory)