File indexing completed on 2024-05-19 05:47:30

0001 /***************************************************************************
0002  *   Copyright 2011-2015 Marco Martin <mart@kde.org>                            *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU Library General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU Library General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU Library General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #include "resourceinstance.h"
0021 
0022 #include <QQuickWindow>
0023 #include <QTimer>
0024 
0025 #include <KActivities/ResourceInstance>
0026 #include <QDebug>
0027 
0028 namespace KActivities {
0029 namespace Imports {
0030 
0031 ResourceInstance::ResourceInstance(QQuickItem *parent)
0032     : QQuickItem(parent)
0033 {
0034     m_syncTimer = new QTimer(this);
0035     m_syncTimer->setSingleShot(true);
0036     connect(m_syncTimer, SIGNAL(timeout()), this, SLOT(syncWid()));
0037 }
0038 
0039 ResourceInstance::~ResourceInstance()
0040 {
0041 }
0042 
0043 void ResourceInstance::syncWid()
0044 {
0045     QWindow *w = window();
0046     if (!w) {
0047         return;
0048     }
0049 
0050     WId wid = w->winId();
0051     if (!m_resourceInstance || m_resourceInstance->winId() != wid) {
0052         // qDebug() << "Creating a new instance of the resource" << m_uri << "window id" << wid;
0053         m_resourceInstance.reset(new KActivities::ResourceInstance(wid, m_uri, m_mimetype, m_title));
0054     } else {
0055 
0056         if (m_uri.scheme().startsWith(QLatin1String("http")) && !m_uri.hasQuery() && m_uri.path().endsWith(QLatin1Char('/'))) {
0057             const QString &oldPath = m_uri.path();
0058             m_uri.setPath(oldPath.left(oldPath.length() - 1));
0059 
0060             // qDebug() << "Old and new path" << oldPath << m_uri;
0061 
0062         } else {
0063             m_resourceInstance->setUri(m_uri);
0064         }
0065 
0066         // qDebug() << "Setting" << m_uri << m_mimetype << "to window" << wid;
0067 
0068         m_resourceInstance->setMimetype(m_mimetype);
0069         m_resourceInstance->setTitle(m_title);
0070     }
0071 }
0072 
0073 QUrl ResourceInstance::uri() const
0074 {
0075     return m_uri;
0076 }
0077 
0078 void ResourceInstance::setUri(const QUrl &uri)
0079 {
0080     if (m_uri == uri) {
0081         return;
0082     }
0083 
0084     m_uri = uri;
0085     m_syncTimer->start(100);
0086 }
0087 
0088 QString ResourceInstance::mimetype() const
0089 {
0090     return m_mimetype;
0091 }
0092 
0093 void ResourceInstance::setMimetype(const QString &mimetype)
0094 {
0095     if (m_mimetype == mimetype) {
0096         return;
0097     }
0098     m_mimetype = mimetype;
0099     m_syncTimer->start(100);
0100 }
0101 
0102 QString ResourceInstance::title() const
0103 {
0104     return m_title;
0105 }
0106 
0107 void ResourceInstance::setTitle(const QString &title)
0108 {
0109     if (m_title == title) {
0110         return;
0111     }
0112     m_title = title;
0113     m_syncTimer->start(100);
0114 }
0115 
0116 void ResourceInstance::notifyModified()
0117 {
0118     //ensure the resource instance exists
0119     syncWid();
0120     m_resourceInstance->notifyModified();
0121 }
0122 
0123 void ResourceInstance::notifyFocusedIn()
0124 {
0125     //ensure the resource instance exists
0126     syncWid();
0127     m_resourceInstance->notifyFocusedIn();
0128 }
0129 
0130 void ResourceInstance::notifyFocusedOut()
0131 {
0132     //ensure the resource instance exists
0133     syncWid();
0134     m_resourceInstance->notifyFocusedOut();
0135 }
0136 
0137 }
0138 }
0139 
0140