File indexing completed on 2024-04-28 15:40:12

0001 /* SPDX-FileCopyrightText: 2016-2020 The KPhotoAlbum Development Team
0002 
0003    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 #include "Options.h"
0006 
0007 #include "Logging.h"
0008 
0009 #include <KLocalizedString>
0010 #include <QCommandLineOption>
0011 #include <QCommandLineParser>
0012 
0013 MainWindow::Options *MainWindow::Options::s_instance = nullptr;
0014 
0015 namespace MainWindow
0016 {
0017 class Options::OptionsPrivate
0018 {
0019 public:
0020     QCommandLineParser parser;
0021 
0022     // legacy option: "-c <imageDirectory>"
0023     QCommandLineOption configFile {
0024         QLatin1String("c"),
0025         i18n("Use <databaseFile> instead of the default. Deprecated - use '--db <databaseFile>' instead."),
0026         i18n("databaseFile")
0027     };
0028     QCommandLineOption dbFile {
0029         QLatin1String("db"),
0030         i18n("Use <databaseFile> instead of the default."),
0031         i18n("databaseFile")
0032     };
0033     QCommandLineOption demoOption { QLatin1String("demo"), i18n("Starts KPhotoAlbum with a prebuilt set of demo images.") };
0034     QCommandLineOption importFile {
0035         QLatin1String("import"),
0036         i18n("Import file."),
0037         i18n("file.kim")
0038     };
0039 #ifdef KPA_ENABLE_REMOTECONTROL
0040     // QCommandLineParser doesn't support optional values.
0041     // therefore, we need two separate options:
0042     QCommandLineOption listen {
0043         QLatin1String("listen"),
0044         i18n("Listen for network connections.")
0045     };
0046     QCommandLineOption listenAddress {
0047         QLatin1String("listen-address"),
0048         i18n("Listen for network connections on address <interface_address>."),
0049         i18n("interface_address")
0050     };
0051 #endif
0052     QCommandLineOption searchOnStartup { QLatin1String("search"), i18n("Search for new images on startup.") };
0053 };
0054 }
0055 
0056 MainWindow::Options *MainWindow::Options::the()
0057 {
0058     if (!s_instance)
0059         s_instance = new Options();
0060     return s_instance;
0061 }
0062 
0063 QCommandLineParser *MainWindow::Options::parser() const
0064 {
0065     return &(d->parser);
0066 }
0067 
0068 QUrl MainWindow::Options::dbFile() const
0069 {
0070     QUrl db;
0071     if (d->parser.isSet(d->dbFile)) {
0072         db = QUrl::fromLocalFile(d->parser.value(d->dbFile));
0073     } else if (d->parser.isSet(d->configFile)) {
0074         // support for legacy option
0075         db = QUrl::fromLocalFile(d->parser.value(d->configFile));
0076     }
0077     return db;
0078 }
0079 
0080 bool MainWindow::Options::demoMode() const
0081 {
0082     return d->parser.isSet(d->demoOption);
0083 }
0084 
0085 QUrl MainWindow::Options::importFile() const
0086 {
0087     if (d->parser.isSet(d->importFile))
0088         return QUrl::fromLocalFile(d->parser.value(d->importFile));
0089     return QUrl();
0090 }
0091 
0092 QHostAddress MainWindow::Options::listen() const
0093 {
0094 #ifdef KPA_ENABLE_REMOTECONTROL
0095     QHostAddress address;
0096     QString value = d->parser.value(d->listenAddress);
0097     if (d->parser.isSet(d->listen) || !value.isEmpty()) {
0098         if (value.isEmpty())
0099             address = QHostAddress::Any;
0100         else
0101             address = QHostAddress(value);
0102     }
0103     if (address.isMulticast() || address == QHostAddress::Broadcast) {
0104         qCWarning(MainWindowLog) << "Won't bind to address" << address;
0105         address = QHostAddress::Null;
0106     }
0107     return address;
0108 #else
0109     return {};
0110 #endif
0111 }
0112 
0113 bool MainWindow::Options::searchForImagesOnStart() const
0114 {
0115     return d->parser.isSet(d->searchOnStartup);
0116 }
0117 
0118 MainWindow::Options::Options()
0119     : d(new OptionsPrivate)
0120 {
0121     d->configFile.setFlags(QCommandLineOption::HiddenFromHelp);
0122     d->parser.addOptions(
0123         QList<QCommandLineOption>()
0124         << d->configFile
0125         << d->dbFile
0126         << d->demoOption
0127         << d->importFile
0128 #ifdef KPA_ENABLE_REMOTECONTROL
0129         << d->listen
0130         << d->listenAddress
0131 #endif
0132         << d->searchOnStartup);
0133 }
0134 
0135 // vi:expandtab:tabstop=4 shiftwidth=4: