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
42import org.kde.newstuff.private as NewStuffPrivate
43
44Kirigami.Action {
45 id: component
46
47 /*
48 * The configuration file is not aliased, because then we end up initialising the
49 * Engine immediately the Action is instantiated, which we want to avoid (as that
50 * is effectively a phone-home scenario, and causes internet traffic in situations
51 * where it would not seem likely that there should be any).
52 * If we want, in the future, to add some status display to the Action (such as "there
53 * are updates to be had" or somesuch, then we can do this, but until that choice is
54 * made, let's not)
55 */
56 /**
57 * The configuration file to use for the Page created by this action
58 */
59 property string configFile
60
61 /**
62 * The view mode of the page spawned by this action, which overrides the
63 * default one (ViewMode.Tiles). This should be set using the
64 * NewStuff.Page.ViewMode enum. Note that ViewMode.Icons has been removed,
65 * and asking for it will return ViewMode.Tiles.
66 * @see NewStuff.Page.ViewMode
67 */
68 property int viewMode: NewStuff.Page.ViewMode.Tiles
69
70 /**
71 * If this is set, the action will push a NewStuff.Page onto this page stack
72 * (and request it is made visible if triggered again). If you do not set this
73 * property, the action will spawn a NewStuff.Dialog instead.
74 * @note If you are building a KCM, set this to your ```kcm``` object.
75 */
76 property Item pageStack
77
78 /**
79 * The engine which handles the content in this Action
80 * This will be null until the action has been triggered the first time
81 */
82 readonly property NewStuff.Engine engine: component._private.engine
83
84 /**
85 * This forwards the entry changed event from the QtQuick engine
86 * @see Engine::entryEvent
87 */
88 signal entryEvent(var entry, int event)
89
90 /**
91 * If this is true (default is false), the action will be shown when the Kiosk settings are such
92 * that Get Hot New Stuff is disallowed (and any other time enabled is set to false).
93 * Usually you would want to leave this alone, but occasionally you may have a reason to
94 * leave a action in place that the user is unable to enable.
95 */
96 property bool visibleWhenDisabled: false
97
98 /**
99 * The parent window for the dialog created by invoking the action
100 *
101 * @since 6.1
102 */
103 // TODO KF7: make this required. without it we have a hard time doing complex window management in systemsettings
104 property Window transientParent
105
106 /**
107 * Show the page/dialog (same as activating the action), if allowed by the Kiosk settings
108 */
109 function showHotNewStuff() {
110 component._private.showHotNewStuff();
111 }
112
113 onTriggered: showHotNewStuff()
114
115 icon.name: "get-hot-new-stuff"
116 visible: enabled || visibleWhenDisabled
117 enabled: NewStuff.Settings.allowedByKiosk
118 onEnabledChanged: {
119 // If the user resets this when kiosk has disallowed ghns, force enabled back to false
120 if (enabled && !NewStuff.Settings.allowedByKiosk) {
121 enabled = false;
122 }
123 }
124
125 readonly property QtObject _private: QtObject {
126 property NewStuff.Engine engine: pageItem ? pageItem.engine : null
127 // Probably wants to be deleted and cleared if the "mode" changes at runtime...
128 property /* NewStuff.Dialog | NewStuff.Page */QtObject pageItem
129
130 readonly property Connections engineConnections: Connections {
131 target: component.engine
132
133 function onEntryEvent(entry, event) {
134 component.entryEvent(entry, event);
135 }
136 }
137
138 function showHotNewStuff() {
139 if (NewStuff.Settings.allowedByKiosk) {
140 if (component.pageStack !== null) {
141 if (component._private.pageItem // If we already have a page created...
142 && (component.pageStack.columnView !== undefined // first make sure that this pagestack is a Kirigami-style one (otherwise just assume we're ok)
143 && component.pageStack.columnView.contains(component._private.pageItem))) // and then check if the page is still in the stack before attempting to...
144 {
145 // ...set the already existing page as the current page
146 component.pageStack.currentItem = component._private.pageItem;
147 } else {
148 component._private.pageItem = newStuffPage.createObject(component);
149 component.pageStack.push(component._private.pageItem);
150 }
151 } else {
152 newStuffDialog.open();
153 }
154 } else {
155 // make some noise, because silently doing nothing is a bit annoying
156 }
157 }
158
159 property Component newStuffPage: Component {
160 NewStuff.Page {
161 configFile: component.configFile
162 viewMode: component.viewMode
163 }
164 }
165
166 property Item newStuffDialog: Loader {
167 id: dialogLoader
168 // Use this function to open the dialog. It seems roundabout, but this ensures
169 // that the dialog is not constructed until we want it to be shown the first time,
170 // since it will initialise itself on the first load (which causes it to phone
171 // home) and we don't want that until the user explicitly asks for it.
172 function open() {
173 if (item) {
174 item.open();
175 } else {
176 active = true;
177 }
178 }
179
180 onLoaded: {
181 component._private.pageItem = item;
182 item.open();
183 }
184
185 active: false
186 asynchronous: true
187
188 sourceComponent: NewStuff.Dialog {
189 transientParent: component.transientParent
190 configFile: component.configFile
191 viewMode: component.viewMode
192 onClosing: {
193 // Unload the dialog when it is closed otherwise it gets stuck in memory because of the weird
194 // constructs we have in play here between nested objects and loaders and what not.
195 // Since the dialog is a top level window it would then prevent the QApplication from quitting.
196 dialogLoader.active = false
197 component._private.pageItem = null
198 }
199 NewStuffPrivate.TransientMagicianAssistant {}
200 }
201 }
202 }
203}
const QList< QKeySequence > & open()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:20:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.