File indexing completed on 2024-12-22 05:15:48

0001 /*
0002     SPDX-FileCopyrightText: 2003-2007 Craig Drummond <craig@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "Viewer.h"
0007 #include "KfiConstants.h"
0008 #include "config-workspace.h"
0009 #include "kfontview_debug.h"
0010 
0011 #include <KAboutData>
0012 #include <KActionCollection>
0013 #include <KConfigGroup>
0014 #include <KDBusService>
0015 #include <KParts/NavigationExtension>
0016 #include <KPluginFactory>
0017 #include <KSharedConfig>
0018 #include <KShortcutsDialog>
0019 #include <KStandardAction>
0020 #include <QAction>
0021 #include <QApplication>
0022 #include <QCommandLineOption>
0023 #include <QCommandLineParser>
0024 #include <QFileDialog>
0025 #include <QUrl>
0026 
0027 namespace KFI
0028 {
0029 CViewer::CViewer()
0030 {
0031     const auto result = KPluginFactory::instantiatePlugin<KParts::ReadOnlyPart>(KPluginMetaData(QStringLiteral("kf6/parts/kfontviewpart")), this);
0032 
0033     if (!result) {
0034         qCWarning(KFONTVIEW_DEBUG) << "Error loading kfontviewpart:" << result.errorString;
0035         exit(1);
0036     }
0037 
0038     m_preview = result.plugin;
0039 
0040     m_openAct = actionCollection()->addAction(KStandardAction::Open, this, SLOT(fileOpen()));
0041     actionCollection()->addAction(KStandardAction::Quit, this, SLOT(close()));
0042     actionCollection()->addAction(KStandardAction::KeyBindings, this, SLOT(configureKeys()));
0043     m_printAct = actionCollection()->addAction(KStandardAction::Print, m_preview, SLOT(print()));
0044 
0045     // Make tooltips more specific, instead of "document".
0046     m_openAct->setToolTip(i18n("Open an existing font file"));
0047     m_printAct->setToolTip(i18n("Print font preview"));
0048 
0049     m_printAct->setEnabled(false);
0050 
0051     if (m_preview->navigationExtension()) {
0052         connect(m_preview->navigationExtension(), &KParts::NavigationExtension::enableAction, this, &CViewer::enableAction);
0053     }
0054 
0055     setCentralWidget(m_preview->widget());
0056     createGUI(m_preview);
0057 
0058     setAutoSaveSettings();
0059     applyMainWindowSettings(KSharedConfig::openConfig()->group(QStringLiteral("MainWindow")));
0060 }
0061 
0062 void CViewer::fileOpen()
0063 {
0064     QFileDialog dlg(this, i18n("Select Font to View"));
0065     dlg.setFileMode(QFileDialog::ExistingFile);
0066     dlg.setMimeTypeFilters(QStringList() << "application/x-font-ttf"
0067                                          << "application/x-font-otf"
0068                                          << "application/x-font-type1"
0069                                          << "application/x-font-bdf"
0070                                          << "application/x-font-pcf");
0071     if (dlg.exec() == QDialog::Accepted) {
0072         QUrl url = dlg.selectedUrls().value(0);
0073         if (url.isValid()) {
0074             showUrl(url);
0075         }
0076     }
0077 }
0078 
0079 void CViewer::showUrl(const QUrl &url)
0080 {
0081     if (url.isValid()) {
0082         m_preview->openUrl(url);
0083     }
0084 }
0085 
0086 void CViewer::configureKeys()
0087 {
0088     KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this);
0089 
0090     dlg.addCollection(actionCollection());
0091     dlg.configure();
0092 }
0093 
0094 void CViewer::enableAction(const char *name, bool enable)
0095 {
0096     if (0 == qstrcmp("print", name)) {
0097         m_printAct->setEnabled(enable);
0098     }
0099 }
0100 
0101 class ViewerApplication : public QApplication
0102 {
0103 public:
0104     ViewerApplication(int &argc, char **argv)
0105         : QApplication(argc, argv)
0106     {
0107         cmdParser.addPositionalArgument(QLatin1String("[URL]"), i18n("URL to open"));
0108     }
0109 
0110     QCommandLineParser *parser()
0111     {
0112         return &cmdParser;
0113     }
0114 
0115 public Q_SLOTS:
0116     void activate(const QStringList &args, const QString &workingDirectory)
0117     {
0118         KFI::CViewer *viewer = new KFI::CViewer;
0119         viewer->show();
0120 
0121         if (!args.isEmpty()) {
0122             cmdParser.process(args);
0123             bool first = true;
0124             foreach (const QString &arg, cmdParser.positionalArguments()) {
0125                 QUrl url(QUrl::fromUserInput(arg, workingDirectory));
0126 
0127                 if (!first) {
0128                     viewer = new KFI::CViewer;
0129                     viewer->show();
0130                 }
0131                 viewer->showUrl(url);
0132                 first = false;
0133             }
0134         }
0135     }
0136 
0137 private:
0138     QCommandLineParser cmdParser;
0139 };
0140 
0141 }
0142 
0143 int main(int argc, char **argv)
0144 {
0145     KFI::ViewerApplication app(argc, argv);
0146 
0147     KLocalizedString::setApplicationDomain(KFI_CATALOGUE);
0148     KAboutData aboutData("kfontview",
0149                          i18n("Font Viewer"),
0150                          WORKSPACE_VERSION_STRING,
0151                          i18n("Simple font viewer"),
0152                          KAboutLicense::GPL,
0153                          i18n("(C) Craig Drummond, 2004-2007"));
0154     KAboutData::setApplicationData(aboutData);
0155 
0156     QCommandLineParser *parser = app.parser();
0157     aboutData.setupCommandLine(parser);
0158     parser->process(app);
0159     aboutData.processCommandLine(parser);
0160 
0161     KDBusService dbusService(KDBusService::Unique);
0162     QGuiApplication::setWindowIcon(QIcon::fromTheme("kfontview"));
0163     app.activate(app.arguments(), QDir::currentPath());
0164     QObject::connect(&dbusService, &KDBusService::activateRequested, &app, &KFI::ViewerApplication::activate);
0165 
0166     return app.exec();
0167 }