File indexing completed on 2024-05-19 05:00:41

0001 /*
0002     SPDX-FileCopyrightText: 2004 Jakub Stachowski <qbast@go2.pl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "dnssdwatcher.h"
0008 #include "kdnssdadaptor.h"
0009 #include "watcher.h"
0010 
0011 #include <KPluginFactory>
0012 
0013 K_PLUGIN_CLASS_WITH_JSON(DNSSDWatcher, "dnssdwatcher.json")
0014 
0015 DNSSDWatcher::DNSSDWatcher(QObject* parent, const QList<QVariant>&)
0016 : KDEDModule(parent)
0017 {
0018     QDBusConnection::sessionBus().connect(QString(), QString(),
0019             QStringLiteral("org.kde.KDirNotify"),
0020             QStringLiteral("enteredDirectory"),
0021             this, SLOT(enteredDirectory(QString)));
0022     QDBusConnection::sessionBus().connect(QString(), QString(),
0023             QStringLiteral("org.kde.KDirNotify"),
0024             QStringLiteral("leftDirectory"),
0025             this, SLOT(leftDirectory(QString)));
0026     new KdnssdAdaptor( this );
0027 }
0028 
0029 QStringList DNSSDWatcher::watchedDirectories()
0030 {
0031     return watchers.keys();
0032 }
0033 
0034 
0035 // from KIO worker
0036 void DNSSDWatcher::dissect(const QUrl& url,QString& name,QString& type)
0037 {
0038     type = url.path().section(QChar::fromLatin1('/'),1,1);
0039     name = url.path().section(QChar::fromLatin1('/'),2,-1);
0040 }
0041 
0042 
0043 
0044 void DNSSDWatcher::enteredDirectory(const QString& _dir)
0045 {
0046     QUrl dir(_dir);
0047     if (dir.scheme() != QLatin1String("zeroconf")) {
0048         return;
0049     }
0050     if (watchers.contains(dir.url())) {
0051         watchers[dir.url()]->refcount++;
0052     }
0053     else {
0054         createNotifier(dir);
0055     }
0056 }
0057 
0058 
0059 void DNSSDWatcher::leftDirectory(const QString& _dir)
0060 {
0061     QUrl dir(_dir);
0062     if (dir.scheme() != QLatin1String("zeroconf")) {
0063         return;
0064     }
0065     Watcher* watcher = watchers.value(dir.url());
0066     if (!watcher) {
0067         return;
0068     }
0069     if (watcher->refcount==1) {
0070         delete watcher;
0071         watchers.remove(dir.url());
0072     } else {
0073         watcher->refcount--;
0074     }
0075 }
0076 
0077 
0078 void DNSSDWatcher::createNotifier(const QUrl& url)
0079 {
0080     QString type,name;
0081     dissect(url,name,type);
0082     if (type.isEmpty()) {
0083         watchers.insert(url.url(), new TypeWatcher());
0084     }
0085     else {
0086         watchers.insert(url.url(), new ServiceWatcher(type));
0087     }
0088 }
0089 
0090 DNSSDWatcher::~DNSSDWatcher()
0091 {
0092     qDeleteAll( watchers );
0093 }
0094 
0095 #include <dnssdwatcher.moc>
0096 #include "moc_dnssdwatcher.cpp"