main.cpp 13.5 KB
Newer Older
Joachim Eibl's avatar
Joachim Eibl committed
1 2 3 4
/***************************************************************************
                          main.cpp  -  Where everything starts.
                             -------------------
    begin                : Don Jul 11 12:31:29 CEST 2002
Joachim Eibl's avatar
Joachim Eibl committed
5
    copyright            : (C) 2002-2007 by Joachim Eibl
Joachim Eibl's avatar
Joachim Eibl committed
6
    email                : joachim.eibl at gmx.de
Joachim Eibl's avatar
Joachim Eibl committed
7 8 9 10 11 12 13 14 15 16 17
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

18
#include <QStandardPaths>
19
#include <QApplication>
Michael Reeves's avatar
Michael Reeves committed
20
#include <KAboutData>
Michael Reeves's avatar
Michael Reeves committed
21
#include <KLocalizedString>
Joachim Eibl's avatar
Joachim Eibl committed
22
#include "kdiff3_shell.h"
Joachim Eibl's avatar
Joachim Eibl committed
23
#include <kstandarddirs.h>
24
#include "version.h"
Joachim Eibl's avatar
Joachim Eibl committed
25 26 27
#include <QTextCodec>
#include <QFile>
#include <QTextStream>
Joachim Eibl's avatar
Joachim Eibl committed
28
#include <QTranslator>
Joachim Eibl's avatar
Joachim Eibl committed
29
#include <QLocale>
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
30
#include <QFont>
31
#include <QClipboard>
Michael Reeves's avatar
Michael Reeves committed
32 33
#include <QCommandLineParser>
#include <QCommandLineOption>
Joachim Eibl's avatar
Joachim Eibl committed
34
#include <vector>
Joachim Eibl's avatar
Joachim Eibl committed
35

Michael Reeves's avatar
Michael Reeves committed
36 37 38 39
#ifdef _WIN32
#include <process.h>
#endif

Joachim Eibl's avatar
0.9.86  
Joachim Eibl committed
40 41 42
#ifdef KREPLACEMENTS_H
#include "optiondialog.h"
#endif
Joachim Eibl's avatar
Joachim Eibl committed
43
#include "common.h"
Joachim Eibl's avatar
Joachim Eibl committed
44

45
void initialiseCmdLineArgs( ) {
46
    QString configFileName = QStandardPaths::locate(QStandardPaths::GenericConfigLocation , "kdiff3rc" );
Michael Reeves's avatar
Michael Reeves committed
47 48 49 50 51 52 53 54 55 56 57 58
    QFile configFile( configFileName );
    QString ignorableOptionsLine = "-u;-query;-html;-abort";
    if( configFile.open( QIODevice::ReadOnly ) ) {
        QTextStream ts( &configFile );
        while( !ts.atEnd() ) {
            QString line = ts.readLine();
            if( line.startsWith( "IgnorableCmdLineOptions=" ) ) {
                int pos = line.indexOf( '=' );
                if( pos >= 0 ) {
                    ignorableOptionsLine = line.mid( pos + 1 );
                }
                break;
Joachim Eibl's avatar
Joachim Eibl committed
59
            }
Michael Reeves's avatar
Michael Reeves committed
60 61
        }
    }
Joachim Eibl's avatar
Joachim Eibl committed
62

Joachim Eibl's avatar
Joachim Eibl committed
63
#ifdef KREPLACEMENTS_H
Michael Reeves's avatar
Michael Reeves committed
64
    QStringList sl = ignorableOptionsLine.split( '|' );
Joachim Eibl's avatar
Joachim Eibl committed
65
#else
Michael Reeves's avatar
Michael Reeves committed
66
    QStringList sl = ignorableOptionsLine.split( ',' );
Joachim Eibl's avatar
Joachim Eibl committed
67
#endif
Michael Reeves's avatar
Michael Reeves committed
68 69 70 71 72 73
    if( !sl.isEmpty() ) {
        QStringList ignorableOptions = sl.front().split( ';' );
        for( QStringList::iterator i = ignorableOptions.begin(); i != ignorableOptions.end(); ++i ) {
            ( *i ).remove( '-' );
            if( !( *i ).isEmpty() ) {
                if( i->length() == 1 ) {
74
                    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << i->toLatin1() << QLatin1String( "ignore" ), i18n( "Ignored. (User defined.)" ) ) );
Michael Reeves's avatar
Michael Reeves committed
75 76
                }
                else {
77
                    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << i->toLatin1(), i18n( "Ignored. (User defined.)" ) ) );
Michael Reeves's avatar
Michael Reeves committed
78 79 80 81
                }
            }
        }
    }
Joachim Eibl's avatar
Joachim Eibl committed
82
}
Joachim Eibl's avatar
Joachim Eibl committed
83

Joachim Eibl's avatar
Joachim Eibl committed
84 85
#ifdef _WIN32
// This command checks the comm
Michael Reeves's avatar
Michael Reeves committed
86 87 88 89 90 91 92
static bool isOptionUsed( const QString& s, int argc, char* argv[] ) {
    for( int j = 0; j < argc; ++j ) {
        if( QString( "-" + s ) == argv[j] || QString( "--" + s ) == argv[j] ) {
            return true;
        }
    }
    return false;
Joachim Eibl's avatar
Joachim Eibl committed
93 94 95
}
#endif

96 97
#ifdef KREPLACEMENTS_H
class ContextFreeTranslator : public QTranslator
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
98 99 100
{
public:
   ContextFreeTranslator( QObject* pParent ) : QTranslator(pParent) {}
101
   virtual QString translate(const char* context, const char* sourceText, const char* comment ) const
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
102 103
   {
      if ( context != 0 )
104
         return QTranslator::translate(0,sourceText,comment, -1);
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
105 106 107
      else
         return QString();
   }
108 109
};
#endif
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
110

Michael Reeves's avatar
Michael Reeves committed
111
int main( int argc, char *argv[] ) {
112
    QApplication app( argc, argv ); // KAboutData and QCommandLineParser depend on this being setup.
Joachim Eibl's avatar
Joachim Eibl committed
113
#ifdef _WIN32
Michael Reeves's avatar
Michael Reeves committed
114 115
    /* KDiff3 can be used as replacement for the text-diff and merge tool provided by
       Clearcase. This is experimental and so far has only been tested under Windows.
Joachim Eibl's avatar
Joachim Eibl committed
116

Michael Reeves's avatar
Michael Reeves committed
117 118 119 120 121 122 123 124 125 126 127 128 129
       There are two ways to use KDiff3 with clearcase
       -  The file lib/mgrs/map contains the list of compare/merge tasks on one side and
          the tool on the other. Originally this contains only clearcase tools, but you can
          edit this file and put kdiff3 there instead. (Recommended method)
       -  Exchange the original program with KDiff3: (Hackish, no fine control)
          1. In the Clearcase "bin"-directory rename "cleardiffmrg.exe" to "cleardiffmrg_orig.exe".
          2. Copy kdiff3.exe into that "bin"-directory and rename it to "cleardiffmrg.exe".
             (Also copy the other files that are needed by KDiff3 there.)
          Now when a file comparison or merge is done by Clearcase then of course KDiff3 will be
          run instead.
          If the commandline contains the option "-directory" then KDiff3 can't do it but will
          run "cleardiffmrg_orig.exe" instead.
    */
