File indexing completed on 2024-05-12 16:39:38

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003 Lucijan Busch <lucijan@kde.org>
0003    Copyright (C) 2003-2015 Jarosław Staniek <staniek@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "kexipartinfo_p.h"
0022 #include "kexipartmanager.h"
0023 #include "KexiMainWindowIface.h"
0024 #include <KexiJsonTrader.h>
0025 
0026 #include <KDbGlobal>
0027 
0028 #include <KActionCollection>
0029 
0030 #include <QStringList>
0031 #include <QDebug>
0032 #include <QJsonArray>
0033 
0034 using namespace KexiPart;
0035 
0036 static bool isTrue(KPluginMetaData *metaData, const char* fieldName, bool defaultValue = false)
0037 {
0038     QString s = metaData->value(QLatin1String(fieldName));
0039     if (s.isEmpty()) {
0040         return defaultValue;
0041     }
0042     return 0 == s.compare(QLatin1String("true"), Qt::CaseInsensitive);
0043 }
0044 
0045 Info::Private::Private(Info *info, const QPluginLoader &loader)
0046     : untranslatedGroupName(info->value("X-Kexi-GroupName"))
0047     , typeName(info->value("X-Kexi-TypeName"))
0048     , supportedViewModes(0)
0049     , supportedUserViewModes(0)
0050     , isVisibleInNavigator(isTrue(info, "X-Kexi-VisibleInProjectNavigator"))
0051     , isDataExportSupported(isTrue(info, "X-Kexi-SupportsDataExport"))
0052     , isPrintingSupported(isTrue(info, "X-Kexi-SupportsPrinting"))
0053     , isExecuteSupported(isTrue(info, "X-Kexi-SupportsExecution"))
0054     , isPropertyEditorAlwaysVisibleInDesignMode(
0055           isTrue(info, "X-Kexi-PropertyEditorAlwaysVisibleInDesignMode", true))
0056 {
0057     const QJsonObject metaDataObject = KexiJsonTrader::metaDataObjectForPluginLoader(loader);
0058     groupName = info->readTranslatedString(metaDataObject, "X-Kexi-GroupName", untranslatedGroupName);
0059     const QStringList serviceTypes = info->serviceTypes();
0060     if (serviceTypes.contains("Kexi/Viewer")) {
0061         supportedViewModes |= Kexi::DataViewMode;
0062     }
0063     if (serviceTypes.contains("Kexi/Designer")) {
0064         supportedViewModes |= Kexi::DesignViewMode;
0065     }
0066     if (serviceTypes.contains("Kexi/Editor")) {
0067         supportedViewModes |= Kexi::TextViewMode;
0068     }
0069 
0070     const QStringList userServiceTypes = metaDataObject.value("X-Kexi-ServiceTypesInUserMode")
0071             .toString().split(QLatin1Char(',')); // NOTE: toArray() does not work
0072     if (userServiceTypes.contains("Kexi/Viewer")) {
0073         supportedUserViewModes |= Kexi::DataViewMode;
0074     }
0075     if (userServiceTypes.contains("Kexi/Designer")) {
0076         supportedUserViewModes |= Kexi::DesignViewMode;
0077     }
0078     if (userServiceTypes.contains("Kexi/Editor")) {
0079         supportedUserViewModes |= Kexi::TextViewMode;
0080     }
0081 }
0082 
0083 Info::Private::Private()
0084     : supportedViewModes(0)
0085     , supportedUserViewModes(0)
0086     , isVisibleInNavigator(false)
0087     , isDataExportSupported(false)
0088     , isPrintingSupported(false)
0089     , isExecuteSupported(false)
0090     , isPropertyEditorAlwaysVisibleInDesignMode(true)
0091 {
0092 }
0093 
0094 //------------------------------
0095 
0096 /*! \return "create" QAction's name for part defined by \a info.
0097  The result is like "tablepart_create". */
0098 static QString nameForCreateAction(const Info& info)
0099 {
0100     return info.id() + ".create";
0101 }
0102 
0103 //------------------------------
0104 
0105 KexiNewObjectAction::KexiNewObjectAction(Info* info, QObject *parent)
0106     : QAction(QIcon::fromTheme(info->iconName()), info->name() + "...", parent)
0107     , m_info(info)
0108 {
0109     setObjectName(nameForCreateAction(*m_info));
0110     // default tooltip and what's this
0111     setToolTip(xi18nc("@info",
0112                       "Create new object of type <resource>%1</resource>",
0113                       m_info->name().toLower()));
0114     setWhatsThis(xi18nc("@info",
0115                         "Creates new object of type <resource>%1</resource>",
0116                         m_info->name().toLower()));
0117     connect(this, SIGNAL(triggered()), this, SLOT(slotTriggered()));
0118     connect(this, SIGNAL(newObjectRequested(KexiPart::Info*)),
0119             &Kexi::partManager(), SIGNAL(newObjectRequested(KexiPart::Info*)));
0120 }
0121 
0122 void KexiNewObjectAction::slotTriggered()
0123 {
0124     emit newObjectRequested(m_info);
0125 }
0126 
0127 //------------------------------
0128 
0129 //Info::Info(const QString &id, const QString &iconName,
0130 //           const QString &objectName)
0131 //        : KPluginMetaData(), d(new Private)
0132 //{
0133 //    d->iconName = iconName;
0134 //    d->objectName = objectName;
0135 //}
0136 
0137 Info::Info(const QPluginLoader &loader)
0138     : KexiPluginMetaData(loader), d(new Private(this, loader))
0139 {
0140 }
0141 
0142 Info::~Info()
0143 {
0144     delete d;
0145 }
0146 
0147 QString Info::typeName() const
0148 {
0149     return d->typeName;
0150 }
0151 
0152 QString Info::groupName() const
0153 {
0154     return d->groupName;
0155 }
0156 
0157 QString Info::untranslatedGroupName() const
0158 {
0159     return d->untranslatedGroupName;
0160 }
0161 
0162 Kexi::ViewModes Info::supportedViewModes() const
0163 {
0164     return d->supportedViewModes;
0165 }
0166 
0167 Kexi::ViewModes Info::supportedUserViewModes() const
0168 {
0169     return d->supportedUserViewModes;
0170 }
0171 
0172 bool Info::isVisibleInNavigator() const
0173 {
0174     return d->isVisibleInNavigator;
0175 }
0176 
0177 bool Info::isDataExportSupported() const
0178 {
0179     return d->isDataExportSupported;
0180 }
0181 
0182 bool Info::isPrintingSupported() const
0183 {
0184     return d->isPrintingSupported;
0185 }
0186 
0187 bool Info::isExecuteSupported() const
0188 {
0189     return d->isExecuteSupported;
0190 }
0191 
0192 bool Info::isPropertyEditorAlwaysVisibleInDesignMode() const
0193 {
0194     return d->isPropertyEditorAlwaysVisibleInDesignMode;
0195 }
0196 
0197 QAction* Info::newObjectAction()
0198 {
0199     if (!isVisibleInNavigator()) {
0200         return 0;
0201     }
0202     if (!KexiMainWindowIface::global() || !KexiMainWindowIface::global()->actionCollection()) {
0203         qWarning() << "Missing Kexi's global action collection";
0204         return 0;
0205     }
0206     QAction *act = KexiMainWindowIface::global()->actionCollection()->action(nameForCreateAction(*this));
0207     if (!act) {
0208         act = new KexiNewObjectAction(this, KexiMainWindowIface::global()->actionCollection());
0209         KexiMainWindowIface::global()->actionCollection()->addAction(act->objectName(), act);
0210     }
0211     return act;
0212 }