File indexing completed on 2024-05-05 03:57:07

0001 /*
0002     SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
0003     SPDX-FileCopyrightText: 2000 Simon Hausmann <hausmann@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "partviewer.h"
0009 
0010 #include <KActionCollection>
0011 #include <KActionMenu>
0012 #include <KLocalizedString>
0013 #include <KPluginFactory>
0014 #include <partloader.h>
0015 
0016 #include <QAction>
0017 #include <QApplication>
0018 #include <QFile>
0019 #include <QFileDialog>
0020 #include <QMimeDatabase>
0021 #include <QTest>
0022 
0023 PartViewer::PartViewer()
0024 {
0025     setXMLFile(QFINDTESTDATA("partviewer_shell.rc"));
0026 
0027     QAction *paOpen = new QAction(QIcon::fromTheme(QStringLiteral("document-open")), QStringLiteral("&Open file"), this);
0028     actionCollection()->addAction(QStringLiteral("file_open"), paOpen);
0029     connect(paOpen, &QAction::triggered, this, &PartViewer::slotFileOpen);
0030 
0031     QAction *paQuit = new QAction(QIcon::fromTheme(QStringLiteral("application-exit")), QStringLiteral("&Quit"), this);
0032     actionCollection()->addAction(QStringLiteral("file_quit"), paQuit);
0033     connect(paQuit, &QAction::triggered, this, &PartViewer::close);
0034 
0035     m_part = nullptr;
0036 
0037     // Set a reasonable size
0038     resize(600, 350);
0039 }
0040 
0041 PartViewer::~PartViewer()
0042 {
0043     qDeleteAll(m_openWithActions);
0044     delete m_part;
0045 }
0046 
0047 void PartViewer::switchToPart(const QUrl &url)
0048 {
0049     setCentralWidget(m_part->widget());
0050     // Integrate its GUI
0051     createGUI(m_part);
0052 
0053     m_part->openUrl(url);
0054 }
0055 
0056 void PartViewer::openUrl(const QUrl &url)
0057 {
0058     unplugActionList(QStringLiteral("file_openwith"));
0059     delete m_part;
0060     QMimeDatabase db;
0061     const QString mimeType = db.mimeTypeForUrl(url).name();
0062 
0063     if (auto res = KParts::PartLoader::instantiatePartForMimeType<KParts::ReadOnlyPart>(mimeType, this, this)) {
0064         m_part = res.plugin;
0065         switchToPart(url);
0066     } else {
0067         qWarning() << res.errorString;
0068     }
0069 
0070     // Show available parts in the GUI
0071 
0072     qDeleteAll(m_openWithActions);
0073     m_openWithActions.clear();
0074     const QList<KPluginMetaData> plugins = KParts::PartLoader::partsForMimeType(mimeType);
0075     for (const KPluginMetaData &plugin : plugins) {
0076         QAction *action = new QAction(plugin.name(), this);
0077         connect(action, &QAction::triggered, this, [this, plugin, url] {
0078             loadPlugin(plugin, url);
0079         });
0080         m_openWithActions.append(action);
0081     }
0082     if (!m_openWithActions.isEmpty()) {
0083         QAction *sep = new QAction(this);
0084         sep->setSeparator(true);
0085         m_openWithActions.append(sep);
0086     }
0087     plugActionList(QStringLiteral("file_openwith"), m_openWithActions);
0088 }
0089 
0090 void PartViewer::slotFileOpen()
0091 {
0092     QUrl url = QFileDialog::getOpenFileUrl();
0093     if (!url.isEmpty()) {
0094         openUrl(url);
0095     }
0096 }
0097 
0098 void PartViewer::loadPlugin(const KPluginMetaData &md, const QUrl &url)
0099 {
0100     delete m_part;
0101     auto factory = KPluginFactory::loadFactory(md).plugin;
0102     Q_ASSERT(factory);
0103     m_part = factory->create<KParts::ReadOnlyPart>(this, this, QVariantList());
0104     if (m_part) {
0105         switchToPart(url);
0106     }
0107 }
0108 
0109 int main(int argc, char **argv)
0110 {
0111     // This is a test window for showing any part
0112 
0113     QApplication app(argc, argv);
0114     PartViewer *shell = new PartViewer;
0115     if (argc > 1) {
0116         QUrl url = QUrl::fromUserInput(QLatin1String(argv[1]));
0117         shell->openUrl(url);
0118     } else {
0119         shell->slotFileOpen();
0120     }
0121     shell->show();
0122     return app.exec();
0123 }
0124 
0125 #include "moc_partviewer.cpp"