main.cpp 8.26 KB
Newer Older
Joachim Eibl's avatar
Joachim Eibl committed
1
/***************************************************************************
Michael Reeves's avatar
Michael Reeves committed
2 3
 *   Copyright (C) 2003-2007 by Joachim Eibl <joachim.eibl at gmx.de>      *
 *   Copyright (C) 2018 Michael Reeves reeves.87@gmail.com                 *
Joachim Eibl's avatar
Joachim Eibl committed
4 5 6 7 8 9 10
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
11
#include "kdiff3_shell.h"
Michael Reeves's avatar
cleanup  
Michael Reeves committed
12 13
#include "version.h"

14 15
#include "Logging.h"

Michael Reeves's avatar
Michael Reeves committed
16
#ifndef Q_OS_WIN
Michael Reeves's avatar
cleanup  
Michael Reeves committed
17
#include <unistd.h>
Michael Reeves's avatar
Michael Reeves committed
18
#endif
19

Michael Reeves's avatar
Michael Reeves committed
20
#include <KAboutData>
21
#include <KCrash/KCrash>
22
#include <KLocalizedString>
Michael Reeves's avatar
Michael Reeves committed
23
#include <KMessageBox>
24 25

#include <QApplication>
Michael Reeves's avatar
Michael Reeves committed
26
#include <QByteArray>
27 28
#include <QCommandLineOption>
#include <QCommandLineParser>
Joachim Eibl's avatar
Joachim Eibl committed
29
#include <QFile>
Michael Reeves's avatar
Michael Reeves committed
30
#include <QStringList>
31
#include <QStandardPaths>
Joachim Eibl's avatar
Joachim Eibl committed
32
#include <QTextStream>
Michael Reeves's avatar
Michael Reeves committed
33

34
void initialiseCmdLineArgs(QCommandLineParser* cmdLineParser)
35
{
36 37
    QString configFileName = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, "kdiff3rc");
    QFile configFile(configFileName);
Michael Reeves's avatar
Michael Reeves committed
38
    QString ignorableOptionsLine = "-u;-query;-html;-abort";
39
    if(configFile.open(QIODevice::ReadOnly))
40
    {
41
        QTextStream ts(&configFile);
42 43
        while(!ts.atEnd())
        {
Michael Reeves's avatar
Michael Reeves committed
44
            QString line = ts.readLine();
Yuri Chornoivan's avatar
Yuri Chornoivan committed
45
            if(line.startsWith(QLatin1String("IgnorableCmdLineOptions=")))
46 47
            {
                int pos = line.indexOf('=');
48
                if(pos >= 0)
49
                {
50
                    ignorableOptionsLine = line.mid(pos + 1);
Michael Reeves's avatar
Michael Reeves committed
51 52
                }
                break;
Joachim Eibl's avatar
Joachim Eibl committed
53
            }
Michael Reeves's avatar
Michael Reeves committed
54 55
        }
    }
Yuri Chornoivan's avatar
Yuri Chornoivan committed
56
    //support our own old preferences this is obsolete
57
    QStringList sl = ignorableOptionsLine.split(',');
58

59 60
    if(!sl.isEmpty())
    {
61 62
        QStringList ignorableOptions = sl.front().split(';');
        for(QStringList::iterator i = ignorableOptions.begin(); i != ignorableOptions.end(); ++i)
63 64 65 66
        {
            (*i).remove('-');
            if(!(*i).isEmpty())
            {
67
                if(i->length() == 1) {
68
                    cmdLineParser->addOption(QCommandLineOption(QStringList() << *i << QLatin1String("ignore"), i18n("Ignored. (User defined.)")));
Michael Reeves's avatar
Michael Reeves committed
69
                }
70 71
                else
                {
72
                    cmdLineParser->addOption(QCommandLineOption(QStringList() << *i, i18n("Ignored. (User defined.)")));
Michael Reeves's avatar
Michael Reeves committed
73 74 75 76
                }
            }
        }
    }
Joachim Eibl's avatar
Joachim Eibl committed
77
}
Joachim Eibl's avatar
Joachim Eibl committed
78

79
int main(int argc, char* argv[])
80
{
81 82
    const QLatin1String appName("kdiff3");

83
    QApplication app(argc, argv); // KAboutData and QCommandLineParser depend on this being setup.
84
    KLocalizedString::setApplicationDomain(appName.data());
85 86

    KCrash::initialize();
Joachim Eibl's avatar
Joachim Eibl committed
87

88
    const QString i18nName = i18n("KDiff3");
89 90
    QString appVersion(KDIFF3_VERSION_STRING);

91
    if(sizeof(void*) == 8)
92
        appVersion += i18n(" (64 bit)");
93
    else if(sizeof(void*) == 4)
94
        appVersion += i18n(" (32 bit)");
95
    const QString description = i18n("Tool for Comparison and Merge of Files and Directories");
Michael Reeves's avatar
Michael Reeves committed
96
    const QString copyright = i18n("(c) 2002-2014 Joachim Eibl, (c) 2017 Michael Reeves KF5/Qt5 port");
97
    const QString homePage = QStringLiteral("https://kde.org/applications/development/kdiff3");
98 99

    KAboutData aboutData(appName, i18nName,
100
                         appVersion, description, KAboutLicense::GPL_V2, copyright, QString(),
101
                         homePage);
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
102

103 104 105
    KAboutData::setApplicationData(aboutData);

    QCommandLineParser* cmdLineParser = KDiff3Shell::getParser();
106
    cmdLineParser->setApplicationDescription(aboutData.shortDescription());
Michael Reeves's avatar
Michael Reeves committed
107 108

    aboutData.setupCommandLine(cmdLineParser);
Michael Reeves's avatar
Michael Reeves committed
109

110
    initialiseCmdLineArgs(cmdLineParser);
Michael Reeves's avatar
Michael Reeves committed
111
    // ignorable command options
112 113 114 115 116
    cmdLineParser->addOption(QCommandLineOption(QStringList() << QLatin1String("m") << QLatin1String("merge"), i18n("Merge the input.")));
    cmdLineParser->addOption(QCommandLineOption(QStringList() << QLatin1String("b") << QLatin1String("base"), i18n("Explicit base file. For compatibility with certain tools."), QLatin1String("file")));
    cmdLineParser->addOption(QCommandLineOption(QStringList() << QLatin1String("o") << QLatin1String("output"), i18n("Output file. Implies -m. E.g.: -o newfile.txt"), QLatin1String("file")));
    cmdLineParser->addOption(QCommandLineOption(QStringList() << QLatin1String("out"), i18n("Output file, again. (For compatibility with certain tools.)"), QLatin1String("file")));
    cmdLineParser->addOption(QCommandLineOption(QStringList() << QLatin1String("auto"), i18n("No GUI if all conflicts are auto-solvable. (Needs -o file)")));
Pino Toscano's avatar
Pino Toscano committed
117
    cmdLineParser->addOption(QCommandLineOption(QStringList() << QLatin1String("qall"), i18n("Do not solve conflicts automatically.")));
118 119 120 121 122 123 124
    cmdLineParser->addOption(QCommandLineOption(QStringList() << QLatin1String("L1"), i18n("Visible name replacement for input file 1 (base)."), QLatin1String("alias1")));
    cmdLineParser->addOption(QCommandLineOption(QStringList() << QLatin1String("L2"), i18n("Visible name replacement for input file 2."), QLatin1String("alias2")));
    cmdLineParser->addOption(QCommandLineOption(QStringList() << QLatin1String("L3"), i18n("Visible name replacement for input file 3."), QLatin1String("alias3")));
    cmdLineParser->addOption(QCommandLineOption(QStringList() << QLatin1String("L") << QLatin1String("fname alias"), i18n("Alternative visible name replacement. Supply this once for every input.")));
    cmdLineParser->addOption(QCommandLineOption(QStringList() << QLatin1String("cs"), i18n("Override a config setting. Use once for every setting. E.g.: --cs \"AutoAdvance=1\""), QLatin1String("string")));
    cmdLineParser->addOption(QCommandLineOption(QStringList() << QLatin1String("confighelp"), i18n("Show list of config settings and current values.")));
    cmdLineParser->addOption(QCommandLineOption(QStringList() << QLatin1String("config"), i18n("Use a different config file."), QLatin1String("file")));
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
125

Michael Reeves's avatar
Michael Reeves committed
126
    // other command options
127 128 129 130
    cmdLineParser->addPositionalArgument(QLatin1String("[File1]"), i18n("file1 to open (base, if not specified via --base)"));
    cmdLineParser->addPositionalArgument(QLatin1String("[File2]"), i18n("file2 to open"));
    cmdLineParser->addPositionalArgument(QLatin1String("[File3]"), i18n("file3 to open"));

131 132 133 134 135
    bool isAtty = true;

#ifndef Q_OS_WIN
    isAtty = isatty(fileno(stderr)) == 1;//will be true for redirected output as well
#endif
136
    /*
137 138
        QCommandLineParser::process does what is expected on windows or when running from a commandline.
        However, it only accounts for a lackof terminal output on windows.
139
    */
