File indexing completed on 2022-11-29 13:02:28

0001 /*
0002     SPDX-FileCopyrightText: 2011-2016 Ivan Cukic <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "resourceinstance.h"
0008 #include "manager_p.h"
0009 
0010 #include "debug_p.h"
0011 #include <QCoreApplication>
0012 
0013 namespace KActivities
0014 {
0015 class ResourceInstancePrivate
0016 {
0017 public:
0018     quintptr wid;
0019     QUrl uri;
0020     QString mimetype;
0021     QString title;
0022     QString application;
0023 
0024     void closeResource();
0025     void openResource();
0026 
0027     enum Type {
0028         Accessed = 0,
0029         Opened = 1,
0030         Modified = 2,
0031         Closed = 3,
0032         FocusedIn = 4,
0033         FocusedOut = 5,
0034     };
0035 
0036     static void registerResourceEvent(const QString &application, quintptr wid, const QUrl &uri, Type event)
0037     {
0038         Q_ASSERT_X(!application.isEmpty(), "ResourceInstance::event", "The application id must not be empty");
0039 
0040         if (uri.isEmpty()) {
0041             return;
0042         }
0043 
0044         Manager::resources()->RegisterResourceEvent(application, wid, uri.toString(), uint(event));
0045     }
0046 };
0047 
0048 void ResourceInstancePrivate::closeResource()
0049 {
0050     registerResourceEvent(application, wid, uri, Closed);
0051 }
0052 
0053 void ResourceInstancePrivate::openResource()
0054 {
0055     registerResourceEvent(application, wid, uri, Opened);
0056 }
0057 
0058 ResourceInstance::ResourceInstance(quintptr wid, QObject *parent)
0059     : QObject(parent)
0060     , d(new ResourceInstancePrivate())
0061 {
0062     qCDebug(KAMD_CORELIB) << "Creating ResourceInstance: empty for now";
0063     d->wid = wid;
0064     d->application = QCoreApplication::instance()->applicationName();
0065 }
0066 
0067 ResourceInstance::ResourceInstance(quintptr wid, const QString &application, QObject *parent)
0068     : QObject(parent)
0069     , d(new ResourceInstancePrivate())
0070 {
0071     qCDebug(KAMD_CORELIB) << "Creating ResourceInstance: empty for now";
0072     d->wid = wid;
0073     d->application = application.isEmpty() ? QCoreApplication::instance()->applicationName() : application;
0074 }
0075 
0076 ResourceInstance::ResourceInstance(quintptr wid, QUrl resourceUri, const QString &mimetype, const QString &title, const QString &application, QObject *parent)
0077     : QObject(parent)
0078     , d(new ResourceInstancePrivate())
0079 {
0080     qCDebug(KAMD_CORELIB) << "Creating ResourceInstance:" << resourceUri;
0081     d->wid = wid;
0082     d->uri = resourceUri.adjusted(QUrl::StripTrailingSlash);
0083     d->application = application.isEmpty() ? QCoreApplication::instance()->applicationName() : application;
0084 
0085     d->openResource();
0086 
0087     setTitle(title);
0088     setMimetype(mimetype);
0089 }
0090 
0091 ResourceInstance::~ResourceInstance()
0092 {
0093     d->closeResource();
0094 }
0095 
0096 void ResourceInstance::notifyModified()
0097 {
0098     d->registerResourceEvent(d->application, d->wid, d->uri, ResourceInstancePrivate::Modified);
0099 }
0100 
0101 void ResourceInstance::notifyFocusedIn()
0102 {
0103     d->registerResourceEvent(d->application, d->wid, d->uri, ResourceInstancePrivate::FocusedIn);
0104 }
0105 
0106 void ResourceInstance::notifyFocusedOut()
0107 {
0108     d->registerResourceEvent(d->application, d->wid, d->uri, ResourceInstancePrivate::FocusedOut);
0109 }
0110 
0111 void ResourceInstance::setUri(const QUrl &newUri)
0112 {
0113     if (d->uri == newUri) {
0114         return;
0115     }
0116 
0117     if (!d->uri.isEmpty()) {
0118         d->closeResource();
0119     }
0120 
0121     d->uri = newUri.adjusted(QUrl::StripTrailingSlash);
0122 
0123     d->openResource();
0124 }
0125 
0126 void ResourceInstance::setMimetype(const QString &mimetype)
0127 {
0128     if (mimetype.isEmpty()) {
0129         return;
0130     }
0131 
0132     d->mimetype = mimetype;
0133     // TODO: update the service info
0134     Manager::resources()->RegisterResourceMimetype(d->uri.toString(), mimetype);
0135 }
0136 
0137 void ResourceInstance::setTitle(const QString &title)
0138 {
0139     qCDebug(KAMD_CORELIB) << "Setting the title:" << title;
0140     if (title.isEmpty()) {
0141         return;
0142     }
0143 
0144     d->title = title;
0145     // TODO: update the service info
0146     Manager::resources()->RegisterResourceTitle(d->uri.toString(), title);
0147 }
0148 
0149 QUrl ResourceInstance::uri() const
0150 {
0151     return d->uri;
0152 }
0153 
0154 QString ResourceInstance::mimetype() const
0155 {
0156     return d->mimetype;
0157 }
0158 
0159 QString ResourceInstance::title() const
0160 {
0161     return d->title;
0162 }
0163 
0164 quintptr ResourceInstance::winId() const
0165 {
0166     return d->wid;
0167 }
0168 
0169 void ResourceInstance::notifyAccessed(const QUrl &uri, const QString &application)
0170 {
0171     ResourceInstancePrivate::registerResourceEvent(application.isEmpty() ? QCoreApplication::instance()->applicationName() : application,
0172                                                    0,
0173                                                    uri,
0174                                                    ResourceInstancePrivate::Accessed);
0175 }
0176 
0177 } // namespace KActivities