summaryrefslogtreecommitdiff
path: root/indra/newview/llpanelexperiencepicker.cpp
blob: 43dc7569a41cf325ab33c7599d6f3d59737de86f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/** 
* @file llpanelexperiencepicker.cpp
* @brief Implementation of llpanelexperiencepicker
* @author dolphin@lindenlab.com
*
* $LicenseInfo:firstyear=2014&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2014, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
* $/LicenseInfo$
*/

#include "llviewerprecompiledheaders.h"

#include "llpanelexperiencepicker.h"


#include "lllineeditor.h"
#include "llfloaterreg.h"
#include "llscrolllistctrl.h"
#include "llviewerregion.h"
#include "llagent.h"
#include "llexperiencecache.h"
#include "llslurl.h"
#include "llavatarnamecache.h"
#include "llcombobox.h"
#include "llviewercontrol.h"
#include "llfloater.h"
#include "lltrans.h"
#include <boost/regex.hpp>

#define BTN_FIND		"find"
#define BTN_OK			"ok_btn"
#define BTN_CANCEL		"cancel_btn"
#define BTN_PROFILE		"profile_btn"
#define BTN_LEFT		"left_btn"
#define BTN_RIGHT		"right_btn"
#define TEXT_EDIT		"edit"
#define TEXT_MATURITY	"maturity"
#define LIST_RESULTS	"search_results"
#define PANEL_SEARCH	"search_panel"

const static std::string columnSpace = " ";

static LLPanelInjector<LLPanelExperiencePicker> t_panel_status("llpanelexperiencepicker");

class LLExperienceSearchResponder : public LLHTTPClient::Responder
{
public:
	LLUUID mQueryID;
	LLHandle<LLPanelExperiencePicker> mParent;

	LLExperienceSearchResponder(const LLUUID& id, const LLHandle<LLPanelExperiencePicker>& parent) : mQueryID(id), mParent(parent) { }

protected:
	/*virtual*/ void httpSuccess()
	{
		if(mParent.isDead())
			return;

		LLPanelExperiencePicker* panel =mParent.get();
		if (panel)
		{
			panel->processResponse(mQueryID, getContent());
		}
	}

	/*virtual*/ void httpFailure()
	{
		if(mParent.isDead())
			return;

		LLPanelExperiencePicker* panel =mParent.get();
		if (panel)
		{
			panel->processResponse(mQueryID, LLSD());
		}
		LL_WARNS() << "experience picker failed [status:" << getStatus() << "]: " << getContent() << LL_ENDL;
	}
};

LLPanelExperiencePicker::LLPanelExperiencePicker()
	:LLPanel()
{
	buildFromFile("panel_experience_search.xml");
	setDefaultFilters();
}

LLPanelExperiencePicker::~LLPanelExperiencePicker()
{
}

BOOL LLPanelExperiencePicker::postBuild()
{
	getChild<LLLineEditor>(TEXT_EDIT)->setKeystrokeCallback( boost::bind(&LLPanelExperiencePicker::editKeystroke, this, _1, _2),NULL);

	childSetAction(BTN_FIND, boost::bind(&LLPanelExperiencePicker::onBtnFind, this));
	getChildView(BTN_FIND)->setEnabled(TRUE);

	LLScrollListCtrl* searchresults = getChild<LLScrollListCtrl>(LIST_RESULTS);
	searchresults->setDoubleClickCallback( boost::bind(&LLPanelExperiencePicker::onBtnSelect, this));
	searchresults->setCommitCallback(boost::bind(&LLPanelExperiencePicker::onList, this));
	getChildView(LIST_RESULTS)->setEnabled(FALSE);
	getChild<LLScrollListCtrl>(LIST_RESULTS)->setCommentText(getString("no_results"));

	childSetAction(BTN_OK, boost::bind(&LLPanelExperiencePicker::onBtnSelect, this));
	getChildView(BTN_OK)->setEnabled(FALSE);
	childSetAction(BTN_CANCEL, boost::bind(&LLPanelExperiencePicker::onBtnClose, this));
	childSetAction(BTN_PROFILE, boost::bind(&LLPanelExperiencePicker::onBtnProfile, this));
	getChildView(BTN_PROFILE)->setEnabled(FALSE);

	getChild<LLComboBox>(TEXT_MATURITY)->setCurrentByIndex(2);
	getChild<LLComboBox>(TEXT_MATURITY)->setCommitCallback(boost::bind(&LLPanelExperiencePicker::onMaturity, this));
	getChild<LLUICtrl>(TEXT_EDIT)->setFocus(TRUE);

	childSetAction(BTN_LEFT, boost::bind(&LLPanelExperiencePicker::onPage, this, -1));
	childSetAction(BTN_RIGHT, boost::bind(&LLPanelExperiencePicker::onPage, this, 1));

	LLPanel* search_panel = getChild<LLPanel>(PANEL_SEARCH);
	if (search_panel)
	{
		// Start searching when Return is pressed in the line editor.
		search_panel->setDefaultBtn(BTN_FIND);
	}
	return TRUE;
}

