1 //**************************************************************************
2 //   Copyright 2006 - 2018 Martin Koller, kollix@aon.at
3 //
4 //   This program is free software; you can redistribute it and/or modify
5 //   it under the terms of the GNU General Public License as published by
6 //   the Free Software Foundation, version 2 of the License
7 //
8 //**************************************************************************
9 
10 #include <signal.h>
11 
12 #include <QApplication>
13 #include <QScopedPointer>
14 #include <QCommandLineParser>
15 #include <QCommandLineOption>
16 #include <QString>
17 #include <QTimer>
18 #include <QPointer>
19 
20 #include <KAboutData>
21 #include <KLocalizedString>
22 
23 #include <MainWindow.hxx>
24 #include <Archiver.hxx>
25 
26 #include <iostream>
27 
28 //--------------------------------------------------------------------------------
29 
sigHandler(int sig)30 void sigHandler(int sig)
31 {
32   Q_UNUSED(sig)
33 
34   QTimer::singleShot(0, Archiver::instance, &Archiver::cancel);
35   QTimer::singleShot(0, QCoreApplication::instance(), SLOT(quit()));
36 }
37 
38 //--------------------------------------------------------------------------------
39 
main(int argc,char ** argv)40 int main(int argc, char **argv)
41 {
42   QScopedPointer<QCoreApplication> app(new QCoreApplication(argc, argv));
43 
44   KLocalizedString::setApplicationDomain("kbackup");
45 
46   KAboutData about(QStringLiteral("kbackup"), i18n("KBackup"),
47                    QStringLiteral(KBACKUP_VERSION), i18n("An easy to use backup program"), KAboutLicense::GPL_V2,
48                    i18n("(c) 2006 - 2018 Martin Koller"),  // copyright
49                    QString(),  // added text
50                    QStringLiteral("https://www.linux-apps.com/content/show.php?content=44998"));  // homepage
51 
52   about.addAuthor(i18n("Martin Koller"), i18n("Developer"), QStringLiteral("kollix@aon.at"));
53 
54   about.setOrganizationDomain(QByteArray("kde.org"));
55   about.setDesktopFileName(QStringLiteral("org.kde.kbackup"));
56 
57   KAboutData::setApplicationData(about);
58 
59   QCommandLineParser cmdLine;
60 
61   cmdLine.addPositionalArgument(QStringLiteral("profile"), i18n("Start with given profile."), QStringLiteral("[profile]"));
62 
63   cmdLine.addOption(QCommandLineOption(QStringLiteral("script"), i18n("Script to run after finishing one archive slice."), QStringLiteral("file")));
64 
65   cmdLine.addOption(QCommandLineOption(QStringLiteral("auto"), i18n("Automatically run the backup with the given profile "
66                                                     "and terminate when done."), QStringLiteral("profile")));
67 
68   cmdLine.addOption(QCommandLineOption(QStringLiteral("autobg"), i18n("Automatically run the backup with the given profile "
69                                                       "in the background (without showing a window) "
70                                                       "and terminate when done."), QStringLiteral("profile")));
71 
72 
73   cmdLine.addOption(QCommandLineOption(QStringLiteral("verbose"), i18n("In autobg mode be verbose and print every "
74                                                        "single filename during backup.")));
75 
76   cmdLine.addOption(QCommandLineOption(QStringLiteral("forceFull"), i18n("In auto/autobg mode force the backup to be a full backup "
77                                                          "instead of acting on the profile settings.")));
78 
79   about.setupCommandLine(&cmdLine);
80   cmdLine.process(*app);
81   about.processCommandLine(&cmdLine);
82 
83   bool interactive = !cmdLine.isSet(QStringLiteral("autobg"));
84 
85   if ( interactive )
86   {
87     delete app.take();  // must make explicitly. Only reset() leads to error
88     // kf5.kcoreaddons.kaboutdata: Could not initialize the equivalent properties of Q*Application: no instance (yet) existing.
89     app.reset(new QApplication(argc, argv));
90     QApplication *qapp = qobject_cast<QApplication *>(app.data());
91     qapp->setAttribute(Qt::AA_UseHighDpiPixmaps, true);
92     qapp->setWindowIcon(QIcon::fromTheme(QStringLiteral("kbackup")));
93 
94     KAboutData::setApplicationData(about);
95   }
96 
97   QPointer<MainWindow> mainWin;
98 
99   if ( interactive )
100   {
101     mainWin = new MainWindow;
102     mainWin->show();
103   }
104   else
105     new Archiver(nullptr);
106 
107   signal(SIGTERM, sigHandler);
108   signal(SIGINT, sigHandler);
109 
110   QString file = cmdLine.value(QStringLiteral("script"));
111   if ( file.length() )
112     Archiver::sliceScript = file;
113 
114   if ( interactive )
115   {
116     QString profile;
117 
118     QStringList args = cmdLine.positionalArguments();
119 
120     if ( !args.isEmpty() )
121       profile = args[0];
122 
123     QString file = cmdLine.value(QStringLiteral("auto"));
124     if ( file.length() )
125       profile = file;
126 
127     if ( profile.length() )
128       mainWin->loadProfile(profile, true);
129 
130     if ( cmdLine.isSet(QStringLiteral("forceFull")) )
131       Archiver::instance->setForceFullBackup();
132 
133     if ( cmdLine.isSet(QStringLiteral("auto")) )
134       mainWin->runBackup();
135 
136     int ret = app->exec();
137 
138     delete mainWin;
139     return ret;
140   }
141   else
142   {
143     QStringList includes, excludes;
144     QString error, fileName = cmdLine.value(QStringLiteral("autobg"));
145 
146     Archiver::instance->setVerbose(cmdLine.isSet(QStringLiteral("verbose")));
147 
148     if ( !Archiver::instance->loadProfile(fileName, includes, excludes, error) )
149     {
150       std::cerr << qPrintable(i18n("Could not open profile '%1' for reading: %2", fileName, error)) << std::endl;
151       return -1;
152     }
153     else
154     {
155       if ( cmdLine.isSet(QStringLiteral("forceFull")) )
156         Archiver::instance->setForceFullBackup();
157 
158       if ( Archiver::instance->createArchive(includes, excludes) )
159         return 0;
160       else
161         return -1;
162     }
163   }
164 
165   return 0;
166 }
167