~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/linguist/lupdate/main.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the linguist application of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <metatranslator.h>
 
30
#include <proparser.h>
 
31
 
 
32
#include <qdir.h>
 
33
#include <qfile.h>
 
34
#include <qfileinfo.h>
 
35
#include <qstring.h>
 
36
#include <qstringlist.h>
 
37
#include <qtextstream.h>
 
38
 
 
39
#include <errno.h>
 
40
#include <string.h>
 
41
 
 
42
// defined in fetchtr.cpp
 
43
extern void fetchtr_cpp( const char *fileName, MetaTranslator *tor,
 
44
                         const char *defaultContext, bool mustExist );
 
45
extern void fetchtr_ui( const char *fileName, MetaTranslator *tor,
 
46
                        const char *defaultContext, bool mustExist );
 
47
 
 
48
// defined in merge.cpp
 
49
extern void merge( MetaTranslator *tor, const MetaTranslator *virginTor,
 
50
                   bool verbose );
 
51
 
 
52
typedef QList<MetaTranslatorMessage> TML;
 
53
 
 
54
static void printUsage()
 
55
{
 
56
    fprintf( stderr, "Usage:\n"
 
57
             "    lupdate [options] project-file\n"
 
58
             "    lupdate [options] source-files -ts ts-files\n"
 
59
             "Options:\n"
 
60
             "    -help  Display this information and exit\n"
 
61
             "    -noobsolete\n"
 
62
             "           Drop all obsolete strings\n"
 
63
             "    -verbose\n"
 
64
             "           Explain what is being done\n"
 
65
             "    -version\n"
 
66
             "           Display the version of lupdate and exit\n" );
 
67
}
 
68
 
 
69
static void updateTsFiles( const MetaTranslator& fetchedTor,
 
70
                           const QStringList& tsFileNames, const QString& codec,
 
71
                           bool noObsolete, bool verbose )
 
72
{
 
73
    QStringList::ConstIterator t = tsFileNames.begin();
 
74
    while ( t != tsFileNames.end() ) {
 
75
        MetaTranslator tor;
 
76
        tor.load( *t );
 
77
        if ( !codec.isEmpty() )
 
78
            tor.setCodec( codec.toLatin1() );
 
79
        if ( verbose )
 
80
            fprintf( stderr, "Updating '%s'...\n", (*t).toLatin1().data() );
 
81
        merge( &tor, &fetchedTor, verbose );
 
82
        if ( noObsolete )
 
83
            tor.stripObsoleteMessages();
 
84
        tor.stripEmptyContexts();
 
85
        if ( !tor.save(*t) )
 
86
            fprintf( stderr, "lupdate error: Cannot save '%s': %s\n",
 
87
                     (*t).toLatin1().constData(), strerror(errno) );
 
88
        ++t;
 
89
    }
 
90
}
 
91
 
 
92
int main( int argc, char **argv )
 
