You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
278 lines
7.2 KiB
278 lines
7.2 KiB
15 years ago
|
/* This file is part of KBabel
|
||
|
based Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
|
||
|
2002-2003 Stanislav Visnovsky <visnovsky@kde.org>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library 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
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
15 years ago
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
15 years ago
|
|
||
|
In addition, as a special exception, the copyright holders give
|
||
|
permission to link the code of this program with any edition of
|
||
14 years ago
|
the TQt library by Trolltech AS, Norway (or with modified versions
|
||
|
of TQt that use the same license as TQt), and distribute linked
|
||
15 years ago
|
combinations including the two. You must obey the GNU General
|
||
|
Public License in all respects for all of the code used other than
|
||
14 years ago
|
TQt. If you modify this file, you may extend this exception to
|
||
15 years ago
|
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 <resources.h>
|
||
|
#include "catalogitem.h"
|
||
|
#include "catalogsettings.h"
|
||
|
#include "main.h"
|
||
|
|
||
12 years ago
|
#include <tdeconfig.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
|
#include <kgenericfactory.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
|
#include <tdemessagebox.h>
|
||
15 years ago
|
|
||
|
/***************************************************
|
||
|
*
|
||
|
* Factory
|
||
|
*
|
||
|
***************************************************/
|
||
|
|
||
|
K_EXPORT_COMPONENT_FACTORY( kbabel_argstool, KGenericFactory<ArgumentTool> ( "kbabeldatatool" ) )
|
||
|
|
||
|
using namespace KBabel;
|
||
|
|
||
13 years ago
|
ArgumentTool::ArgumentTool( TQObject* parent, const char* name, const TQStringList & )
|
||
|
: KDataTool( parent, name ), _cache_origin( 0 )
|
||
15 years ago
|
{
|
||
|
i18n( "what check found errors","arguments");
|
||
|
}
|
||
|
|
||
15 years ago
|
bool ArgumentTool::run( const TQString& command, void* data, const TQString& datatype, const TQString& mimetype )
|
||
15 years ago
|
{
|
||
|
if ( command != "validate" )
|
||
|
{
|
||
|
kdDebug(KBABEL) << "Argument Tool does only accept the command 'validate' and 'shortcut'" << endl;
|
||
|
kdDebug(KBABEL) << " The commands " << command << " is not accepted" << endl;
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
// Check wether we can accept the data
|
||
|
if ( datatype != "CatalogItem" )
|
||
|
{
|
||
|
kdDebug(KBABEL) << "Argument Tool only accepts datatype CatalogItem" << endl;
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
if ( mimetype != "application/x-kbabel-catalogitem" )
|
||
|
{
|
||
|
kdDebug(KBABEL) << "Argument Tool only accepts mimetype application/x-kbabel-catalogitem" << endl;
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
if( command == "validate" )
|
||
|
{
|
||
|
CatalogItem* item = (CatalogItem*)(data);
|
||
|
|
||
|
if( item->isNoCformat() ) return true;
|
||
|
bool hasError = false;
|
||
|
|
||
|
if( _cache_origin != item->project() )
|
||
|
{
|
||
|
_context = item->project()->miscSettings().contextInfo;
|
||
|
_checkPlurals = item->project()->identitySettings().checkPluralArgument;
|
||
|
_cache_origin = item->project();
|
||
|
}
|
||
|
|
||
|
if(!item->isUntranslated())
|
||
|
{
|
||
|
|
||
15 years ago
|
TQString formatChars="dioxXucsfeEgGp%";
|
||
15 years ago
|
|
||
|
if( _checkPlurals ) formatChars+="n";
|
||
|
|
||
|
// FIXME: this should care about plural forms in msgid
|
||
15 years ago
|
TQString line=item->msgid().first();
|
||
|
TQStringList argList;
|
||
15 years ago
|
|
||
|
// if( isPluralForm() )
|
||
|
{
|
||
|
// FIXME: this is KDE specific
|
||
|
if( line.startsWith("_n:" ))
|
||
|
{
|
||
|
// truncate text after first \n to get args only once
|
||
13 years ago
|
line = line.mid(0,line.find("\\n"));
|
||
15 years ago
|
}
|
||
|
}
|
||
13 years ago
|
line.replace(_context, "");
|
||
|
line.replace(TQRegExp("\\n"),"");
|
||
15 years ago
|
line.simplifyWhiteSpace();
|
||
|
|
||
|
// flag, for GNU only we can allow reordering
|
||
|
bool non_gnu = (item->pluralForm() == KDESpecific);
|
||
|
|
||
13 years ago
|
int index=line.find(TQRegExp("%."));
|
||
15 years ago
|
|
||
|
while(index>=0)
|
||
|
{
|
||
13 years ago
|
int endIndex=line.find(TQRegExp("[^\\d]"),index+1);
|
||
15 years ago
|
if(endIndex<0)
|
||
|
{
|
||
|
endIndex=line.length();
|
||
|
}
|
||
13 years ago
|
else if( formatChars.contains(line[endIndex]) )
|
||
15 years ago
|
{
|
||
|
endIndex++;
|
||
|
}
|
||
|
|
||
|
if(endIndex - index > 1 ) {
|
||
15 years ago
|
TQString arg = line.mid(index,endIndex-index);
|
||
13 years ago
|
if( arg.contains( TQRegExp("\\d") ) ) {
|
||
15 years ago
|
non_gnu = true;
|
||
|
}
|
||
|
argList.append(arg);
|
||
|
|
||
|
}
|
||
|
|
||
13 years ago
|
index=line.find(TQRegExp("%."),endIndex);
|
||
15 years ago
|
}
|
||
|
|
||
|
if( item->pluralForm()==KDESpecific)
|
||
|
{
|
||
|
// FIXME: this is KDE specific
|
||
13 years ago
|
if( _checkPlurals && line.startsWith("_n:" ) && !argList.contains("%n") )
|
||
15 years ago
|
{
|
||
|
argList.append("%n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// generate for each plural form to be checked separately
|
||
|
line=item->msgstr().first();
|
||
15 years ago
|
TQStringList lines;
|
||
15 years ago
|
|
||
|
// FIXME; this is KDE specific
|
||
|
if( item->pluralForm() == KDESpecific )
|
||
|
{
|
||
15 years ago
|
lines = TQStringList::split("\\n",line);
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
lines.append(line);
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList argCache = argList;
|
||
|
TQStringList foundArgs;
|
||
15 years ago
|
|
||
15 years ago
|
for(TQStringList::Iterator i = lines.begin() ; i!=lines.end() ; i++)
|
||
15 years ago
|
{
|
||
|
// initialize for the next plural form
|
||
|
foundArgs.clear();
|
||
|
argList = argCache;
|
||
|
|
||
|
line=(*i);
|
||
|
|
||
13 years ago
|
line.replace(TQRegExp("\\n"),"");
|
||
15 years ago
|
|
||
15 years ago
|
TQRegExp argdesc(
|
||
15 years ago
|
"%((["+formatChars+"])"
|
||
|
+"|(\\d)+"
|
||
|
+"|(\\d)+\\$(["+formatChars+"])"
|
||
|
+")" ) ;
|
||
|
index = -1;
|
||
|
|
||
|
do {
|
||
|
|
||
|
index = argdesc.search( line, index+1 );
|
||
|
|
||
|
if( index == -1 ) break;
|
||
|
|
||
|
// do not add a redundant argument, if it is non GNU
|
||
13 years ago
|
if( !non_gnu || !foundArgs.contains( argdesc.cap(0) ) )
|
||
15 years ago
|
foundArgs.append( argdesc.cap( 0 ) );
|
||
|
} while( true );
|
||
|
|
||
|
// now, compare the list
|
||
|
argList = argCache;
|
||
|
|
||
|
if( non_gnu ) {
|
||
15 years ago
|
for ( TQStringList::Iterator it = foundArgs.begin(); it != foundArgs.end(); ++it ) {
|
||
13 years ago
|
if( argList.find( *it ) == argList.end() ) {
|
||
15 years ago
|
hasError = true;
|
||
|
break;
|
||
|
} else {
|
||
|
argList.remove( *it );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if( ! argList.isEmpty() ) {
|
||
|
hasError = true;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// handle GNU with replacements
|
||
15 years ago
|
TQStringList::Iterator oit = argList.begin();
|
||
|
for ( TQStringList::Iterator it = foundArgs.begin(); it != foundArgs.end(); ++it , ++oit) {
|
||
15 years ago
|
if( *it == *oit ) {
|
||
|
// argument is the same, mark as used
|
||
14 years ago
|
*oit = TQString();
|
||
15 years ago
|
} else {
|
||
|
// try to strip replacement
|
||
13 years ago
|
int index = (*it).find( '$' );
|
||
15 years ago
|
if( index == -1 ) {
|
||
|
// there is no replacement, this is wrong
|
||
|
hasError = true;
|
||
|
break;
|
||
|
}
|
||
15 years ago
|
TQString place = (*it).mid( 1, index-1 );
|
||
|
TQString arg = (*it).right( index );
|
||
15 years ago
|
arg[0] = '%';
|
||
15 years ago
|
TQStringList::Iterator a = argList.at( place.toInt()-1 );
|
||
15 years ago
|
if( a != argList.end() && (*a) == arg )
|
||
|
{
|
||
14 years ago
|
(*a) = TQString();
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
// duplicate or index is too high
|
||
|
hasError = true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
for ( TQStringList::Iterator it = argList.begin(); it != argList.end(); ++it) {
|
||
15 years ago
|
if( ! (*it).isNull () ) {
|
||
|
// argument is the same, mark as used
|
||
|
hasError = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(hasError)
|
||
|
{
|
||
|
item->appendError( "Arguments" );
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
item->removeError( "Arguments" );
|
||
|
}
|
||
|
|
||
|
return !hasError;
|
||
|
}
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
#include "main.moc"
|