Warning, /network/smb4k/plasmoid/package/contents/ui/NetworkBrowserPage.qml is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2017-2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 import QtQuick 2.3
0008 import QtQuick.Layouts 1.3
0009 import QtQml.Models 2.3
0010 import org.kde.plasma.core 2.0 as PlasmaCore
0011 import org.kde.plasma.plasmoid 2.0
0012 import org.kde.plasma.components 2.0 as PlasmaComponents
0013 import org.kde.plasma.extras 2.0 as PlasmaExtras
0014 import org.kde.smb4k.smb4kqmlplugin 2.0
0015 
0016 
0017 PlasmaComponents.Page {
0018   id: networkBrowserPage
0019   anchors.fill: parent
0020   
0021   property var parentObject: 0
0022   
0023   //
0024   // Tool bar
0025   //
0026   PlasmaComponents.ToolBar {
0027     id: networkBrowserToolBar
0028     
0029     anchors {
0030       top: parent.top
0031       left: parent.left
0032       right: parent.right
0033     }
0034     
0035     tools: PlasmaComponents.ToolBarLayout {
0036       PlasmaComponents.ToolButton {
0037         id: rescanButton
0038         tooltip: i18n("Rescan")
0039         iconSource: "view-refresh"
0040         onClicked: {
0041           rescan()
0042         }
0043       }
0044 
0045       PlasmaComponents.ToolButton {
0046         id: abortButton
0047         tooltip: i18n("Abort")
0048         iconSource: "process-stop"
0049         onClicked: {
0050           abort()
0051         }
0052       }
0053       
0054       PlasmaComponents.ToolButton {
0055         id: upButton
0056         tooltip: i18n("Go one level up")
0057         iconSource: "go-up-symbolic"
0058         onClicked: {
0059           up()
0060         }
0061       }
0062       
0063       PlasmaComponents.ToolButton {
0064         id: mountDialogButton
0065         tooltip: i18n("Open the mount dialog")
0066         iconSource: "view-form"
0067         onClicked: {
0068           // FIXME: Use Plasma dialog
0069           iface.openMountDialog()
0070         }
0071       }
0072     }
0073   }
0074   
0075   //
0076   // Delegate Model (used for sorting)
0077   //
0078   DelegateModel {
0079     id: networkBrowserItemDelegateModel
0080     
0081     function lessThan(left, right) {
0082       var less = false
0083       
0084       switch (left.type) {
0085         case NetworkObject.Workgroup:
0086           less = (left.workgroupName < right.workgroupName)
0087           break
0088         case NetworkObject.Host:
0089           less = (left.hostName < right.hostName)
0090           break
0091         case NetworkObject.Share:
0092           less = (left.shareName < right.shareName)
0093           break
0094         default:
0095           break          
0096       }
0097       return less
0098     }
0099     
0100     function insertPosition(item) {
0101       var lower = 0
0102       var upper = items.count
0103       
0104       while (lower < upper) {
0105         var middle = Math.floor(lower + (upper - lower) / 2)
0106         var result = lessThan(item.model.object, items.get(middle).model.object)
0107         if (result) {
0108           upper = middle
0109         }
0110         else {
0111           lower = middle + 1
0112         }
0113       }
0114       return lower
0115     }
0116     
0117     function sort() {
0118       while (unsortedItems.count > 0) {
0119         var item = unsortedItems.get(0)
0120         var index = insertPosition(item)
0121         
0122         item.groups = "items"
0123         items.move(item.itemsIndex, index)
0124       }
0125     }
0126     
0127     items.includeByDefault: false
0128     
0129     groups: [ 
0130       DelegateModelGroup {
0131         id: unsortedItems
0132         name: "unsorted"
0133       
0134         includeByDefault: true
0135       
0136         onChanged: {
0137           networkBrowserItemDelegateModel.sort()
0138         }
0139       }
0140     ]
0141 
0142     filterOnGroup: "items"
0143     
0144     model: ListModel {}
0145     
0146     delegate: NetworkBrowserItemDelegate {
0147       id: networkBrowserItemDelegate
0148         
0149       onItemClicked: {
0150         networkBrowserListView.currentIndex = DelegateModel.itemsIndex
0151         networkItemClicked()
0152       }
0153         
0154       onBookmarkClicked: {
0155         networkBrowserListView.currentIndex = DelegateModel.itemsIndex
0156         iface.addBookmark(object)
0157       }
0158         
0159       onPreviewClicked: {
0160         networkBrowserListView.currentIndex = DelegateModel.itemsIndex
0161         iface.preview(object)
0162       }
0163         
0164       onConfigureClicked: {
0165         networkBrowserListView.currentIndex = DelegateModel.itemsIndex
0166         iface.openCustomOptionsDialog(object)
0167       }
0168     }
0169   }
0170       
0171   //
0172   // List view 
0173   //
0174   PlasmaExtras.ScrollArea {
0175     id: networkBrowserScrollArea
0176     
0177     anchors {
0178       top: networkBrowserToolBar.bottom
0179       left: parent.left
0180       right: parent.right
0181       bottom: parent.bottom
0182     }
0183     
0184     ListView {
0185       id: networkBrowserListView
0186       anchors.fill: parent
0187       model: networkBrowserItemDelegateModel
0188       clip: true
0189       focus: true
0190       highlightRangeMode: ListView.StrictlyEnforceRange
0191 //       highlight: Rectangle {
0192 //         color: theme.highlightColor
0193 //         radius: 5
0194 //         opacity: 0.2
0195 //       }
0196     }
0197   }
0198   
0199   //
0200   // Connections
0201   //
0202   Connections {
0203     target: iface
0204     function onWorkgroupsChanged() { getWorkgroups() }
0205     function onHostsChanged() { getHosts() }
0206     function onSharesChanged() { getShares() }
0207     function onMountedSharesChanged() { shareMountedOrUnmounted() }
0208   }
0209   
0210   //
0211   // Initialization
0212   //
0213   Component.onCompleted: {
0214     if (networkBrowserListView.count == 0) {
0215       getWorkgroups()
0216     }
0217   }  
0218   
0219   //
0220   // Functions
0221   //
0222   function rescan() {
0223     if (parentObject !== null) {
0224       iface.lookup(parentObject)
0225     }
0226     else {
0227       iface.lookup()
0228     }
0229   }
0230   
0231   function abort() {
0232     iface.abortClient()
0233     iface.abortMounter()
0234   }
0235   
0236   function up() {
0237     if (parentObject !== null) {
0238       
0239       switch (parentObject.type) {
0240         case NetworkObject.Workgroup:
0241           networkBrowserListView.currentIndex = -1
0242           iface.lookup()
0243           break
0244         case NetworkObject.Host:
0245           var object = iface.findNetworkItem(parentObject.parentUrl, parentObject.parentType)
0246           if (object !== null) {
0247             parentObject = object
0248             iface.lookup(object)
0249           }
0250           break
0251         default:
0252           break
0253       }
0254     }
0255   }
0256   
0257   function networkItemClicked() {
0258     if (networkBrowserListView.currentIndex != -1) {
0259       var object = networkBrowserItemDelegateModel.items.get(networkBrowserListView.currentIndex).model.object
0260       
0261       if (object.type == NetworkObject.Share) {
0262         if (!object.isPrinter) {
0263           iface.mountShare(object)
0264         }
0265         else {
0266           iface.print(object)
0267         }
0268       }
0269       else {
0270         parentObject = object
0271         iface.lookup(object)
0272       }
0273     }
0274   }
0275   
0276   function getWorkgroups() {
0277     while (networkBrowserItemDelegateModel.model.count != 0) {
0278       networkBrowserItemDelegateModel.model.remove(0)
0279     }
0280     
0281     if (iface.workgroups.length != 0) {
0282       for (var i = 0; i < iface.workgroups.length; i++) {
0283         networkBrowserItemDelegateModel.model.append({"object": iface.workgroups[i]})
0284       }
0285     }
0286     
0287     networkBrowserListView.currentIndex = 0
0288   }
0289   
0290   function getHosts() {
0291     var workgroupName = parentObject.workgroupName
0292     
0293     while (networkBrowserItemDelegateModel.model.count != 0) {
0294       networkBrowserItemDelegateModel.model.remove(0)
0295     }
0296     
0297     if (iface.hosts.length != 0) {
0298       for (var i = 0; i < iface.hosts.length; i++) {
0299         if (workgroupName.length != 0 && workgroupName == iface.hosts[i].workgroupName) {
0300           networkBrowserItemDelegateModel.model.append({"object": iface.hosts[i]})
0301         }
0302       }
0303     }
0304     
0305     networkBrowserListView.currentIndex = 0
0306   }
0307   
0308   function getShares() {
0309     var hostName = parentObject.hostName
0310     
0311     while (networkBrowserItemDelegateModel.model.count != 0) {
0312       networkBrowserItemDelegateModel.model.remove(0)
0313     }
0314     
0315     if (iface.shares.length != 0) {
0316       for (var i = 0; i < iface.shares.length; i++) {
0317         if (hostName.length != 0 && hostName == iface.shares[i].hostName) {
0318           networkBrowserItemDelegateModel.model.append({"object": iface.shares[i]})
0319         }
0320       }
0321     }
0322     
0323     networkBrowserListView.currentIndex = 0
0324   }
0325   
0326   function shareMountedOrUnmounted() {
0327     for (var i = 0; i < networkBrowserItemDelegateModel.model.count; i++) {
0328       var object = networkBrowserItemDelegateModel.model.get(i).object
0329       
0330       if (object !== null && object.type == NetworkObject.Share) {
0331         object.isMounted = iface.isShareMounted(object.url)
0332         networkBrowserItemDelegateModel.model.set(i, {"object": object})
0333       }
0334     }
0335   }
0336 }