summaryrefslogtreecommitdiffstats
path: root/kbabel/common/catalogitem.cpp
blob: 234637d1c7e3ded45cccbd16d162f56001e50f03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* ****************************************************************************
  This file is part of KBabel

  Copyright (C) 1999-2000 by Matthias Kiefer <matthias.kiefer@gmx.de>
		2002	  by Stanislav Visnovsky <visnovsky@nenya.ms.mff.cuni.cz>
  Copyright (C) 2006 by Nicolas GOUTTE <goutte@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 TQt library by Trolltech AS, Norway (or with modified versions
  of TQt that use the same license as TQt), 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
  TQt. 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 "catalogitem.h"
#include "catalogitem_private.h"
#include "editcmd.h"
#include "resources.h"
#include "libgettext/pofiles.h"
#include "libgettext/tokens.h"

#include "regexpextractor.h"

#include <tqtextstream.h>
#include <tqtextcodec.h>
#include <tqdom.h>

using namespace KBabel;

CatalogItem::CatalogItem(Project::Ptr project)
{
   d=0;
   clear();
   d->_project = project;
}

CatalogItem::CatalogItem(const CatalogItem& item)
{
   d=0;
   clear();
   *d=*(item.d);
}

CatalogItem::~CatalogItem()
{
    delete d;
}

TQString CatalogItem::comment() const
{
    return d->_comment;
}

TQString CatalogItem::msgctxt(const bool noNewlines) const
{
    if( noNewlines )
    {
        TQString tmp = d->_msgctxt;
        tmp.replace("\n", " "); // ### TODO: " " or "" ?
        return tmp;
    }
    else
        return d->_msgctxt;
}

TQStringList CatalogItem::msgid(const bool noNewlines) const
{
    TQStringList result=d->_msgid;
    if( noNewlines )
    {
        result.gres( "\n", "" );
    }
    return result;
}

TQStringList CatalogItem::msgstr(const bool noNewlines) const
{
    TQStringList result=d->_msgstr;
    if( noNewlines )
    {
        result.gres( "\n", "" );
    }
    return result;
}

TQStringList CatalogItem::msgstrAsList(int nr) const
{
   TQString str;
   if(d->_gettextPluralForm && nr > 0)
   {
       TQStringList::ConstIterator it = d->_msgstr.at(nr);
       if(it != d->_msgstr.end())
       {
           str = *it;
       }
       else
       {
           kdDebug(KBABEL) << "request for non existing plural form "
               << nr << endl;
       }
   }
   else
   {
       str = d->_msgstr.first();
   }

   TQStringList list=TQStringList::split("\n",str);

   if(str.left(1)=="\n")
      list.prepend("");

   if(list.isEmpty())
      list.append("");

   return list;
}

bool CatalogItem::isValid() const
{
    return d->_valid;
}

void CatalogItem::setMsgctxt(TQString msg)
{
    d->_msgctxt=msg;
}

void CatalogItem::setMsgid(TQString msg)
{
    d->_msgid=msg;
}

void CatalogItem::setMsgid(TQStringList msg)
{
    d->_msgid=msg;
}

void CatalogItem::setMsgstr(TQString msg)
{
    d->_msgstr=msg;
}

void CatalogItem::setMsgstr(TQStringList msg)
{
    d->_msgstr=msg;
}

void CatalogItem::setComment(TQString com)
{
    d->_comment=com;
}

void CatalogItem::setGettextPluralForm( bool _gettextPlural )
{
    d->_gettextPluralForm = _gettextPlural;
}

TQStringList CatalogItem::errors() const
{
    return d->_errors;
}

TQStringList CatalogItem::tagList( RegExpExtractor& te)
{
    if(!d->_haveTagList)
    {
	// FIXME: should care about plural forms in msgid
        te.setString(msgid(true).first());
        d->_tagList = TQStringList(te.matches());
	d->_haveTagList = true;
    }

    return d->_tagList;
}

TQStringList CatalogItem::argList( RegExpExtractor& te)
{
    if(!d->_haveArgList)
    {
	// FIXME: should care about plural forms in msgid
        te.setString(msgid(true).first());
        d->_argList = TQStringList(te.matches());
    }

    return d->_argList;
}


