summaryrefslogtreecommitdiff
path: root/indra/newview/llconversationloglist.cpp
blob: d39e090c2201b60056fa1f70dd2a055e97ddd3a3 (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
/**
 * @file llconversationloglist.cpp
 *
 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2012, 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 "llavataractions.h"
#include "llagent.h"
#include "llfloaterreg.h"
#include "llfloaterconversationpreview.h"
#include "llgroupactions.h"
#include "llconversationloglist.h"
#include "llconversationloglistitem.h"
#include "llviewermenu.h"

static LLDefaultChildRegistry::Register<LLConversationLogList> r("conversation_log_list");

static LLConversationLogListNameComparator NAME_COMPARATOR;
static LLConversationLogListDateComparator DATE_COMPARATOR;

LLConversationLogList::LLConversationLogList(const Params& p)
:	LLFlatListViewEx(p),
	mIsDirty(true)
{
	LLConversationLog::instance().addObserver(this);

	// Set up context menu.
	LLUICtrl::CommitCallbackRegistry::ScopedRegistrar registrar;
	LLUICtrl::EnableCallbackRegistry::ScopedRegistrar check_registrar;
	LLUICtrl::EnableCallbackRegistry::ScopedRegistrar enable_registrar;

	registrar.add		("Calllog.Action",	boost::bind(&LLConversationLogList::onCustomAction,	this, _2));
	check_registrar.add ("Calllog.Check",	boost::bind(&LLConversationLogList::isActionChecked,this, _2));
	enable_registrar.add("Calllog.Enable",	boost::bind(&LLConversationLogList::isActionEnabled,this, _2));

	LLToggleableMenu* context_menu = LLUICtrlFactory::getInstance()->createFromFile<LLToggleableMenu>(
									"menu_conversation_log_gear.xml",
									gMenuHolder,
									LLViewerMenuHolderGL::child_registry_t::instance());
	if(context_menu)
	{
		mContextMenu = context_menu->getHandle();
	}

	mIsFriendsOnTop = gSavedSettings.getBOOL("SortFriendsFirst");
}

LLConversationLogList::~LLConversationLogList()
{
	if (mContextMenu.get())
	{
		mContextMenu.get()->die();
	}

	LLConversationLog::instance().removeObserver(this);
}

void LLConversationLogList::draw()
{
	if (mIsDirty)
	{
		refresh();
	}
	LLFlatListViewEx::draw();
}

BOOL LLConversationLogList::handleRightMouseDown(S32 x, S32 y, MASK mask)
{
	BOOL handled = LLUICtrl::handleRightMouseDown(x, y, mask);

	LLToggleableMenu* context_menu = mContextMenu.get();
	{
		context_menu->buildDrawLabels();
	if (context_menu && size())
		context_menu->updateParent(LLMenuGL::sMenuContainer);
		LLMenuGL::showPopup(this, context_menu, x, y);
	}

	return handled;
}

void LLConversationLogList::setNameFilter(const std::string& filter)
{
	std::string filter_upper = filter;
	LLStringUtil::toUpper(filter_upper);
	if (mNameFilter != filter_upper)
	{
		mNameFilter = filter_upper;
		setDirty();
	}
}

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

void LLConversationLogList::sortByName()
{
	setComparator(&NAME_COMPARATOR);
	sort();
}

void LLConversationLogList::sortByDate()
{
	setComparator(&DATE_COMPARATOR);
	sort();
}

void LLConversationLogList::toggleSortFriendsOnTop()
{
	mIsFriendsOnTop = !mIsFriendsOnTop;
	gSavedSettings.setBOOL("SortFriendsFirst", mIsFriendsOnTop);
	sort();
}

void LLConversationLogList::changed()
{
	refresh();
}

void LLConversationLogList::changed(const LLUUID& session_id, U32 mask)
{
	if (mask & LLConversationLogObserver::VOICE_STATE)
	{
		std::vector<LLPanel*> panels;
		LLFlatListViewEx::getItems(panels);

		std::vector<LLPanel*>::iterator iter = panels.begin();

		for (; iter != panels.end(); ++iter)
		{
			LLConversationLogListItem* item = dynamic_cast<LLConversationLogListItem*>(*iter);

			if (item && session_id == item->getConversation()->getSessionID() && !item->getConversation()->isConversationPast())
			{
				item->initIcons();
				return;
			}
		}
	}
}

void LLConversationLogList::addNewItem(const LLConversation* conversation)
{
	LLConversationLogListItem* item = new LLConversationLogListItem(&*conversation);
	if (!mNameFilter.empty())
	{
		item->highlightNameDate(mNameFilter);
	}
	addItem(item, conversation->getSessionID(), ADD_TOP);
}

void LLConversationLogList::refresh()
{
	rebuildList();
	sort();

	mIsDirty = false;
}

void LLConversationLogList::rebuildList()
{
	clear();

	bool have_filter = !mNameFilter.empty();

	const std::vector<LLConversation>& conversations = LLConversationLog::instance().getConversations();
	std::vector<LLConversation>::const_iterator iter = conversations.begin();

	for (; iter != conversations.end(); ++iter)
	{
		bool not_found = have_filter && !findInsensitive(iter->getConversationName(), mNameFilter) && !findInsensitive(iter->getTimestamp(), mNameFilter);
		if (not_found)
			continue;

		addNewItem(&*iter);
	}
}

void LLConversationLogList::onCustomAction(const LLSD& userdata)
{
	const std::string command_name = userdata.asString();
	const LLUUID& selected_id = getSelectedConversation()->getParticipantID();
	LLIMModel::LLIMSession::SType stype = getSelectedSessionType();

	if ("im" == command_name)
	{
		switch (stype)
		{
		case LLIMModel::LLIMSession::P2P_SESSION:
			LLAvatarActions::startIM(selected_id);
			break;

		case LLIMModel::LLIMSession::GROUP_SESSION:
			LLGroupActions::startIM(getSelectedConversation()->getSessionID());
			break;

		default:
			break;
		}
	}
	else if ("call" == command_name)
	{
		switch (stype)
		{
		case LLIMModel::LLIMSession::P2P_SESSION:
			LLAvatarActions::startCall(selected_id);
			break;

		case LLIMModel::LLIMSession::GROUP_SESSION:
			LLGroupActions::startCall(getSelectedConversation()->getSessionID());
			break;

		default:
			break;
		}
	}
	else if ("view_profile" == command_name)
	{
		switch (stype)
		{
		case LLIMModel::LLIMSession::P2P_SESSION:
			LLAvatarActions::showProfile(selected_id);
			break;

		case LLIMModel::LLIMSession::GROUP_SESSION:
			LLGroupActions::show(getSelectedConversation()->getSessionID());
			break;

		default:
			break;
		}
	}
	else if ("chat_history" == command_name)
	{
		const LLUUID& session_id = getSelectedConversation()->getSessionID();
		LLFloaterReg::showInstance("preview_conversation", session_id, true);
	}
	else if ("offer_teleport" == command_name)
	{
		LLAvatarActions::offerTeleport(selected_id);
	}
	else if("add_friend" == command_name)
	{
		if (!LLAvatarActions::isFriend(selected_id))
		{
			LLAvatarActions::requestFriendshipDialog(selected_id);
		}
	}
	else if("remove_friend" == command_name)
	{
		if (LLAvatarActions::isFriend(selected_id))
		{
			LLAvatarActions::removeFriendDialog(selected_id);
		}
	}
	else if ("invite_to_group" == command_name)
	{
		LLAvatarActions::inviteToGroup(selected_id);
	}
	else if ("show_on_map" == command_name)
	{
		LLAvatarActions::showOnMap(selected_id);
	}
	else if ("share" == command_name)
	{
		LLAvatarActions::share(selected_id);
	}
	else if ("pay" == command_name)
	{
		LLAvatarActions::pay(selected_id);
	}
	else if ("block" == command_name)
	{
		LLAvatarActions::toggleBlock(selected_id);
	}
}

bool LLConversationLogList::isActionEnabled(const LLSD& userdata)
{
	if (numSelected() != 1)
	{
		return false;
	}

	const std::string command_name = userdata.asString();

	LLIMModel::LLIMSession::SType stype = getSelectedSessionType();
	const LLUUID& selected_id = getSelectedConversation()->getParticipantID();

	bool is_p2p   = LLIMModel::LLIMSession::P2P_SESSION == stype;
	bool is_group = LLIMModel::LLIMSession::GROUP_SESSION == stype;

	if ("can_im" == command_name || "can_view_profile" == command_name)
	{
		return is_p2p || is_group;
	}
	else if ("can_view_chat_history" == command_name)
	{
		return true;
	}
	else if ("can_call"	== command_name)
	{
		return (is_p2p || is_group) && LLAvatarActions::canCall();
	}
	else if ("add_rem_friend"		== command_name ||
			 "can_invite_to_group"	== command_name ||
			 "can_share"			== command_name ||
			 "can_block"			== command_name ||
			 "can_pay"				== command_name)
	{
		return is_p2p;
	}
	else if("can_offer_teleport" == command_name)
	{
		return is_p2p && LLAvatarActions::canOfferTeleport(selected_id);
	}
	else if ("can_show_on_map")
	{
		return is_p2p && ((LLAvatarTracker::instance().isBuddyOnline(selected_id) && is_agent_mappable(selected_id)) || gAgent.isGodlike());
	}

	return false;
}

bool LLConversationLogList::isActionChecked(const LLSD& userdata)
{
	const std::string command_name = userdata.asString();

	const LLUUID& selected_id = getSelectedConversation()->getParticipantID();
	bool is_p2p = LLIMModel::LLIMSession::P2P_SESSION == getSelectedSessionType();

	if ("is_blocked" == command_name)
	{
		return is_p2p && LLAvatarActions::isBlocked(selected_id);
	}
	else if ("is_friend" == command_name)
	{
		return is_p2p && LLAvatarActions::isFriend(selected_id);
	}
	else if ("is_not_friend" == command_name)
	{
		return is_p2p && !LLAvatarActions::isFriend(selected_id);
	}

	return false;
}

LLIMModel::LLIMSession::SType LLConversationLogList::getSelectedSessionType()
{
	const LLConversationLogListItem* item = getSelectedConversationPanel();

	if (item)
	{
		return item->getConversation()->getConversationType();
	}

	return LLIMModel::LLIMSession::NONE_SESSION;
}

const LLConversationLogListItem* LLConversationLogList::getSelectedConversationPanel()
{
	LLPanel* panel = LLFlatListViewEx::getSelectedItem();
	LLConversationLogListItem* conv_panel = dynamic_cast<LLConversationLogListItem*>(panel);

	return conv_panel;
}

const LLConversation* LLConversationLogList::getSelectedConversation()
{
	const LLConversationLogListItem* panel = getSelectedConversationPanel();

	if (panel)
	{
		return panel->getConversation();
	}

	return NULL;
}

bool LLConversationLogListItemComparator::compare(const LLPanel* item1, const LLPanel* item2) const
{
	const LLConversationLogListItem* conversation_item1 = dynamic_cast<const LLConversationLogListItem*>(item1);
	const LLConversationLogListItem* conversation_item2 = dynamic_cast<const LLConversationLogListItem*>(item2);

	if (!conversation_item1 || !conversation_item2)
	{
		llerror("conversation_item1 and conversation_item2 cannot be null", 0);
		return true;
	}

	return doCompare(conversation_item1, conversation_item2);
}

bool LLConversationLogListNameComparator::doCompare(const LLConversationLogListItem* conversation1, const LLConversationLogListItem* conversation2) const
{
	std::string name1 = conversation1->getConversation()->getConversationName();
	std::string name2 = conversation2->getConversation()->getConversationName();
	const LLUUID& id1 = conversation1->getConversation()->getParticipantID();
	const LLUUID& id2 = conversation2->getConversation()->getParticipantID();

	LLStringUtil::toUpper(name1);
	LLStringUtil::toUpper(name2);

	bool friends_first = gSavedSettings.getBOOL("SortFriendsFirst");
	if (friends_first && (LLAvatarActions::isFriend(id1) ^ LLAvatarActions::isFriend(id2)))
	{
		return LLAvatarActions::isFriend(id1);
	}

	return name1 < name2;
}

bool LLConversationLogListDateComparator::doCompare(const LLConversationLogListItem* conversation1, const LLConversationLogListItem* conversation2) const
{
	time_t date1 = conversation1->getConversation()->getTime();
	time_t date2 = conversation2->getConversation()->getTime();
	const LLUUID& id1 = conversation1->getConversation()->getParticipantID();
	const LLUUID& id2 = conversation2->getConversation()->getParticipantID();

	bool friends_first = gSavedSettings.getBOOL("SortFriendsFirst");
	if (friends_first && (LLAvatarActions::isFriend(id1) ^ LLAvatarActions::isFriend(id2)))
	{
		return LLAvatarActions::isFriend(id1);
	}

	return date1 > date2;
}