1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is autocomplete code.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Google Inc.
18 : * Portions created by the Initial Developer are Copyright (C) 2005
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Brett Wilson <brettw@gmail.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsAutoCompleteSimpleResult.h"
39 :
40 5579 : NS_IMPL_ISUPPORTS2(nsAutoCompleteSimpleResult,
41 : nsIAutoCompleteResult,
42 : nsIAutoCompleteSimpleResult)
43 :
44 218 : nsAutoCompleteSimpleResult::nsAutoCompleteSimpleResult() :
45 : mDefaultIndex(-1),
46 : mSearchResult(RESULT_NOMATCH),
47 218 : mTypeAheadResult(false)
48 : {
49 218 : }
50 :
51 : // searchString
52 : NS_IMETHODIMP
53 0 : nsAutoCompleteSimpleResult::GetSearchString(nsAString &aSearchString)
54 : {
55 0 : aSearchString = mSearchString;
56 0 : return NS_OK;
57 : }
58 : NS_IMETHODIMP
59 217 : nsAutoCompleteSimpleResult::SetSearchString(const nsAString &aSearchString)
60 : {
61 217 : mSearchString.Assign(aSearchString);
62 217 : return NS_OK;
63 : }
64 :
65 : // searchResult
66 : NS_IMETHODIMP
67 1857 : nsAutoCompleteSimpleResult::GetSearchResult(PRUint16 *aSearchResult)
68 : {
69 1857 : *aSearchResult = mSearchResult;
70 1857 : return NS_OK;
71 : }
72 : NS_IMETHODIMP
73 395 : nsAutoCompleteSimpleResult::SetSearchResult(PRUint16 aSearchResult)
74 : {
75 395 : mSearchResult = aSearchResult;
76 395 : return NS_OK;
77 : }
78 :
79 : // defaultIndex
80 : NS_IMETHODIMP
81 29 : nsAutoCompleteSimpleResult::GetDefaultIndex(PRInt32 *aDefaultIndex)
82 : {
83 29 : *aDefaultIndex = mDefaultIndex;
84 29 : return NS_OK;
85 : }
86 : NS_IMETHODIMP
87 27 : nsAutoCompleteSimpleResult::SetDefaultIndex(PRInt32 aDefaultIndex)
88 : {
89 27 : mDefaultIndex = aDefaultIndex;
90 27 : return NS_OK;
91 : }
92 :
93 : // errorDescription
94 : NS_IMETHODIMP
95 0 : nsAutoCompleteSimpleResult::GetErrorDescription(nsAString & aErrorDescription)
96 : {
97 0 : aErrorDescription = mErrorDescription;
98 0 : return NS_OK;
99 : }
100 : NS_IMETHODIMP
101 0 : nsAutoCompleteSimpleResult::SetErrorDescription(
102 : const nsAString &aErrorDescription)
103 : {
104 0 : mErrorDescription.Assign(aErrorDescription);
105 0 : return NS_OK;
106 : }
107 :
108 : // typeAheadResult
109 : NS_IMETHODIMP
110 1343 : nsAutoCompleteSimpleResult::GetTypeAheadResult(bool *aTypeAheadResult)
111 : {
112 1343 : *aTypeAheadResult = mTypeAheadResult;
113 1343 : return NS_OK;
114 : }
115 : NS_IMETHODIMP
116 27 : nsAutoCompleteSimpleResult::SetTypeAheadResult(bool aTypeAheadResult)
117 : {
118 27 : mTypeAheadResult = aTypeAheadResult;
119 27 : return NS_OK;
120 : }
121 :
122 : NS_IMETHODIMP
123 490 : nsAutoCompleteSimpleResult::AppendMatch(const nsAString& aValue,
124 : const nsAString& aComment,
125 : const nsAString& aImage,
126 : const nsAString& aStyle)
127 : {
128 490 : CheckInvariants();
129 :
130 490 : if (! mValues.AppendElement(aValue))
131 0 : return NS_ERROR_OUT_OF_MEMORY;
132 490 : if (! mComments.AppendElement(aComment)) {
133 0 : mValues.RemoveElementAt(mValues.Length() - 1);
134 0 : return NS_ERROR_OUT_OF_MEMORY;
135 : }
136 490 : if (! mImages.AppendElement(aImage)) {
137 0 : mValues.RemoveElementAt(mValues.Length() - 1);
138 0 : mComments.RemoveElementAt(mComments.Length() - 1);
139 0 : return NS_ERROR_OUT_OF_MEMORY;
140 : }
141 490 : if (! mStyles.AppendElement(aStyle)) {
142 0 : mValues.RemoveElementAt(mValues.Length() - 1);
143 0 : mComments.RemoveElementAt(mComments.Length() - 1);
144 0 : mImages.RemoveElementAt(mImages.Length() - 1);
145 0 : return NS_ERROR_OUT_OF_MEMORY;
146 : }
147 490 : return NS_OK;
148 : }
149 :
150 : NS_IMETHODIMP
151 3024 : nsAutoCompleteSimpleResult::GetMatchCount(PRUint32 *aMatchCount)
152 : {
153 3024 : CheckInvariants();
154 :
155 3024 : *aMatchCount = mValues.Length();
156 3024 : return NS_OK;
157 : }
158 :
159 : NS_IMETHODIMP
160 543 : nsAutoCompleteSimpleResult::GetValueAt(PRInt32 aIndex, nsAString& _retval)
161 : {
162 543 : NS_ENSURE_TRUE(aIndex >= 0 && aIndex < PRInt32(mValues.Length()),
163 : NS_ERROR_ILLEGAL_VALUE);
164 543 : CheckInvariants();
165 :
166 543 : _retval = mValues[aIndex];
167 543 : return NS_OK;
168 : }
169 :
170 : NS_IMETHODIMP
171 514 : nsAutoCompleteSimpleResult::GetLabelAt(PRInt32 aIndex, nsAString& _retval)
172 : {
173 514 : return GetValueAt(aIndex, _retval);
174 : }
175 :
176 : NS_IMETHODIMP
177 350 : nsAutoCompleteSimpleResult::GetCommentAt(PRInt32 aIndex, nsAString& _retval)
178 : {
179 350 : NS_ENSURE_TRUE(aIndex >= 0 && aIndex < PRInt32(mComments.Length()),
180 : NS_ERROR_ILLEGAL_VALUE);
181 350 : CheckInvariants();
182 350 : _retval = mComments[aIndex];
183 350 : return NS_OK;
184 : }
185 :
186 : NS_IMETHODIMP
187 0 : nsAutoCompleteSimpleResult::GetImageAt(PRInt32 aIndex, nsAString& _retval)
188 : {
189 0 : NS_ENSURE_TRUE(aIndex >= 0 && aIndex < PRInt32(mImages.Length()),
190 : NS_ERROR_ILLEGAL_VALUE);
191 0 : CheckInvariants();
192 0 : _retval = mImages[aIndex];
193 0 : return NS_OK;
194 : }
195 :
196 : NS_IMETHODIMP
197 84 : nsAutoCompleteSimpleResult::GetStyleAt(PRInt32 aIndex, nsAString& _retval)
198 : {
199 84 : NS_ENSURE_TRUE(aIndex >= 0 && aIndex < PRInt32(mStyles.Length()),
200 : NS_ERROR_ILLEGAL_VALUE);
201 84 : CheckInvariants();
202 84 : _retval = mStyles[aIndex];
203 84 : return NS_OK;
204 : }
205 :
206 : NS_IMETHODIMP
207 192 : nsAutoCompleteSimpleResult::SetListener(nsIAutoCompleteSimpleResultListener* aListener)
208 : {
209 192 : mListener = aListener;
210 192 : return NS_OK;
211 : }
212 :
213 : NS_IMETHODIMP
214 3 : nsAutoCompleteSimpleResult::RemoveValueAt(PRInt32 aRowIndex,
215 : bool aRemoveFromDb)
216 : {
217 3 : NS_ENSURE_TRUE(aRowIndex >= 0 && aRowIndex < PRInt32(mValues.Length()),
218 : NS_ERROR_ILLEGAL_VALUE);
219 :
220 6 : nsAutoString removedValue(mValues[aRowIndex]);
221 3 : mValues.RemoveElementAt(aRowIndex);
222 3 : mComments.RemoveElementAt(aRowIndex);
223 3 : mImages.RemoveElementAt(aRowIndex);
224 3 : mStyles.RemoveElementAt(aRowIndex);
225 :
226 3 : if (mListener)
227 2 : mListener->OnValueRemoved(this, removedValue, aRemoveFromDb);
228 :
229 3 : return NS_OK;
230 : }
|