summaryrefslogtreecommitdiff
path: root/indra/newview/llavatarlist.cpp
blob: 2e64c10bb25623a1c5e2b343483a1c7f6eed55ae (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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/** 
 * @file llavatarlist.h
 * @brief Generic avatar list
 *
 * $LicenseInfo:firstyear=2009&license=viewergpl$
 * 
 * Copyright (c) 2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "llavatarlist.h"

// newview
#include "llcallingcard.h" // for LLAvatarTracker
#include "llcachename.h"
#include "lloutputmonitorctrl.h"
#include "llvoiceclient.h"

static LLDefaultChildRegistry::Register<LLAvatarList> r("avatar_list");
static LLDefaultChildRegistry::Register<LLAvatarListTmp> r_tmp("avatar_list_tmp");

static const std::string COMMENT_TEXTBOX = "comment_text";

LLAvatarList::Params::Params()
:
	volume_column_width("volume_column_width", 0)
	, online_go_first("online_go_first", true)
{
	draw_heading	= true;
	draw_stripes	= false;
	multi_select	= false;
	column_padding	= 0;
	search_column	= COL_NAME;
	sort_column		= COL_NAME;
}

LLAvatarList::LLAvatarList(const Params& p)
:	LLScrollListCtrl(p)
	, mHaveVolumeColumn(p.volume_column_width > 0)
	, mOnlineGoFirst(p.online_go_first)
{
	setCommitOnSelectionChange(TRUE); // there's no such param in LLScrollListCtrl::Params

	// display a context menu appropriate for a list of avatar names
	setContextMenu(LLScrollListCtrl::MENU_AVATAR);

    // "volume" column
    {
    	LLScrollListColumn::Params col_params;
    	col_params.name = "volume";
    	col_params.header.label = "Volume"; // *TODO: localize or remove the header
    	col_params.width.pixel_width = p.volume_column_width;
    	addColumn(col_params);
    }

    // "name" column
    {
    	LLScrollListColumn::Params col_params;
    	col_params.name = "name";
    	col_params.header.label = "Name"; // *TODO: localize or remove the header
    	col_params.width.dynamic_width = true;
    	addColumn(col_params);
    }

    // "online status" column
    {
    	LLScrollListColumn::Params col_params;
    	col_params.name = "online";
    	col_params.header.label = "Online"; // *TODO: localize or remove the header
    	col_params.width.pixel_width = 0; // invisible column
    	addColumn(col_params);
    }

    
    // invisible "id" column
    {
    	LLScrollListColumn::Params col_params;
    	col_params.name = "id";
    	col_params.header.label = "ID"; // *TODO: localize or remove the header
    	col_params.width.pixel_width = 0;
    	addColumn(col_params);
    }

	// Primary sort = online status, secondary sort = name
	// The corresponding parameters don't work because we create columns dynamically.
	sortByColumnIndex(COL_NAME, TRUE);
	if (mOnlineGoFirst)
		sortByColumnIndex(COL_ONLINE, FALSE);
	setSearchColumn(COL_NAME);
}

// virtual
void LLAvatarList::draw()
{
	LLScrollListCtrl::draw();
	if (mHaveVolumeColumn)
	{
		updateVolume();
	}
}

std::vector<LLUUID> LLAvatarList::getSelectedIDs()
{
	LLUUID selected_id;
	std::vector<LLUUID> avatar_ids;
	std::vector<LLScrollListItem*> selected = getAllSelected();
	for(std::vector<LLScrollListItem*>::iterator itr = selected.begin(); itr != selected.end(); ++itr)
	{
		avatar_ids.push_back((*itr)->getUUID());
	}
	return avatar_ids;
}

void LLAvatarList::addItem(const LLUUID& id, const std::string& name, BOOL is_bold, EAddPosition pos)
{
	std::string fullname;

	// Populate list item.
	LLSD element;
	element["id"] = id;

	// Update volume column (if we have one)
	{
		std::string icon = mHaveVolumeColumn ? getVolumeIcon(id) : "";
		LLSD& volume_column = element["columns"][COL_VOLUME];
		volume_column["column"] = "volume";
		volume_column["type"] = "icon";
		volume_column["value"] = icon;
	}

	LLSD& friend_column = element["columns"][COL_NAME];
	friend_column["column"] = "name";
	friend_column["value"] = name;

	LLSD& online_column = element["columns"][COL_ONLINE];
	online_column["column"] = "online";
	online_column["value"] = is_bold ? "1" : "0";

	LLScrollListItem* new_itemp = addElement(element, pos);

	// Indicate buddy online status.
	// (looks like parsing font parameters from LLSD is broken)
	if (is_bold)
	{
		LLScrollListText* name_textp = dynamic_cast<LLScrollListText*>(new_itemp->getColumn(COL_NAME));
		if (name_textp)
			name_textp->setFontStyle(LLFontGL::BOLD);
		else
		{
			llwarns << "Name column not found" << llendl;
		}
	}
}

static bool findInsensitive(std::string haystack, const std::string& needle_upper)
{
    LLStringUtil::toUpper(haystack);
    return haystack.find(needle_upper) != std::string::npos;
}

BOOL LLAvatarList::update(const std::vector<LLUUID>& all_buddies, const std::string& name_filter)
{
	BOOL have_names = TRUE;
	
	// Save selection.	
	std::vector<LLUUID> selected_ids = getSelectedIDs();
	LLUUID current_id = getCurrentID();
	S32 pos = getScrollPos();
	
	std::vector<LLUUID>::const_iterator buddy_it = all_buddies.begin();
	deleteAllItems();
	for(; buddy_it != all_buddies.end(); ++buddy_it)
	{
		std::string name;
		const LLUUID& buddy_id = *buddy_it;
		have_names &= gCacheName->getFullName(buddy_id, name);
		if (name_filter != LLStringUtil::null && !findInsensitive(name, name_filter))
			continue;
		addItem(buddy_id, name, LLAvatarTracker::instance().isBuddyOnline(buddy_id));
	}

	// Changed item in place, need to request sort and update columns
	// because we might have changed data in a column on which the user
	// has already sorted. JC
	updateSort();

	// re-select items
	selectMultiple(selected_ids);
	setCurrentByID(current_id);
#if 0
	// Restore selection.
	if(selected_ids.size() > 0)
	{
		// only non-null if friends was already found. This may fail,
		// but we don't really care here, because refreshUI() will
		// clean up the interface.
		for(std::vector<LLUUID>::iterator itr = selected_ids.begin(); itr != selected_ids.end(); ++itr)
		{
			setSelectedByValue(*itr, true);
		}
	}
#endif
	setScrollPos(pos);

	updateLineHeight();
	LLRect	rect = getRequiredRect();

	LLSD params;
	params["action"] = "size_changes";
	params["width"] = rect.getWidth();
	params["height"] = llmax(rect.getHeight(),20) + 5;

	getParent()->notifyParent(params);

	return have_names;
}

// static
std::string LLAvatarList::getVolumeIcon(const LLUUID& id)
{
	//
	// Determine icon appropriate for the current avatar volume.
	//
	// *TODO: remove this in favor of LLOutputMonitorCtrl
	// when ListView widget is implemented
	// which is capable of containing arbitrary widgets.
	//
	static LLOutputMonitorCtrl::Params default_monitor_params(LLUICtrlFactory::getDefaultParams<LLOutputMonitorCtrl>());
	bool		muted = gVoiceClient->getIsModeratorMuted(id) || gVoiceClient->getOnMuteList(id);
	F32			power = gVoiceClient->getCurrentPower(id);
	std::string	icon;

	if (muted)
	{
		icon = default_monitor_params.image_mute.name;
	}
	else if (power == 0.f)
	{
		icon = default_monitor_params.image_off.name;
	}
	else if (power < LLVoiceClient::OVERDRIVEN_POWER_LEVEL)
	{
		S32 icon_image_idx = llmin(2, llfloor((power / LLVoiceClient::OVERDRIVEN_POWER_LEVEL) * 3.f));
		switch(icon_image_idx)
		{
		default:
		case 0:
			icon = default_monitor_params.image_on.name;
			break;
		case 1:
			icon = default_monitor_params.image_level_1.name;
			break;
		case 2:
			icon = default_monitor_params.image_level_2.name;
			break;
		}
	}
	else
	{
		// overdriven
		icon = default_monitor_params.image_level_3.name;
	}

	return icon;
}

// Update volume column for all list rows.
void LLAvatarList::updateVolume()
{
	item_list& items = getItemList();

	for (item_list::iterator item_it = items.begin();
		item_it != items.end();
		++item_it)
	{
		LLScrollListItem* itemp = (*item_it);
		LLUUID speaker_id = itemp->getUUID();

		LLScrollListCell* icon_cell = itemp->getColumn(COL_VOLUME);
		if (icon_cell)
			icon_cell->setValue(getVolumeIcon(speaker_id));
	}
}




#include "llavatarlistitem.h"

LLAvatarListTmp::Params::Params()
:
volume_column_width("volume_column_width", 0)
, online_go_first("online_go_first", true)
{
}



LLAvatarListTmp::LLAvatarListTmp(const Params& p)
:	LLFlatListView(p)
, mHaveVolumeColumn(p.volume_column_width > 0)
, mOnlineGoFirst(p.online_go_first)
{
	LLRect item_list_rect = getLocalRect();
	item_list_rect.stretch( -getBorderWidth());

	LLTextBox::Params text_p;
	text_p.name(COMMENT_TEXTBOX);
	text_p.border_visible(false);
	text_p.rect(item_list_rect);
	text_p.follows.flags(FOLLOWS_ALL);
	addChild(LLUICtrlFactory::create<LLTextBox>(text_p));
}

// virtual
void LLAvatarListTmp::draw()
{
	LLFlatListView::draw();
	if (mHaveVolumeColumn)
	{
		updateVolume();
	}
}

std::vector<LLUUID> LLAvatarListTmp::getSelectedIDs()
{
	LLUUID selected_id;
	std::vector<LLUUID> avatar_ids;

	getSelectedUUIDs(avatar_ids);

	return avatar_ids;
}

void LLAvatarListTmp::addNewItem(const LLUUID& id, const std::string& name, BOOL is_bold, EAddPosition pos)
{
	LLAvatarListItem* item = new LLAvatarListItem();
	item->showStatus(true);
	item->showInfoBtn(true);
	item->showSpeakingIndicator(true);
	item->setName(name);
	item->setAvatarId(id);

	item->childSetVisible("info_btn", false);

	addItem(item, id, pos);

	setCommentVisible(false);
}

BOOL LLAvatarListTmp::update(const std::vector<LLUUID>& all_buddies, const std::string& name_filter)
{
	BOOL have_names = TRUE;

	// Save selection.	
	std::vector<LLUUID> selected_ids = getSelectedIDs();
	LLUUID current_id = getSelectedUUID();
	LLRect pos = getScrolledViewRect();

	std::vector<LLUUID>::const_iterator buddy_it = all_buddies.begin();
	clear();
	for(; buddy_it != all_buddies.end(); ++buddy_it)
	{
		std::string name;
		const LLUUID& buddy_id = *buddy_it;
		have_names &= gCacheName->getFullName(buddy_id, name);
		if (name_filter != LLStringUtil::null && !findInsensitive(name, name_filter))
			continue;
		addNewItem(buddy_id, name, LLAvatarTracker::instance().isBuddyOnline(buddy_id));
	}

	// Changed item in place, need to request sort and update columns
	// because we might have changed data in a column on which the user
	// has already sorted. JC
	//	updateSort(); // TODO: implement sorting

	// re-select items
	//	selectMultiple(selected_ids); // TODO: implement in LLFlatListView if need
	selectItemByUUID(current_id);

	scrollToShowRect(pos);


	setCommentVisible(false);

	return have_names;
}


const LLUUID LLAvatarListTmp::getCurrentID() const
{
	return getSelectedUUID();
}

void LLAvatarListTmp::setCommentText(const std::string& comment_text)
{
	getChild<LLTextBox>(COMMENT_TEXTBOX)->setValue(comment_text);
}


//////////////////////////////////////////////////////////////////////////
// PROTECTED SECTION
//////////////////////////////////////////////////////////////////////////

// virtual overridden
bool LLAvatarListTmp::removeItemPair(item_pair_t* item_pair)
{
	bool removed = LLFlatListView::removeItemPair(item_pair);
	setCommentVisible(size() == 0);
	return removed;
}


//////////////////////////////////////////////////////////////////////////
// PRIVATE SECTION
//////////////////////////////////////////////////////////////////////////

// static
std::string LLAvatarListTmp::getVolumeIcon(const LLUUID& id)
{
	//
	// Determine icon appropriate for the current avatar volume.
	//
	// *TODO: remove this in favor of LLOutputMonitorCtrl
	// when ListView widget is implemented
	// which is capable of containing arbitrary widgets.
	//
	static LLOutputMonitorCtrl::Params default_monitor_params(LLUICtrlFactory::getDefaultParams<LLOutputMonitorCtrl>());
	bool		muted = gVoiceClient->getIsModeratorMuted(id) || gVoiceClient->getOnMuteList(id);
	F32			power = gVoiceClient->getCurrentPower(id);
	std::string	icon;

	if (muted)
	{
		icon = default_monitor_params.image_mute.name;
	}
	else if (power == 0.f)
	{
		icon = default_monitor_params.image_off.name;
	}
	else if (power < LLVoiceClient::OVERDRIVEN_POWER_LEVEL)
	{
		S32 icon_image_idx = llmin(2, llfloor((power / LLVoiceClient::OVERDRIVEN_POWER_LEVEL) * 3.f));
		switch(icon_image_idx)
		{
		default:
		case 0:
			icon = default_monitor_params.image_on.name;
			break;
		case 1:
			icon = default_monitor_params.image_level_1.name;
			break;
		case 2:
			icon = default_monitor_params.image_level_2.name;
			break;
		}
	}
	else
	{
		// overdriven
		icon = default_monitor_params.image_level_3.name;
	}

	return icon;
}

// Update volume column for all list rows.
void LLAvatarListTmp::updateVolume()
{
	// TODO: implement via Listener
	/*
	item_list& items = getItemList();

	for (item_list::iterator item_it = items.begin();
	item_it != items.end();
	++item_it)
	{
	LLScrollListItem* itemp = (*item_it);
	LLUUID speaker_id = itemp->getUUID();

	LLScrollListCell* icon_cell = itemp->getColumn(COL_VOLUME);
	if (icon_cell)
	icon_cell->setValue(getVolumeIcon(speaker_id));
	}
	*/
}

void LLAvatarListTmp::setCommentVisible(bool visible) const
{
	getChildView(COMMENT_TEXTBOX)->setVisible(visible);
}

// EOF