140 141 142
    if(isAtty)
    {
        cmdLineParser->process(QCoreApplication::arguments());
143
    }
144 145 146
    else
    {
        /*
147
            There is no terminal connected so don't just exit mysteriously on error.
148 149 150 151
        */
        if(!cmdLineParser->parse(QCoreApplication::arguments()))
        {
            QString errorMessage = cmdLineParser->errorText();
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166
            KMessageBox::error(nullptr, "<html><head/><body><h2>" + errorMessage + "</h2><pre>" + i18n("See kdiff3 --help for supported options.") + "</pre></body></html>", aboutData.displayName());
            exit(1);
        }

        if(cmdLineParser->isSet(QStringLiteral("version"))) {
            KMessageBox::information(nullptr,
                                    aboutData.displayName() + ' ' + aboutData.version(), aboutData.displayName());
            exit(0);
        }
        if(cmdLineParser->isSet(QStringLiteral("help"))) {
            KMessageBox::information(nullptr, "<html><head/><body><pre>" + cmdLineParser->helpText() + "</pre></body></html>", aboutData.displayName());

            exit(0);
        }
167
    }
Michael Reeves's avatar
Michael Reeves committed
168 169

    aboutData.processCommandLine(cmdLineParser);
170 171 172 173 174 175 176

    KDiff3Shell* p = new KDiff3Shell();
    p->show();
    //p->setWindowState( p->windowState() | Qt::WindowActive ); // Patch for ubuntu: window not active on startup
    //app.installEventFilter( new CFilter );
    int retVal = app.exec();
    /*  if (QApplication::clipboard()->text().size() == 0)
177
     QApplication::clipboard()->clear(); // Patch for Ubuntu: Fix issue with Qt clipboard*/
178
    return retVal;
Joachim Eibl's avatar
Joachim Eibl committed
179
}
Joachim Eibl's avatar
Joachim Eibl committed
180