summaryrefslogtreecommitdiff
path: root/indra/newview/llpanelgroupbulk.cpp
blob: 8032e207cd567b44dae542e599448136dcf64c8e (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
/**
* @file llpanelgroupbulk.cpp
* @brief Implementation of llpanelgroupbulk
* @author Baker@lindenlab.com
*
* $LicenseInfo:firstyear=2013&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2013, 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 "llpanelgroupbulk.h"
#include "llpanelgroupbulkimpl.h"

#include "llagent.h"
#include "llavatarnamecache.h"
#include "llfloateravatarpicker.h"
#include "llbutton.h"
#include "llcallingcard.h"
#include "llcombobox.h"
#include "llgroupactions.h"
#include "llgroupmgr.h"
#include "llnamelistctrl.h"
#include "llnotificationsutil.h"
#include "llscrolllistitem.h"
#include "llspinctrl.h"
#include "lltextbox.h"
#include "llviewerobject.h"
#include "llviewerobjectlist.h"
#include "lluictrlfactory.h"
#include "llviewerwindow.h"


//////////////////////////////////////////////////////////////////////////
// Implementation of llpanelgroupbulkimpl.h functions
//////////////////////////////////////////////////////////////////////////
LLPanelGroupBulkImpl::LLPanelGroupBulkImpl(const LLUUID& group_id) :
    mGroupID(group_id),
    mBulkAgentList(NULL),
    mOKButton(NULL),
    mAddButton(nullptr),
    mRemoveButton(NULL),
    mGroupName(NULL),
    mLoadingText(),
    mTooManySelected(),
    mCloseCallback(NULL),
    mCloseCallbackUserData(NULL),
    mAvatarNameCacheConnection(),
    mRoleNames(NULL),
    mOwnerWarning(),
    mAlreadyInGroup(),
    mConfirmedOwnerInvite(false),
    mListFullNotificationSent(false)
{
}

LLPanelGroupBulkImpl::~LLPanelGroupBulkImpl()
{
    if (mAvatarNameCacheConnection.connected())
    {
        mAvatarNameCacheConnection.disconnect();
    }
}

void LLPanelGroupBulkImpl::callbackClickAdd(LLPanelGroupBulk* panelp)
{
    LLFloater* root_floater = gFloaterView->getParentFloater(panelp);
    LLFloaterAvatarPicker* picker = LLFloaterAvatarPicker::show(
        [this](const uuid_vec_t& agent_ids, const std::vector<LLAvatarName>&)
        {
            addUsers(agent_ids);
        }, true, false, false, root_floater->getName(), mAddButton);
    if (picker)
    {
        root_floater->addDependentFloater(picker);
        LLGroupMgr::getInstance()->sendCapGroupMembersRequest(mGroupID);
    }
}

// static
void LLPanelGroupBulkImpl::callbackClickRemove(void* userdata)
{
    if (LLPanelGroupBulkImpl* selfp = (LLPanelGroupBulkImpl*)userdata)
    {
        selfp->handleRemove();
    }
}

// static
void LLPanelGroupBulkImpl::callbackClickCancel(void* userdata)
{
    if (LLPanelGroupBulkImpl* selfp = (LLPanelGroupBulkImpl*)userdata)
    {
        (*(selfp->mCloseCallback))(selfp->mCloseCallbackUserData);
    }
}

// static
void LLPanelGroupBulkImpl::callbackSelect(LLUICtrl* ctrl, void* userdata)
{
    if (LLPanelGroupBulkImpl* selfp = (LLPanelGroupBulkImpl*)userdata)
    {
        selfp->handleSelection();
    }
}

void LLPanelGroupBulkImpl::addUsers(const uuid_vec_t& agent_ids)
{
    std::vector<std::string> names;
    for (const LLUUID& agent_id : agent_ids)
    {
        LLAvatarName av_name;
        if (LLAvatarNameCache::get(agent_id, &av_name))
        {
            onAvatarNameCache(agent_id, av_name);
        }
        else
        {
            if (mAvatarNameCacheConnection.connected())
            {
                mAvatarNameCacheConnection.disconnect();
            }
            // *TODO : Add a callback per avatar name being fetched.
            mAvatarNameCacheConnection = LLAvatarNameCache::get(agent_id,
                [&](const LLUUID& agent_id, const LLAvatarName& av_name)
                {
                    onAvatarNameCache(agent_id, av_name);
                });
        }
    }
}

void LLPanelGroupBulkImpl::onAvatarNameCache(const LLUUID& agent_id, const LLAvatarName& av_name)
{
    if (mAvatarNameCacheConnection.connected())
    {
        mAvatarNameCacheConnection.disconnect();
    }

    std::vector<std::string> names;
    uuid_vec_t agent_ids;
    agent_ids.push_back(agent_id);
    names.push_back(av_name.getCompleteName());

    addUsers(names, agent_ids);
}

void LLPanelGroupBulkImpl::handleRemove()
{
    std::vector<LLScrollListItem*> selection = mBulkAgentList->getAllSelected();
    if (selection.empty())
        return;

    for (const LLScrollListItem* item : selection)
    {
        mInviteeIDs.erase(item->getUUID());
    }

    mBulkAgentList->deleteSelectedItems();
    mRemoveButton->setEnabled(false);

    if (mOKButton && mOKButton->getEnabled() && mBulkAgentList->isEmpty())
    {
        mOKButton->setEnabled(false);
    }
}

void LLPanelGroupBulkImpl::handleSelection()
{
    mRemoveButton->setEnabled(mBulkAgentList->getFirstSelected());
}

void LLPanelGroupBulkImpl::addUsers(const std::vector<std::string>& names, const uuid_vec_t& agent_ids)
{
    if (mListFullNotificationSent)
    {
        return;
    }

    if (!mListFullNotificationSent &&
        (names.size() + mInviteeIDs.size() > MAX_GROUP_INVITES))
    {
        mListFullNotificationSent = true;

        // Fail! Show a warning and don't add any names.
        LLSD msg;
        msg["MESSAGE"] = mTooManySelected;
        LLNotificationsUtil::add("GenericAlert", msg);
        return;
    }

    for (S32 i = 0; i < (S32)names.size(); ++i)
    {
        const LLUUID& id = agent_ids[i];

        if (mInviteeIDs.find(id) != mInviteeIDs.end())
        {
            continue;
        }

        // Add the name to the name list
        LLSD row;
        row["id"] = id;
        row["columns"][0]["value"] = names[i];

        mBulkAgentList->addElement(row);
        mInviteeIDs.insert(id);

        // We've successfully added someone to the list.
        if (mOKButton && !mOKButton->getEnabled())
        {
            mOKButton->setEnabled(true);
        }
    }
}

void LLPanelGroupBulkImpl::setGroupName(std::string name)
{
    if (mGroupName)
    {
        mGroupName->setText(name);
    }
}


LLPanelGroupBulk::LLPanelGroupBulk(const LLUUID& group_id) :
    LLPanel(),
    mImplementation(new LLPanelGroupBulkImpl(group_id)),
    mPendingGroupPropertiesUpdate(false),
    mPendingRoleDataUpdate(false),
    mPendingMemberDataUpdate(false)
{
}

LLPanelGroupBulk::~LLPanelGroupBulk()
{
    delete mImplementation;
}

void LLPanelGroupBulk::clear()
{
    mImplementation->mInviteeIDs.clear();

    if (mImplementation->mBulkAgentList)
    {
        mImplementation->mBulkAgentList->deleteAllItems();
    }

    if (mImplementation->mOKButton)
    {
        mImplementation->mOKButton->setEnabled(false);
    }
}

void LLPanelGroupBulk::update()
{
    updateGroupName();
    updateGroupData();
}

void LLPanelGroupBulk::draw()
{
    LLPanel::draw();
    update();
}

void LLPanelGroupBulk::updateGroupName()
{
    LLGroupMgrGroupData* gdatap = LLGroupMgr::getInstance()->getGroupData(mImplementation->mGroupID);

    if (gdatap &&
        gdatap->isGroupPropertiesDataComplete())
    {
        // Only do work if the current group name differs
        if (mImplementation->mGroupName->getText().compare(gdatap->mName) != 0)
        {
            mImplementation->setGroupName(gdatap->mName);
        }
    }
    else
    {
        mImplementation->setGroupName(mImplementation->mLoadingText);
    }
}

void LLPanelGroupBulk::updateGroupData()
{
    LLGroupMgrGroupData* gdatap = LLGroupMgr::getInstance()->getGroupData(mImplementation->mGroupID);
    if (gdatap && gdatap->isGroupPropertiesDataComplete())
    {
        mPendingGroupPropertiesUpdate = false;
    }
    else if (!mPendingGroupPropertiesUpdate)
    {
        mPendingGroupPropertiesUpdate = true;
        LLGroupMgr::getInstance()->sendGroupPropertiesRequest(mImplementation->mGroupID);
    }

    if (gdatap && gdatap->isRoleDataComplete())
    {
        mPendingRoleDataUpdate = false;
    }
    else if (!mPendingRoleDataUpdate)
    {
        mPendingRoleDataUpdate = true;
        LLGroupMgr::getInstance()->sendGroupRoleDataRequest(mImplementation->mGroupID);
    }

    if (gdatap && gdatap->isMemberDataComplete())
    {
        mPendingMemberDataUpdate = false;
    }
    else if (!mPendingMemberDataUpdate)
    {
        mPendingMemberDataUpdate = true;
        LLGroupMgr::getInstance()->sendCapGroupMembersRequest(mImplementation->mGroupID);
    }
}

void LLPanelGroupBulk::addUserCallback(const LLUUID& id, const LLAvatarName& av_name)
{
    std::vector<std::string> names;
    uuid_vec_t agent_ids;
    agent_ids.push_back(id);
    names.push_back(av_name.getAccountName());

    mImplementation->addUsers(names, agent_ids);
}

void LLPanelGroupBulk::setCloseCallback(void (*close_callback)(void*), void* data)
{
    mImplementation->mCloseCallback         = close_callback;
    mImplementation->mCloseCallbackUserData = data;
}

void LLPanelGroupBulk::addUsers(uuid_vec_t& agent_ids)
{
    std::vector<std::string> names;
    for (size_t i = 0; i < agent_ids.size(); i++)
    {
        std::string fullname;
        const LLUUID& agent_id = agent_ids[i];
        LLViewerObject* dest = gObjectList.findObject(agent_id);
        if (dest && dest->isAvatar())
        {
            LLNameValue* nvfirst = dest->getNVPair("FirstName");
            LLNameValue* nvlast = dest->getNVPair("LastName");
            if (nvfirst && nvlast)
            {
                fullname = LLCacheName::buildFullName(nvfirst->getString(), nvlast->getString());
            }

            if (!fullname.empty())
            {
                names.push_back(fullname);
            }
            else
            {
                LL_WARNS() << "llPanelGroupBulk: Selected avatar has no name: " << dest->getID() << LL_ENDL;
                names.push_back("(Unknown)");
            }
        }
        else
        {
            // Looks like the user tries to invite a friend which is offline.
            // For offline avatar_id gObjectList.findObject() will return null
            // so we need to do this additional search in avatar tracker, see EXT-4732
            if (LLAvatarTracker::instance().isBuddy(agent_id))
            {
                LLAvatarName av_name;
                if (!LLAvatarNameCache::get(agent_id, &av_name))
                {
                    // Actually it shouldn't happen, just in case
                    LLAvatarNameCache::get(LLUUID(agent_id),
                        [&](const LLUUID& agent_id, const LLAvatarName& av_name)
                        {
                            addUserCallback(agent_id, av_name);
                        });
                    // for this special case!
                    // when there is no cached name we should remove resident from agent_ids list to avoid breaking of sequence
                    // removed id will be added in callback
                    agent_ids.erase(agent_ids.begin() + i);
                    i--; // To process the next agent_id with the same index
                }
                else
                {
                    names.push_back(av_name.getAccountName());
                }
            }
        }
    }
    mImplementation->mListFullNotificationSent = false;
    mImplementation->addUsers(names, agent_ids);
}