File indexing completed on 2024-05-26 04:57:40

0001 /**
0002  * \file mainkde.cpp
0003  * Main program.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 9 Jan 2003
0008  *
0009  * Copyright (C) 2003-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include <QFile>
0028 #include <QApplication>
0029 #include <KAboutData>
0030 #include <KConfigGroup>
0031 #include <KSharedConfig>
0032 #include <QCommandLineParser>
0033 #include <typeinfo>
0034 #include "fileconfig.h"
0035 #include "loadtranslation.h"
0036 #include "kdemainwindow.h"
0037 #include "kdeplatformtools.h"
0038 #include "kid3application.h"
0039 
0040 namespace {
0041 
0042 /**
0043  * QApplication subclass which catches exceptions.
0044  */
0045 class Kid3KdeApplication : public QApplication {
0046 public:
0047   /**
0048    * Constructor.
0049    * @param argc number of command line arguments
0050    * @param argv array of command line arguments
0051    */
0052   Kid3KdeApplication(int& argc, char** argv) : QApplication(argc, argv) {}
0053 
0054   /**
0055    * Send event to receiver.
0056    * @param receiver receiver
0057    * @param event event
0058    * @return return value from receiver's event handler.
0059    */
0060   bool notify(QObject* receiver, QEvent* event) override;
0061 };
0062 
0063 /**
0064  * Send event to receiver.
0065  * @param receiver receiver
0066  * @param event event
0067  * @return return value from receiver's event handler.
0068  */
0069 bool Kid3KdeApplication::notify(QObject* receiver, QEvent* event)
0070 {
0071   try {
0072     return QApplication::notify(receiver, event);
0073   } catch (std::exception& ex) {
0074     qWarning("Exception %s (%s) was caught", typeid(ex).name(), ex.what());
0075   }
0076   return false;
0077 }
0078 
0079 }
0080 
0081 /**
0082  * Main program.
0083  *
0084  * @param argc number of arguments including command name
0085  * @param argv arguments, argv[0] is command name
0086  *
0087  * @return exit code of application.
0088  */
0089 int main(int argc, char* argv[])
0090 {
0091   // Enable support for high resolution "@2x" images
0092   QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
0093 #if QT_VERSION >= 0x050600
0094   QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
0095 #endif
0096   Kid3KdeApplication app(argc, argv);
0097   KAboutData aboutData(QStringLiteral("kid3"),
0098                        QStringLiteral("Kid3"),
0099                        QStringLiteral(VERSION),
0100                        QStringLiteral("Audio Tag Editor"), KAboutLicense::GPL,
0101                        QStringLiteral("(c) 2003-" RELEASE_YEAR " Urs Fleisch"),
0102                        QString(),
0103                        QStringLiteral("https://kid3.kde.org"));
0104   aboutData.setOrganizationDomain(QByteArray("kde.org"));
0105   aboutData.addAuthor(QStringLiteral("Urs Fleisch"), QString(),
0106                       QStringLiteral("ufleisch@users.sourceforge.net"));
0107   aboutData.setProductName(QByteArray("kid3"));
0108   KAboutData::setApplicationData(aboutData);
0109   QCoreApplication::setApplicationName(aboutData.componentName());
0110   QGuiApplication::setApplicationDisplayName(aboutData.displayName());
0111   QCoreApplication::setOrganizationDomain(aboutData.organizationDomain());
0112   QCoreApplication::setApplicationVersion(aboutData.version());
0113   QApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("kid3")));
0114 
0115   QCommandLineParser parser;
0116   aboutData.setupCommandLine(&parser);
0117   parser.setApplicationDescription(aboutData.shortDescription());
0118   parser.addHelpOption();
0119   parser.addVersionOption();
0120   const char* const directoryToOpenStr =
0121       QT_TRANSLATE_NOOP("@default", "folder to open");
0122   parser.addPositionalArgument(
0123         QStringLiteral("dir"), QCoreApplication::translate("@default",
0124         directoryToOpenStr),
0125         QStringLiteral("[dir...]"));
0126   parser.process(app);
0127   aboutData.processCommandLine(&parser);
0128 
0129   QString configuredLanguage =
0130       KConfigGroup(KSharedConfig::openConfig(), "Locale").readEntry("Language");
0131   Utils::loadTranslation(configuredLanguage);
0132 
0133   const char* const audioTagEditorStr =
0134       QT_TRANSLATE_NOOP("@default", "Audio Tag Editor");
0135   aboutData.setShortDescription(
0136         QCoreApplication::translate("@default", audioTagEditorStr));
0137   aboutData.setTranslator(
0138         QCoreApplication::translate("@default",
0139             // i18n NAME OF TRANSLATORS
0140             QT_TRANSLATE_NOOP("@default", "Your names")),
0141         QCoreApplication::translate("@default",
0142             // i18n EMAIL OF TRANSLATORS
0143             QT_TRANSLATE_NOOP("@default", "Your emails")));
0144 
0145   IPlatformTools* platformTools = new KdePlatformTools;
0146   auto kid3App = new Kid3Application(platformTools);
0147 #ifdef HAVE_QTDBUS
0148   kid3App->activateDbusInterface();
0149 #endif
0150   if (app.isSessionRestored()) {
0151     int n = 1;
0152     while (KMainWindow::canBeRestored(n)) {
0153       (new KdeMainWindow(platformTools, kid3App))->restore(n);
0154       n++;
0155     }
0156   } else {
0157     auto kid3 = new KdeMainWindow(platformTools, kid3App);
0158     kid3->show();
0159 
0160     if (parser.positionalArguments().count()) {
0161       kid3App->openDirectory(parser.positionalArguments());
0162     } else if (FileConfig::instance().loadLastOpenedFile() &&
0163                !FileConfig::instance().lastOpenedFile().isEmpty()) {
0164       kid3App->openDirectory(QStringList()
0165                              << FileConfig::instance().lastOpenedFile());
0166     }
0167   }
0168 
0169   int rc = QApplication::exec();
0170   delete kid3App;
0171   delete platformTools;
0172   return rc;
0173 }