Joachim Eibl's avatar
Joachim Eibl committed
130

Michael Reeves's avatar
Michael Reeves committed
131 132 133 134 135 136
    // Write all args into a temporary file. Uncomment this for debugging purposes.
    /*
    FILE* f = fopen(QDir::toNativeSeparators(QDir::homePath()+"//kdiff3_call_args.txt").toLatin1().data(),"w");
    for(int i=0; i< argc; ++i)
       fprintf(f,"Arg %d: %s\n", i, argv[i]);
    fclose(f);
Joachim Eibl's avatar
Joachim Eibl committed
137

Michael Reeves's avatar
Michael Reeves committed
138 139 140 141 142 143 144 145 146 147 148 149
    // Call orig cleardiffmrg.exe to see what result it returns.
    int result=0;
    result = ::_spawnvp(_P_WAIT , "C:\\Programme\\Rational\\ClearCase\\bin\\cleardiffmrg.exe", argv );
    fprintf(f,"Result: %d\n", result );
    fclose(f);
    return result;
    */

    // KDiff3 can replace cleardiffmrg from clearcase. But not all functions.
    if( isOptionUsed( "directory", argc, argv ) ) {
        return ::_spawnvp( _P_WAIT , "cleardiffmrg_orig", argv );
    }
Joachim Eibl's avatar
Joachim Eibl committed
150

Joachim Eibl's avatar
Joachim Eibl committed
151
#endif
Joachim Eibl's avatar
Joachim Eibl committed
152
#ifdef Q_OS_OS2
Michael Reeves's avatar
Michael Reeves committed
153 154
    // expand wildcards on the command line
    _wildcard( &argc, &argv );
Joachim Eibl's avatar
Joachim Eibl committed
155 156
#endif

