• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

lokalize

  • sources
  • kde-4.14
  • kdesdk
  • lokalize
  • src
  • catalog
  • gettext
catalogitem.cpp
Go to the documentation of this file.
1 /* ****************************************************************************
2  This file is based on the one from KBabel
3 
4  Copyright (C) 1999-2000 by Matthias Kiefer <matthias.kiefer@gmx.de>
5  2002 by Stanislav Visnovsky <visnovsky@nenya.ms.mff.cuni.cz>
6  Copyright (C) 2006 by Nicolas GOUTTE <goutte@kde.org>
7  2007-2012 by Nick Shaforostoff <shafff@ukr.net>
8 
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 
23  In addition, as a special exception, the copyright holders give
24  permission to link the code of this program with any edition of
25  the Qt library by Trolltech AS, Norway (or with modified versions
26  of Qt that use the same license as Qt), and distribute linked
27  combinations including the two. You must obey the GNU General
28  Public License in all respects for all of the code used other than
29  Qt. If you modify this file, you may extend this exception to
30  your version of the file, but you are not obligated to do so. If
31  you do not wish to do so, delete this exception statement from
32  your version.
33 
34 **************************************************************************** */
35 
36 #include "catalogitem.h"
37 #include "catalogitem_private.h"
38 
39 #include <kdebug.h>
40 #include <QMutexLocker>
41 
42 using namespace GettextCatalog;
43 
44 CatalogItem::CatalogItem()
45  : d(new CatalogItemPrivate())
46 {
47 }
48 
49 CatalogItem::CatalogItem(const CatalogItem& item)
50  : d(new CatalogItemPrivate())
51 {
52  *d=*(item.d);
53 }
54 
55 CatalogItem::~CatalogItem()
56 {
57  delete d;
58 }
59 
60 
61 
62 QString CatalogItem::comment() const
63 {
64  return QString::fromUtf8(d->_comment);
65 }
66 
67 const QString& CatalogItem::msgctxt(const bool noNewlines) const
68 {
69  if (noNewlines) return (d->_msgctxt).replace('\n', ' '); //" " or "" ?
70  else return d->_msgctxt;
71 }
72 
73 const QString& CatalogItem::msgid(const int form) const
74 {
75  return d->msgid(form);
76 }
77 
78 const QString& CatalogItem::msgstr(const int form) const
79 {
80  if (KDE_ISLIKELY (form<d->_msgstrPlural.size()))
81  return d->_msgstrPlural.at(form);
82  else
83  return d->_msgstrPlural.last();
84 }
85 
86 bool CatalogItem::prependEmptyForMsgid(const int form) const
87 {
88  return d->_prependMsgIdEmptyLine;
89 }
90 
91 bool CatalogItem::prependEmptyForMsgstr(const int form) const
92 {
93  return d->_prependMsgStrEmptyLine;
94 }
95 
96 const QVector<QString>& CatalogItem::msgstrPlural() const
97 {
98  return d->_msgstrPlural;
99 }
100 
101 QStringList CatalogItem::allPluralForms(CatalogItem::Part part, bool stripNewLines) const
102 {
103  QStringList result=(part==CatalogItem::Source?d->_msgidPlural:d->_msgstrPlural).toList();
104  if (stripNewLines)
105  {
106  static QString nl="\n";
107  result.replaceInStrings(nl, QString());
108  }
109  return result;
110 }
111 
112 bool CatalogItem::isValid() const
113 {
114  return d->_valid;
115 }
116 
117 void CatalogItem::setValid(bool a)
118 {
119  d->_valid=a;
120 }
121 
122 void CatalogItem::setMsgctxt(const QString& msg)
123 {
124  d->_msgctxt=msg;
125  d->_msgctxt.squeeze();
126  d->_keepEmptyMsgCtxt=msg.isEmpty();
127 }
128 
129 bool CatalogItem::keepEmptyMsgCtxt() const
130 {
131  return d->_keepEmptyMsgCtxt;
132 }
133 
134 void CatalogItem::setMsgid(const QString& msg, const int form)
135 {
136  if (form>=d->_msgidPlural.size())
137  d->_msgidPlural.resize(form+1);
138  d->_msgidPlural[form]=msg;
139 }
140 
141 void CatalogItem::setMsgid(const QStringList& msg)
142 {
143  d->_msgidPlural=msg.toVector(); //TODO
144  for (QVector<QString>::iterator it=d->_msgidPlural.begin();it!=d->_msgidPlural.end();++it)
145  it->squeeze();
146 }
147 
148 void CatalogItem::setMsgid(const QStringList& msg, bool prependEmptyLine)
149 {
150  d->_prependMsgIdEmptyLine=prependEmptyLine;
151  d->_msgidPlural=msg.toVector(); //TODO
152  for (QVector<QString>::iterator it=d->_msgidPlural.begin();it!=d->_msgidPlural.end();++it)
153  it->squeeze();
154 }
155 
156 void CatalogItem::setMsgid(const QVector<QString>& msg)
157 {
158  d->_msgidPlural=msg;
159  for (QVector<QString>::iterator it=d->_msgidPlural.begin();it!=d->_msgidPlural.end();++it)
160  it->squeeze();
161 }
162 
163 void CatalogItem::setMsgstr(const QString& msg, const int form)
164 {
165  if (form>=d->_msgstrPlural.size())
166  d->_msgstrPlural.resize(form+1);
167  d->_msgstrPlural[form]=msg;
168 }
169 
170 void CatalogItem::setMsgstr(const QStringList& msg)
171 {
172  //TODO
173  d->_msgstrPlural=msg.toVector();
174 }
175 
176 void CatalogItem::setMsgstr(const QStringList& msg, bool prependEmptyLine)
177 {
178  d->_prependMsgStrEmptyLine=prependEmptyLine;
179  d->_msgstrPlural=msg.toVector();
180 }
181 
182 void CatalogItem::setMsgstr(const QVector<QString>& msg)
183 {
184  d->_msgstrPlural=msg;
185 }
186 
187 void CatalogItem::setComment(const QString& com)
188 {
189  static QMutex reMutex;
190  QMutexLocker reLock(&reMutex); //avoid crash #281033
191  static QRegExp fuzzyRegExp("((?:^|\n)#(?:,[^,]*)*),\\s*fuzzy");
192  d->_fuzzyCached=com.contains( fuzzyRegExp );
193  d->_comment=com.toUtf8();
194  d->_comment.squeeze();
195 }
196 
197 void CatalogItem::setPlural(bool plural)
198 {
199  d->_plural=plural;
200 }
201 
202 bool CatalogItem::isPlural() const
203 {
204  return d->_plural;
205 }
206 
207 bool CatalogItem::isFuzzy() const
208 {
209  return d->_fuzzyCached;
210 }
211 
212 bool CatalogItem::isUntranslated() const
213 {
214  return d->isUntranslated();
215 }
216 
217 bool CatalogItem::isUntranslated(uint form) const
218 {
219  return d->isUntranslated(form);
220 }
221 
222 #if 0
223 QStringList CatalogItem::errors() const
224 {
225  return d->_errors;
226 }
227 
228 bool CatalogItem::isCformat() const
229 {
230  // Allow "possible-c-format" (from xgettext --debug) or "c-format"
231  // Note the regexp (?: ) is similar to () but it does not capture (so it is faster)
232  return d->_comment.indexOf( QRegExp(",\\s*(?:possible-)c-format") ) == -1;
233 }
234 
235 bool CatalogItem::isNoCformat() const
236 {
237  return d->_comment.indexOf( QRegExp(",\\s*no-c-format") ) == -1;
238 }
239 
240 bool CatalogItem::isQtformat() const
241 {
242  return d->_comment.indexOf( QRegExp(",\\s*qt-format") ) == -1;
243 }
244 
245 bool CatalogItem::isNoQtformat() const
246 {
247  return d->_comment.indexOf( QRegExp(",\\s*no-qt-format") ) == -1;
248 }
249 
250 bool CatalogItem::isUntranslated() const
251 {
252  return d->_msgstr.first().isEmpty();
253 }
254 
255 int CatalogItem::totalLines() const
256 {
257  int lines=0;
258  if(!d->_comment.isEmpty())
259  {
260  lines = d->_comment.count('\n')+1;
261  }
262  int msgctxtLines=0;
263  if(!d->_msgctxt.isEmpty())
264  {
265  msgctxtLines=d->_msgctxt.count('\n')+1;
266  }
267  int msgidLines=0;
268  QStringList::ConstIterator it;
269  for(it=d->_msgid.begin(); it != d->_msgid.end(); ++it)
270  {
271  msgidLines += (*it).count('\n')+1;
272  }
273  int msgstrLines=0;
274  for(it=d->_msgstr.begin(); it != d->_msgstr.end(); ++it)
275  {
276  msgstrLines += (*it).count('\n')+1;
277  }
278 
279  if(msgctxtLines>1)
280  msgctxtLines++;
281  if(msgidLines>1)
282  msgidLines++;
283  if(msgstrLines>1)
284  msgstrLines++;
285 
286  lines+=( msgctxtLines+msgidLines+msgstrLines );
287 
288  return lines;
289 }
290 
291 
292 void CatalogItem::setSyntaxError(bool on)
293 {
294  if(on && !d->_errors.contains("syntax error"))
295  d->_errors.append("syntax error");
296  else
297  d->_errors.removeAll("syntax error");
298 }
299 
300 #endif
301 
302 void CatalogItem::clear()
303 {
304  d->clear();
305 }
306 
307 void CatalogItem::operator=(const CatalogItem& rhs)
308 {
309  d->assign(*rhs.d);
310 }
311 
312 
313 QStringList CatalogItem::msgstrAsList() const
314 {
315  if (d->_msgstrPlural.isEmpty())
316  {
317  kWarning()<<"This should never happen!";
318  return QStringList();
319  }
320  QStringList list(d->_msgstrPlural.first().split('\n', QString::SkipEmptyParts ));
321 
322  if(d->_msgstrPlural.first()=="\n")
323  list.prepend(QString());
324 
325  if(list.isEmpty())
326  list.append(QString());
327 
328  return list;
329 }
330 
331 
332 
333 void CatalogItem::setFuzzy()
334 {
335  d->_fuzzyCached=true;
336 
337  if (d->_comment.isEmpty())
338  {
339  d->_comment="#, fuzzy";
340  return;
341  }
342 
343  int p=d->_comment.indexOf("#,");
344  if(p!=-1)
345  {
346  d->_comment.replace(p,2,"#, fuzzy,");
347  return;
348  }
349 
350  QString comment=QString::fromUtf8(d->_comment);
351  static QRegExp a("\\#\\:[^\n]*\n");
352  p=a.indexIn(comment);
353  if (p!=-1)
354  {
355  d->_comment=comment.insert(p+a.matchedLength(),"#, fuzzy\n").toUtf8();
356  return;
357  }
358 
359  if( !(d->_comment.endsWith('\n')) )
360  d->_comment+='\n';
361  d->_comment+="#, fuzzy";
362 }
363 
364 void CatalogItem::unsetFuzzy()
365 {
366  d->_fuzzyCached=false;
367 
368  QString comment=QString::fromUtf8(d->_comment);
369 
370  static const QRegExp rmFuzzyRe(",\\s*fuzzy");
371  comment.remove( rmFuzzyRe );
372 
373  // remove empty comment lines
374  comment.remove( QRegExp("\n#\\s*$") );
375  comment.remove( QRegExp("^#\\s*$") );
376  comment.remove( QRegExp("#\\s*\n") );
377  comment.remove( QRegExp("^#\\s*\n") );
378 
379  d->_comment=comment.toUtf8();
380 }
381 
382 
383 
384 
385 #if 0
386 QString CatalogItem::nextError() const
387 {
388  return d->_errors.first();
389 }
390 
391 void CatalogItem::clearErrors()
392 {
393  d->_errors.clear();
394 }
395 
396 void CatalogItem::appendError(const QString& error )
397 {
398  if( !d->_errors.contains( error ) )
399  d->_errors.append(error);
400 }
401 
402 void CatalogItem::removeError(const QString& error )
403 {
404  d->_errors.removeAt( d->_errors.indexOf( error ) );
405 }
406 #endif
407 
408 // kate: space-indent on; indent-width 4; replace-tabs on;
GettextCatalog::CatalogItemPrivate::_valid
bool _valid
Definition: catalogitem_private.h:62
GettextCatalog::CatalogItem::isValid
bool isValid() const
Definition: catalogitem.cpp:112
QByteArray::squeeze
void squeeze()
GettextCatalog::CatalogItem::msgstrAsList
QStringList msgstrAsList() const
Definition: catalogitem.cpp:313
GettextCatalog::CatalogItem::isQtformat
bool isQtformat() const
QMutex
GettextCatalog::CatalogItem::clear
void clear()
cleares the item
Definition: catalogitem.cpp:302
GettextCatalog::CatalogItem::setMsgid
void setMsgid(const QString &msg, const int form=0)
Definition: catalogitem.cpp:134
GettextCatalog::CatalogItem::setSyntaxError
void setSyntaxError(bool)
QVector::begin
iterator begin()
GettextCatalog::CatalogItem::msgid
const QString & msgid(const int form=0) const
Definition: catalogitem.cpp:73
GettextCatalog::CatalogItem::operator=
void operator=(const CatalogItem &rhs)
Definition: catalogitem.cpp:307
catalogitem_private.h
GettextCatalog::CatalogItem
This class represents an entry in a catalog.
Definition: catalogitem.h:55
QVector::last
T & last()
GettextCatalog::CatalogItem::isFuzzy
bool isFuzzy() const
Definition: catalogitem.cpp:207
QByteArray::isEmpty
bool isEmpty() const
catalogitem.h
GettextCatalog::CatalogItem::prependEmptyForMsgid
bool prependEmptyForMsgid(const int form=0) const
Definition: catalogitem.cpp:86
QList::toVector
QVector< T > toVector() const
QVector::first
T & first()
QString::remove
QString & remove(int position, int n)
GettextCatalog::CatalogItemPrivate::assign
void assign(const CatalogItemPrivate &other)
Definition: catalogitem_private.h:106
GettextCatalog::CatalogItem::CatalogItem
CatalogItem()
Definition: catalogitem.cpp:44
GettextCatalog::CatalogItemPrivate::_keepEmptyMsgCtxt
bool _keepEmptyMsgCtxt
Definition: catalogitem_private.h:66
GettextCatalog::CatalogItem::keepEmptyMsgCtxt
bool keepEmptyMsgCtxt() const
Definition: catalogitem.cpp:129
GettextCatalog::CatalogItem::totalLines
int totalLines() const
returns the number of lines, the entry will need in a file
QRegExp
QByteArray::indexOf
int indexOf(char ch, int from) const
GettextCatalog::CatalogItemPrivate::_prependMsgIdEmptyLine
bool _prependMsgIdEmptyLine
Definition: catalogitem_private.h:64
GettextCatalog::CatalogItem::setPlural
void setPlural(bool plural=true)
Definition: catalogitem.cpp:197
GettextCatalog::CatalogItem::Source
Definition: catalogitem.h:88
QString::fromUtf8
QString fromUtf8(const char *str, int size)
QString::insert
QString & insert(int position, QChar ch)
QVector::resize
void resize(int size)
GettextCatalog::CatalogItem::prependEmptyForMsgstr
bool prependEmptyForMsgstr(const int form=0) const
Definition: catalogitem.cpp:91
GettextCatalog::CatalogItem::setMsgctxt
void setMsgctxt(const QString &msg)
Definition: catalogitem.cpp:122
QString::isEmpty
bool isEmpty() const
QByteArray::replace
QByteArray & replace(int pos, int len, const char *after)
GettextCatalog::CatalogItem::isNoQtformat
bool isNoQtformat() const
QByteArray::count
int count(char ch) const
QStringList::replaceInStrings
QStringList & replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs)
QString
QStringList
GettextCatalog::CatalogItem::isUntranslated
bool isUntranslated() const
Definition: catalogitem.cpp:212
GettextCatalog::CatalogItemPrivate::_msgctxt
QString _msgctxt
Definition: catalogitem_private.h:69
QVector::squeeze
void squeeze()
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
GettextCatalog::CatalogItemPrivate::msgid
const QString & msgid(const int form) const
Definition: catalogitem_private.h:138
GettextCatalog::CatalogItemPrivate::_plural
bool _plural
Definition: catalogitem_private.h:61
GettextCatalog::CatalogItemPrivate::isUntranslated
bool isUntranslated() const
Definition: catalogitem_private.h:119
GettextCatalog::CatalogItem::comment
QString comment() const
Definition: catalogitem.cpp:62
GettextCatalog::CatalogItem::msgstrPlural
const QVector< QString > & msgstrPlural() const
Definition: catalogitem.cpp:96
GettextCatalog::CatalogItem::allPluralForms
QStringList allPluralForms(CatalogItem::Part, bool stripNewLines=false) const
Definition: catalogitem.cpp:101
QString::replace
QString & replace(int position, int n, QChar after)
QVector::at
const T & at(int i) const
GettextCatalog::CatalogItem::~CatalogItem
~CatalogItem()
Definition: catalogitem.cpp:55
QVector< QString >
GettextCatalog::CatalogItem::setComment
void setComment(const QString &com)
Definition: catalogitem.cpp:187
GettextCatalog::CatalogItem::msgstr
const QString & msgstr(const int form=0) const
Definition: catalogitem.cpp:78
QMutexLocker
QVector::isEmpty
bool isEmpty() const
QString::count
int count() const
GettextCatalog::CatalogItemPrivate::clear
void clear()
Definition: catalogitem_private.h:94
QList::ConstIterator
typedef ConstIterator
GettextCatalog::CatalogItem::setValid
void setValid(bool)
Definition: catalogitem.cpp:117
GettextCatalog::CatalogItem::msgctxt
const QString & msgctxt(const bool noNewlines=false) const
Definition: catalogitem.cpp:67
GettextCatalog::CatalogItem::setMsgstr
void setMsgstr(const QString &msg, const int form=0)
Definition: catalogitem.cpp:163
QList::prepend
void prepend(const T &value)
GettextCatalog::CatalogItem::isCformat
bool isCformat() const
GettextCatalog::CatalogItemPrivate::_comment
QByteArray _comment
Definition: catalogitem_private.h:68
GettextCatalog::CatalogItem::Part
Part
Definition: catalogitem.h:88
GettextCatalog::CatalogItemPrivate::_prependMsgStrEmptyLine
bool _prependMsgStrEmptyLine
Definition: catalogitem_private.h:65
GettextCatalog::CatalogItem::isPlural
bool isPlural() const
Definition: catalogitem.cpp:202
GettextCatalog::CatalogItemPrivate::_msgidPlural
QVector< QString > _msgidPlural
Definition: catalogitem_private.h:71
GettextCatalog::CatalogItemPrivate::_fuzzyCached
bool _fuzzyCached
Definition: catalogitem_private.h:63
QVector::size
int size() const
QVector::end
iterator end()
GettextCatalog::CatalogItemPrivate::_msgstrPlural
QVector< QString > _msgstrPlural
Definition: catalogitem_private.h:72
QString::squeeze
void squeeze()
GettextCatalog::CatalogItemPrivate
This class represents data for an entry in a catalog.
Definition: catalogitem_private.h:57
QByteArray::endsWith
bool endsWith(const QByteArray &ba) const
GettextCatalog::CatalogItem::isNoCformat
bool isNoCformat() const
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:40:06 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

lokalize

Skip menu "lokalize"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal