|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "nsAutoCompleteSimpleResult.h" |
|
6 |
|
7 NS_IMPL_ISUPPORTS(nsAutoCompleteSimpleResult, |
|
8 nsIAutoCompleteResult, |
|
9 nsIAutoCompleteSimpleResult) |
|
10 |
|
11 nsAutoCompleteSimpleResult::nsAutoCompleteSimpleResult() : |
|
12 mDefaultIndex(-1), |
|
13 mSearchResult(RESULT_NOMATCH), |
|
14 mTypeAheadResult(false) |
|
15 { |
|
16 } |
|
17 |
|
18 // searchString |
|
19 NS_IMETHODIMP |
|
20 nsAutoCompleteSimpleResult::GetSearchString(nsAString &aSearchString) |
|
21 { |
|
22 aSearchString = mSearchString; |
|
23 return NS_OK; |
|
24 } |
|
25 NS_IMETHODIMP |
|
26 nsAutoCompleteSimpleResult::SetSearchString(const nsAString &aSearchString) |
|
27 { |
|
28 mSearchString.Assign(aSearchString); |
|
29 return NS_OK; |
|
30 } |
|
31 |
|
32 // searchResult |
|
33 NS_IMETHODIMP |
|
34 nsAutoCompleteSimpleResult::GetSearchResult(uint16_t *aSearchResult) |
|
35 { |
|
36 *aSearchResult = mSearchResult; |
|
37 return NS_OK; |
|
38 } |
|
39 NS_IMETHODIMP |
|
40 nsAutoCompleteSimpleResult::SetSearchResult(uint16_t aSearchResult) |
|
41 { |
|
42 mSearchResult = aSearchResult; |
|
43 return NS_OK; |
|
44 } |
|
45 |
|
46 // defaultIndex |
|
47 NS_IMETHODIMP |
|
48 nsAutoCompleteSimpleResult::GetDefaultIndex(int32_t *aDefaultIndex) |
|
49 { |
|
50 *aDefaultIndex = mDefaultIndex; |
|
51 return NS_OK; |
|
52 } |
|
53 NS_IMETHODIMP |
|
54 nsAutoCompleteSimpleResult::SetDefaultIndex(int32_t aDefaultIndex) |
|
55 { |
|
56 mDefaultIndex = aDefaultIndex; |
|
57 return NS_OK; |
|
58 } |
|
59 |
|
60 // errorDescription |
|
61 NS_IMETHODIMP |
|
62 nsAutoCompleteSimpleResult::GetErrorDescription(nsAString & aErrorDescription) |
|
63 { |
|
64 aErrorDescription = mErrorDescription; |
|
65 return NS_OK; |
|
66 } |
|
67 NS_IMETHODIMP |
|
68 nsAutoCompleteSimpleResult::SetErrorDescription( |
|
69 const nsAString &aErrorDescription) |
|
70 { |
|
71 mErrorDescription.Assign(aErrorDescription); |
|
72 return NS_OK; |
|
73 } |
|
74 |
|
75 // typeAheadResult |
|
76 NS_IMETHODIMP |
|
77 nsAutoCompleteSimpleResult::GetTypeAheadResult(bool *aTypeAheadResult) |
|
78 { |
|
79 *aTypeAheadResult = mTypeAheadResult; |
|
80 return NS_OK; |
|
81 } |
|
82 NS_IMETHODIMP |
|
83 nsAutoCompleteSimpleResult::SetTypeAheadResult(bool aTypeAheadResult) |
|
84 { |
|
85 mTypeAheadResult = aTypeAheadResult; |
|
86 return NS_OK; |
|
87 } |
|
88 |
|
89 NS_IMETHODIMP |
|
90 nsAutoCompleteSimpleResult::AppendMatch(const nsAString& aValue, |
|
91 const nsAString& aComment, |
|
92 const nsAString& aImage, |
|
93 const nsAString& aStyle, |
|
94 const nsAString& aFinalCompleteValue) |
|
95 { |
|
96 CheckInvariants(); |
|
97 |
|
98 if (! mValues.AppendElement(aValue)) |
|
99 return NS_ERROR_OUT_OF_MEMORY; |
|
100 if (! mComments.AppendElement(aComment)) { |
|
101 mValues.RemoveElementAt(mValues.Length() - 1); |
|
102 return NS_ERROR_OUT_OF_MEMORY; |
|
103 } |
|
104 if (! mImages.AppendElement(aImage)) { |
|
105 mValues.RemoveElementAt(mValues.Length() - 1); |
|
106 mComments.RemoveElementAt(mComments.Length() - 1); |
|
107 return NS_ERROR_OUT_OF_MEMORY; |
|
108 } |
|
109 if (! mStyles.AppendElement(aStyle)) { |
|
110 mValues.RemoveElementAt(mValues.Length() - 1); |
|
111 mComments.RemoveElementAt(mComments.Length() - 1); |
|
112 mImages.RemoveElementAt(mImages.Length() - 1); |
|
113 return NS_ERROR_OUT_OF_MEMORY; |
|
114 } |
|
115 if (!mFinalCompleteValues.AppendElement(aFinalCompleteValue)) { |
|
116 mValues.RemoveElementAt(mValues.Length() - 1); |
|
117 mComments.RemoveElementAt(mComments.Length() - 1); |
|
118 mImages.RemoveElementAt(mImages.Length() - 1); |
|
119 mStyles.RemoveElementAt(mStyles.Length() - 1); |
|
120 return NS_ERROR_OUT_OF_MEMORY; |
|
121 } |
|
122 return NS_OK; |
|
123 } |
|
124 |
|
125 NS_IMETHODIMP |
|
126 nsAutoCompleteSimpleResult::GetMatchCount(uint32_t *aMatchCount) |
|
127 { |
|
128 CheckInvariants(); |
|
129 |
|
130 *aMatchCount = mValues.Length(); |
|
131 return NS_OK; |
|
132 } |
|
133 |
|
134 NS_IMETHODIMP |
|
135 nsAutoCompleteSimpleResult::GetValueAt(int32_t aIndex, nsAString& _retval) |
|
136 { |
|
137 NS_ENSURE_TRUE(aIndex >= 0 && aIndex < int32_t(mValues.Length()), |
|
138 NS_ERROR_ILLEGAL_VALUE); |
|
139 CheckInvariants(); |
|
140 |
|
141 _retval = mValues[aIndex]; |
|
142 return NS_OK; |
|
143 } |
|
144 |
|
145 NS_IMETHODIMP |
|
146 nsAutoCompleteSimpleResult::GetLabelAt(int32_t aIndex, nsAString& _retval) |
|
147 { |
|
148 return GetValueAt(aIndex, _retval); |
|
149 } |
|
150 |
|
151 NS_IMETHODIMP |
|
152 nsAutoCompleteSimpleResult::GetCommentAt(int32_t aIndex, nsAString& _retval) |
|
153 { |
|
154 NS_ENSURE_TRUE(aIndex >= 0 && aIndex < int32_t(mComments.Length()), |
|
155 NS_ERROR_ILLEGAL_VALUE); |
|
156 CheckInvariants(); |
|
157 _retval = mComments[aIndex]; |
|
158 return NS_OK; |
|
159 } |
|
160 |
|
161 NS_IMETHODIMP |
|
162 nsAutoCompleteSimpleResult::GetImageAt(int32_t aIndex, nsAString& _retval) |
|
163 { |
|
164 NS_ENSURE_TRUE(aIndex >= 0 && aIndex < int32_t(mImages.Length()), |
|
165 NS_ERROR_ILLEGAL_VALUE); |
|
166 CheckInvariants(); |
|
167 _retval = mImages[aIndex]; |
|
168 return NS_OK; |
|
169 } |
|
170 |
|
171 NS_IMETHODIMP |
|
172 nsAutoCompleteSimpleResult::GetStyleAt(int32_t aIndex, nsAString& _retval) |
|
173 { |
|
174 NS_ENSURE_TRUE(aIndex >= 0 && aIndex < int32_t(mStyles.Length()), |
|
175 NS_ERROR_ILLEGAL_VALUE); |
|
176 CheckInvariants(); |
|
177 _retval = mStyles[aIndex]; |
|
178 return NS_OK; |
|
179 } |
|
180 |
|
181 NS_IMETHODIMP |
|
182 nsAutoCompleteSimpleResult::GetFinalCompleteValueAt(int32_t aIndex, |
|
183 nsAString& _retval) |
|
184 { |
|
185 NS_ENSURE_TRUE(aIndex >= 0 && aIndex < int32_t(mFinalCompleteValues.Length()), |
|
186 NS_ERROR_ILLEGAL_VALUE); |
|
187 CheckInvariants(); |
|
188 _retval = mFinalCompleteValues[aIndex]; |
|
189 if (_retval.Length() == 0) |
|
190 _retval = mValues[aIndex]; |
|
191 return NS_OK; |
|
192 } |
|
193 |
|
194 NS_IMETHODIMP |
|
195 nsAutoCompleteSimpleResult::SetListener(nsIAutoCompleteSimpleResultListener* aListener) |
|
196 { |
|
197 mListener = aListener; |
|
198 return NS_OK; |
|
199 } |
|
200 |
|
201 NS_IMETHODIMP |
|
202 nsAutoCompleteSimpleResult::RemoveValueAt(int32_t aRowIndex, |
|
203 bool aRemoveFromDb) |
|
204 { |
|
205 NS_ENSURE_TRUE(aRowIndex >= 0 && aRowIndex < int32_t(mValues.Length()), |
|
206 NS_ERROR_ILLEGAL_VALUE); |
|
207 |
|
208 nsAutoString removedValue(mValues[aRowIndex]); |
|
209 mValues.RemoveElementAt(aRowIndex); |
|
210 mComments.RemoveElementAt(aRowIndex); |
|
211 mImages.RemoveElementAt(aRowIndex); |
|
212 mStyles.RemoveElementAt(aRowIndex); |
|
213 mFinalCompleteValues.RemoveElementAt(aRowIndex); |
|
214 |
|
215 if (mListener) |
|
216 mListener->OnValueRemoved(this, removedValue, aRemoveFromDb); |
|
217 |
|
218 return NS_OK; |
|
219 } |