File indexing completed on 2024-05-05 16:08:23

0001 /* This file is part of the KDE project
0002    Copyright (C) 1998, 1999, 2000 Torben Weis <weis@kde.org>
0003    Copyright (C) 2001 David Faure <faure@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 "kdatatool.h"
0022 
0023 #include <kactioncollection.h>
0024 
0025 #include <kservicetypetrader.h>
0026 
0027 #include <QPixmap>
0028 #include <QFile>
0029 #include <QDebug>
0030 
0031 /*************************************************
0032  *
0033  * KDataToolInfo
0034  *
0035  *************************************************/
0036 class Q_DECL_HIDDEN KDataToolInfo::KDataToolInfoPrivate
0037 {
0038 public:
0039     KDataToolInfoPrivate()
0040         : service(nullptr)
0041     {}
0042 
0043     KService::Ptr service;
0044     QString componentName;
0045 };
0046 
0047 KDataToolInfo::KDataToolInfo()
0048     : d(new KDataToolInfoPrivate)
0049 {
0050 }
0051 
0052 KDataToolInfo::KDataToolInfo(const KService::Ptr &service, const QString &componentName)
0053     : d(new KDataToolInfoPrivate)
0054 {
0055     d->service = service;
0056     d->componentName = componentName;
0057 
0058     if (!d->service && !d->service->serviceTypes().contains("KDataTool")) {
0059         /*qDebug() << "The service" << d->service->name()
0060                        << "does not feature the service type KDataTool";*/
0061         d->service = nullptr;
0062     }
0063 }
0064 
0065 KDataToolInfo::KDataToolInfo(const KDataToolInfo &info)
0066     : d(new KDataToolInfoPrivate)
0067 {
0068     d->service = info.service();
0069     d->componentName = info.componentName();
0070 }
0071 
0072 KDataToolInfo &KDataToolInfo::operator= (const KDataToolInfo &info)
0073 {
0074     d->service = info.service();
0075     d->componentName = info.componentName();
0076     return *this;
0077 }
0078 
0079 KDataToolInfo::~KDataToolInfo()
0080 {
0081     delete d;
0082 }
0083 
0084 QString KDataToolInfo::dataType() const
0085 {
0086     if (!d->service) {
0087         return QString();
0088     }
0089 
0090     return d->service->property("DataType").toString();
0091 }
0092 
0093 QStringList KDataToolInfo::mimeTypes() const
0094 {
0095     if (!d->service) {
0096         return QStringList();
0097     }
0098 
0099     return d->service->property("DataMimeTypes").toStringList();
0100 }
0101 
0102 bool KDataToolInfo::isReadOnly() const
0103 {
0104     if (!d->service) {
0105         return true;
0106     }
0107 
0108     return d->service->property("ReadOnly").toBool();
0109 }
0110 
0111 QString KDataToolInfo::iconName() const
0112 {
0113     if (!d->service) {
0114         return QString();
0115     }
0116     return d->service->icon();
0117 }
0118 
0119 QStringList KDataToolInfo::commands() const
0120 {
0121     if (!d->service) {
0122         return QStringList();
0123     }
0124 
0125     return d->service->property("Commands").toStringList();
0126 }
0127 
0128 QStringList KDataToolInfo::userCommands() const
0129 {
0130     if (!d->service) {
0131         return QStringList();
0132     }
0133 
0134     return d->service->comment().split(',', QString::SkipEmptyParts);
0135 }
0136 
0137 KDataTool *KDataToolInfo::createTool(QObject *parent) const
0138 {
0139     if (!d->service) {
0140         return nullptr;
0141     }
0142 
0143     KDataTool *tool = d->service->createInstance<KDataTool>(parent);
0144     if (tool) {
0145         tool->setComponentName(d->componentName);
0146     }
0147     return tool;
0148 }
0149 
0150 KService::Ptr KDataToolInfo::service() const
0151 {
0152     return d->service;
0153 }
0154 
0155 QString KDataToolInfo::componentName() const
0156 {
0157     return d->componentName;
0158 }
0159 
0160 QList<KDataToolInfo> KDataToolInfo::query(const QString &datatype, const QString &mimetype, const QString &componentName)
0161 {
0162     QList<KDataToolInfo> lst;
0163 
0164     QString constr;
0165 
0166     if (!datatype.isEmpty()) {
0167         constr = QString::fromLatin1("DataType == '%1'").arg(datatype);
0168     }
0169     if (!mimetype.isEmpty()) {
0170         QString tmp = QString::fromLatin1("'%1' in DataMimeTypes").arg(mimetype);
0171         if (constr.isEmpty()) {
0172             constr = tmp;
0173         } else {
0174             constr = constr + " and " + tmp;
0175         }
0176     }
0177     /* Bug in KServiceTypeTrader ? Test with HEAD-kdelibs!
0178         if (!componentName.isEmpty())
0179         {
0180             QString tmp = QString::fromLatin1( "not ('%1' in ExcludeFrom)" ).arg(componentName);
0181             if ( constr.isEmpty() )
0182                 constr = tmp;
0183             else
0184                 constr = constr + " and " + tmp;
0185         } */
0186 
0187     // Query the trader
0188     //qDebug() << constr;
0189     const KService::List offers = KServiceTypeTrader::self()->query("KDataTool", constr);
0190 
0191     KService::List::ConstIterator it = offers.begin();
0192     for (; it != offers.end(); ++it) {
0193         // Temporary replacement for the non-working trader query above
0194         if (componentName.isEmpty() || !(*it)->property("ExcludeFrom").toStringList()
0195                 .contains(componentName)) {
0196             lst.append(KDataToolInfo(*it, componentName));
0197         } else {
0198             //qDebug() << (*it)->entryPath() << " excluded.";
0199         }
0200     }
0201 
0202     return lst;
0203 }
0204 
0205 bool KDataToolInfo::isValid() const
0206 {
0207     return (d->service);
0208 }
0209 
0210 /*************************************************
0211  *
0212  * KDataToolAction
0213  *
0214  *************************************************/
0215 class Q_DECL_HIDDEN KDataToolAction::KDataToolActionPrivate
0216 {
0217 public:
0218     KDataToolActionPrivate() {}
0219 
0220     QString command;
0221     KDataToolInfo info;
0222 };
0223 
0224 KDataToolAction::KDataToolAction(const QString &text, const KDataToolInfo &info, const QString &command,
0225                                  QObject *parent)
0226     : QAction(text, parent),
0227       d(new KDataToolActionPrivate)
0228 {
0229     setIcon(QIcon::fromTheme(info.iconName()));
0230     d->command = command;
0231     d->info = info;
0232 }
0233 
0234 KDataToolAction::~KDataToolAction()
0235 {
0236     delete d;
0237 }
0238 
0239 void KDataToolAction::slotActivated()
0240 {
0241     emit toolActivated(d->info, d->command);
0242 }
0243 
0244 QList<QAction *> KDataToolAction::dataToolActionList(const QList<KDataToolInfo> &tools, const QObject *receiver, const char *slot, KActionCollection *parent)
0245 {
0246     QList<QAction *> actionList;
0247     if (tools.isEmpty()) {
0248         return actionList;
0249     }
0250 
0251     QAction *sep_action = new QAction(parent);
0252     sep_action->setSeparator(true);
0253     actionList.append(sep_action);
0254     QList<KDataToolInfo>::ConstIterator entry = tools.begin();
0255     for (; entry != tools.end(); ++entry) {
0256         const QStringList userCommands = (*entry).userCommands();
0257         const QStringList commands = (*entry).commands();
0258         Q_ASSERT(!commands.isEmpty());
0259         if (commands.count() != userCommands.count())
0260             qWarning() << "KDataTool desktop file error (" << (*entry).service()->entryPath()
0261                        << ")." << commands.count() << "commands and"
0262                        << userCommands.count() << " descriptions.";
0263         QStringList::ConstIterator uit = userCommands.begin();
0264         QStringList::ConstIterator cit = commands.begin();
0265         for (; uit != userCommands.end() && cit != commands.end(); ++uit, ++cit) {
0266             //qDebug() << "creating action " << *uit << " " << *cit;
0267             const QString name = (*entry).service()->entryPath(); // something unique
0268             KDataToolAction *action = new KDataToolAction(*uit, *entry, *cit, parent);
0269             parent->addAction(name, action);
0270             connect(action, SIGNAL(toolActivated(KDataToolInfo,QString)),
0271                     receiver, slot);
0272             actionList.append(action);
0273         }
0274     }
0275 
0276     return actionList;
0277 }
0278 
0279 /*************************************************
0280  *
0281  * KDataTool
0282  *
0283  *************************************************/
0284 class Q_DECL_HIDDEN KDataTool::KDataToolPrivate
0285 {
0286 public:
0287     KDataToolPrivate() {}
0288 
0289     QString componentName;
0290 };
0291 
0292 KDataTool::KDataTool(QObject *parent)
0293     : QObject(parent), d(new KDataToolPrivate)
0294 {
0295 }
0296 
0297 KDataTool::~KDataTool()
0298 {
0299     delete d;
0300 }
0301 
0302 void KDataTool::setComponentName(const QString &componentName)
0303 {
0304     d->componentName = componentName;
0305 }
0306 
0307 QString KDataTool::componentName() const
0308 {
0309     return d->componentName;
0310 }
0311 
0312 #include "moc_kdatatool.cpp"