Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

main.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 ** Copyright (C) 2003 zecke
00004 **
00005 ** This file is part of Qt Linguist.
00006 **
00007 ** This file may be distributed and/or modified under the terms of the
00008 ** GNU General Public License version 2 as published by the Free Software
00009 ** Foundation and appearing in the file LICENSE.GPL included in the
00010 ** packaging of this file.
00011 **
00012 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 **
00015 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00016 **
00017 ** Contact info@trolltech.com if any conditions of this licensing are
00018 ** not clear to you.
00019 **
00020 **********************************************************************/
00021 
00022 #include <metatranslator.h>
00023 #include <proparser.h>
00024 #include <opie.h>
00025 
00026 #include <qfile.h>
00027 #include <qfileinfo.h>
00028 #include <qstring.h>
00029 #include <qstringlist.h>
00030 #include <qtextstream.h>
00031 
00032 #include <errno.h>
00033 #include <string.h>
00034 
00035 // defined in fetchtr.cpp
00036 extern void fetchtr_cpp( const char *fileName, MetaTranslator *tor,
00037                          const char *defaultContext, bool mustExist );
00038 extern void fetchtr_ui( const char *fileName, MetaTranslator *tor,
00039                         const char *defaultContext, bool mustExist );
00040 
00041 // defined in merge.cpp
00042 extern void merge( MetaTranslator *tor, const MetaTranslator *virginTor,
00043                    bool verbose );
00044 
00045 typedef QValueList<MetaTranslatorMessage> TML;
00046 
00047 static const char* LUPDATE_VERSION = "0.1";
00048 
00049 static void printUsage()
00050 {
00051     fprintf( stderr, "Usage:\n"
00052              "    opie-lupdate [options] project-file\n"
00053              "    opie-lupdate [options] source-files -ts ts-files\n"
00054              "Options:\n"
00055              "    -opie  The OPIE base dir if not supplied $OPIEDIR will be taken\n"
00056              "    -help  Display this information and exit\n"
00057              "    -noobsolete\n"
00058              "           Drop all obsolete strings\n"
00059              "    -verbose\n"
00060              "           Explain what is being done\n"
00061              "    -version\n"
00062              "           Display the version of lupdate and exit\n" );
00063 }
00064 
00065 /*static QString opie_escape( const QString& str ) {
00066     QString ret = str.stripWhiteSpace();
00067     qWarning(ret);
00068     if ( ret.startsWith("$$(OPIEDIR)") )
00069         ret = ret.replace("$$(OPIEDIR)", OPIE::self()->opieDir() );
00070     qWarning(ret);
00071     return ret;
00072     }*/
00073 
00074 static void updateTsFiles( const MetaTranslator& fetchedTor,
00075                            const QString& opiedir,
00076                            const QStringList& languages,
00077                            const QString& basename,
00078                            const QString& codec,
00079                            bool noObsolete, bool verbose )
00080 {
00081     QStringList::ConstIterator it = languages.begin();
00082     for ( ; it != languages.end(); ++it ) {
00083         QString fileName = opiedir + "/i18n/" + (*it) + "/" + basename;
00084         MetaTranslator tor;
00085         tor.load( fileName );
00086         if ( !codec.isEmpty() )
00087             tor.setCodec( codec );
00088         if ( verbose )
00089             fprintf( stderr, "Updating '%s'...\n", fileName.latin1() );
00090         merge( &tor, &fetchedTor, verbose );
00091         if ( noObsolete )
00092             tor.stripObsoleteMessages();
00093         tor.stripEmptyContexts();
00094         if ( !tor.save(fileName) )
00095             fprintf( stderr, "lupdate error: Cannot save '%s': %s\n",
00096                      fileName.latin1(), strerror(errno) );
00097     }
00098 }
00099 
00100 int main( int argc, char **argv )
00101 {
00102     QString defaultContext = "@default";
00103     MetaTranslator fetchedTor;
00104     QCString codec;
00105     QStringList tsFileNames;
00106     QString opiedir;
00107     QString translationBase;
00108     QString target;
00109 
00110     bool verbose = FALSE;
00111     bool noObsolete = FALSE;
00112     bool metSomething = FALSE;
00113     bool isLib = FALSE;
00114     int numFiles = 0;
00115 
00116     int i;
00117 
00118     QStringList languageList = OPIE::self()->languageList(opiedir);
00119 
00120     for ( i = 1; i < argc; i++ ) {
00121         if ( qstrcmp(argv[i], "-help") == 0 ) {
00122             printUsage();
00123             return 0;
00124         } else if ( qstrcmp(argv[i], "-noobsolete") == 0 ) {
00125             noObsolete = TRUE;
00126             continue;
00127         } else if ( qstrcmp(argv[i], "-verbose") == 0 ) {
00128             verbose = TRUE;
00129             continue;
00130         } else if ( qstrcmp(argv[i], "-version") == 0 ) {
00131             fprintf( stderr, "lupdate version %s\n", LUPDATE_VERSION );
00132             return 0;
00133         } else if ( qstrcmp(argv[i], "-opie") == 0 ) {
00134             if( i+1 < argc ) {
00135                 opiedir = argv[i+1];
00136                 languageList = OPIE::self()->languageList(opiedir);
00137             }
00138             i++; // UGLY but we want to skip the next argument
00139             continue;
00140         }
00141 
00142         numFiles++;
00143 
00144         QString fullText;
00145 
00146         QFile f( argv[i] );
00147         if ( !f.open(IO_ReadOnly) ) {
00148             fprintf( stderr, "lupdate error: Cannot open file '%s': %s\n",
00149                      argv[i], strerror(errno) );
00150             return 1;
00151         }
00152 
00153         QTextStream t( &f );
00154         fullText = t.read();
00155         f.close();
00156 
00157         fetchedTor = MetaTranslator();
00158         codec.truncate( 0 );
00159         tsFileNames.clear();
00160         isLib = FALSE;
00161 
00162         QMap<QString, QString> tagMap = proFileTagMap( fullText, OPIE::self()->opieDir() );
00163         QMap<QString, QString>::Iterator it;
00164 
00165         for ( it = tagMap.begin(); it != tagMap.end(); ++it ) {
00166             QStringList toks = QStringList::split( ' ', it.data() );
00167             QStringList::Iterator t;
00168 
00169             for ( t = toks.begin(); t != toks.end(); ++t ) {
00170                 if ( it.key() == "HEADERS" || it.key() == "SOURCES" ) {
00171                     fetchtr_cpp( *t, &fetchedTor, defaultContext, TRUE );
00172                     metSomething = TRUE;
00173                 } else if ( it.key() == "INTERFACES" ||
00174                             it.key() == "FORMS" ) {
00175                     fetchtr_ui( *t, &fetchedTor, defaultContext, TRUE );
00176                     fetchtr_cpp( *t + ".h", &fetchedTor, defaultContext,
00177                                  FALSE );
00178                     metSomething = TRUE;
00179                 } else if ( it.key() == "TRANSLATIONS" ) {
00180                     // we do not care for that attribute anymore
00181                     //tsFileNames.append( *t );
00182                     metSomething = TRUE;
00183                 } else if ( it.key() == "CODEC" ) {
00184                     codec = (*t).latin1();
00185                 } else if ( it.key() == "TARGET" ) {
00186                     target = *t;
00187                     metSomething = TRUE;
00188                 } else if ( it.key() == "TEMPLATE" ) {
00189                     if ( (*t).stripWhiteSpace().lower() == "lib" )
00190                         isLib = true;
00191                 }
00192             }
00193         }
00201         qWarning("TARGET %s IsLib:%d",  target.latin1(), isLib );
00202         qWarning("LANGS %s", languageList.join(";").latin1() );
00203         qWarning("OPIEDIR %s",  OPIE::self()->opieDir(opiedir).latin1() );
00204         if (isLib )
00205             target.prepend("lib");
00206         target += ".ts";
00207         updateTsFiles( fetchedTor,  OPIE::self()->opieDir(opiedir),
00208                        languageList, target, codec, noObsolete, verbose );
00209 
00210         if ( !metSomething ) {
00211             fprintf( stderr,
00212                      "lupdate warning: File '%s' does not look like a"
00213                      " project file\n",
00214                      argv[i] );
00215         }
00216 
00217     }
00218 
00219     if ( numFiles == 0 ) {
00220         printUsage();
00221         return 1;
00222     }
00223     return 0;
00224 }

Generated on Sat Nov 5 16:15:25 2005 for OPIE by  doxygen 1.4.2