bool CatalogItem::isFuzzy() const
{
    return d->_comment.find( TQRegExp(",\\s*fuzzy") ) != -1;
}

bool CatalogItem::isCformat() const
{
    // Allow "possible-c-format" (from xgettext --debug) or "c-format"
    // Note the regexp (?: ) is similar to () but it does not capture (so it is faster)
    return d->_comment.find( TQRegExp(",\\s*(?:possible-)c-format") ) == -1;
}

bool CatalogItem::isNoCformat() const
{
    return d->_comment.find( TQRegExp(",\\s*no-c-format") ) == -1;
}

bool CatalogItem::isTQtformat() const
{
    return d->_comment.find( TQRegExp(",\\s*qt-format") ) == -1;
}

bool CatalogItem::isNoTQtformat() const
{
    return d->_comment.find( TQRegExp(",\\s*no-qt-format") ) == -1;
}

bool CatalogItem::isUntranslated() const
{
   return d->_msgstr.first().isEmpty();
}

PluralFormType CatalogItem::pluralForm() const
{
    if( d->_gettextPluralForm )
        return Gettext;
    if( d->_msgid.first().startsWith( "_n: " ) )
        return KDESpecific;
    else
        return NoPluralForm;
}

int CatalogItem::totalLines() const
{
   int lines=0;
   if(!d->_comment.isEmpty())
   {
      lines = d->_comment.contains('\n')+1;
   }
   int msgctxtLines=0;
   if(!d->_msgctxt.isEmpty())
   {
      msgctxtLines=d->_msgctxt.contains('\n')+1;
   }
   int msgidLines=0;
   TQStringList::ConstIterator it;
   for(it=d->_msgid.begin(); it != d->_msgid.end(); ++it)
   {
      msgidLines += (*it).contains('\n')+1;
   }
   int msgstrLines=0;
   for(it=d->_msgstr.begin(); it != d->_msgstr.end(); ++it)
   {
      msgstrLines += (*it).contains('\n')+1;
   }

   if(msgctxtLines>1)
      msgctxtLines++;
   if(msgidLines>1)
      msgidLines++;
   if(msgstrLines>1)
      msgstrLines++;

   lines+=( msgctxtLines+msgidLines+msgstrLines );

   return lines;
}


void CatalogItem::setSyntaxError(bool on)
{
	if(on && !d->_errors.contains("syntax error"))
		d->_errors.append("syntax error");
	else
		d->_errors.remove("syntax error");
}

TQPtrList<EditCommand> CatalogItem::removeFuzzy(bool doIt)
{
   TQPtrList<EditCommand> editList;
   editList.setAutoDelete(false);

   TQString comment=d->_comment;

   if(isFuzzy())
   {
       EditCommand *cmd;
       TQRegExp fuzzyStr(",\\s*fuzzy");

       int offset;
       offset=comment.find(fuzzyStr);
       while(offset>=0)
       {
          cmd = new DelTextCmd(offset,fuzzyStr.cap(),0);
          cmd->setPart(Comment);
          editList.append(cmd);

          comment.remove(offset,fuzzyStr.cap().length());

          offset=comment.find(fuzzyStr,offset+1);
       }

       // remove empty comment lines
       if( comment.contains( TQRegExp("^#\\s*$") ))
       {
          cmd = new DelTextCmd(0,comment,0);
          cmd->setPart(Comment);
          editList.append(cmd);

          comment="";
       }
       if( comment.contains( TQRegExp("\n#\\s*$") ))
       {
          offset=comment.find( TQRegExp("\n#\\s*$") );
          while(offset>=0)
          {
             cmd = new DelTextCmd(offset,comment.mid(offset),0);
             cmd->setPart(Comment);
             editList.append(cmd);

             comment.remove(offset,comment.length()-offset);

             offset=comment.find( TQRegExp("\n#\\s*$"), offset+1 );
          }
       }
       if( comment.contains( TQRegExp("\n#\\s*\n") ))
       {
          offset=comment.find( TQRegExp("\n#\\s*\n") )+1;
          while(offset>=0)
          {
             int endIndex=comment.find("\n",offset)+1;

             cmd = new DelTextCmd(offset,comment.mid(offset,endIndex-offset),0);
             cmd->setPart(Comment);
             editList.append(cmd);

             comment.remove(offset,endIndex-offset);

             offset=comment.find( TQRegExp("\n#\\s*\n"), offset+1 );
          }
       }

       if(doIt)
          d->_comment=comment;

   }

   return editList;
}