Michael Reeves's avatar
Michael Reeves committed
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
    //QApplication::setColorSpec( QApplication::ManyColor ); // Grab all 216 colors

    const QByteArray& appName = "kdiff3";
    const QByteArray& appCatalog = appName;
    const QString i18nName = i18n( "kdiff3" );
    QByteArray appVersion = QByteArray( VERSION );
    if( sizeof( void* ) == 8 )
        appVersion += " (64 bit)";
    else if( sizeof( void* ) == 4 )
        appVersion += " (32 bit)";
    const QString description = i18n( "Tool for Comparison and Merge of Files and Directories" );
    const QString copyright = i18n( "(c) 2002-2012 Joachim Eibl" );
    const QString& homePage = QStringLiteral( "http://kdiff3.sourceforge.net/" );
    const QString& bugsAddress = QStringLiteral( "joachim.eibl@gmx.de" );

    KLocalizedString::setApplicationDomain( appCatalog );
    KAboutData aboutData( appName, i18nName,
                          appVersion, description, KAboutLicense::GPL_V2, copyright, description,
                          homePage, bugsAddress );

    aboutData.addAuthor( i18n( "Joachim Eibl" ), QString(), QStringLiteral( "joachim.eibl@gmx.de" ) );
    aboutData.addCredit( i18n( "Eike Sauer" ), i18n( "Bugfixes, Debian package maintainer" ) );
    aboutData.addCredit( i18n( "Sebastien Fricker" ), i18n( "Windows installer" ) );
    aboutData.addCredit( i18n( "Stephan Binner" ), i18n( "i18n-help" ), QStringLiteral( "binner@kde.org" ) );
    aboutData.addCredit( i18n( "Stefan Partheymueller" ), i18n( "Clipboard-patch" ) );
    aboutData.addCredit( i18n( "David Faure" ), i18n( "KIO-Help" ), QStringLiteral( "faure@kde.org" ) );
    aboutData.addCredit( i18n( "Bernd Gehrmann" ), i18n( "Class CvsIgnoreList from Cervisia" ) );
    aboutData.addCredit( i18n( "Andre Woebbeking" ), i18n( "Class StringMatcher" ) );
    aboutData.addCredit( i18n( "Michael Denio" ), i18n( "Directory Equality-Coloring patch" ) );
    aboutData.addCredit( i18n( "Manfred Koehler" ), i18n( "Fix for slow startup on Windows" ) );
    aboutData.addCredit( i18n( "Sergey Zorin" ), i18n( "Diff Ext for Windows" ) );
    aboutData.addCredit( i18n( "Paul Eggert, Mike Haertel, David Hayes, Richard Stallman, Len Tower" ), i18n( "GNU-Diffutils" ) );
    aboutData.addCredit( i18n( "Tino Boellsterling, Timothy Mee" ), i18n( "Intensive test, use and feedback" ) );
    aboutData.addCredit( i18n( "Michael Schmidt" ), i18n( "Mac support" ) );
    aboutData.addCredit( i18n( "Valentin Rusu" ), i18n( "KDE4 porting" ), QStringLiteral( "kde@rusu.info" ) );
    aboutData.addCredit( i18n( "Albert Astals Cid" ), i18n( "KDE4 porting" ), QStringLiteral( "aacid@kde.org" ) );
    aboutData.addCredit( i18n( "Silvan Scherrer" ), i18n( "OS2 port" ) );
    aboutData.addCredit( i18n( "Nigel Stewart" ), i18n( "Cygwin support" ) );
    aboutData.addCredit( i18n( "Maurice van der Pot" ), i18n( "Framework for automated tests" ) );
Joachim Eibl's avatar
Joachim Eibl committed
196

Michael Reeves's avatar
Michael Reeves committed
197
    aboutData.addCredit( i18n( "+ Many thanks to those who reported bugs and contributed ideas!" ) );
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
198

Michael Reeves's avatar
Michael Reeves committed
199
    KAboutData::setApplicationData( aboutData );
200
    
201 202 203 204
    KDiff3Shell::getParser()->setApplicationDescription(aboutData.shortDescription());
    KDiff3Shell::getParser()->addVersionOption();
    KDiff3Shell::getParser()->addHelpOption();
    initialiseCmdLineArgs( );
Michael Reeves's avatar
Michael Reeves committed
205
    // ignorable command options
