1 module webkit2.UserContentFilterStore;
2 
3 private import gio.AsyncResultIF;
4 private import gio.Cancellable;
5 private import gio.FileIF;
6 private import glib.Bytes;
7 private import glib.ConstructionException;
8 private import glib.ErrorG;
9 private import glib.GException;
10 private import glib.Str;
11 private import gobject.ObjectG;
12 private import webkit2.UserContentFilter;
13 private import webkit2.c.functions;
14 public  import webkit2.c.types;
15 
16 
17 /**
18  * The WebKitUserContentFilterStore provides the means to import and save
19  * [JSON rule sets](https://webkit.org/blog/3476/content-blockers-first-look/),
20  * which can be loaded later in an efficient manner. Once filters are stored,
21  * the #WebKitUserContentFilter objects which represent them can be added to
22  * a #WebKitUserContentManager with webkit_user_content_manager_add_filter().
23  * 
24  * JSON rule sets are imported using webkit_user_content_filter_store_save() and stored
25  * on disk in an implementation defined format. The contents of a filter store must be
26  * managed using the #WebKitUserContentFilterStore: a list of all the stored filters
27  * can be obtained with webkit_user_content_filter_store_fetch_identifiers(),
28  * webkit_user_content_filter_store_load() can be used to retrieve a previously saved
29  * filter, and removed from the store with webkit_user_content_filter_store_remove().
30  *
31  * Since: 2.24
32  */
33 public class UserContentFilterStore : ObjectG
34 {
35 	/** the main Gtk struct */
36 	protected WebKitUserContentFilterStore* webKitUserContentFilterStore;
37 
38 	/** Get the main Gtk struct */
39 	public WebKitUserContentFilterStore* getUserContentFilterStoreStruct(bool transferOwnership = false)
40 	{
41 		if (transferOwnership)
42 			ownedRef = false;
43 		return webKitUserContentFilterStore;
44 	}
45 
46 	/** the main Gtk struct as a void* */
47 	protected override void* getStruct()
48 	{
49 		return cast(void*)webKitUserContentFilterStore;
50 	}
51 
52 	/**
53 	 * Sets our main struct and passes it to the parent class.
54 	 */
55 	public this (WebKitUserContentFilterStore* webKitUserContentFilterStore, bool ownedRef = false)
56 	{
57 		this.webKitUserContentFilterStore = webKitUserContentFilterStore;
58 		super(cast(GObject*)webKitUserContentFilterStore, ownedRef);
59 	}
60 
61 
62 	/** */
63 	public static GType getType()
64 	{
65 		return webkit_user_content_filter_store_get_type();
66 	}
67 
68 	/**
69 	 * Create a new #WebKitUserContentFilterStore to manipulate filters stored at @storage_path.
70 	 * The path must point to a local filesystem, and will be created if needed.
71 	 *
72 	 * Params:
73 	 *     storagePath = path where data for filters will be stored on disk
74 	 *
75 	 * Returns: a newly created #WebKitUserContentFilterStore
76 	 *
77 	 * Since: 2.24
78 	 *
79 	 * Throws: ConstructionException GTK+ fails to create the object.
80 	 */
81 	public this(string storagePath)
82 	{
83 		auto __p = webkit_user_content_filter_store_new(Str.toStringz(storagePath));
84 
85 		if(__p is null)
86 		{
87 			throw new ConstructionException("null returned by new");
88 		}
89 
90 		this(cast(WebKitUserContentFilterStore*) __p, true);
91 	}
92 
93 	/**
94 	 * Asynchronously retrieve a list of the identifiers for all the stored filters.
95 	 *
96 	 * When the operation is finished, @callback will be invoked, which then can use
97 	 * webkit_user_content_filter_store_fetch_identifiers_finish() to obtain the list of
98 	 * filter identifiers.
99 	 *
100 	 * Params:
101 	 *     cancellable = a #GCancellable or %NULL to ignore
102 	 *     callback = a #GAsyncReadyCallback to call when the removal is completed
103 	 *     userData = the data to pass to the callback function
104 	 *
105 	 * Since: 2.24
106 	 */
107 	public void fetchIdentifiers(Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
108 	{
109 		webkit_user_content_filter_store_fetch_identifiers(webKitUserContentFilterStore, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
110 	}
111 
112 	/**
113 	 * Finishes an asynchronous fetch of the list of identifiers for the stored filters previously
114 	 * started with webkit_user_content_filter_store_fetch_identifiers().
115 	 *
116 	 * Params:
117 	 *     result = a #GAsyncResult
118 	 *
119 	 * Returns: a %NULL-terminated list of filter identifiers.
120 	 *
121 	 * Since: 2.24
122 	 */
123 	public string[] fetchIdentifiersFinish(AsyncResultIF result)
124 	{
125 		auto retStr = webkit_user_content_filter_store_fetch_identifiers_finish(webKitUserContentFilterStore, (result is null) ? null : result.getAsyncResultStruct());
126 
127 		scope(exit) Str.freeStringArray(retStr);
128 		return Str.toStringArray(retStr);
129 	}
130 
131 	/**
132 	 * Returns: The storage path for user content filters.
133 	 *
134 	 * Since: 2.24
135 	 */
136 	public string getPath()
137 	{
138 		return Str.toString(webkit_user_content_filter_store_get_path(webKitUserContentFilterStore));
139 	}
140 
141 	/**
142 	 * Asynchronously load a content filter given its @identifier. The filter must have been
143 	 * previously stored using webkit_user_content_filter_store_save().
144 	 *
145 	 * When the operation is finished, @callback will be invoked, which then can use
146 	 * webkit_user_content_filter_store_load_finish() to obtain the resulting filter.
147 	 *
148 	 * Params:
149 	 *     identifier = a filter identifier
150 	 *     cancellable = a #GCancellable or %NULL to ignore
151 	 *     callback = a #GAsyncReadyCallback to call when the load is completed
152 	 *     userData = the data to pass to the callback function
153 	 *
154 	 * Since: 2.24
155 	 */
156 	public void load(string identifier, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
157 	{
158 		webkit_user_content_filter_store_load(webKitUserContentFilterStore, Str.toStringz(identifier), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
159 	}
160 
161 	/**
162 	 * Finishes an asynchronous filter load previously started with
163 	 * webkit_user_content_filter_store_load().
164 	 *
165 	 * Params:
166 	 *     result = a #GAsyncResult
167 	 *
168 	 * Returns: a #WebKitUserContentFilter, or %NULL if the load failed
169 	 *
170 	 * Since: 2.24
171 	 *
172 	 * Throws: GException on failure.
173 	 */
174 	public UserContentFilter loadFinish(AsyncResultIF result)
175 	{
176 		GError* err = null;
177 
178 		auto __p = webkit_user_content_filter_store_load_finish(webKitUserContentFilterStore, (result is null) ? null : result.getAsyncResultStruct(), &err);
179 
180 		if (err !is null)
181 		{
182 			throw new GException( new ErrorG(err) );
183 		}
184 
185 		if(__p is null)
186 		{
187 			return null;
188 		}
189 
190 		return ObjectG.getDObject!(UserContentFilter)(cast(WebKitUserContentFilter*) __p, true);
191 	}
192 
193 	/**
194 	 * Asynchronously remove a content filter given its @identifier.
195 	 *
196 	 * When the operation is finished, @callback will be invoked, which then can use
197 	 * webkit_user_content_filter_store_remove_finish() to check whether the removal was
198 	 * successful.
199 	 *
200 	 * Params:
201 	 *     identifier = a filter identifier
202 	 *     cancellable = a #GCancellable or %NULL to ignore
203 	 *     callback = a #GAsyncReadyCallback to call when the removal is completed
204 	 *     userData = the data to pass to the callback function
205 	 *
206 	 * Since: 2.24
207 	 */
208 	public void remove(string identifier, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
209 	{
210 		webkit_user_content_filter_store_remove(webKitUserContentFilterStore, Str.toStringz(identifier), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
211 	}
212 
213 	/**
214 	 * Finishes an asynchronous filter removal previously started with
215 	 * webkit_user_content_filter_store_remove().
216 	 *
217 	 * Params:
218 	 *     result = a #GAsyncResult
219 	 *
220 	 * Returns: whether the removal was successful
221 	 *
222 	 * Since: 2.24
223 	 *
224 	 * Throws: GException on failure.
225 	 */
226 	public bool removeFinish(AsyncResultIF result)
227 	{
228 		GError* err = null;
229 
230 		auto __p = webkit_user_content_filter_store_remove_finish(webKitUserContentFilterStore, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
231 
232 		if (err !is null)
233 		{
234 			throw new GException( new ErrorG(err) );
235 		}
236 
237 		return __p;
238 	}
239 
240 	/**
241 	 * Asynchronously save a content filter from a source rule set in the
242 	 * [WebKit content extesions JSON format](https://webkit.org/blog/3476/content-blockers-first-look/).
243 	 *
244 	 * The @identifier can be used afterwards to refer to the filter when using
245 	 * webkit_user_content_filter_store_remove() and webkit_user_content_filter_store_load().
246 	 * When the @identifier has been used in the past, the new filter source will replace
247 	 * the one saved beforehand for the same identifier.
248 	 *
249 	 * When the operation is finished, @callback will be invoked, which then can use
250 	 * webkit_user_content_filter_store_save_finish() to obtain the resulting filter.
251 	 *
252 	 * Params:
253 	 *     identifier = a string used to identify the saved filter
254 	 *     source = #GBytes containing the rule set in JSON format
255 	 *     cancellable = a #GCancellable or %NULL to ignore
256 	 *     callback = a #GAsyncReadyCallback to call when saving is completed
257 	 *     userData = the data to pass to the callback function
258 	 *
259 	 * Since: 2.24
260 	 */
261 	public void save(string identifier, Bytes source, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
262 	{
263 		webkit_user_content_filter_store_save(webKitUserContentFilterStore, Str.toStringz(identifier), (source is null) ? null : source.getBytesStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
264 	}
265 
266 	/**
267 	 * Finishes an asynchronous filter save previously started with
268 	 * webkit_user_content_filter_store_save().
269 	 *
270 	 * Params:
271 	 *     result = a #GAsyncResult
272 	 *
273 	 * Returns: a #WebKitUserContentFilter, or %NULL if saving failed
274 	 *
275 	 * Since: 2.24
276 	 *
277 	 * Throws: GException on failure.
278 	 */
279 	public UserContentFilter saveFinish(AsyncResultIF result)
280 	{
281 		GError* err = null;
282 
283 		auto __p = webkit_user_content_filter_store_save_finish(webKitUserContentFilterStore, (result is null) ? null : result.getAsyncResultStruct(), &err);
284 
285 		if (err !is null)
286 		{
287 			throw new GException( new ErrorG(err) );
288 		}
289 
290 		if(__p is null)
291 		{
292 			return null;
293 		}
294 
295 		return ObjectG.getDObject!(UserContentFilter)(cast(WebKitUserContentFilter*) __p, true);
296 	}
297 
298 	/**
299 	 * Asynchronously save a content filter from the contents of a file, which must be
300 	 * native to the platform, as checked by g_file_is_native(). See
301 	 * webkit_user_content_filter_store_save() for more details.
302 	 *
303 	 * When the operation is finished, @callback will be invoked, which then can use
304 	 * webkit_user_content_filter_store_save_finish() to obtain the resulting filter.
305 	 *
306 	 * Params:
307 	 *     identifier = a string used to identify the saved filter
308 	 *     file = a #GFile containing the rule set in JSON format
309 	 *     cancellable = a #GCancellable or %NULL to ignore
310 	 *     callback = a #GAsyncReadyCallback to call when saving is completed
311 	 *     userData = the data to pass to the callback function
312 	 *
313 	 * Since: 2.24
314 	 */
315 	public void saveFromFile(string identifier, FileIF file, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
316 	{
317 		webkit_user_content_filter_store_save_from_file(webKitUserContentFilterStore, Str.toStringz(identifier), (file is null) ? null : file.getFileStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
318 	}
319 
320 	/**
321 	 * Finishes and asynchronous filter save previously started with
322 	 * webkit_user_content_filter_store_save_from_file().
323 	 *
324 	 * Params:
325 	 *     result = a #GAsyncResult
326 	 *
327 	 * Returns: a #WebKitUserContentFilter, or %NULL if saving failed.
328 	 *
329 	 * Since: 2.24
330 	 *
331 	 * Throws: GException on failure.
332 	 */
333 	public UserContentFilter saveFromFileFinish(AsyncResultIF result)
334 	{
335 		GError* err = null;
336 
337 		auto __p = webkit_user_content_filter_store_save_from_file_finish(webKitUserContentFilterStore, (result is null) ? null : result.getAsyncResultStruct(), &err);
338 
339 		if (err !is null)
340 		{
341 			throw new GException( new ErrorG(err) );
342 		}
343 
344 		if(__p is null)
345 		{
346 			return null;
347 		}
348 
349 		return ObjectG.getDObject!(UserContentFilter)(cast(WebKitUserContentFilter*) __p, true);
350 	}
351 }