1 /*
2  * This file is part of d-dazzle.
3  *
4  * d-dazzle is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * d-dazzle is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with d-dazzle; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module dazzle.FuzzyIndex;
20 
21 private import dazzle.c.functions;
22 public  import dazzle.c.types;
23 private import gio.AsyncResultIF;
24 private import gio.Cancellable;
25 private import gio.FileIF;
26 private import gio.ListModelIF;
27 private import glib.ConstructionException;
28 private import glib.ErrorG;
29 private import glib.GException;
30 private import glib.Str;
31 private import glib.Variant;
32 private import gobject.ObjectG;
33 
34 
35 /** */
36 public class FuzzyIndex : ObjectG
37 {
38 	/** the main Gtk struct */
39 	protected DzlFuzzyIndex* dzlFuzzyIndex;
40 
41 	/** Get the main Gtk struct */
42 	public DzlFuzzyIndex* getFuzzyIndexStruct(bool transferOwnership = false)
43 	{
44 		if (transferOwnership)
45 			ownedRef = false;
46 		return dzlFuzzyIndex;
47 	}
48 
49 	/** the main Gtk struct as a void* */
50 	protected override void* getStruct()
51 	{
52 		return cast(void*)dzlFuzzyIndex;
53 	}
54 
55 	/**
56 	 * Sets our main struct and passes it to the parent class.
57 	 */
58 	public this (DzlFuzzyIndex* dzlFuzzyIndex, bool ownedRef = false)
59 	{
60 		this.dzlFuzzyIndex = dzlFuzzyIndex;
61 		super(cast(GObject*)dzlFuzzyIndex, ownedRef);
62 	}
63 
64 
65 	/** */
66 	public static GType getType()
67 	{
68 		return dzl_fuzzy_index_get_type();
69 	}
70 
71 	/** */
72 	public this()
73 	{
74 		auto p = dzl_fuzzy_index_new();
75 
76 		if(p is null)
77 		{
78 			throw new ConstructionException("null returned by new");
79 		}
80 
81 		this(cast(DzlFuzzyIndex*) p, true);
82 	}
83 
84 	/**
85 	 * Looks up the metadata for @key.
86 	 *
87 	 * Returns: A #GVariant or %NULL.
88 	 */
89 	public Variant getMetadata(string key)
90 	{
91 		auto p = dzl_fuzzy_index_get_metadata(dzlFuzzyIndex, Str.toStringz(key));
92 
93 		if(p is null)
94 		{
95 			return null;
96 		}
97 
98 		return new Variant(cast(GVariant*) p, true);
99 	}
100 
101 	/** */
102 	public string getMetadataString(string key)
103 	{
104 		return Str.toString(dzl_fuzzy_index_get_metadata_string(dzlFuzzyIndex, Str.toStringz(key)));
105 	}
106 
107 	/** */
108 	public uint getMetadataUint32(string key)
109 	{
110 		return dzl_fuzzy_index_get_metadata_uint32(dzlFuzzyIndex, Str.toStringz(key));
111 	}
112 
113 	/** */
114 	public ulong getMetadataUint64(string key)
115 	{
116 		return dzl_fuzzy_index_get_metadata_uint64(dzlFuzzyIndex, Str.toStringz(key));
117 	}
118 
119 	/** */
120 	public bool loadFile(FileIF file, Cancellable cancellable)
121 	{
122 		GError* err = null;
123 
124 		auto p = dzl_fuzzy_index_load_file(dzlFuzzyIndex, (file is null) ? null : file.getFileStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
125 
126 		if (err !is null)
127 		{
128 			throw new GException( new ErrorG(err) );
129 		}
130 
131 		return p;
132 	}
133 
134 	/** */
135 	public void loadFileAsync(FileIF file, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
136 	{
137 		dzl_fuzzy_index_load_file_async(dzlFuzzyIndex, (file is null) ? null : file.getFileStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
138 	}
139 
140 	/** */
141 	public bool loadFileFinish(AsyncResultIF result)
142 	{
143 		GError* err = null;
144 
145 		auto p = dzl_fuzzy_index_load_file_finish(dzlFuzzyIndex, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
146 
147 		if (err !is null)
148 		{
149 			throw new GException( new ErrorG(err) );
150 		}
151 
152 		return p;
153 	}
154 
155 	/** */
156 	public void queryAsync(string query, uint maxMatches, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
157 	{
158 		dzl_fuzzy_index_query_async(dzlFuzzyIndex, Str.toStringz(query), maxMatches, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
159 	}
160 
161 	/**
162 	 * Completes an asynchronous request to dzl_fuzzy_index_query_async().
163 	 *
164 	 * Returns: A #GListModel of results.
165 	 *
166 	 * Throws: GException on failure.
167 	 */
168 	public ListModelIF queryFinish(AsyncResultIF result)
169 	{
170 		GError* err = null;
171 
172 		auto p = dzl_fuzzy_index_query_finish(dzlFuzzyIndex, (result is null) ? null : result.getAsyncResultStruct(), &err);
173 
174 		if (err !is null)
175 		{
176 			throw new GException( new ErrorG(err) );
177 		}
178 
179 		if(p is null)
180 		{
181 			return null;
182 		}
183 
184 		return ObjectG.getDObject!(ListModelIF)(cast(GListModel*) p, true);
185 	}
186 }