File indexing completed on 2024-04-28 05:42:48

0001 /*
0002     SPDX-FileCopyrightText: 2001-2005, 2009 Otto Bruggeman <bruggie@gmail.com>
0003     SPDX-FileCopyrightText: 2001-2003 John Firebaugh <jfirebaugh@kde.org>
0004     SPDX-FileCopyrightText: 2007-2012 Kevin Kofler <kevin.kofler@chello.at>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 /**
0010  * @file main.cpp
0011  * This is the main entry point for Kompare.
0012  * The command line arguments are handled and the application is started.
0013  * @author Otto Bruggeman <otto.bruggeman@home.nl>
0014  * @author John Firebaugh <jfirebaugh@kde.org>
0015  * @author Kevin Kofler   <kevin.kofler@chello.at>
0016  */
0017 
0018 #include <KAboutData>
0019 
0020 #include <KFile>
0021 #include <KLocalizedString>
0022 
0023 #include <QApplication>
0024 #include <QPushButton>
0025 #include <QDebug>
0026 #include <QCommandLineParser>
0027 
0028 #include "kompareinterface.h"
0029 
0030 #include "../kompare_version.h"
0031 #include "kompare_shell.h"
0032 #include "kompareurldialog.h"
0033 
0034 /**
0035  * Setting up the KAboutData structure.
0036  * Parsing and handling of the given command line arguments.
0037  * @param argc   the number of arguments
0038  * @param argv   the array of arguments
0039  * @return exit status
0040  */
0041 int main(int argc, char* argv[])
0042 {
0043     QApplication app(argc, argv);
0044     KLocalizedString::setApplicationDomain("kompare");
0045 
0046     KAboutData aboutData(QStringLiteral("kompare"),  i18n("Kompare"),
0047                          QStringLiteral(KOMPARE_VERSION_STRING),
0048                          i18n("A program to view the differences between files and optionally generate a diff"),
0049                          KAboutLicense::GPL,
0050                          i18n("(c) 2001-2004 John Firebaugh, (c) 2001-2005,2009 Otto Bruggeman, (c) 2004-2005 Jeff Snyder, (c) 2007-2012 Kevin Kofler"),
0051                          QString(),
0052                          QStringLiteral("https://apps.kde.org/kompare"));
0053     aboutData.addAuthor(i18n("John Firebaugh"), i18n("Author"), QStringLiteral("jfirebaugh@kde.org"));
0054     aboutData.addAuthor(i18n("Otto Bruggeman"), i18n("Author"), QStringLiteral("bruggie@gmail.com"));
0055     aboutData.addAuthor(i18n("Jeff Snyder"), i18n("Developer"), QStringLiteral("jeff@caffeinated.me.uk"));
0056     aboutData.addCredit(i18n("Kevin Kofler"), i18n("Maintainer"), QStringLiteral("kevin.kofler@chello.at"));
0057     aboutData.addCredit(i18n("Chris Luetchford"), i18n("Kompare icon artist"), QStringLiteral("chris@os11.com"));
0058     aboutData.addCredit(i18n("Malte Starostik"), i18n("A lot of good advice"), QStringLiteral("malte@kde.org"));
0059     aboutData.addCredit(i18n("Bernd Gehrmann"), i18n("Cervisia diff viewer"), QStringLiteral("bernd@physik.hu-berlin.de"));
0060 
0061     KAboutData::setApplicationData(aboutData);
0062     app.setWindowIcon(QIcon::fromTheme(QStringLiteral("kompare"), app.windowIcon()));
0063 
0064     QCommandLineParser parser;
0065     aboutData.setupCommandLine(&parser);
0066     parser.addOption(QCommandLineOption(QStringLiteral("c"), i18n("This will compare URL1 with URL2")));
0067     parser.addOption(QCommandLineOption(QStringLiteral("o"), i18n("This will open URL1 and expect it to be diff output. URL1 can also be a '-' and then it will read from standard input. Can be used for instance for cvs diff | kompare -o -. Kompare will do a check to see if it can find the original file(s) and then blend the original file(s) into the diffoutput and show that in the viewer. -n disables the check.")));
0068     parser.addOption(QCommandLineOption(QStringLiteral("b"), i18n("This will blend URL2 into URL1, URL2 is expected to be diff output and URL1 the file or folder that the diffoutput needs to be blended into. ")));
0069     parser.addOption(QCommandLineOption(QStringLiteral("n"), i18n("Disables the check for automatically finding the original file(s) when using '-' as URL with the -o option.")));
0070     parser.addOption(QCommandLineOption(QStringLiteral("e <encoding>"), i18n("Use this to specify the encoding when calling it from the command line. It will default to the local encoding if not specified.")));
0071 
0072     parser.process(app);
0073     aboutData.processCommandLine(&parser);
0074 
0075     bool difault = false;
0076 
0077     KompareShell* ks;
0078 
0079     QStringList args = parser.positionalArguments();
0080 
0081     // see if we are starting with session management
0082     if (app.isSessionRestored())
0083     {
0084         kRestoreMainWindows<KompareShell>();
0085     }
0086     else
0087     {
0088 
0089         ks = new KompareShell();
0090         ks->setObjectName(QStringLiteral("FirstKompareShell"));
0091 
0092         qCDebug(KOMPARESHELL) << "Arg Count = " << args.count() ;
0093         for (int i = 0; i < args.count(); ++i)
0094         {
0095             qCDebug(KOMPARESHELL) << "Argument " << (i + 1) << ": " << args.at(i) ;
0096         }
0097 
0098         if (parser.isSet(QStringLiteral("e <encoding>")))
0099         {
0100             // Encoding given...
0101             // FIXME: Need to implement this...
0102         }
0103 
0104         if (parser.isSet(QStringLiteral("o")))
0105         {
0106             qCDebug(KOMPARESHELL) << "Option -o is set" ;
0107             if (args.count() != 1)
0108             {
0109                 difault = true;
0110             }
0111             else
0112             {
0113                 ks->show();
0114                 qCDebug(KOMPARESHELL) << "OpenDiff..." ;
0115                 if (args.at(0) == QLatin1String("-"))
0116                     ks->openStdin();
0117                 else
0118                     ks->openDiff(QUrl::fromUserInput(args.at(0), QDir::currentPath(), QUrl::AssumeLocalFile));
0119                 difault = false;
0120             }
0121         }
0122         else if (parser.isSet(QStringLiteral("c")))
0123         {
0124             qCDebug(KOMPARESHELL) << "Option -c is set" ;
0125             if (args.count() != 2)
0126             {
0127                 parser.showHelp();
0128                 difault = true;
0129             }
0130             else
0131             {
0132                 ks->show();
0133                 QUrl url0 = QUrl::fromUserInput(args.at(0), QDir::currentPath(), QUrl::AssumeLocalFile);
0134                 qCDebug(KOMPARESHELL) << "URL0 = " << url0.url() ;
0135                 QUrl url1 = QUrl::fromUserInput(args.at(1), QDir::currentPath(), QUrl::AssumeLocalFile);
0136                 qCDebug(KOMPARESHELL) << "URL1 = " << url1.url() ;
0137                 ks->compare(url0, url1);
0138                 difault = false;
0139             }
0140         }
0141         else if (parser.isSet(QStringLiteral("b")))
0142         {
0143             qCDebug(KOMPARESHELL) << "Option -b is set" ;
0144             if (args.count() != 2)
0145             {
0146                 parser.showHelp();
0147                 difault = true;
0148             }
0149             else
0150             {
0151                 ks->show();
0152                 qCDebug(KOMPARESHELL) << "blend..." ;
0153                 QUrl url0 = QUrl::fromUserInput(args.at(0), QDir::currentPath(), QUrl::AssumeLocalFile);
0154                 qCDebug(KOMPARESHELL) << "URL0 = " << url0.url() ;
0155                 QUrl url1 = QUrl::fromUserInput(args.at(1), QDir::currentPath(), QUrl::AssumeLocalFile);
0156                 qCDebug(KOMPARESHELL) << "URL1 = " << url1.url() ;
0157                 ks->blend(url0, url1);
0158                 difault = false;
0159             }
0160         }
0161         else if (args.count() == 1)
0162         {
0163             ks->show();
0164 
0165             qCDebug(KOMPARESHELL) << "Single file. so openDiff/openStdin is only possible..." ;
0166             if (args.at(0) == QLatin1String("-"))
0167                 ks->openStdin();
0168             else
0169                 ks->openDiff(QUrl::fromUserInput(args.at(0), QDir::currentPath(), QUrl::AssumeLocalFile));
0170 
0171             difault = false;
0172         }
0173         else if (args.count() == 2)
0174         {
0175             // In this case we are assuming you want to compare files/dirs
0176             // and not blending because that is almost impossible to detect
0177             ks->show();
0178             qCDebug(KOMPARESHELL) << "Dunno, we'll have to figure it out later, trying compare for now..." ;
0179             QUrl url0 = QUrl::fromUserInput(args.at(0), QDir::currentPath(), QUrl::AssumeLocalFile);
0180             qCDebug(KOMPARESHELL) << "URL0 = " << url0.url() ;
0181             QUrl url1 = QUrl::fromUserInput(args.at(1), QDir::currentPath(), QUrl::AssumeLocalFile);
0182             qCDebug(KOMPARESHELL) << "URL1 = " << url1.url() ;
0183             ks->compare(url0, url1);
0184             difault = false;
0185         }
0186         else if (args.count() == 0)   // no options and no args
0187         {
0188             difault = true;
0189         }
0190 
0191         if (difault)
0192         {
0193             KompareURLDialog dialog(nullptr);
0194 
0195             dialog.setWindowTitle(i18nc("@title:window", "Compare Files or Folders"));
0196             dialog.setFirstGroupBoxTitle(i18nc("@title:group", "Source"));
0197             dialog.setSecondGroupBoxTitle(i18nc("@title:group", "Destination"));
0198 
0199             QPushButton* okButton = dialog.button(QDialogButtonBox::Ok);
0200             okButton->setText(i18nc("@action:button", "Compare"));
0201             okButton->setToolTip(i18nc("@info:tooltip", "Compare these files or folders"));
0202             okButton->setWhatsThis(i18nc("@info:whatsthis", "If you have entered 2 filenames or 2 folders in the fields in this dialog then this button will be enabled and pressing it will start a comparison of the entered files or folders. "));
0203 
0204             dialog.setGroup(QStringLiteral("Recent Compare Files"));
0205 
0206             dialog.setFirstURLRequesterMode(KFile::File | KFile::Directory | KFile::ExistingOnly);
0207             dialog.setSecondURLRequesterMode(KFile::File | KFile::Directory | KFile::ExistingOnly);
0208 
0209             if (dialog.exec() == QDialog::Accepted)
0210             {
0211                 ks->show();
0212                 ks->viewPart()->setEncoding(dialog.encoding());
0213                 ks->compare(dialog.getFirstURL(), dialog.getSecondURL());
0214             }
0215             else
0216             {
0217                 return -1;
0218             }
0219         }
0220 
0221 
0222     }
0223 
0224     return app.exec();
0225 }