Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

omggbifconsolesearch.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Tim Sutton   *
00003  *   tim@linfiniti.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "omggbifconsolesearch.h"
00022 #include <QString>
00023 
00024 
00025 OmgGbifConsoleSearch::OmgGbifConsoleSearch(QObject *parent)
00026 {
00027 
00028 }
00029 
00030 OmgGbifConsoleSearch::~OmgGbifConsoleSearch()
00031 {
00032 
00033 }
00034 
00035 bool OmgGbifConsoleSearch::search(QString theFileName)
00036 {
00037   setSearchList(theFileName);
00038   mBusyFlag = true;
00039   mListPosition = 0;
00040   mListSize = static_cast<unsigned int>(mNameList.count());
00041   QStringListIterator myIterator(mNameList);
00042   while (myIterator.hasNext())
00043   {
00044     qDebug(myIterator.next().toLocal8Bit());
00045   }
00046   processNextTaxon();
00047   //while (mBusyFlag==true)
00048   //{
00049   //  sleep(1000);
00050   //}
00051   return true;
00052 }
00053 
00054 
00055 void OmgGbifConsoleSearch::processNextTaxon()
00056 {
00057   qDebug("processNextTaxon Called");
00058   mOmgScraperGbif = new OmgScraperGbif(this);
00059   connect(mOmgScraperGbif, SIGNAL(gettingTaxonID()),
00060           this, SLOT(gettingTaxonID()));
00061   connect(mOmgScraperGbif, SIGNAL(gettingBody()),
00062           this, SLOT(gettingBody()));
00063   connect(mOmgScraperGbif, SIGNAL(fileWritten(QString, QString, QString,int)),
00064           this, SLOT(fileWritten(QString, QString,QString, int)));
00065   connect(mOmgScraperGbif, SIGNAL(fileNotWritten(QString)),
00066           this, SLOT(fileNotWritten(QString)));
00067   connect(mOmgScraperGbif, SIGNAL(statusChanged(QString)),
00068           this, SLOT(setStatus(QString)));
00069   connect(mOmgScraperGbif, SIGNAL(error(QString)),
00070           this, SLOT(setError(QString)));
00071 
00072   QString mySearchString=mNameList.at( mListPosition );
00073   qDebug("processNextTaxon searching for" + mySearchString.toLocal8Bit());
00074   //QString myFileName = leOutputPath->text() + QDir::separator() +mySearchString + ".shp";
00075   QString myFileName = "/tmp/" +mySearchString + ".shp";
00076   myFileName.replace(" ","_");
00077   setStatus("Creating : " + myFileName.toLocal8Bit());
00078   mOmgScraperGbif->search(mySearchString, myFileName);
00079   mListPosition++;
00080 }
00081 
00082 
00083 
00084 
00085 void OmgGbifConsoleSearch::setSearchList(QString theFileName)
00086 {
00087   //
00088   // Add all search strings from text file
00089   //
00090   //first build a regex to match text at the beginning of the line
00091   QRegExp myQRegExp( "^[^#][ a-zA-Z]*" ); //seconf caret means 'not'
00092   QStringList myTaxonList;
00093   QFile myQFile( theFileName );
00094   if ( myQFile.open( QIODevice::ReadOnly ) )
00095   {
00096     //now we parse the file, checking each line for its taxon string
00097     QTextStream myQTextStream( &myQFile );
00098     QString myCurrentLineQString;
00099     while ( !myQTextStream.atEnd() )
00100     {
00101       myCurrentLineQString = myQTextStream.readLine(); // line of text excluding '\n'
00102       int  myPosInt = myQRegExp.indexIn( myCurrentLineQString,0 );
00103       if (myPosInt<0) continue;
00104       QStringList myMatchesQStringList = myQRegExp.capturedTexts();
00105       QStringList::Iterator myIterator = myMatchesQStringList.begin();
00106       QString myTaxonQString=*myIterator;
00107       //strip ending and leading whitespace
00108       QString myTaxonName=myTaxonQString.simplified();
00109       if (myTaxonName != "")
00110       {
00111         //make sure there are only single spaces separating words.
00112         myTaxonQString=myTaxonQString.replace( QRegExp(" {2,}"), " " );
00113         myTaxonList.append(myTaxonQString);
00114       }
00115     }
00116     myQFile.close();
00117     //sort the taxon list alpabetically
00118     myTaxonList.sort();
00119     //now find the uniqe entries in the qstringlist and
00120     //add each entry to the list
00121     QString myLastTaxon="";
00122     mNameList.clear();
00123     QStringList::Iterator myIterator= myTaxonList.begin();
00124     while( myIterator!= myTaxonList.end() )
00125     {
00126       QString myCurrentTaxon=*myIterator;
00127       if (myCurrentTaxon!=myLastTaxon)
00128       {
00129         mNameList << myCurrentTaxon;
00130       }
00131       myLastTaxon=*myIterator;
00132       ++myIterator;
00133     }
00134   }
00135   else
00136   {
00137     std::cerr << "Localities Fetcher  Error\n" << "The file is not readable. Check you have the neccessary file permissions and try again." << std::endl;
00138     return;
00139   }
00140 
00141 }
00142 
00143 
00144 void OmgGbifConsoleSearch::gettingTaxonID()
00145 {
00146   setStatus("Connecting...");
00147 }
00148 
00149 void OmgGbifConsoleSearch::gettingBody()
00150 {
00151   setStatus("Searching...");
00152 }
00153 
00154 void OmgGbifConsoleSearch::fileWritten(QString theShapeFile, QString theTextFile,QString theTaxonName,int theCount)
00155 {
00156   disconnect(mOmgScraperGbif);
00157   delete mOmgScraperGbif;
00158   setStatus("Saving...");
00159   
00160   //show the retrieved points in the gui - disabled for now as its too verbose
00161   //showPoints(theTextFile); 
00162   setStatus(QString(theTaxonName + "(" + QString::number(theCount) + ")"));
00163   
00164   
00165   if (mListPosition<mListSize)
00166   {
00167     processNextTaxon();
00168   }
00169   else
00170   {
00171     mBusyFlag = false;
00172     setStatus("Search complete!");
00173     emit searchDone();
00174    
00175   }
00176 }
00177 
00178 void OmgGbifConsoleSearch::fileNotWritten(QString theTaxonName)
00179 {
00180   disconnect(mOmgScraperGbif);
00181   delete mOmgScraperGbif;
00182   setStatus("Saving failed...");
00183   setStatus("No results found, file not written");
00184   setStatus(theTaxonName + "(0)");
00185   
00186   if (mListPosition<mListSize)
00187   {
00188     processNextTaxon();
00189   }
00190   else
00191   {
00192     mBusyFlag = false;
00193     emit searchDone();
00194     setStatus("Search complete!");
00195   }
00196 }
00197 
00198 
00199 void OmgGbifConsoleSearch::showPoints(QString theFileName)
00200 {
00201   QFile myFile( theFileName );
00202   if ( myFile.open( QIODevice::ReadOnly ) )
00203   {
00204     //teResults->append(myFile.readAll());
00205     myFile.close();
00206   }
00207   else
00208   {
00209     setError(tr("Could not open the generated Points file") + " (" + theFileName.toLocal8Bit() + ")");
00210   }
00211   return ;
00212 }
00213 
00214 void OmgGbifConsoleSearch::setStatus(QString theStatus)
00215 {
00216   qDebug ( theStatus.toLocal8Bit() );
00217 }
00218 
00219 
00220 void OmgGbifConsoleSearch::appendLogMessage(QString theMessage)
00221 {
00222   qDebug ( theMessage.toLocal8Bit() );
00223 }
00224 
00225 void OmgGbifConsoleSearch::setError(QString theError)
00226 {
00227 qDebug ("********************");
00228 qDebug ("*  ERROR           *");
00229 qDebug ("********************");
00230 qDebug (theError.toLocal8Bit() );
00231 qDebug ("********************");
00232 }
00233 
00234 

Generated on Mon Apr 28 15:06:41 2008 for openModellerDesktop by  doxygen 1.4.1-20050210