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.FuzzyIndexBuilder;
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 glib.ConstructionException;
27 private import glib.ErrorG;
28 private import glib.GException;
29 private import glib.Str;
30 private import glib.Variant;
31 private import gobject.ObjectG;
32 
33 
34 /** */
35 public class FuzzyIndexBuilder : ObjectG
36 {
37 	/** the main Gtk struct */
38 	protected DzlFuzzyIndexBuilder* dzlFuzzyIndexBuilder;
39 
40 	/** Get the main Gtk struct */
41 	public DzlFuzzyIndexBuilder* getFuzzyIndexBuilderStruct(bool transferOwnership = false)
42 	{
43 		if (transferOwnership)
44 			ownedRef = false;
45 		return dzlFuzzyIndexBuilder;
46 	}
47 
48 	/** the main Gtk struct as a void* */
49 	protected override void* getStruct()
50 	{
51 		return cast(void*)dzlFuzzyIndexBuilder;
52 	}
53 
54 	/**
55 	 * Sets our main struct and passes it to the parent class.
56 	 */
57 	public this (DzlFuzzyIndexBuilder* dzlFuzzyIndexBuilder, bool ownedRef = false)
58 	{
59 		this.dzlFuzzyIndexBuilder = dzlFuzzyIndexBuilder;
60 		super(cast(GObject*)dzlFuzzyIndexBuilder, ownedRef);
61 	}
62 
63 
64 	/** */
65 	public static GType getType()
66 	{
67 		return dzl_fuzzy_index_builder_get_type();
68 	}
69 
70 	/** */
71 	public this()
72 	{
73 		auto p = dzl_fuzzy_index_builder_new();
74 
75 		if(p is null)
76 		{
77 			throw new ConstructionException("null returned by new");
78 		}
79 
80 		this(cast(DzlFuzzyIndexBuilder*) p, true);
81 	}
82 
83 	/** */
84 	public bool getCaseSensitive()
85 	{
86 		return dzl_fuzzy_index_builder_get_case_sensitive(dzlFuzzyIndexBuilder) != 0;
87 	}
88 
89 	/**
90 	 * Returns the document that was inserted in a previous call to
91 	 * dzl_fuzzy_index_builder_insert().
92 	 *
93 	 * Returns: A #GVariant
94 	 */
95 	public Variant getDocument(ulong documentId)
96 	{
97 		auto p = dzl_fuzzy_index_builder_get_document(dzlFuzzyIndexBuilder, documentId);
98 
99 		if(p is null)
100 		{
101 			return null;
102 		}
103 
104 		return new Variant(cast(GVariant*) p);
105 	}
106 
107 	/**
108 	 * Inserts @document into the index using @key as the lookup key.
109 	 *
110 	 * If a matching document (checked by hashing @document) has already
111 	 * been inserted, only a single instance of the document will be stored.
112 	 *
113 	 * If @document is floating, it will be consumed.
114 	 *
115 	 * @priority may be used to group results by priority. Priority must be
116 	 * less than 256.
117 	 *
118 	 * Params:
119 	 *     key = The UTF-8 encoded key for the document
120 	 *     document = The document to store
121 	 *     priority = An optional priority for the keyword.
122 	 *
123 	 * Returns: The document id registered for @document.
124 	 */
125 	public ulong insert(string key, Variant document, uint priority)
126 	{
127 		return dzl_fuzzy_index_builder_insert(dzlFuzzyIndexBuilder, Str.toStringz(key), (document is null) ? null : document.getVariantStruct(), priority);
128 	}
129 
130 	/** */
131 	public void setCaseSensitive(bool caseSensitive)
132 	{
133 		dzl_fuzzy_index_builder_set_case_sensitive(dzlFuzzyIndexBuilder, caseSensitive);
134 	}
135 
136 	/** */
137 	public void setMetadata(string key, Variant value)
138 	{
139 		dzl_fuzzy_index_builder_set_metadata(dzlFuzzyIndexBuilder, Str.toStringz(key), (value is null) ? null : value.getVariantStruct());
140 	}
141 
142 	/** */
143 	public void setMetadataString(string key, string value)
144 	{
145 		dzl_fuzzy_index_builder_set_metadata_string(dzlFuzzyIndexBuilder, Str.toStringz(key), Str.toStringz(value));
146 	}
147 
148 	/** */
149 	public void setMetadataUint32(string key, uint value)
150 	{
151 		dzl_fuzzy_index_builder_set_metadata_uint32(dzlFuzzyIndexBuilder, Str.toStringz(key), value);
152 	}
153 
154 	/** */
155 	public void setMetadataUint64(string key, ulong value)
156 	{
157 		dzl_fuzzy_index_builder_set_metadata_uint64(dzlFuzzyIndexBuilder, Str.toStringz(key), value);
158 	}
159 
160 	/** */
161 	public bool write(FileIF file, int ioPriority, Cancellable cancellable)
162 	{
163 		GError* err = null;
164 
165 		auto p = dzl_fuzzy_index_builder_write(dzlFuzzyIndexBuilder, (file is null) ? null : file.getFileStruct(), ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
166 
167 		if (err !is null)
168 		{
169 			throw new GException( new ErrorG(err) );
170 		}
171 
172 		return p;
173 	}
174 
175 	/**
176 	 * Builds and writes the index to @file. The file format is a
177 	 * GVariant on disk and can be loaded and searched using
178 	 * #FuzzyIndex.
179 	 *
180 	 * Params:
181 	 *     file = A #GFile to write the index to
182 	 *     ioPriority = The priority for IO operations
183 	 *     cancellable = An optional #GCancellable or %NULL
184 	 *     callback = A callback for completion or %NULL
185 	 *     userData = User data for @callback
186 	 */
187 	public void writeAsync(FileIF file, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
188 	{
189 		dzl_fuzzy_index_builder_write_async(dzlFuzzyIndexBuilder, (file is null) ? null : file.getFileStruct(), ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
190 	}
191 
192 	/** */
193 	public bool writeFinish(AsyncResultIF result)
194 	{
195 		GError* err = null;
196 
197 		auto p = dzl_fuzzy_index_builder_write_finish(dzlFuzzyIndexBuilder, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
198 
199 		if (err !is null)
200 		{
201 			throw new GException( new ErrorG(err) );
202 		}
203 
204 		return p;
205 	}
206 }