KNewStuff

Action.qml
1/*
2 SPDX-FileCopyrightText: 2021 Dan Leinir Turthra Jensen <admin@leinir.dk>
3 SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8/**
9 * @brief An action which when triggered will open a NewStuff.Dialog or a NewStuff.Page, depending on settings
10 *
11 * This component is equivalent to the old Button component, but functions in more modern applications
12 *
13 * The following is a simple example of how to use this Action to show wallpapers from the KDE Store, on a
14 * system where Plasma has been installed (and consequently the wallpaper knsrc file is available). This also
15 * shows how to make the action push a page to a pageStack rather than opening a dialog:
16 *
17\code{.qml}
18import org.kde.newstuff as NewStuff
19
20NewStuff.Action {
21 configFile: "wallpaper.knsrc"
22 text: i18n("&Get New Wallpapers…")
23 pageStack: applicationWindow().pageStack
24 onEntryEvent: function(entry, event) {
25 if (event === NewStuff.Entry.StatusChangedEvent) {
26 // A entry was installed, updated or removed
27 } else if (event === NewStuff.Entry.AdoptedEvent) {
28 // The "AdoptionCommand" from the knsrc file was run for the given entry.
29 // This should not require refreshing the data for the model
30 }
31 }
32}
33\endcode
34 *
35 * @see NewStuff.Button
36 * @since 5.81
37 */
38
39import QtQuick
40import org.kde.kirigami as Kirigami
41import org.kde.newstuff as NewStuff
42
43Kirigami.Action {
44 id: component
45
46 /*
47 * The configuration file is not aliased, because then we end up initialising the
48 * Engine immediately the Action is instantiated, which we want to avoid (as that
49 * is effectively a phone-home scenario, and causes internet traffic in situations
50 * where it would not seem likely that there should be any).
51 * If we want, in the future, to add some status display to the Action (such as "there
52 * are updates to be had" or somesuch, then we can do this, but until that choice is
53 * made, let's not)
54 */
55 /**
56 * The configuration file to use for the Page created by this action
57 */
58 property string configFile
59
60 /**
61 * The view mode of the page spawned by this action, which overrides the
62 * default one (ViewMode.Tiles). This should be set using the
63 * NewStuff.Page.ViewMode enum. Note that ViewMode.Icons has been removed,
64 * and asking for it will return ViewMode.Tiles.
65 * @see NewStuff.Page.ViewMode
66 */
67 property int viewMode: NewStuff.Page.ViewMode.Tiles
68
69 /**
70 * If this is set, the action will push a NewStuff.Page onto this page stack
71 * (and request it is made visible if triggered again). If you do not set this
72 * property, the action will spawn a NewStuff.Dialog instead.
73 * @note If you are building a KCM, set this to your ```kcm``` object.
74 */
75 property Item pageStack
76
77 /**
78 * The engine which handles the content in this Action
79 * This will be null until the action has been triggered the first time
80 */
81 readonly property NewStuff.Engine engine: component._private.engine
82
83 /**
84 * This forwards the entry changed event from the QtQuick engine
85 * @see Engine::entryEvent
86 */
87 signal entryEvent(var entry, int event)
88
89 /**
90 * If this is true (default is false), the action will be shown when the Kiosk settings are such
91 * that Get Hot New Stuff is disallowed (and any other time enabled is set to false).
92 * Usually you would want to leave this alone, but occasionally you may have a reason to
93 * leave a action in place that the user is unable to enable.
94 */
95 property bool visibleWhenDisabled: false
96
97 /**
98 * The parent window for the dialog created by invoking the action
99 *
100 * @since 6.1
101 */
102 property Window transientParent
103
104 /**
105 * Show the page/dialog (same as activating the action), if allowed by the Kiosk settings
106 */
107 function showHotNewStuff() {
108 component._private.showHotNewStuff();
109 }
110
111 onTriggered: showHotNewStuff()
112
113 icon.name: "get-hot-new-stuff"
114 visible: enabled || visibleWhenDisabled
115 enabled: NewStuff.Settings.allowedByKiosk
116 onEnabledChanged: {
117 // If the user resets this when kiosk has disallowed ghns, force enabled back to false
118 if (enabled && !NewStuff.Settings.allowedByKiosk) {
119 enabled = false;
120 }
121 }
122
123 readonly property QtObject _private: QtObject {
124 property NewStuff.Engine engine: pageItem ? pageItem.engine : null
125 // Probably wants to be deleted and cleared if the "mode" changes at runtime...
126 property /* NewStuff.Dialog | NewStuff.Page */QtObject pageItem
127
128 readonly property Connections engineConnections: Connections {
129 target: component.engine
130
131 function onEntryEvent(entry, event) {
132 component.entryEvent(entry, event);
133 }
134 }
135
136 function showHotNewStuff() {
137 if (NewStuff.Settings.allowedByKiosk) {
138 if (component.pageStack !== null) {
139 if (component._private.pageItem // If we already have a page created...
140 && (component.pageStack.columnView !== undefined // first make sure that this pagestack is a Kirigami-style one (otherwise just assume we're ok)
141 && component.pageStack.columnView.contains(component._private.pageItem))) // and then check if the page is still in the stack before attempting to...
142 {
143 // ...set the already existing page as the current page
144 component.pageStack.currentItem = component._private.pageItem;
145 } else {
146 component._private.pageItem = newStuffPage.createObject(component);
147 component.pageStack.push(component._private.pageItem);
148 }
149 } else {
150 newStuffDialog.open();
151 }
152 } else {
153 // make some noise, because silently doing nothing is a bit annoying
154 }
155 }
156
157 property Component newStuffPage: Component {
158 NewStuff.Page {
159 configFile: component.configFile
160 viewMode: component.viewMode
161 }
162 }
163
164 property Item newStuffDialog: Loader {
165 // Use this function to open the dialog. It seems roundabout, but this ensures
166 // that the dialog is not constructed until we want it to be shown the first time,
167 // since it will initialise itself on the first load (which causes it to phone
168 // home) and we don't want that until the user explicitly asks for it.
169 function open() {
170 if (item) {
171 item.open();
172 } else {
173 active = true;
174 }
175 }
176
177 onLoaded: {
178 component._private.pageItem = item;
179 item.open();
180 }
181
182 active: false
183 asynchronous: true
184
185 sourceComponent: NewStuff.Dialog {
186 transientParent: component.transientParent
187 configFile: component.configFile
188 viewMode: component.viewMode
189 }
190 }
191 }
192}
KGuiItem open()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:56:35 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.