summaryrefslogtreecommitdiffstats
path: root/kbabel/filters/gettext/gettextexport.cpp
blob: c2fe9c677c239de353ccfe7fa6f22891fceeb5a9 (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
/* ****************************************************************************
  This file is part of KBabel

  Copyright (C) 1999-2000 by Matthias Kiefer <matthias.kiefer@gmx.de>
		2001-2002 by Stanislav Visnovsky <visnovsky@kde.org>
  Copyright (C) 2005,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 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 "gettextexport.h"

#include <resources.h>
#include "catalog.h"
#include "catalogitem.h"
#include "catalogsettings.h"
#include "kbprojectsettings.h"

#include <tqfile.h>
#include <tqtextcodec.h>

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

K_EXPORT_COMPONENT_FACTORY( kbabel_gettextexport, KGenericFactory<GettextExportPlugin> ( "kbabelgettextexportfilter" ) )

using namespace KBabel;

GettextExportPlugin::GettextExportPlugin(TQObject* parent, const char* name, const TQStringList &) :
    CatalogExportPlugin(parent,name), m_wrapWidth( -1 )
{
}

ConversionStatus GettextExportPlugin::save(const TQString& localFile , const TQString& mimetype, const Catalog* catalog)
{
   // check, whether we know how to handle the extra data
   if( catalog->importPluginID() != "GNU gettext") 
      return UNSUPPORTED_TYPE;

   // we support on the application/x-gettext MIME type
   if( mimetype != "application/x-gettext") 
      return UNSUPPORTED_TYPE;

   TQFile file(localFile);

   if(file.open(IO_WriteOnly))
   {
      int progressRatio = QMAX(100/ QMAX(catalog->numberOfEntries(),1), 1);
      emit signalResetProgressBar(i18n("saving file"),100);

      TQTextStream stream(&file);
      
      SaveSettings _saveSettings = catalog->saveSettings();
      
      if(_saveSettings.useOldEncoding && catalog->fileCodec())
      {
	    stream.setCodec(catalog->fileCodec());
      }
      else
      {
         switch(_saveSettings.encoding)
         {
            case ProjectSettingsBase::UTF8:
               stream.setCodec(TQTextCodec::codecForName("utf-8"));
               break;
            case ProjectSettingsBase::UTF16:
               stream.setCodec(TQTextCodec::codecForName("utf-16"));
               break;
            default:
	       stream.setCodec(TQTextCodec::codecForLocale());
               break;
         }
      }

      // only save header if it is not empty
      const TQString headerComment( catalog->header().comment() );
      // ### TODO: why is this useful to have a header with an empty msgstr?
      if( !headerComment.isEmpty() || !catalog->header().msgstr().isEmpty() )
      {
            // write header
            writeComment( stream, headerComment );
    
            const TQString headerMsgid = catalog->header().msgid().first();
    
            // Gettext PO files should have an empty msgid as header
            if ( !headerMsgid.isEmpty() )
            {
                // ### TODO: perhaps it is grave enough for a user message
                kdWarning() << "Non-empty msgid for the header, assuming empty msgid!" << endl << headerMsgid << "---" << endl;
            }
          
            // ### FIXME: if it is the header, then the msgid should be empty! (Even if KBabel has made something out of a non-header first entry!)
            stream << "msgid \"\"\n";
            
            writeKeyword( stream, "msgstr", catalog->header().msgstr().first() );

            stream << "\n";
      }

      TQStringList list;
      for( uint counter = 0; counter < catalog->numberOfEntries() ; counter++ )
      {
          if(counter%10==0) {
             emit signalProgress(counter/progressRatio);
	  }
	  
            // write entry
            writeComment( stream, catalog->comment(counter) );
    
            const TQString msgctxt = catalog->msgctxt(counter);
            if (! msgctxt.isEmpty() )
            {
                writeKeyword( stream, "msgctxt", msgctxt );
            }

            writeKeyword( stream, "msgid", catalog->msgid( counter ).first() );
            if( catalog->pluralForm( counter ) == Gettext )
            {
                writeKeyword( stream, "msgid_plural", catalog->msgid( counter ).last() );
            }
	  
            if( catalog->pluralForm(counter) != Gettext)
            {
                writeKeyword( stream, "msgstr", catalog->msgstr( counter ).first() );
            }
            else
            {
                kdDebug(KBABEL) << "Saving gettext plural form" << endl;
                const int forms = catalog->msgstr( counter ).count();
                for ( int i = 0; i < forms; ++i )
                {
                    TQString keyword ( "msgstr[" );
                    keyword += TQString::number( i );
                    keyword += ']';

                    writeKeyword( stream, keyword, *( catalog->msgstr( counter ).at( i ) ) );
                }
            }
            
            stream << "\n";

	 kapp->processEvents(10);
	 if( isStopped() )
	 {
	    return STOPPED;
	 }
      }

      if( _saveSettings.saveObsolete )
      {
          TQValueList<TQString>::ConstIterator oit;
	  
	  TQStringList _obsolete = catalog->catalogExtraData();

          for( oit = _obsolete.begin(); oit != _obsolete.end(); ++oit )
    	  {
              stream << (*oit) << "\n\n";

	      kapp->processEvents(10);
	      if( isStopped() )
	      {
		  return STOPPED;
	      }
          }
      }
      
      emit signalProgress(100);
      file.close();

      emit signalClearProgressBar();
   }
   else
   {
      //emit signalError(i18n("Wasn't able to open file %1").arg(filename.ascii()));
      return OS_ERROR;
   }

   return OK;
}

void GettextExportPlugin::writeComment( TQTextStream& stream, const TQString& comment ) const
{
    if( !comment.isEmpty() )
    {
        // We must check that each comment line really starts with a #, to avoid syntax errors
        int pos = 0;
        for(;;)
        {
            const int newpos = comment.find( '\n', pos, false );
            if ( newpos == pos )
            {
                ++pos;
                stream << "\n";
                continue;
            }
            const TQString span ( ( newpos == -1 ) ? comment.mid( pos ) : comment.mid( pos, newpos-pos ) );

            const int len = span.length();
            TQString spaces; // Stored leading spaces
            for ( int i = 0 ; i < len ; ++i )
            {
                const TQChar& ch = span[ i ];
                if ( ch == '#' )
                {
                    stream << spaces << span.mid( i );
                    break;
                }
                else if ( ch == ' ' || ch == '\t' )
                {
                    // We have a leading white space character, so store it temporary
                    spaces += ch;
                }
                else
                {
                    // Not leading white space and not a # character. so consider that the # character was missing at first position.
                    stream << "# " << spaces << span.mid( i );
                    break;
                }
            }
            stream << "\n";

            if ( newpos == -1 )
                break;
            else
                pos = newpos + 1;
        }
    }
}

void GettextExportPlugin::writeKeyword( TQTextStream& stream, const TQString& keyword, const TQString& text ) const
{
    if ( text.isEmpty() )
    {
        // Whatever the wrapping mode, an empty line is an empty line
        stream << keyword << " \"\"\n";
        return; 
    }
    else if ( m_wrapWidth == -1 )
    {
        // Traditional KBabel wrapping
        TQStringList list = TQStringList::split( '\n', text );

        if ( text.startsWith( "\n" ) )
            list.prepend( TQString() );

        if(list.isEmpty())
            list.append( TQString() );
    
        if( list.count() > 1 )
            list.prepend( TQString() );

        stream << keyword << " ";
    
        TQStringList::const_iterator it;
        for( it = list.constBegin(); it != list.constEnd(); ++it )
        {
            stream << "\"" << (*it) << "\"\n";
        }
        return;
    }
    else if ( ( !m_wrapWidth )
        || ( m_wrapWidth < 0 ) // Unknown special wrapping, so assume "no wrap" instead
        )
    {
        // No wrapping (like Gettext's --no.wrap or -w0 )
        
        // we need to remove the \n characters, as they are extra characters
        TQString realText( text );
        realText.remove( '\n' );
        stream << keyword << " \"" << realText << "\"\n";
        return;
    }

    // ### TODO: test!
    // Normal wrapping like Gettext's -w parameter with a value bigger than 0
    // From here on, we assume that we have an non-empty text and a positive non-null m_wrapWidth
    
    // we need to remove the \n characters, as they are extra characters
    TQString realText( text );
    realText.remove( '\n' );

    bool needFirstEmptyLine = false;
    if ( realText.find( "\\n" ) != -1 )
    {
        // We have more than one (logical) line, so write the extra empty line
        needFirstEmptyLine = true;
    }
    else
    {
        // We must see if the text would fit in one line, including the keyword, a space and two quote characters.
        const int rest = text.length() + keyword.length() + 3 - m_wrapWidth;
        if ( rest > 0 )
        {
            needFirstEmptyLine = true;
        }
    }
    int availableWidth = m_wrapWidth;
    if ( needFirstEmptyLine )
    {
        stream << keyword << " \"\"\n";
    }
    else
    {
        stream << keyword << " ";
        availableWidth -= keyword.length();
        availableWidth--; // The space after the keyword
    }
    
    const int spanLength = realText.length();
    for ( int pos = 0; pos < spanLength; )
    {
        availableWidth -= 2; // Count the quote characters
        if ( availableWidth < 2 )
        {
            // Be sure that at least two useful characters are written, even if the wrap width is too small
            availableWidth = 2;
        }
        const int newlinePos = realText.find ( "\\n", pos );
        if ( ( newlinePos >= 0 ) && ( newlinePos - pos + 2 < availableWidth ) )
        {
            // The newline is near than the maximum available numbers of characters
            availableWidth = newlinePos - pos + 2;
        }
        stream << '\"' << realText.mid( pos, availableWidth ) << "\"\n";
        pos += availableWidth;
    }
}