File indexing completed on 2024-05-05 17:01:33
0001 /*************************************************************************** 0002 This class provides the interface for Plasma and QtQuick 0003 ------------------- 0004 begin : Mo 02 Sep 2013 0005 copyright : (C) 2013-2019 by Alexander Reinholdt 0006 email : alexander.reinholdt@kdemail.net 0007 ***************************************************************************/ 0008 0009 /*************************************************************************** 0010 * This program is free software; you can redistribute it and/or modify * 0011 * it under the terms of the GNU General Public License as published by * 0012 * the Free Software Foundation; either version 2 of the License, or * 0013 * (at your option) any later version. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, but * 0016 * WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 0018 * General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program; if not, write to the * 0022 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,* 0023 * MA 02110-1335, USA * 0024 ***************************************************************************/ 0025 0026 #ifdef HAVE_CONFIG_H 0027 #include <config.h> 0028 #endif 0029 0030 // application specific includes 0031 #include "smb4kdeclarative.h" 0032 #include "smb4kdeclarative_p.h" 0033 #include "smb4kbookmarkobject.h" 0034 #include "smb4knetworkobject.h" 0035 #include "smb4kprofileobject.h" 0036 #include "core/smb4kmounter.h" 0037 #include "core/smb4kglobal.h" 0038 #include "core/smb4kworkgroup.h" 0039 #include "core/smb4khost.h" 0040 #include "core/smb4kshare.h" 0041 #include "core/smb4kbasicnetworkitem.h" 0042 #include "core/smb4kbookmarkhandler.h" 0043 #include "core/smb4kbookmark.h" 0044 #include "core/smb4kcustomoptionsmanager.h" 0045 #include "core/smb4kprofilemanager.h" 0046 #include "core/smb4ksynchronizer.h" 0047 #include "core/smb4kclient.h" 0048 0049 // Qt includes 0050 #include <QDebug> 0051 #include <QTest> 0052 0053 // KDE includes 0054 #include <KCoreAddons/KPluginLoader> 0055 #include <KCoreAddons/KPluginFactory> 0056 #include <KConfigWidgets/KConfigDialog> 0057 0058 0059 Smb4KDeclarative::Smb4KDeclarative(QObject* parent) 0060 : QObject(parent), d(new Smb4KDeclarativePrivate) 0061 { 0062 // 0063 // Initialize the core 0064 // 0065 Smb4KGlobal::initCore(true, false); 0066 0067 // 0068 // Connections 0069 // 0070 connect(Smb4KClient::self(), SIGNAL(workgroups()), this, SLOT(slotWorkgroupsListChanged())); 0071 connect(Smb4KClient::self(), SIGNAL(hosts(WorkgroupPtr)), this, SLOT(slotHostsListChanged())); 0072 connect(Smb4KClient::self(), SIGNAL(shares(HostPtr)), this, SLOT(slotSharesListChanged())); 0073 connect(Smb4KClient::self(), SIGNAL(aboutToStart(NetworkItemPtr,int)), this, SIGNAL(busy())); 0074 connect(Smb4KClient::self(), SIGNAL(finished(NetworkItemPtr,int)), this, SIGNAL(idle())); 0075 0076 connect(Smb4KMounter::self(), SIGNAL(mountedSharesListChanged()), this, SLOT(slotMountedSharesListChanged())); 0077 connect(Smb4KMounter::self(), SIGNAL(aboutToStart(int)), this, SIGNAL(busy())); 0078 connect(Smb4KMounter::self(), SIGNAL(finished(int)), this, SIGNAL(idle())); 0079 0080 connect(Smb4KBookmarkHandler::self(), SIGNAL(updated()), this, SLOT(slotBookmarksListChanged())); 0081 0082 connect(Smb4KProfileManager::self(), SIGNAL(profilesListChanged(QStringList)), this, SLOT(slotProfilesListChanged(QStringList))); 0083 connect(Smb4KProfileManager::self(), SIGNAL(activeProfileChanged(QString)), this, SLOT(slotActiveProfileChanged(QString))); 0084 connect(Smb4KProfileManager::self(), SIGNAL(profileUsageChanged(bool)), this, SLOT(slotProfileUsageChanged(bool))); 0085 0086 // 0087 // Do the initial loading of items 0088 // 0089 slotBookmarksListChanged(); 0090 slotProfilesListChanged(Smb4KProfileManager::self()->profilesList()); 0091 slotActiveProfileChanged(Smb4KProfileManager::self()->activeProfile()); 0092 slotProfileUsageChanged(Smb4KProfileManager::self()->useProfiles()); 0093 } 0094 0095 0096 Smb4KDeclarative::~Smb4KDeclarative() 0097 { 0098 while (!d->workgroupObjects.isEmpty()) 0099 { 0100 delete d->workgroupObjects.takeFirst(); 0101 } 0102 0103 while (!d->hostObjects.isEmpty()) 0104 { 0105 delete d->hostObjects.takeFirst(); 0106 } 0107 0108 while (!d->shareObjects.isEmpty()) 0109 { 0110 delete d->shareObjects.takeFirst(); 0111 } 0112 0113 while (!d->mountedObjects.isEmpty()) 0114 { 0115 delete d->mountedObjects.takeFirst(); 0116 } 0117 0118 while (!d->bookmarkObjects.isEmpty()) 0119 { 0120 delete d->bookmarkObjects.takeFirst(); 0121 } 0122 0123 while (!d->bookmarkGroupObjects.isEmpty()) 0124 { 0125 delete d->bookmarkGroupObjects.takeFirst(); 0126 } 0127 0128 while (!d->profileObjects.isEmpty()) 0129 { 0130 delete d->profileObjects.takeFirst(); 0131 } 0132 } 0133 0134 0135 QQmlListProperty<Smb4KNetworkObject> Smb4KDeclarative::workgroups() 0136 { 0137 return QQmlListProperty<Smb4KNetworkObject>(this, d->workgroupObjects); 0138 } 0139 0140 0141 QQmlListProperty<Smb4KNetworkObject> Smb4KDeclarative::hosts() 0142 { 0143 return QQmlListProperty<Smb4KNetworkObject>(this, d->hostObjects); 0144 } 0145 0146 0147 QQmlListProperty<Smb4KNetworkObject> Smb4KDeclarative::shares() 0148 { 0149 return QQmlListProperty<Smb4KNetworkObject>(this, d->shareObjects); 0150 } 0151 0152 0153 QQmlListProperty<Smb4KNetworkObject> Smb4KDeclarative::mountedShares() 0154 { 0155 return QQmlListProperty<Smb4KNetworkObject>(this, d->mountedObjects); 0156 } 0157 0158 0159 QQmlListProperty<Smb4KBookmarkObject> Smb4KDeclarative::bookmarks() 0160 { 0161 return QQmlListProperty<Smb4KBookmarkObject>(this, d->bookmarkObjects); 0162 } 0163 0164 0165 QQmlListProperty<Smb4KBookmarkObject> Smb4KDeclarative::bookmarkGroups() 0166 { 0167 return QQmlListProperty<Smb4KBookmarkObject>(this, d->bookmarkGroupObjects); 0168 } 0169 0170 0171 QQmlListProperty<Smb4KProfileObject> Smb4KDeclarative::profiles() 0172 { 0173 return QQmlListProperty<Smb4KProfileObject>(this, d->profileObjects); 0174 } 0175 0176 0177 void Smb4KDeclarative::lookup(Smb4KNetworkObject *object) 0178 { 0179 if (object) 0180 { 0181 switch (object->type()) 0182 { 0183 case Smb4KNetworkObject::Network: 0184 { 0185 Smb4KClient::self()->lookupDomains(); 0186 break; 0187 } 0188 case Smb4KNetworkObject::Workgroup: 0189 { 0190 // Check if the workgroup is known. 0191 WorkgroupPtr workgroup = Smb4KGlobal::findWorkgroup(object->url().host().toUpper()); 0192 0193 if (workgroup) 0194 { 0195 Smb4KClient::self()->lookupDomainMembers(workgroup); 0196 } 0197 0198 break; 0199 } 0200 case Smb4KNetworkObject::Host: 0201 { 0202 // Check if the host is known. 0203 HostPtr host = Smb4KGlobal::findHost(object->url().host().toUpper()); 0204 0205 if (host) 0206 { 0207 Smb4KClient::self()->lookupShares(host); 0208 } 0209 0210 break; 0211 } 0212 case Smb4KNetworkObject::Share: 0213 { 0214 break; 0215 } 0216 default: 0217 { 0218 // Shares are ignored 0219 break; 0220 } 0221 } 0222 } 0223 else 0224 { 0225 // If the object is 0, scan the whole network. 0226 Smb4KClient::self()->lookupDomains(); 0227 } 0228 } 0229 0230 0231 Smb4KNetworkObject *Smb4KDeclarative::findNetworkItem(const QUrl &url, int type) 0232 { 0233 Smb4KNetworkObject *object = 0; 0234 0235 if (url.isValid()) 0236 { 0237 switch (type) 0238 { 0239 case Smb4KNetworkObject::Workgroup: 0240 { 0241 for (Smb4KNetworkObject *obj : d->workgroupObjects) 0242 { 0243 if (url == obj->url()) 0244 { 0245 object = obj; 0246 break; 0247 } 0248 else 0249 { 0250 continue; 0251 } 0252 } 0253 break; 0254 } 0255 case Smb4KNetworkObject::Host: 0256 { 0257 for (Smb4KNetworkObject *obj : d->hostObjects) 0258 { 0259 if (url == obj->url()) 0260 { 0261 object = obj; 0262 break; 0263 } 0264 else 0265 { 0266 continue; 0267 } 0268 } 0269 break; 0270 } 0271 case Smb4KNetworkObject::Share: 0272 { 0273 for (Smb4KNetworkObject *obj : d->shareObjects) 0274 { 0275 if (url == obj->url()) 0276 { 0277 object = obj; 0278 break; 0279 } 0280 else 0281 { 0282 continue; 0283 } 0284 } 0285 break; 0286 } 0287 default: 0288 { 0289 break; 0290 } 0291 } 0292 } 0293 0294 return object; 0295 } 0296 0297 0298 void Smb4KDeclarative::openMountDialog() 0299 { 0300 Smb4KMounter::self()->openMountDialog(); 0301 } 0302 0303 0304 void Smb4KDeclarative::mountShare(Smb4KNetworkObject *object) 0305 { 0306 if (object && object->type() == Smb4KNetworkObject::Share) 0307 { 0308 SharePtr share = Smb4KGlobal::findShare(object->url(), object->workgroupName()); 0309 0310 if (share) 0311 { 0312 Smb4KMounter::self()->mountShare(share); 0313 } 0314 } 0315 } 0316 0317 0318 void Smb4KDeclarative::mountBookmark(Smb4KBookmarkObject *object) 0319 { 0320 if (object) 0321 { 0322 // If the share is not in the global list of shares, 0323 // try the list of bookmarks. 0324 BookmarkPtr bookmark = Smb4KBookmarkHandler::self()->findBookmarkByUrl(object->url()); 0325 0326 SharePtr share = SharePtr(new Smb4KShare()); 0327 share->setUrl(object->url()); 0328 share->setWorkgroupName(bookmark->workgroupName()); 0329 share->setHostIpAddress(bookmark->hostIpAddress()); 0330 0331 Smb4KMounter::self()->mountShare(share); 0332 0333 share.clear(); 0334 } 0335 } 0336 0337 0338 void Smb4KDeclarative::unmount(Smb4KNetworkObject *object) 0339 { 0340 if (object && object->type()) 0341 { 0342 if (object->mountpoint().isValid()) 0343 { 0344 SharePtr share = Smb4KGlobal::findShareByPath(object->mountpoint().path()); 0345 0346 if (share) 0347 { 0348 Smb4KMounter::self()->unmountShare(share); 0349 } 0350 } 0351 } 0352 } 0353 0354 0355 void Smb4KDeclarative::unmountAll() 0356 { 0357 Smb4KMounter::self()->unmountAllShares(false); 0358 } 0359 0360 0361 Smb4KNetworkObject* Smb4KDeclarative::findMountedShare(const QUrl& url, bool exactMatch) 0362 { 0363 Smb4KNetworkObject *object = 0; 0364 0365 if (url.isValid()) 0366 { 0367 for (Smb4KNetworkObject *obj : d->mountedObjects) 0368 { 0369 if (url.matches(obj->url(), QUrl::None)) 0370 { 0371 object = obj; 0372 break; 0373 } 0374 else if (!exactMatch && url.matches(obj->url(), QUrl::RemoveUserInfo|QUrl::RemovePort|QUrl::StripTrailingSlash)) 0375 { 0376 object = obj; 0377 continue; 0378 } 0379 else 0380 { 0381 continue; 0382 } 0383 } 0384 } 0385 0386 return object; 0387 } 0388 0389 0390 void Smb4KDeclarative::print(Smb4KNetworkObject* object) 0391 { 0392 if (object && object->type() == Smb4KNetworkObject::Share) 0393 { 0394 SharePtr printer = Smb4KGlobal::findShare(object->url(), object->workgroupName()); 0395 0396 if (printer) 0397 { 0398 Smb4KClient::self()->openPrintDialog(printer); 0399 } 0400 } 0401 } 0402 0403 0404 void Smb4KDeclarative::addBookmark(Smb4KNetworkObject* object) 0405 { 0406 if (object) 0407 { 0408 QList<SharePtr> shares; 0409 0410 // First, search the list of shares gathered by the scanner. 0411 for (const SharePtr &share : Smb4KGlobal::sharesList()) 0412 { 0413 if (share->url() == object->url()) 0414 { 0415 shares << share; 0416 break; 0417 } 0418 else 0419 { 0420 continue; 0421 } 0422 } 0423 0424 // Second, if the list is still empty, try the list of mounted shares. 0425 if (shares.isEmpty()) 0426 { 0427 for (const SharePtr &mountedShare : Smb4KGlobal::mountedSharesList()) 0428 { 0429 if (mountedShare->url() == object->url()) 0430 { 0431 shares << mountedShare; 0432 break; 0433 } 0434 else 0435 { 0436 continue; 0437 } 0438 } 0439 } 0440 0441 // Now add the share. 0442 if (!shares.isEmpty()) 0443 { 0444 for (const SharePtr &p : shares) 0445 { 0446 qDebug() << p->url(); 0447 } 0448 0449 Smb4KBookmarkHandler::self()->addBookmarks(shares); 0450 } 0451 } 0452 } 0453 0454 0455 void Smb4KDeclarative::removeBookmark(Smb4KBookmarkObject* object) 0456 { 0457 if (object) 0458 { 0459 // 0460 // Find the bookmark in the list and remove it. 0461 // 0462 BookmarkPtr bookmark = Smb4KBookmarkHandler::self()->findBookmarkByUrl(object->url()); 0463 0464 if (bookmark) 0465 { 0466 Smb4KBookmarkHandler::self()->removeBookmark(bookmark); 0467 } 0468 } 0469 } 0470 0471 0472 void Smb4KDeclarative::editBookmarks() 0473 { 0474 Smb4KBookmarkHandler::self()->editBookmarks(); 0475 } 0476 0477 0478 void Smb4KDeclarative::synchronize(Smb4KNetworkObject* object) 0479 { 0480 if (object && object->type() == Smb4KNetworkObject::Share) 0481 { 0482 for (const SharePtr &share : Smb4KGlobal::mountedSharesList()) 0483 { 0484 if (share->url() == object->url()) 0485 { 0486 Smb4KSynchronizer::self()->synchronize(share); 0487 } 0488 } 0489 } 0490 } 0491 0492 0493 void Smb4KDeclarative::openCustomOptionsDialog(Smb4KNetworkObject *object) 0494 { 0495 if (object) 0496 { 0497 switch (object->type()) 0498 { 0499 case Smb4KNetworkObject::Host: 0500 { 0501 for (const HostPtr &host : Smb4KGlobal::hostsList()) 0502 { 0503 if (host->url() == object->url()) 0504 { 0505 Smb4KCustomOptionsManager::self()->openCustomOptionsDialog(host); 0506 break; 0507 } 0508 else 0509 { 0510 continue; 0511 } 0512 } 0513 break; 0514 } 0515 case Smb4KNetworkObject::Share: 0516 { 0517 for (const SharePtr &share : Smb4KGlobal::sharesList()) 0518 { 0519 if (share->url() == object->url()) 0520 { 0521 Smb4KCustomOptionsManager::self()->openCustomOptionsDialog(share); 0522 break; 0523 } 0524 else 0525 { 0526 continue; 0527 } 0528 } 0529 break; 0530 } 0531 default: 0532 { 0533 break; 0534 } 0535 } 0536 } 0537 } 0538 0539 0540 void Smb4KDeclarative::startClient() 0541 { 0542 Smb4KClient::self()->start(); 0543 } 0544 0545 0546 void Smb4KDeclarative::abortClient() 0547 { 0548 Smb4KClient::self()->abort(); 0549 } 0550 0551 0552 void Smb4KDeclarative::startMounter() 0553 { 0554 Smb4KMounter::self()->start(); 0555 } 0556 0557 0558 void Smb4KDeclarative::abortMounter() 0559 { 0560 Smb4KMounter::self()->abort(); 0561 } 0562 0563 0564 QString Smb4KDeclarative::activeProfile() const 0565 { 0566 QString activeProfile; 0567 0568 for (Smb4KProfileObject *profile : d->profileObjects) 0569 { 0570 if (profile->isActiveProfile()) 0571 { 0572 activeProfile = profile->profileName(); 0573 break; 0574 } 0575 else 0576 { 0577 continue; 0578 } 0579 } 0580 0581 return activeProfile; 0582 } 0583 0584 0585 void Smb4KDeclarative::setActiveProfile(const QString& profile) 0586 { 0587 Smb4KProfileManager::self()->setActiveProfile(profile); 0588 } 0589 0590 0591 bool Smb4KDeclarative::profileUsage() const 0592 { 0593 return Smb4KProfileManager::self()->useProfiles(); 0594 } 0595 0596 0597 void Smb4KDeclarative::preview(Smb4KNetworkObject* object) 0598 { 0599 if (object->type() == Smb4KNetworkObject::Share) 0600 { 0601 SharePtr share = Smb4KGlobal::findShare(object->url(), object->workgroupName()); 0602 0603 if (share) 0604 { 0605 Smb4KClient::self()->openPreviewDialog(share); 0606 } 0607 } 0608 } 0609 0610 0611 void Smb4KDeclarative::openConfigurationDialog() 0612 { 0613 // 0614 // Check if the configuration dialog exists and try to show it. 0615 // 0616 if (KConfigDialog::exists("Smb4KConfigDialog")) 0617 { 0618 KConfigDialog::showDialog("Smb4KConfigDialog"); 0619 return; 0620 } 0621 0622 // 0623 // If the dialog does not exist, load and show it: 0624 // 0625 KPluginLoader loader("smb4kconfigdialog", this); 0626 KPluginFactory *configFactory = loader.factory(); 0627 0628 if (configFactory) 0629 { 0630 KConfigDialog *dlg = configFactory->create<KConfigDialog>(); 0631 0632 if (dlg) 0633 { 0634 dlg->setObjectName("Smb4KConfigDialog"); 0635 dlg->show(); 0636 } 0637 } 0638 } 0639 0640 0641 void Smb4KDeclarative::slotWorkgroupsListChanged() 0642 { 0643 // (Re)fill the list of workgroup objects. 0644 while (!d->workgroupObjects.isEmpty()) 0645 { 0646 delete d->workgroupObjects.takeFirst(); 0647 } 0648 0649 for (const WorkgroupPtr &workgroup : Smb4KGlobal::workgroupsList()) 0650 { 0651 d->workgroupObjects << new Smb4KNetworkObject(workgroup.data()); 0652 } 0653 0654 emit workgroupsListChanged(); 0655 } 0656 0657 0658 void Smb4KDeclarative::slotHostsListChanged() 0659 { 0660 // (Re)fill the list of host object. 0661 while (!d->hostObjects.isEmpty()) 0662 { 0663 delete d->hostObjects.takeFirst(); 0664 } 0665 0666 for (const HostPtr &host : Smb4KGlobal::hostsList()) 0667 { 0668 d->hostObjects << new Smb4KNetworkObject(host.data()); 0669 } 0670 0671 emit hostsListChanged(); 0672 } 0673 0674 0675 void Smb4KDeclarative::slotSharesListChanged() 0676 { 0677 // (Re)fill the list of share objects. 0678 while (!d->shareObjects.isEmpty()) 0679 { 0680 delete d->shareObjects.takeFirst(); 0681 } 0682 0683 for (const SharePtr &share : Smb4KGlobal::sharesList()) 0684 { 0685 d->shareObjects << new Smb4KNetworkObject(share.data()); 0686 } 0687 0688 emit sharesListChanged(); 0689 } 0690 0691 0692 void Smb4KDeclarative::slotMountedSharesListChanged() 0693 { 0694 // (Re)fill the list of share objects. 0695 while (!d->mountedObjects.isEmpty()) 0696 { 0697 delete d->mountedObjects.takeFirst(); 0698 } 0699 0700 for (const SharePtr &mountedShare : Smb4KGlobal::mountedSharesList()) 0701 { 0702 d->mountedObjects << new Smb4KNetworkObject(mountedShare.data()); 0703 } 0704 0705 emit mountedSharesListChanged(); 0706 } 0707 0708 0709 void Smb4KDeclarative::slotBookmarksListChanged() 0710 { 0711 // (Re)fill the list of bookmark and group objects. 0712 while (!d->bookmarkObjects.isEmpty()) 0713 { 0714 delete d->bookmarkObjects.takeFirst(); 0715 } 0716 0717 while (!d->bookmarkGroupObjects.isEmpty()) 0718 { 0719 delete d->bookmarkGroupObjects.takeFirst(); 0720 } 0721 0722 for (const BookmarkPtr &bookmark : Smb4KBookmarkHandler::self()->bookmarksList()) 0723 { 0724 d->bookmarkObjects << new Smb4KBookmarkObject(bookmark.data()); 0725 } 0726 0727 for (const QString &group : Smb4KBookmarkHandler::self()->groupsList()) 0728 { 0729 d->bookmarkGroupObjects << new Smb4KBookmarkObject(group); 0730 } 0731 0732 emit bookmarksListChanged(); 0733 } 0734 0735 0736 void Smb4KDeclarative::slotProfilesListChanged(const QStringList& profiles) 0737 { 0738 while (!d->profileObjects.isEmpty()) 0739 { 0740 delete d->profileObjects.takeFirst(); 0741 } 0742 0743 for (const QString &p : profiles) 0744 { 0745 Smb4KProfileObject *profile = new Smb4KProfileObject(); 0746 profile->setProfileName(p); 0747 0748 if (QString::compare(p, Smb4KProfileManager::self()->activeProfile()) == 0) 0749 { 0750 profile->setActiveProfile(true); 0751 } 0752 else 0753 { 0754 profile->setActiveProfile(false); 0755 } 0756 0757 d->profileObjects << profile; 0758 } 0759 0760 emit profilesListChanged(); 0761 } 0762 0763 0764 void Smb4KDeclarative::slotActiveProfileChanged(const QString& activeProfile) 0765 { 0766 for (Smb4KProfileObject *profile : d->profileObjects) 0767 { 0768 if (QString::compare(profile->profileName(), activeProfile) == 0) 0769 { 0770 profile->setActiveProfile(true); 0771 } 0772 else 0773 { 0774 profile->setActiveProfile(false); 0775 } 0776 } 0777 0778 emit activeProfileChanged(); 0779 } 0780 0781 0782 void Smb4KDeclarative::slotProfileUsageChanged(bool /*use*/) 0783 { 0784 emit profileUsageChanged(); 0785 } 0786