File indexing completed on 2024-05-12 04:57:16

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0003     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "config.h"
0009 #include "config-subtitlecomposer.h"
0010 
0011 #include "application.h"
0012 #include "mainwindow.h"
0013 #include "helpers/commondefs.h"
0014 #include "videoplayer/backend/glrenderer.h"
0015 
0016 #include <KAboutData>
0017 #include <KLocalizedString>
0018 
0019 #include <QFile>
0020 #include <QDir>
0021 #include <QCommandLineParser>
0022 #include <QCommandLineOption>
0023 #include <QProcessEnvironment>
0024 #include <QResource>
0025 #include <QMimeDatabase>
0026 
0027 extern "C" {
0028 #include <libavcodec/avcodec.h>
0029 #include <libavformat/avformat.h>
0030 }
0031 
0032 using namespace SubtitleComposer;
0033 
0034 static bool
0035 mimeIsMedia(const QMimeType &mime)
0036 {
0037     const static QStringList mimeList = {
0038         "video/webm", "video/x-msvideo", "video/mp2t", "application/x-matroska",
0039         "video/x-matroska", "video/mp4", "video/mpeg", "video/ogg"
0040     };
0041     for(const QString &m: mimeList) {
0042         if(mime.inherits(m))
0043             return true;
0044     }
0045     return false;
0046 }
0047 
0048 static bool
0049 mimeIsSubtitle(const QMimeType &mime)
0050 {
0051     const static QStringList mimeList = {
0052         "application/x-subrip", "text/x-ssa", "text/x-ass","text/x-microdvd",
0053         "text/x-mpsub", "text/x-subviewer", "text/x-mplsub", "text/x-tmplayer",
0054         "text/x-tmplayer+", "application/x-vobsub"
0055     };
0056     for(const QString &m: mimeList) {
0057         if(mime.inherits(m))
0058             return true;
0059     }
0060     return false;
0061 }
0062 
0063 static void
0064 handleCommandLine(SubtitleComposer::Application &app, KAboutData &aboutData)
0065 {
0066     QCommandLineParser parser;
0067     aboutData.setupCommandLine(&parser);
0068     parser.setApplicationDescription(aboutData.shortDescription());
0069 
0070     QCommandLineOption subtitleOption("subtitle",
0071         i18n("The primary subtitle to be edited."),
0072         "file name");
0073     parser.addOption(subtitleOption);
0074 
0075     QCommandLineOption videoOption("video",
0076         i18n("The video to be subtitled."),
0077         "file name");
0078     parser.addOption(videoOption);
0079 
0080     QCommandLineOption translationOption("translation",
0081         i18n("The translation subtitle to be edited.\n"
0082             "If included, a second pane gets added to the right of the primary subtitle "
0083             "for side-by-side translation."),
0084         "file name");
0085     parser.addOption(translationOption);
0086 
0087     // parse command line
0088     parser.process(app);
0089     aboutData.processCommandLine(&parser);
0090 
0091     app.mainWindow()->show();
0092 
0093     QString fileSub;
0094     QString fileTrans;
0095     QString fileVideo;
0096 
0097     // load files by specific options
0098     if(parser.isSet(subtitleOption))
0099         fileSub = parser.value(subtitleOption);
0100     if(parser.isSet(videoOption))
0101         fileVideo = parser.value(videoOption);
0102     if(parser.isSet(translationOption))
0103         fileTrans = parser.value(translationOption);
0104 
0105     // load sub/video if positional param is provided
0106     const QStringList args = parser.positionalArguments();
0107     for(const QString &arg: args) {
0108         const QMimeType mime = QMimeDatabase().mimeTypeForFile(arg);
0109         if(mimeIsSubtitle(mime)) {
0110             // try to open primary subtitle if not already opened
0111             if(fileSub.isEmpty()) {
0112                 fileSub = arg;
0113                 continue;
0114             }
0115             // try to open translation if not already opened
0116             if(fileTrans.isEmpty()) {
0117                 fileTrans = arg;
0118                 continue;
0119             }
0120         } else if(mimeIsMedia(mime)) {
0121             // try to open video if not already opened
0122             if(fileVideo.isEmpty()) {
0123                 fileVideo = arg;
0124                 continue;
0125             }
0126         }
0127     }
0128 
0129     if(!fileSub.isEmpty())
0130         app.openSubtitle(System::urlFromPath(fileSub));
0131     else
0132         app.newSubtitle();
0133     if(!fileTrans.isEmpty())
0134         app.openSubtitleTr(System::urlFromPath(fileTrans));
0135     if(!fileVideo.isEmpty())
0136         app.openVideo(System::urlFromPath(fileVideo));
0137 }
0138 
0139 int
0140 main(int argc, char **argv)
0141 {
0142     GLRenderer::setupProfile();
0143 
0144 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
0145     av_register_all();
0146     avcodec_register_all();
0147 #endif
0148 
0149     SubtitleComposer::Application app(argc, argv);
0150 
0151     KAboutData aboutData(
0152         QStringLiteral("subtitlecomposer"),
0153         i18n("Subtitle Composer"),
0154         SUBTITLECOMPOSER_VERSION_STRING,
0155         i18n("A KDE subtitle editor."),
0156         KAboutLicense::GPL_V2,
0157         QStringLiteral("&copy; 2007-2021 The Subtitle Composer Authors"),
0158         QString(), // Additional text
0159         QStringLiteral("https://subtitlecomposer.kde.org/"));
0160 
0161     aboutData.addAuthor(QStringLiteral("Mladen Milinković"), i18n("Maintainer"), "maxrd2@smoothware.net");
0162     aboutData.addAuthor(QStringLiteral("Sergio Pistone"), i18n("Former Maintainer"), "sergio_pistone@yahoo.com.ar");
0163     aboutData.addAuthor(QStringLiteral("Thiago Sueto"), i18n("Patches and Website"), "herzenschein@gmail.com");
0164 
0165     aboutData.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
0166 
0167     aboutData.addCredit(QStringLiteral("Martin Steghöfer"), i18n("code contributions"));
0168     aboutData.addCredit(QStringLiteral("Marius Kittler"), i18n("code contributions, Arch Linux packaging"));
0169 
0170     aboutData.addCredit(i18n("All people who have contributed and I have forgotten to mention"));
0171 
0172     // register about data
0173     KAboutData::setApplicationData(aboutData);
0174 
0175     // set app stuff from about data component name
0176     app.setApplicationName(aboutData.componentName());
0177     app.setOrganizationDomain(aboutData.organizationDomain());
0178     app.setApplicationVersion(aboutData.version());
0179     app.setApplicationDisplayName(aboutData.displayName());
0180     app.setWindowIcon(QIcon::fromTheme(aboutData.componentName()));
0181 
0182     // do it sooner and different stuff will break in different KF5 versions
0183     app.createMainWindow();
0184 
0185     handleCommandLine(app, aboutData);
0186 
0187     return app.exec();
0188 }