void LLPanelExperiencePicker::editKeystroke( class LLLineEditor* caller, void* user_data )
{
	getChildView(BTN_FIND)->setEnabled(true);
}

void LLPanelExperiencePicker::onBtnFind()
{
	mCurrentPage=1;
	boost::cmatch what;
	std::string text = getChild<LLUICtrl>(TEXT_EDIT)->getValue().asString();
	const boost::regex expression("secondlife:///app/experience/[\\da-f-]+/profile");
	if (boost::regex_match(text.c_str(), what, expression))
	{
		LLURI uri(text);
		LLSD path_array = uri.pathArray();
		if (path_array.size() == 4)
		{
			std::string exp_id = path_array.get(2).asString();
			LLUUID experience_id(exp_id);
			if (!experience_id.isNull())
			{
				const LLSD& experience_details = LLExperienceCache::get(experience_id);
				if(!experience_details.isUndefined())
				{
					std::string experience_name_string = experience_details[LLExperienceCache::NAME].asString();
					if(!experience_name_string.empty())
					{
						getChild<LLUICtrl>(TEXT_EDIT)->setValue(experience_name_string);
					}
				}
				else
				{
					getChild<LLScrollListCtrl>(LIST_RESULTS)->deleteAllItems();
					getChild<LLScrollListCtrl>(LIST_RESULTS)->setCommentText(getString("searching"));

					getChildView(BTN_OK)->setEnabled(FALSE);
					getChildView(BTN_PROFILE)->setEnabled(FALSE);

					getChildView(BTN_RIGHT)->setEnabled(FALSE);
					getChildView(BTN_LEFT)->setEnabled(FALSE);
					LLExperienceCache::get(experience_id, boost::bind(&LLPanelExperiencePicker::onBtnFind, this));
					return;
				}
			}
		}
	}


	find();
}

void LLPanelExperiencePicker::onList()
{
	bool enabled = isSelectButtonEnabled();
	getChildView(BTN_OK)->setEnabled(enabled);

	enabled = enabled && getChild<LLScrollListCtrl>(LIST_RESULTS)->getNumSelected() == 1;
	getChildView(BTN_PROFILE)->setEnabled(enabled);
}

void LLPanelExperiencePicker::find()
{
	std::string text = getChild<LLUICtrl>(TEXT_EDIT)->getValue().asString();
	mQueryID.generate();
	std::ostringstream url;

	LLViewerRegion* region = gAgent.getRegion();
	std::string cap = region->getCapability("FindExperienceByName");
	if (!cap.empty())
	{
		url << cap << "?page=" << mCurrentPage << "&page_size=30&query=" << LLURI::escape(text);
		LLHTTPClient::get(url.str(), new LLExperienceSearchResponder(mQueryID, getDerivedHandle<LLPanelExperiencePicker>()));

	}
	getChild<LLScrollListCtrl>(LIST_RESULTS)->deleteAllItems();
	getChild<LLScrollListCtrl>(LIST_RESULTS)->setCommentText(getString("searching"));
	
	getChildView(BTN_OK)->setEnabled(FALSE);
	getChildView(BTN_PROFILE)->setEnabled(FALSE);

	getChildView(BTN_RIGHT)->setEnabled(FALSE);
	getChildView(BTN_LEFT)->setEnabled(FALSE);
}

