// kate: space-indent on; indent-width 3; replace-tabs on;

/* ****************************************************************************
  This file is part of KBabel

  Copyright (C) 1999-2000 by Matthias Kiefer
                            <matthias.kiefer@gmx.de>
		2001-2003 by Stanislav Visnovsky
			    <visnovsky@kde.org>

  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; either version 2 of the License, or
  (at your option) any later version.

  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; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

  In addition, as a special exception, the copyright holders give
  permission to link the code of this program with any edition of
  the Qt library by Trolltech AS, Norway (or with modified versions
  of Qt that use the same license as Qt), and distribute linked
  combinations including the two.  You must obey the GNU General
  Public License in all respects for all of the code used other than
  Qt. If you modify this file, you may extend this exception to
  your version of the file, but you are not obligated to do so.  If
  you do not wish to do so, delete this exception statement from
  your version.

**************************************************************************** */

#include "gettextimport.h"

#include <catalogitem.h>
#include <resources.h>

#include <qfile.h>
#include <qfileinfo.h>
#include <qregexp.h>
#include <qtextcodec.h>

#include <kapplication.h>
#include <kdebug.h>
#include <kgenericfactory.h>
#include <klocale.h>

K_EXPORT_COMPONENT_FACTORY( kbabel_gettextimport, KGenericFactory<GettextImportPlugin> )

using namespace KBabel;

GettextImportPlugin::GettextImportPlugin(QObject* parent, const char* name, const QStringList &) : CatalogImportPlugin(parent,name)
{
}

ConversionStatus GettextImportPlugin::load(const QString& filename, const QString&)
{
   if ( filename.isEmpty() ) {
      kdDebug(KBABEL) << "fatal error: empty filename to open" << endl;
      return NO_FILE;
   }

   QFileInfo info(filename);

   if(!info.exists() || info.isDir())
      return NO_FILE;

   if(!info.isReadable())
      return NO_PERMISSIONS;

   QFile file(filename);


   bool recoveredError=false;
   bool docbookFile=false;
   	
   if ( !file.open( IO_ReadOnly ) )
      return NO_PERMISSIONS;
   
   uint oldPercent = 0;
   emit signalResetProgressBar(i18n("loading file"),100);

   QByteArray ba = file.readAll();
   file.close();

   // find codec for file
   bool hadCodec;
   QTextCodec* codec=codecForArray( ba, &hadCodec );
   
   QTextStream stream(ba,IO_ReadOnly);
   if(codec)
      stream.setCodec(codec);
   QIODevice *dev = stream.device();
   int fileSize = dev->size();

   // if somethings goes wrong with the parsing, we don't have deleted the old contents
   CatalogItem tempHeader;
   QStringList tempObsolete;


   kdDebug(KBABEL) << "start parsing..." << endl;

   // first read header
   ConversionStatus status = readHeader(stream);

   if(status!=OK)
   {
      emit signalClearProgressBar();

      if( status == RECOVERED_PARSE_ERROR ) status = HEADER_ERROR;

      return status;
   }

   tempHeader.setMsgid( _msgid );
   tempHeader.setMsgstr( _msgstr );
   tempHeader.setComment( _comment );
   if(tempHeader.isFuzzy())
   {
      tempHeader.removeFuzzy();
   }

   // check if header seems to indicate docbook content generated by xml2pot
   const bool docbookContent = (tempHeader.msgstr().find("application/x-xml2pot") != tempHeader.msgstr().end());

   // now parse the rest of the file
   ConversionStatus success=OK;
   uint counter=0;
   QValueList<uint> errorIndex;

   while(!stream.eof() && success==OK )
   {
      kapp->processEvents(10);
      if( isStopped() )
      {
         return STOPPED;
      }

      success=readEntry(stream);

      if(success==OK)
      {
         if( _obsolete )
         {
               success=OK;
               tempObsolete.append(_comment);
         }
         else
         {
               CatalogItem tempCatItem;
               tempCatItem.setMsgctxt( _msgctxt );
               tempCatItem.setMsgid( _msgid );
               tempCatItem.setMsgstr( _msgstr );
               tempCatItem.setComment( _comment );
               tempCatItem.setGettextPluralForm( _gettextPluralForm );
               
               // add new entry to the list of entries
               appendCatalogItem(tempCatItem);
               // check if first comment seems to indicate a docbook source file
               if(counter==0)
                  docbookFile = ( tempCatItem.comment().find(".docbook") != -1 );
         }
      }
      else if(success==RECOVERED_PARSE_ERROR)
      {
         success=OK;
         recoveredError=true;
         errorIndex.append(counter);
         
         CatalogItem tempCatItem;
         tempCatItem.setMsgctxt( _msgctxt );
         tempCatItem.setMsgid( _msgid );
         tempCatItem.setMsgstr( _msgstr );
         tempCatItem.setComment( _comment );
         tempCatItem.setGettextPluralForm( _gettextPluralForm );

         // add new entry to the list of entries
         appendCatalogItem(tempCatItem);
      }
      counter++;

      const uint newPercent = (100*dev->at())/fileSize;
      if(newPercent > oldPercent)
      {
         oldPercent = newPercent;
         emit signalProgress(oldPercent);
      }
   }


   // to be sure it is set to 100, if someone don't connect to
   // signalClearProgressBar()
   emit signalProgress(100);

   emit signalClearProgressBar();

   kdDebug(KBABEL) << "ready." << endl;
      
      if(counter==0)
      {
        // no items read
	return PARSE_ERROR;
      }

   if(success!=PARSE_ERROR)
   {
      setGeneratedFromDocbook(docbookContent || docbookFile);

      setHeader(tempHeader);
      setCatalogExtraData(tempObsolete);
      setErrorIndex(errorIndex);

      if(hadCodec)
         setFileCodec(codec);
      else
         setFileCodec(0);
   }
   else
   {
      return PARSE_ERROR;
   }
   
   setMimeTypes( "application/x-gettext" );

   if(recoveredError)
   	return RECOVERED_PARSE_ERROR;

   return OK;
}

