Debian Codegraph @

avogadro/qtplugins/scriptfileformats/scriptfileformats.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "scriptfileformats.h"
7
8 #include "fileformatscript.h"
9
10 #include <avogadro/io/fileformatmanager.h>
11 #include <avogadro/qtgui/packagemanager.h>
12 #include <avogadro/qtgui/utilities.h>
13
14 #include <QtCore/QDebug>
15
16 namespace Avogadro::QtPlugins {
17
18 ScriptFileFormats::ScriptFileFormats(QObject* p) : ExtensionPlugin(p)
19 {
20 refreshFileFormats();
21
22 // Connect to PackageManager for pyproject.toml-based packages
23 auto* pm = QtGui::PackageManager::instance();
24 connect(pm, &QtGui::PackageManager::featureRegistered, this,
25 &ScriptFileFormats::registerFeature);
26 connect(pm, &QtGui::PackageManager::featureRemoved, this,
27 &ScriptFileFormats::unregisterFeature);
28 }
29
30 ScriptFileFormats::~ScriptFileFormats() {}
31
32 QList<QAction*> ScriptFileFormats::actions() const
33 {
34 return QList<QAction*>();
35 }
36
37 QStringList ScriptFileFormats::menuPath(QAction*) const
38 {
39 return QStringList();
40 }
41
42 void ScriptFileFormats::setMolecule(QtGui::Molecule*) {}
43
44 void ScriptFileFormats::refreshFileFormats()
45 {
46 unregisterFileFormats();
47 qDeleteAll(m_formats);
48 m_formats.clear();
49 m_packageFormats.clear();
50 registerFileFormats();
51 }
52
53 void ScriptFileFormats::unregisterFileFormats()
54 {
55 for (auto* format : m_formats)
56 Io::FileFormatManager::unregisterFormat(format->identifier());
57 }
58
59 void ScriptFileFormats::registerFileFormats()
60 {
61 for (auto* format : m_formats) {
62 if (!Io::FileFormatManager::registerFormat(format->newInstance())) {
63 qDebug() << "Could not register format" << format->identifier().c_str()
64 << "due to name conflict.";
65 }
66 }
67 }
68
69 void ScriptFileFormats::registerFeature(const QString& type,
70 const QString& packageDir,
71 const QString& command,
72 const QString& identifier,
73 const QVariantMap& metadata)
74 {
75 if (type != QLatin1String("file-formats"))
76 return;
77
78 auto* format = new FileFormatScript();
79 format->setPackageInfo(packageDir, command, identifier);
80 format->readMetaData(metadata);
81 if (format->isValid()) {
82 QString managerId = QString::fromStdString(format->identifier());
83 if (!Io::FileFormatManager::registerFormat(format->newInstance())) {
84 qDebug() << "Could not register file format" << identifier
85 << "due to name conflict.";
86 delete format;
87 } else {
88 m_formats.push_back(format);
89 m_packageFormats.insert(QtGui::PackageManager::packageFeatureKey(
90 packageDir, command, identifier),
91 managerId);
92 }
93 } else {
94 delete format;
95 }
96 }
97
98 void ScriptFileFormats::unregisterFeature(const QString& type,
99 const QString& packageDir,
100 const QString& command,
101 const QString& identifier)
102 {
103 if (type != QLatin1String("file-formats"))
104 return;
105
106 const QString featureKey =
107 QtGui::PackageManager::packageFeatureKey(packageDir, command, identifier);
108 const QList<QString> managerIds = m_packageFormats.values(featureKey);
109 if (managerIds.isEmpty())
110 return;
111
112 m_packageFormats.remove(featureKey);
113 for (const QString& managerId : managerIds) {
114 Io::FileFormatManager::unregisterFormat(managerId.toStdString());
115 for (int i = m_formats.size() - 1; i >= 0; --i) {
116 if (QString::fromStdString(m_formats[i]->identifier()) == managerId)
117 delete m_formats.takeAt(i);
118 }
119 }
120 }
121
122 } // namespace Avogadro::QtPlugins