1/*
2    SPDX-FileCopyrightText: 2014 Eike Hein <hein@kde.org>
3
4    SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7import QtQuick 2.5
8import QtQuick.Controls 2.5
9import QtQuick.Layouts 1.0
10
11import org.kde.plasma.plasmoid 2.0
12import org.kde.plasma.core 2.0 as PlasmaCore
13import org.kde.kirigami 2.5 as Kirigami
14
15import org.kde.private.desktopcontainment.folder 0.1 as Folder
16
17Item {
18    id: configLocation
19
20    property string cfg_url
21    property alias cfg_labelMode: labelMode.currentIndex
22    property alias cfg_labelText: labelText.text
23    property bool titleVisible: !("containmentType" in plasmoid)
24
25    onCfg_urlChanged: applyConfig()
26
27    function applyConfig(force) {
28        if (!force && locationGroup.checkedButton != null) {
29            return;
30        }
31
32        if (cfg_url == "desktop:/") {
33            locationDesktop.checked = true;
34            locationCustomValue.text = "";
35        } else if (cfg_url == "activities:/current/") {
36            locationCurrentActivity.checked = true;
37            locationCustomValue.text = "";
38        } else {
39            var placeForUrl = placesModel.indexForUrl(cfg_url);
40
41            if (placeForUrl !== -1) {
42                locationPlace.checked = true;
43                locationPlaceValue.currentIndex = placeForUrl;
44                locationCustomValue.text = "";
45            } else {
46                locationCustom.checked = true;
47                locationCustomValue.text = cfg_url;
48            }
49        }
50
51        locationPlaceValue.enabled = locationPlace.checked;
52    }
53
54    Folder.PlacesModel {
55        id: placesModel
56        showDesktopEntry: false
57
58        onPlacesChanged: applyConfig(true)
59    }
60
61    ButtonGroup {
62        id: locationGroup
63
64        buttons:  [locationDesktop, locationCurrentActivity, locationPlace, locationCustom]
65
66        onCheckedButtonChanged: {
67            if (checkedButton == locationDesktop) {
68                cfg_url = "desktop:/";
69            } else if (checkedButton == locationCurrentActivity) {
70                cfg_url = "activities:/current/";
71            }
72        }
73    }
74
75    Kirigami.FormLayout {
76        anchors.horizontalCenter: parent.horizontalCenter
77
78        RadioButton {
79            id: locationDesktop
80
81            Kirigami.FormData.label: i18n("Show:")
82
83            text: i18n("Desktop folder")
84        }
85
86        RadioButton {
87            id: locationCurrentActivity
88            visible: placesModel.activityLinkingEnabled
89
90            text: i18n("Files linked to the current activity")
91        }
92
93        RowLayout {
94            RadioButton {
95                id: locationPlace
96
97                text: i18n("Places panel item:")
98
99                onCheckedChanged: {
100                    locationPlaceValue.enabled = checked;
101                }
102            }
103
104            ComboBox {
105                id: locationPlaceValue
106
107                Layout.fillWidth: true
108
109                model: placesModel
110                textRole: "display"
111
112                enabled: true
113
114                onEnabledChanged: {
115                    if (enabled && currentIndex != -1) {
116                        cfg_url = placesModel.urlForIndex(currentIndex);
117                    }
118                }
119
120                onActivated: {
121                    cfg_url = placesModel.urlForIndex(index);
122                }
123            }
124        }
125
126        RowLayout {
127            RadioButton {
128                id: locationCustom
129
130                text: i18n("Custom location:")
131            }
132
133            TextField {
134                id: locationCustomValue
135                enabled: locationCustom.checked
136                Layout.fillWidth: true
137
138                placeholderText: i18n("Type path or URL…")
139
140                inputMethodHints: Qt.ImhNoPredictiveText
141
142                onEnabledChanged: {
143                    if (enabled && text != "") {
144                        cfg_url = text;
145                    }
146                }
147
148                onTextChanged: {
149                    if (enabled) {
150                        cfg_url = text;
151                    }
152                }
153            }
154            Button {
155                icon.name: "document-open"
156
157                enabled: locationCustom.checked
158
159                onClicked: {
160                    directoryPicker.open();
161                }
162            }
163            Folder.DirectoryPicker {
164                id: directoryPicker
165
166                onUrlChanged: {
167                    locationCustomValue.text = url;
168                }
169            }
170        }
171
172        Item {
173            visible: titleVisible
174            Kirigami.FormData.isSection: true
175        }
176
177        ComboBox {
178            id: labelMode
179            visible: titleVisible
180
181            Kirigami.FormData.label: i18n("Title:")
182
183            model: [i18n("None"), i18n("Default"), i18n("Full path"), i18n("Custom title")]
184        }
185
186        RowLayout {
187            visible: titleVisible
188
189            Item {
190                width: PlasmaCore.Units.largeSpacing
191            }
192
193            TextField {
194                id: labelText
195                Layout.fillWidth: true
196                enabled: (labelMode.currentIndex == 3)
197
198                placeholderText: i18n("Enter custom title…")
199            }
200        }
201    }
202}
203