QTextCodec* GettextImportPlugin::codecForArray(QByteArray& array, bool* hadCodec)
{
   if(hadCodec)
   {
      *hadCodec=false;
   }

   QTextStream stream( array, IO_ReadOnly );
   // If needed this will detect a UTF-16 BOM (Byte Order mark), otherwise we assume that the header is in ASCII.
   stream.setEncoding( QTextStream::UnicodeUTF8 );
   // ### Qt4: set encoding to UTF-8 and set Unicode auto-detection

   // first read header
   ConversionStatus status = readHeader(stream);
   if(status!=OK && status != RECOVERED_PARSE_ERROR)
   {
       kdDebug(KBABEL) << "wasn't able to read header" << endl;
       return 0;
   }

   const QString head = _msgstr.first();

   QRegExp regexp("Content-Type:\\s*\\w+/[-\\w]+;?\\s*charset\\s*=\\s*(\\S+)\\s*\\\\n");
   if( regexp.search( head ) == -1 )
   {
      kdDebug(KBABEL) << "no charset entry found" << endl;
      return 0;
   }
   
   const QString charset = regexp.cap(1);
   kdDebug(KBABEL) << "charset: " << charset << endl;

   QTextCodec* codec=0;

   if(!charset.isEmpty())
   {
      // "CHARSET" is the default charset entry in a template (pot).
      // characters in a template should be either pure ascii or 
      // at least utf8, so utf8-codec can be used for both.
      if( charset == "CHARSET")
      {
          if(hadCodec)
             *hadCodec=false;

          codec=QTextCodec::codecForName("utf8");
          kdDebug(KBABEL) 
              << QString("file seems to be a template: using utf-8 encoding.")
              << endl;
      }
      else
      {
         codec=QTextCodec::codecForName(charset.latin1());
         if(hadCodec)
            *hadCodec=true;
      }

      if(!codec)
      {
         kdWarning(KBABEL) << "charset found, but no codec available, using UTF-8 instead" << endl;
	 codec=QTextCodec::codecForName("utf8");
      }
   }
   else
   {
      // No charset? So it is probably ASCII, therefore UTF-8
      kdWarning(KBABEL) << "No charset defined! Assuming UTF-8!" << endl;
      codec=QTextCodec::codecForName("utf8");
   }
         

   return codec;
}

