File indexing completed on 2024-04-21 05:49:59

0001 //**************************************************************************
0002 //   Copyright 2006 - 2018 Martin Koller, kollix@aon.at
0003 //
0004 //   This program is free software; you can redistribute it and/or modify
0005 //   it under the terms of the GNU General Public License as published by
0006 //   the Free Software Foundation, version 2 of the License
0007 //
0008 //**************************************************************************
0009 
0010 #include <signal.h>
0011 
0012 #include <memory>
0013 
0014 #include <QApplication>
0015 #include <QCommandLineParser>
0016 #include <QCommandLineOption>
0017 #include <QString>
0018 #include <QTimer>
0019 #include <QPointer>
0020 
0021 #include <KAboutData>
0022 #include <KLocalizedString>
0023 
0024 #include <MainWindow.hxx>
0025 #include <Archiver.hxx>
0026 
0027 #include <iostream>
0028 
0029 //--------------------------------------------------------------------------------
0030 
0031 void sigHandler(int sig)
0032 {
0033   Q_UNUSED(sig)
0034 
0035   QTimer::singleShot(0, Archiver::instance, &Archiver::cancel);
0036   QTimer::singleShot(0, QCoreApplication::instance(), SLOT(quit()));
0037 }
0038 
0039 //--------------------------------------------------------------------------------
0040 
0041 int main(int argc, char **argv)
0042 {
0043   std::unique_ptr<QCoreApplication> app(new QCoreApplication(argc, argv));
0044 
0045   KLocalizedString::setApplicationDomain(QByteArrayLiteral("kbackup"));
0046 
0047   KAboutData about(QStringLiteral("kbackup"), i18n("KBackup"),
0048                    QStringLiteral(KBACKUP_VERSION), i18n("An easy to use backup program"), KAboutLicense::GPL_V2,
0049                    i18n("(c) 2006 - 2018 Martin Koller"),  // copyright
0050                    QString(),  // added text
0051                    QStringLiteral("https://www.linux-apps.com/content/show.php?content=44998"));  // homepage
0052 
0053   about.addAuthor(i18n("Martin Koller"), i18n("Developer"), QStringLiteral("kollix@aon.at"));
0054 
0055   about.setDesktopFileName(QStringLiteral("org.kde.kbackup"));
0056 
0057   KAboutData::setApplicationData(about);
0058 
0059   QCommandLineParser cmdLine;
0060 
0061   cmdLine.addPositionalArgument(QStringLiteral("profile"), i18n("Start with given profile."), QStringLiteral("[profile]"));
0062 
0063   cmdLine.addOption(QCommandLineOption(QStringLiteral("script"), i18n("Script to run after finishing one archive slice."), QStringLiteral("file")));
0064 
0065   cmdLine.addOption(QCommandLineOption(QStringLiteral("auto"), i18n("Automatically run the backup with the given profile "
0066                                                     "and terminate when done."), QStringLiteral("profile")));
0067 
0068   cmdLine.addOption(QCommandLineOption(QStringLiteral("autobg"), i18n("Automatically run the backup with the given profile "
0069                                                       "in the background (without showing a window) "
0070                                                       "and terminate when done."), QStringLiteral("profile")));
0071 
0072 
0073   cmdLine.addOption(QCommandLineOption(QStringLiteral("verbose"), i18n("In autobg mode be verbose and print every "
0074                                                        "single filename during backup.")));
0075 
0076   cmdLine.addOption(QCommandLineOption(QStringLiteral("forceFull"), i18n("In auto/autobg mode force the backup to be a full backup "
0077                                                          "instead of acting on the profile settings.")));
0078 
0079   about.setupCommandLine(&cmdLine);
0080   cmdLine.process(*app);
0081   about.processCommandLine(&cmdLine);
0082 
0083   bool interactive = !cmdLine.isSet(QStringLiteral("autobg"));
0084 
0085   if ( interactive )
0086   {
0087     delete app.release();  // must make explicitly. Only reset() leads to error
0088     // kf5.kcoreaddons.kaboutdata: Could not initialize the equivalent properties of Q*Application: no instance (yet) existing.
0089     app.reset(new QApplication(argc, argv));
0090     QApplication *qapp = qobject_cast<QApplication *>(app.get());
0091     qapp->setWindowIcon(QIcon::fromTheme(QStringLiteral("kbackup")));
0092 
0093     KAboutData::setApplicationData(about);
0094   }
0095 
0096   QPointer<MainWindow> mainWin;
0097 
0098   if ( interactive )
0099   {
0100     mainWin = new MainWindow;
0101     mainWin->show();
0102   }
0103   else
0104     new Archiver(nullptr);
0105 
0106   signal(SIGTERM, sigHandler);
0107   signal(SIGINT, sigHandler);
0108 
0109   QString file = cmdLine.value(QStringLiteral("script"));
0110   if ( file.length() )
0111     Archiver::sliceScript = file;
0112 
0113   if ( interactive )
0114   {
0115     QString profile;
0116 
0117     QStringList args = cmdLine.positionalArguments();
0118 
0119     if ( !args.isEmpty() )
0120       profile = args[0];
0121 
0122     const QString file = cmdLine.value(QStringLiteral("auto"));
0123     if ( !file.isEmpty() )
0124       profile = file;
0125 
0126     if ( !profile.isEmpty() )
0127       mainWin->loadProfile(profile, true);
0128 
0129     if ( cmdLine.isSet(QStringLiteral("forceFull")) )
0130       Archiver::instance->setForceFullBackup();
0131 
0132     if ( cmdLine.isSet(QStringLiteral("auto")) )
0133       mainWin->runBackup();
0134 
0135     int ret = app->exec();
0136 
0137     delete mainWin;
0138     return ret;
0139   }
0140   else
0141   {
0142     QStringList includes, excludes;
0143     QString error, fileName = cmdLine.value(QStringLiteral("autobg"));
0144 
0145     Archiver::instance->setVerbose(cmdLine.isSet(QStringLiteral("verbose")));
0146 
0147     if ( !Archiver::instance->loadProfile(fileName, includes, excludes, error) )
0148     {
0149       std::cerr << qPrintable(i18n("Could not open profile '%1' for reading: %2", fileName, error)) << std::endl;
0150       return -1;
0151     }
0152     else
0153     {
0154       if ( cmdLine.isSet(QStringLiteral("forceFull")) )
0155         Archiver::instance->setForceFullBackup();
0156 
0157       if ( Archiver::instance->createArchive(includes, excludes) )
0158         return 0;
0159       else
0160         return -1;
0161     }
0162   }
0163 
0164   return 0;
0165 }