bool LLPanelExperiencePicker::isSelectButtonEnabled()
{
	LLScrollListCtrl* list=getChild<LLScrollListCtrl>(LIST_RESULTS);
	return list->getFirstSelectedIndex() >=0;
}

void LLPanelExperiencePicker::getSelectedExperienceIds( const LLScrollListCtrl* results, uuid_vec_t &experience_ids )
{
	std::vector<LLScrollListItem*> items = results->getAllSelected();
	for(std::vector<LLScrollListItem*>::iterator it = items.begin(); it != items.end(); ++it)
	{
		LLScrollListItem* item = *it;
		if (item->getUUID().notNull())
		{
			experience_ids.push_back(item->getUUID());
		}
	}
}

void LLPanelExperiencePicker::setAllowMultiple( bool allow_multiple )
{
	getChild<LLScrollListCtrl>(LIST_RESULTS)->setAllowMultipleSelection(allow_multiple);
}


void name_callback(const LLHandle<LLPanelExperiencePicker>& floater, const LLUUID& experience_id, const LLUUID& agent_id, const LLAvatarName& av_name)
{
	if(floater.isDead())
		return;
	LLPanelExperiencePicker* picker = floater.get();
	LLScrollListCtrl* search_results = picker->getChild<LLScrollListCtrl>(LIST_RESULTS);

	LLScrollListItem* item = search_results->getItem(experience_id);
	if(!item)
		return;

	item->getColumn(2)->setValue(columnSpace+av_name.getDisplayName());

}

void LLPanelExperiencePicker::processResponse( const LLUUID& query_id, const LLSD& content )
{
	if(query_id != mQueryID)
	{
		return;
	}

	mResponse = content;

	const LLSD& experiences=mResponse["experience_keys"];
	LLSD::array_const_iterator it = experiences.beginArray();
	for ( ; it != experiences.endArray(); ++it)
	{
		LLExperienceCache::insert(*it);
	}

	getChildView(BTN_RIGHT)->setEnabled(content.has("next_page_url"));
	getChildView(BTN_LEFT)->setEnabled(content.has("previous_page_url"));

	filterContent();

}

void LLPanelExperiencePicker::onBtnSelect()
{
	if(!isSelectButtonEnabled())
	{
		return;
	}

	if(mSelectionCallback)
	{
		const LLScrollListCtrl* results = getChild<LLScrollListCtrl>(LIST_RESULTS);
		uuid_vec_t experience_ids;
		
		getSelectedExperienceIds(results, experience_ids);
		mSelectionCallback(experience_ids);
		getChild<LLScrollListCtrl>(LIST_RESULTS)->deselectAllItems(TRUE);
		if(mCloseOnSelect)
		{
			mCloseOnSelect = FALSE;
			onBtnClose();
		}
	}
	else 
	{
		onBtnProfile();
	}
}

void LLPanelExperiencePicker::onBtnClose()
{
	LLFloater* floater = getParentByType<LLFloater>();
	if (floater)
	{
		floater->closeFloater();
	}
}

void LLPanelExperiencePicker::onBtnProfile()
{
	LLScrollListItem* item = getChild<LLScrollListCtrl>(LIST_RESULTS)->getFirstSelected();
	if(item)
	{
		LLFloaterReg::showInstance("experience_profile", item->getUUID(), true);
	}
}

std::string LLPanelExperiencePicker::getMaturityString(int maturity)
{
	if(maturity <= SIM_ACCESS_PG)
	{
		return getString("maturity_icon_general");
	}
	else if(maturity <= SIM_ACCESS_MATURE)
	{
		return getString("maturity_icon_moderate");
	}
	return getString("maturity_icon_adult");
}