ConversionStatus GettextImportPlugin::readHeader(QTextStream& stream)
{
   CatalogItem temp;
   int filePos=stream.device()->at();
   ConversionStatus status=readEntry(stream);

   if(status==OK || status==RECOVERED_PARSE_ERROR)
   {
      // test if this is the header
      if(!_msgid.first().isEmpty())
      {
         stream.device()->at(filePos);
      }

      return status;
   }

   return PARSE_ERROR;
}

ConversionStatus GettextImportPlugin::readEntry(QTextStream& stream)
{
   enum {Begin,Comment,Msgctxt,Msgid,Msgstr} part=Begin;

   QString line;
   bool cancelLoop=false;
   bool error=false;
   bool recoverableError=false;
   bool seenMsgctxt=false;
   _msgstr.clear();
   _msgstr.append(QString());
   _msgid.clear();
   _msgid.append(QString());
   _msgctxt=QString();
   _comment=QString();
   _gettextPluralForm=false;
   _obsolete=false;
   
   QStringList::Iterator msgstrIt=_msgstr.begin();
   
   while(!stream.eof() && !cancelLoop)
   {
       const int pos=stream.device()->at();

       line=stream.readLine();

       // ### Qt4: no need of a such a check
       if(line.isNull()) // file end
          break;
       else if ( line.startsWith( "<<<<<<<" ) || line.startsWith( "=======" ) || line.startsWith( ">>>>>>>" ) )
       {
          // We have found a CVS/SVN conflict marker. Abort.
          // (It cannot be any useful data of the PO file, as otheriwse the line would start with a quote)
          kdError(KBABEL) << "CVS/SVN conflict marker found! Aborting!" << endl << line << endl;
          return PARSE_ERROR;
       }
       
       // remove whitespaces from beginning and end of line
       line=line.stripWhiteSpace();

       if(part==Begin)
       {
           // ignore trailing newlines
           if(line.isEmpty())
              continue;

           if(line.startsWith("#~"))
           {
              _obsolete=true;
	      part=Comment;
	      _comment=line;
           }
           else if(line.startsWith("#"))
           {
               part=Comment;
               _comment=line;
           }
           else if(line.find(QRegExp("^msgctxt\\s*\".*\"$")) != -1)
           {
               part=Msgctxt;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgctxt\\s*\""));
               line.remove(QRegExp("\"$"));
               _msgctxt=line;
               seenMsgctxt=true;
           }
           else if(line.find(QRegExp("^msgid\\s*\".*\"$")) != -1)
           {
               part=Msgid;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgid\\s*\""));
               line.remove(QRegExp("\"$"));

               (*(_msgid).begin())=line;
           }
		     // one of the quotation marks is missing
           else if(line.find(QRegExp("^msgid\\s*\"?.*\"?$")) != -1)
           {
               part=Msgid;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgid\\s*\"?"));
               line.remove(QRegExp("\"$"));

               (*(_msgid).begin())=line;

               if(!line.isEmpty())
                  recoverableError=true;
           }
           else
           {
              kdDebug(KBABEL) << "no comment or msgid found after a comment: " << line << endl;

               cancelLoop=true;
               error=true;
           }
       }
       else if(part==Comment)
       {
            if(line.isEmpty() && _obsolete ) return OK;
	    if(line.isEmpty() )
	       continue;
            else if(line.startsWith("#~"))
            {
               _comment+=("\n"+line);
	       _obsolete=true;
            }
            else if(line.startsWith("#"))
            {
               _comment+=("\n"+line);
            }
            else if(line.find(QRegExp("^msgctxt\\s*\".*\"$")) != -1)
            {
               part=Msgctxt;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgctxt\\s*\""));
               line.remove(QRegExp("\"$"));
               _msgctxt=line;
               seenMsgctxt=true;
            }
            else if(line.find(QRegExp("^msgid\\s*\".*\"$")) != -1)
            {
               part=Msgid;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgid\\s*\""));
               line.remove(QRegExp("\"$"));

               (*(_msgid).begin())=line;
            }
            // one of the quotation marks is missing
            else if(line.find(QRegExp("^msgid\\s*\"?.*\"?$")) != -1)
            {
               part=Msgid;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgid\\s*\"?"));
               line.remove(QRegExp("\"$"));

               (*(_msgid).begin())=line;
			   
               if(!line.isEmpty())
                     recoverableError=true;
            }
            else
            {
               kdDebug(KBABEL) << "no comment or msgid found after a comment while parsing: " << _comment << endl;

               error=true;
               cancelLoop=true;
            }
        }
        else if(part==Msgctxt)
        {
            if(line.isEmpty())
               continue;
            else if(line.find(QRegExp("^\".*\\n?\"$")) != -1)
            {
               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^\""));
               line.remove(QRegExp("\"$"));
               
               // add Msgctxt line to item
               if(_msgctxt.isEmpty())
                  _msgctxt=line;
               else
                  _msgctxt+=("\n"+line);
            }
            else if(line.find(QRegExp("^msgid\\s*\".*\"$")) != -1)
            {
               part=Msgid;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgid\\s*\""));
               line.remove(QRegExp("\"$"));

               (*(_msgid).begin())=line;
            }
            // one of the quotation marks is missing
            else if(line.find(QRegExp("^msgid\\s*\"?.*\"?$")) != -1)
            {
               part=Msgid;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgid\\s*\"?"));
               line.remove(QRegExp("\"$"));

               (*(_msgid).begin())=line;
			   
               if(!line.isEmpty())
                     recoverableError=true;
            }
            else
            {
               kdDebug(KBABEL) << "no msgid found after a msgctxt while parsing: " << _msgctxt << endl;

               error=true;
               cancelLoop=true;
            }
        }
        else if(part==Msgid)
        {
            if(line.isEmpty())
               continue;
            else if(line.find(QRegExp("^\".*\\n?\"$")) != -1)
            {
               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^\""));
               line.remove(QRegExp("\"$"));

               QStringList::Iterator it;
               if(_gettextPluralForm)
                   it = _msgid.fromLast();
               else
                   it = _msgid.begin();
               
               // add Msgid line to item
               if((*it).isEmpty())
                  (*it)=line;
               else
                  (*it)+=("\n"+line);
            }
            else if(line.find(QRegExp("^msgid_plural\\s*\".*\"$")) != -1)
            {
               part=Msgid;
               _gettextPluralForm = true;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgid_plural\\s*\""));
               line.remove(QRegExp("\"$"));

               _msgid.append(line);
            }
            // one of the quotation marks is missing
            else if(line.find(QRegExp("^msgid_plural\\s*\"?.*\"?$")) != -1)
            {
               part=Msgid;
               _gettextPluralForm = true;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgid_plural\\s*\"?"));
               line.remove(QRegExp("\"$"));

               _msgid.append(line);
			   
               if(!line.isEmpty())
                  recoverableError=true;
            }
           else if(!_gettextPluralForm 
                    && (line.find(QRegExp("^msgstr\\s*\".*\\n?\"$")) != -1))
            {
               part=Msgstr;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgstr\\s*\"?"));
               line.remove(QRegExp("\"$"));

               (*msgstrIt)=line;
            }
            else if(!_gettextPluralForm
                    && line.find(QRegExp("^msgstr\\s*\"?.*\\n?\"?$")) != -1)
            {
               part=Msgstr;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgstr\\s*\"?"));
               line.remove(QRegExp("\"$"));

               (*msgstrIt)=line;

               if(!line.isEmpty())
                  recoverableError=true;
            }
            else if( _gettextPluralForm 
                     && (line.find(QRegExp("^msgstr\\[0\\]\\s*\".*\\n?\"$")) != -1))
            {
               part=Msgstr;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgstr\\[0\\]\\s*\"?"));
               line.remove(QRegExp("\"$"));

               (*msgstrIt)=line;
            }
            else if( _gettextPluralForm
                     && (line.find(QRegExp("^msgstr\\[0\\]\\s*\"?.*\\n?\"?$")) != -1))
            {
               part=Msgstr;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgstr\\[0\\]\\s*\"?"));
               line.remove(QRegExp("\"$"));

               (*msgstrIt)=line;

               if(!line.isEmpty())
                  recoverableError=true;
            }
 
            else if(line.startsWith("#") || line.startsWith("msgid"))
            {
               kdDebug(KBABEL) << "no msgstr or msgid found after a msgid while parsing: " << _msgid.first() << endl;

               cancelLoop=true;
               error=true;
            }
            // a line of the msgid with a missing quotation mark
            else if(line.find(QRegExp("^\"?.+\\n?\"?$")) != -1)
            {
               recoverableError=true;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^\""));
               line.remove(QRegExp("\"$"));

               QStringList::Iterator it;
               if( _gettextPluralForm )
                   it = _msgid.fromLast();
               else
                   it = _msgid.begin();
               
               // add Msgid line to item
               if((*it).isEmpty())
                  (*it)=line;
               else
                  (*it)+=("\n"+line);
            }
            else
            {
               kdDebug(KBABEL) << "no msgstr or msgid found after a msgid while parsing: " << _msgid.first() << endl;

               cancelLoop=true;
               error=true;
            }
        }
        else if(part==Msgstr)
        {
            if(line.isEmpty())
               continue;
            // another line of the msgstr
            else if(line.find(QRegExp("^\".*\\n?\"$")) != -1)
            {
               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^\""));
               line.remove(QRegExp("\"$"));

               if((*msgstrIt).isEmpty())
                  (*msgstrIt)=line;
               else
                  (*msgstrIt)+=("\n"+line);
            }
            else if( _gettextPluralForm
                     && (line.find(QRegExp("^msgstr\\[[0-9]+\\]\\s*\".*\\n?\"$")) != -1))
            {
               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgstr\\[[0-9]+\\]\\s*\"?"));
               line.remove(QRegExp("\"$"));

               msgstrIt=_msgstr.append(line);
            }
	    else if( _gettextPluralForm 
                     && (line.find(QRegExp("^msgstr\\[[0-9]\\]\\s*\"?.*\\n?\"?$")) != -1))
            {
               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^msgstr\\[[0-9]\\]\\s*\"?"));
               line.remove(QRegExp("\"$"));

               msgstrIt=_msgstr.append(line);

               if(!line.isEmpty())
                  recoverableError=true;
            }
            else if((line.find(QRegExp("^\\s*msgid")) != -1) || (line.find(QRegExp("^\\s*#")) != -1))
            {
               cancelLoop=true;
               stream.device()->at(pos);// reset position in stream to beginning of this line
               break;
            }
            else if(line.startsWith("msgstr"))
            {
               kdDebug(KBABEL) << "no msgid or comment found after a msgstr while parsing: " << _msgstr.last() << endl;

               cancelLoop=true;
               error=true;
            }
            // another line of the msgstr with a missing quotation mark
            else if(line.find(QRegExp("^\"?.+\\n?\"?$")) != -1)
            {
               recoverableError=true;

               // remove quotes at beginning and the end of the lines
               line.remove(QRegExp("^\""));
               line.remove(QRegExp("\"$"));

               if((*msgstrIt).isEmpty())
                  (*msgstrIt)=line;
               else
                  (*msgstrIt)+=("\n"+line);
            }
            else
            {
               kdDebug(KBABEL) << "no msgid or comment found after a msgstr while parsing: " << _msgstr.last() << endl;

               cancelLoop=true;
               error=true;
            }
        }
    }

/*
   if(_gettextPluralForm)
   {
       kdDebug(KBABEL) << "gettext plural form:\n"
                 << "msgid:\n" << _msgid.first() << "\n"
                 << "msgid_plural:\n" << _msgid.last() << "\n" << endl;
       int counter=0;
       for(QStringList::Iterator it = _msgstr.begin(); it != _msgstr.end(); ++it)
       {
           kdDebug(KBABEL) << "msgstr[" << counter << "]:\n" 
                     << (*it) << endl;
           counter++;
       }
   }
  */ 
    if(error)
       return PARSE_ERROR;
	else if(recoverableError)
		return RECOVERED_PARSE_ERROR;
    else
    {
      return OK;
    }
}