206 207 208 209 210 211 212 213 214 215 216 217 218
    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "m" ) << QLatin1String( "merge" ), i18n( "Merge the input." ) ) );
    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "b" ) << QLatin1String( "base file" ), i18n( "Explicit base file. For compatibility with certain tools." ) ) );
    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "o" ) << QLatin1String( "output file" ), i18n( "Output file. Implies -m. E.g.: -o newfile.txt" ) ) );
    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "out file" ), i18n( "Output file, again. (For compatibility with certain tools.)" ) ) );
    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "auto" ), i18n( "No GUI if all conflicts are auto-solvable. (Needs -o file)" ) ) );
    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "qall" ), i18n( "Don't solve conflicts automatically." ) ) );
    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "L1 alias1" ), i18n( "Visible name replacement for input file 1 (base)." ) ) );
    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "L2 alias2" ), i18n( "Visible name replacement for input file 2." ) ) );
    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "L3 alias3" ), i18n( "Visible name replacement for input file 3." ) ) );
    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "L" ) << QLatin1String( "fname alias" ), i18n( "Alternative visible name replacement. Supply this once for every input." ) ) );
    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "cs string" ), i18n( "Override a config setting. Use once for every setting. E.g.: --cs \"AutoAdvance=1\"" ) ) );
    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "confighelp" ), i18n( "Show list of config settings and current values." ) ) );
    KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "config file" ), i18n( "Use a different config file." ) ) );
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
219

Michael Reeves's avatar
Michael Reeves committed
220
    // other command options
221 222 223
    KDiff3Shell::getParser()->addPositionalArgument( QLatin1String( "[File1]" ), i18n( "file1 to open (base, if not specified via --base)" ) );
    KDiff3Shell::getParser()->addPositionalArgument( QLatin1String( "[File2]" ), i18n( "file2 to open" ) );
    KDiff3Shell::getParser()->addPositionalArgument( QLatin1String( "[File3]" ), i18n( "file3 to open" ) );
Michael Reeves's avatar
Michael Reeves committed
224

225
    KDiff3Shell::getParser()->process( app ); // PORTING SCRIPT: move this to after any parser.addOption
Michael Reeves's avatar
Michael Reeves committed
226
    //must be after process or parse call
227
    aboutData.setupCommandLine( KDiff3Shell::getParser() );
228 229 230 231 232 233 234 235
    /**
     * take component name and org. name from KAboutData
     */
    app.setApplicationName(aboutData.componentName());
    app.setApplicationDisplayName(aboutData.displayName());
    app.setOrganizationDomain(aboutData.organizationDomain());
    app.setApplicationVersion(aboutData.version());
    
Joachim Eibl's avatar
Joachim Eibl committed
236
#ifdef KREPLACEMENTS_H
Michael Reeves's avatar
Michael Reeves committed
237
    QString locale;
Joachim Eibl's avatar
Joachim Eibl committed
238

Michael Reeves's avatar
Michael Reeves committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    locale = app.config()->readEntry( "Language", "Auto" );
    int spacePos = locale.indexOf( ' ' );
    if( spacePos > 0 ) locale = locale.left( spacePos );
    ContextFreeTranslator kdiff3Translator( 0 );
    QTranslator qtTranslator( 0 );
    if( locale != "en_orig" ) {
        if( locale == "Auto" || locale.isEmpty() )
            locale = locale = QLocale::system().name().left( 2 );

        QString translationDir = getTranslationDir( locale );
        kdiff3Translator.load( QString( "kdiff3_" ) + locale, translationDir );
        app.installTranslator( &kdiff3Translator );

        qtTranslator.load( QString( "qt_" ) + locale, translationDir );
        app.installTranslator( &qtTranslator );
    }
Joachim Eibl's avatar
Joachim Eibl committed
255
#endif
Joachim Eibl's avatar
Joachim Eibl committed
256

257
#ifndef QT_NO_SESSIONMANAGER
Michael Reeves's avatar
Michael Reeves committed
258 259 260 261
    if( app.isSessionRestored() ) {
        RESTORE( KDiff3Shell );
    }
    else
262
#endif
Michael Reeves's avatar
Michael Reeves committed
263 264 265 266 267
    {
        KDiff3Shell* p = new KDiff3Shell();
        p->show();
        p->setWindowState( p->windowState() | Qt::WindowActive ); // Patch for ubuntu: window not active on startup
    }
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
268
//app.installEventFilter( new CFilter );
Michael Reeves's avatar
Michael Reeves committed
269 270 271 272
    int retVal = app.exec();
    if( QApplication::clipboard()->text().size() == 0 )
        QApplication::clipboard()->clear(); // Patch for Ubuntu: Fix issue with Qt clipboard
    return retVal;
Joachim Eibl's avatar
Joachim Eibl committed
273
}
Joachim Eibl's avatar
Joachim Eibl committed
274 275 276

// Suppress warning with --enable-final
#undef VERSION