Warning, /network/smb4k/plasmoid/package/contents/ui/BookmarksPage.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 
0015 PlasmaComponents.Page {
0016   id: bookmarksPage
0017   
0018   //
0019   // Tool bar
0020   //
0021   PlasmaComponents.ToolBar {
0022     id: bookmarksToolBar
0023     anchors {
0024       top: parent.top
0025       left: parent.left
0026       right: parent.right
0027     }
0028     
0029     tools: PlasmaComponents.ToolBarLayout {
0030       PlasmaComponents.ToolButton {
0031         id: backButton
0032         tooltip: i18n("Go back")
0033         iconSource: "go-previous"
0034         width: minimumWidth
0035         onClicked: {
0036           back()
0037         }
0038       }
0039       PlasmaComponents.ToolButton {
0040         id: editButton
0041         tooltip: i18n("Edit bookmarks")
0042         iconSource: "bookmarks-organize"
0043         width: minimumWidth
0044         onClicked: {
0045           iface.editBookmarks()
0046         }
0047       }
0048     }
0049   }
0050   
0051   //
0052   // Delegate Model (used for sorting)
0053   //
0054   DelegateModel {
0055     id: bookmarkItemDelegateModel
0056     
0057     function lessThan(left, right) {
0058       var less = false
0059 
0060       if (left.isCategory && right.isCategory) {
0061         less = (left.categoryName < right.categoryName)
0062       }
0063       else if (!left.isCategory && !right.isCategory) {        
0064         if (left.hostName == right.hostName) {
0065           less = (left.shareName < right.shareName)
0066         }
0067         else {
0068           less = (left.shareName < right.shareName && left.hostName < right.hostName)
0069         }
0070       }
0071 
0072       return less
0073     }
0074     
0075     function insertPosition(item) {
0076       var lower = 0
0077       var upper = items.count
0078       
0079       while (lower < upper) {
0080         var middle = Math.floor(lower + (upper - lower) / 2)
0081         var result = lessThan(item.model.object, items.get(middle).model.object)
0082         if (result) {
0083           upper = middle
0084         }
0085         else {
0086           lower = middle + 1
0087         }
0088       }
0089       return lower
0090     }
0091     
0092     function sort() {
0093       while (unsortedItems.count > 0) {
0094         var item = unsortedItems.get(0)
0095         var index = insertPosition(item)
0096         
0097         item.groups = "items"
0098         items.move(item.itemsIndex, index)
0099       }
0100     }
0101     
0102     items.includeByDefault: false
0103     
0104     groups: [ 
0105       DelegateModelGroup {
0106         id: unsortedItems
0107         name: "unsorted"
0108       
0109         includeByDefault: true
0110       
0111         onChanged: {
0112           bookmarkItemDelegateModel.sort()
0113         }
0114       }
0115     ]
0116 
0117     filterOnGroup: "items"
0118     
0119     model: ListModel {}
0120     
0121     delegate: BookmarkItemDelegate {
0122       id: bookmarkItemDelegate
0123         
0124       onItemClicked: {
0125         bookmarksListView.currentIndex = DelegateModel.itemsIndex
0126         bookmarkOrCategoryClicked(object)
0127       }
0128     }
0129   }
0130   
0131   //
0132   // List view
0133   //
0134   PlasmaExtras.ScrollArea {
0135     id: bookmarksScrollArea
0136     
0137     anchors {
0138       top: bookmarksToolBar.bottom
0139       left: parent.left
0140       right: parent.right
0141       bottom: parent.bottom
0142     }
0143     
0144     ListView {
0145       id: bookmarksListView
0146       model: bookmarkItemDelegateModel
0147       clip: true
0148       focus: true
0149       highlightRangeMode: ListView.StrictlyEnforceRange
0150     }
0151   }
0152   
0153   //
0154   // Connections
0155   //
0156   Connections {
0157     target: iface
0158     function onMountedSharesChanged() { shareMountedOrUnmounted() }
0159     function onBookmarksListChanged() { fillView() }
0160   }
0161   
0162   //
0163   // Initialization
0164   //
0165   Component.onCompleted: {
0166     fillView()
0167   }
0168   
0169   //
0170   // Functions
0171   //
0172   function back() {
0173     // Since the 'Back' button is only useful when you
0174     // are currently in a category subfolder and want to 
0175     // go back to the toplevel, just run fillView() here.
0176     fillView()
0177   }
0178   
0179   function bookmarkOrCategoryClicked(object) {
0180     if (object.isCategory) {
0181       while (bookmarkItemDelegateModel.model.count != 0) {
0182         bookmarkItemDelegateModel.model.remove(0)
0183       }
0184       
0185       getBookmarks(object.categoryName)
0186       
0187       // Set the current item to 0
0188       bookmarksListView.currentIndex = 0
0189     }
0190     else {
0191       iface.mountBookmark(object)
0192     }
0193   }
0194   
0195   function shareMountedOrUnmounted() {
0196     for (var i = 0; i < bookmarkItemDelegateModel.model.count; i++) {
0197       var object = bookmarkItemDelegateModel.model.get(i).object
0198       
0199       if (object !== null) {
0200         if (!object.isCategory) {
0201           object.isMounted = iface.isShareMounted(object.url)
0202           bookmarkItemDelegateModel.model.set(i, {"object": object})
0203         }
0204       }
0205     }
0206   }
0207   
0208   function fillView() {
0209     while (bookmarkItemDelegateModel.model.count != 0) {
0210       bookmarkItemDelegateModel.model.remove(0)
0211     }
0212     
0213     // Get categories
0214     if (iface.bookmarkCategories.length != 0) {
0215       for (var i = 0; i < iface.bookmarkCategories.length; i++) {
0216         if (iface.bookmarkCategories[i].categoryName.length != 0) {
0217           bookmarkItemDelegateModel.model.append({"object": iface.bookmarkCategories[i]})
0218         }
0219       }
0220     }
0221     
0222     // Get toplevel bookmarks
0223     getBookmarks("")
0224     
0225     // Set the current item to 0
0226     bookmarksListView.currentIndex = 0
0227   }
0228   
0229   function getBookmarks(categoryName) {
0230     if (iface.bookmarks.length != 0) {
0231       for (var i = 0; i < iface.bookmarks.length; i++) {
0232         if (iface.bookmarks[i].categoryName == categoryName) {
0233           var bookmark = iface.bookmarks[i]
0234           bookmark.isMounted = iface.isShareMounted(bookmark.url)
0235           bookmarkItemDelegateModel.model.append({"object": bookmark})
0236         }
0237       }
0238     }
0239   }
0240 }