93
{
 
94
    QString defaultContext = "@default";
 
95
    MetaTranslator fetchedTor;
 
96
    QByteArray codec;
 
97
    QStringList tsFileNames;
 
98
 
 
99
    bool verbose = false;
 
100
    bool noObsolete = false;
 
101
    bool metSomething = false;
 
102
    int numFiles = 0;
 
103
    bool standardSyntax = true;
 
104
    bool metTsFlag = false;
 
105
 
 
106
    int i;
 
107
 
 
108
    for ( i = 1; i < argc; i++ ) {
 
109
        if ( qstrcmp(argv[i], "-ts") == 0 )
 
110
            standardSyntax = false;
 
111
    }
 
112
 
 
113
    for ( i = 1; i < argc; i++ ) {
 
114
        if ( qstrcmp(argv[i], "-help") == 0 ) {
 
115
            printUsage();
 
116
            return 0;
 
117
        } else if ( qstrcmp(argv[i], "-noobsolete") == 0 ) {
 
118
            noObsolete = true;
 
119
            continue;
 
120
        } else if ( qstrcmp(argv[i], "-verbose") == 0 ) {
 
121
            verbose = true;
 
122
            continue;
 
123
        } else if ( qstrcmp(argv[i], "-version") == 0 ) {
 
124
            fprintf( stderr, "lupdate version %s\n", QT_VERSION_STR );
 
125
            return 0;
 
126
        } else if ( qstrcmp(argv[i], "-ts") == 0 ) {
 
127
            metTsFlag = true;
 
128
            continue;
 
129
        }
 
130
 
 
131
        numFiles++;
 
132
 
 
133
        QString fullText;
 
134
 
 
135
        if ( !metTsFlag ) {
 
136
            QFile f( argv[i] );
 
137
            if ( !f.open(QIODevice::ReadOnly) ) {
 
138
                fprintf( stderr, "lupdate error: Cannot open file '%s': %s\n",
 
139
                         argv[i], strerror(errno) );
 
140
                return 1;
 
141
            }
 
142
 
 
143
            QTextStream t( &f );
 
144
            fullText = t.readAll();
 
145
            f.close();
 
146
        }
 
147
 
 
148
        QString oldDir = QDir::currentPath();
 
149
        QDir::setCurrent( QFileInfo(argv[i]).path() );
 
150
 
 
151
        if ( standardSyntax ) {
 
152
            fetchedTor = MetaTranslator();
 
153
            codec.truncate( 0 );
 
154
            tsFileNames.clear();
 
155
 
 
156
            QMap<QString, QString> tagMap = proFileTagMap( fullText );
 
157
            QMap<QString, QString>::Iterator it;
 
158
 
 
159
            for ( it = tagMap.begin(); it != tagMap.end(); ++it ) {
 
160
                QStringList toks = it.value().split(' ');
 
161
                QStringList::Iterator t;
 
162
 
 
163
                for ( t = toks.begin(); t != toks.end(); ++t ) {
 
164
                    if ( it.key() == "HEADERS" || it.key() == "SOURCES" ) {
 
165
                        fetchtr_cpp( (*t).toAscii(), &fetchedTor, defaultContext.toAscii(), true );
 
166
                        metSomething = true;
 
167
                    } else if ( it.key() == "INTERFACES" ||
 
168
                                it.key() == "FORMS" ) {
 
169
                        fetchtr_ui( (*t).toAscii(), &fetchedTor, defaultContext.toAscii(), true );
 
170
                        fetchtr_cpp( (*t).toAscii() + ".h", &fetchedTor, defaultContext.toAscii(), false );
 
171
                        metSomething = true;
 
172
                    } else if ( it.key() == "TRANSLATIONS" ) {
 
173
                        tsFileNames.append( *t );
 
174
                        metSomething = true;
 
175
                    } else if ( it.key() == "CODEC" ||
 
176
                                it.key() == "DEFAULTCODEC" ) {
 
177
                        codec = (*t).toLatin1();
 
178
                    }
 
179
                }
 
180
            }
 
181
 
 
182
            updateTsFiles( fetchedTor, tsFileNames, codec, noObsolete,
 
183
                           verbose );
 
184
 
 
185
            if ( !metSomething ) {
 
186
                fprintf( stderr,
 
187
                         "lupdate warning: File '%s' does not look like a"
 
188
                         " project file\n",
 
189
                         argv[i] );
 
190
            } else if ( tsFileNames.isEmpty() ) {
 
191
                fprintf( stderr,
 
192
                         "lupdate warning: Met no 'TRANSLATIONS' entry in"
 
193
                         " project file '%s'\n",
 
194
                         argv[i] );
 
195
            }
 
196
        } else {
 
197
            if ( metTsFlag ) {
 
198
                if ( QString(argv[i]).toLower().endsWith(".ts") ) {
 
199
                    QFileInfo fi( argv[i] );
 
200
                    if ( !fi.exists() || fi.isWritable() ) {
 
201
                        tsFileNames.append( argv[i] );
 
202
                    } else {
 
203
                        fprintf( stderr,
 
204
                                 "lupdate warning: For some reason, I cannot"
 
205
                                 " save '%s'\n",
 
206
                                 argv[i] );
 
207
                    }
 
208
                } else {
 
209
                    fprintf( stderr,
 
210
                             "lupdate error: File '%s' lacks .ts extension\n",
 
211
                             argv[i] );
 
212
                }
 
213
            } else {
 
214
                QFileInfo fi(argv[i]);
 
215
                if ( QString(argv[i]).toLower().endsWith(".ui") ) {
 
216
                    fetchtr_ui( fi.fileName().toAscii(), &fetchedTor, defaultContext.toAscii(), true );
 
217
                    fetchtr_cpp( fi.fileName().toAscii() + ".h", &fetchedTor,
 
218
                                 defaultContext.toAscii(), false );
 
219
                } else {
 
220
                    fetchtr_cpp( fi.fileName().toAscii(), &fetchedTor, defaultContext.toAscii(), true );
 
221
                }
 
222
            }
 
223
        }
 
224
        QDir::setCurrent( oldDir );
 
225
    }
 
226
 
 
227
    if ( !standardSyntax )
 
228
        updateTsFiles( fetchedTor, tsFileNames, codec, noObsolete, verbose );
 
229
 
 
230
    if ( numFiles == 0 ) {
 
231
        printUsage();
 
232
        return 1;
 
233
    }
 
234
    return 0;
 
235
}