TQPtrList<EditCommand> CatalogItem::addFuzzy(bool doIt)
{
   TQPtrList<EditCommand> editList;
   editList.setAutoDelete(false);


   if(!isFuzzy())
   {
       EditCommand *cmd;
       int offset=d->_comment.length();

       TQString addStr;
       if(offset > 0 && d->_comment[offset-1] != '\n')
       {
           addStr='\n';
       }
       addStr+="#, fuzzy";

       cmd = new InsTextCmd(offset,addStr,0);
       cmd->setPart(Comment);
       editList.append(cmd);


       if(doIt)
          d->_comment+=addStr;
   }

   return editList;
}


void CatalogItem::processCommand(EditCommand* cmd, bool undo)
{
    kdDebug(KBABEL) << "CatalogItem::processCommand" << endl;
    if(cmd->terminator()!=0)
       return;

    DelTextCmd* delcmd = (DelTextCmd*) cmd;

    bool ins =  true;
    if (delcmd->type() == EditCommand::Delete )
       ins = undo;
    else if (delcmd->type() == EditCommand::Insert )
       ins = !undo;
    else
    {
       kdDebug(KBABEL) << "what kind of edit command is this?" << endl;
       return;
    }

    if ( ins )
    {
       if(delcmd->part()==Msgstr)
       {
          if(delcmd->pluralNumber >= (int)d->_msgstr.count() )
	  {
	      for( uint i = d->_msgstr.count() ; i < (uint)(delcmd->pluralNumber+1) ; i++ )
	          d->_msgstr.append( "" );
	  }

	  (*(d->_msgstr).at(delcmd->pluralNumber)).insert(delcmd->offset,delcmd->str);
       }
       else if(delcmd->part()==Comment)
       {
          d->_comment.insert(delcmd->offset,delcmd->str);
       }
    }
    else
    { // del
       if(delcmd->part()==Msgstr)
       {
          if(delcmd->pluralNumber >= (int)d->_msgstr.count() )
	  {
	      for( uint i = d->_msgstr.count() ; i < (uint)(delcmd->pluralNumber+1) ; i++ )
	          d->_msgstr.append( "" );
	  }

          (*(d->_msgstr).at(delcmd->pluralNumber)).remove(delcmd->offset,delcmd->str.length());
       }
       else if(delcmd->part()==Comment)
       {
          d->_comment.remove(delcmd->offset,delcmd->str.length());
       }
    }
}


void CatalogItem::clear()
{
   if( !d ) d = new CatalogItemPrivate();
   else {
      d->_msgid.clear();
      d->_msgstr.clear();
      d->_errors.clear();

      d->_tagList.clear();
      d->_argList.clear();
   }
   d->_comment="";
   d->_msgctxt="";
   d->_valid=true;
   d->_gettextPluralForm=false;
   d->_haveTagList=false;
   d->_haveArgList=false;

   d->_msgid.append("");
   d->_msgstr.append("");
}

void CatalogItem::operator=(const CatalogItem& rhs)
{
    d->_comment = rhs.d->_comment;
    d->_msgctxt = rhs.d->_msgctxt;
    d->_msgid = rhs.d->_msgid;
    d->_msgstr = rhs.d->_msgstr;
    d->_valid = rhs.d->_valid;
    d->_errors = rhs.d->_errors;
    d->_gettextPluralForm = rhs.d->_gettextPluralForm;
}

TQString CatalogItem::nextError() const
{
    return d->_errors.first();
}

void CatalogItem::clearErrors()
{
    d->_errors.clear();
}

void CatalogItem::appendError(const TQString& error )
{
    if( !d->_errors.contains( error ) )
	d->_errors.append(error);
}

void CatalogItem::removeError(const TQString& error )
{
    d->_errors.remove(error);
}

Project::Ptr CatalogItem::project() const
{
    return d->_project;
}

void CatalogItem::setProject( Project::Ptr project )
{
    d->_project = project;
}

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