void LLPanelExperiencePicker::filterContent()
{
	LLScrollListCtrl* search_results = getChild<LLScrollListCtrl>(LIST_RESULTS);

	const LLSD& experiences=mResponse["experience_keys"];

	search_results->deleteAllItems();

	LLSD item;
	LLSD::array_const_iterator it = experiences.beginArray();
	for ( ; it != experiences.endArray(); ++it)
	{
		const LLSD& experience = *it;

		if(isExperienceHidden(experience))
			continue;

		std::string experience_name_string = experience[LLExperienceCache::NAME].asString();
		if (experience_name_string.empty())
		{
			experience_name_string = LLTrans::getString("ExperienceNameUntitled");
		}

		item["id"]=experience[LLExperienceCache::EXPERIENCE_ID];
		LLSD& columns = item["columns"];
		columns[0]["column"] = "maturity";
		columns[0]["value"] = getMaturityString(experience[LLExperienceCache::MATURITY].asInteger());
		columns[0]["type"]="icon";
		columns[0]["halign"]="right";
		columns[1]["column"] = "experience_name";
		columns[1]["value"] = columnSpace+experience_name_string;
		columns[2]["column"] = "owner";
		columns[2]["value"] = columnSpace+getString("loading");
		search_results->addElement(item);
		LLAvatarNameCache::get(experience[LLExperienceCache::AGENT_ID], boost::bind(name_callback, getDerivedHandle<LLPanelExperiencePicker>(), experience[LLExperienceCache::EXPERIENCE_ID], _1, _2));
	}
	
	if (search_results->isEmpty())
	{
		LLStringUtil::format_map_t map;
		std::string search_text = getChild<LLUICtrl>(TEXT_EDIT)->getValue().asString();
		map["[TEXT]"] = search_text;
		if (search_text.empty())
		{
			getChild<LLScrollListCtrl>(LIST_RESULTS)->setCommentText(getString("no_results"));
		}
		else
		{
			getChild<LLScrollListCtrl>(LIST_RESULTS)->setCommentText(getString("not_found", map));
		}
		search_results->setEnabled(false);
		getChildView(BTN_OK)->setEnabled(false);
		getChildView(BTN_PROFILE)->setEnabled(false);
	}
	else
	{
		getChildView(BTN_OK)->setEnabled(true);
		search_results->setEnabled(true);
		search_results->sortByColumnIndex(1, TRUE);
		std::string text = getChild<LLUICtrl>(TEXT_EDIT)->getValue().asString();
		if (!search_results->selectItemByLabel(text, TRUE, 1))
		{
			search_results->selectFirstItem();
		}			
		onList();
		search_results->setFocus(TRUE);
	}
}

void LLPanelExperiencePicker::onMaturity()
{
	if(mResponse.has("experience_keys") && mResponse["experience_keys"].beginArray() != mResponse["experience_keys"].endArray())
	{
		filterContent();
	}
}

bool LLPanelExperiencePicker::isExperienceHidden( const LLSD& experience) const
{
	bool hide=false;
	filter_list::const_iterator it = mFilters.begin();
	for(/**/;it != mFilters.end(); ++it)
	{
		if((*it)(experience)){
			return true;
		}
	}

	return hide;
}

bool LLPanelExperiencePicker::FilterOverRating( const LLSD& experience )
{
	int maturity = getChild<LLComboBox>(TEXT_MATURITY)->getSelectedValue().asInteger();
	return experience[LLExperienceCache::MATURITY].asInteger() > maturity;
}

bool LLPanelExperiencePicker::FilterWithProperty( const LLSD& experience, S32 prop)
{
	return (experience[LLExperienceCache::PROPERTIES].asInteger() & prop) != 0;
}

bool LLPanelExperiencePicker::FilterWithoutProperties( const LLSD& experience, S32 prop)
{
	return ((experience[LLExperienceCache::PROPERTIES].asInteger() & prop) == prop);
}

bool LLPanelExperiencePicker::FilterWithoutProperty( const LLSD& experience, S32 prop )
{
	return (experience[LLExperienceCache::PROPERTIES].asInteger() & prop) == 0;
}

void LLPanelExperiencePicker::setDefaultFilters()
{
	mFilters.clear();
	addFilter(boost::bind(&LLPanelExperiencePicker::FilterOverRating, this, _1));
}

bool LLPanelExperiencePicker::FilterMatching( const LLSD& experience, const LLUUID& id )
{
	if(experience.isUUID())
	{
		return experience.asUUID() == id;
	}
	return experience[LLExperienceCache::EXPERIENCE_ID].asUUID() == id;
}

void LLPanelExperiencePicker::onPage( S32 direction )
{
	mCurrentPage += direction;
	if(mCurrentPage < 1)
	{
		mCurrentPage = 1;
	}
	find();
}