summaryrefslogtreecommitdiff
path: root/indra/newview/llinventorylistener.cpp
blob: 79726c3e0b3271fddebb8e55299aea900bdd95ec (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
/**
 * @file llinventorylistener.cpp
 *
 * $LicenseInfo:firstyear=2024&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2024, 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 "llinventorylistener.h"

#include "llappearancemgr.h"
#include "llinventoryfunctions.h"
#include "lltransutil.h"
#include "llwearableitemslist.h"
#include "resultset.h"
#include "stringize.h"
#include <algorithm>                // std::min()

constexpr S32 MAX_ITEM_LIMIT = 100;

LLInventoryListener::LLInventoryListener()
  : LLEventAPI("LLInventory",
               "API for interactions with viewer Inventory items")
{
    add("getItemsInfo",
        "Return information about items or folders defined in [\"item_ids\"]:\n"
        "reply will contain [\"items\"] and [\"categories\"] result set keys",
        &LLInventoryListener::getItemsInfo,
        llsd::map("item_ids", LLSD(), "reply", LLSD()));

    add("getFolderTypeNames",
        "Return the table of folder type names, contained in [\"names\"]\n",
        &LLInventoryListener::getFolderTypeNames,
        llsd::map("reply", LLSD()));

    add("getAssetTypeNames",
        "Return the table of asset type names, contained in [\"names\"]\n",
        &LLInventoryListener::getAssetTypeNames,
        llsd::map("reply", LLSD()));

    add("getBasicFolderID",
        "Return the UUID of the folder by specified folder type name, for example:\n"
        "\"Textures\", \"My outfits\", \"Sounds\" and other basic folders which have associated type",
        &LLInventoryListener::getBasicFolderID,
        llsd::map("ft_name", LLSD(), "reply", LLSD()));

    add("getDirectDescendants",
        "Return result set keys [\"categories\"] and [\"items\"] for the direct\n"
        "descendants of the [\"folder_id\"]",
        &LLInventoryListener::getDirectDescendants,
        llsd::map("folder_id", LLSD(), "reply", LLSD()));

    add("collectDescendantsIf",
        "Return result set keys [\"categories\"] and [\"items\"] for the descendants\n"
        "of the [\"folder_id\"], if it passes specified filters:\n"
        "[\"name\"] is a substring of object's name,\n"
        "[\"desc\"] is a substring of object's description,\n"
        "asset [\"type\"] corresponds to the string name of the object's asset type\n"
        "[\"limit\"] sets item count limit in result set (default unlimited)\n"
        "[\"filter_links\"]: EXCLUDE_LINKS - don't show links, ONLY_LINKS - only show links, INCLUDE_LINKS - show links too (default)",
        &LLInventoryListener::collectDescendantsIf,
        llsd::map("folder_id", LLSD(), "reply", LLSD()));

/*==========================================================================*|
    add("getSingle",
        "Return LLSD [\"single\"] for a single folder or item from the specified\n"
        "[\"result\"] key at the specified 0-relative [\"index\"].",
        &LLInventoryListener::getSingle,
        llsd::map("result", LLSD::Integer(), "index", LLSD::Integer(),
                  "reply", LLSD::String()));
|*==========================================================================*/

    add("getSlice",
        stringize(
        "Return an LLSD array [\"slice\"] from the specified [\"result\"] key\n"
        "starting at 0-relative [\"index\"] with (up to) [\"count\"] entries.\n"
        "count is limited to ", MAX_ITEM_LIMIT, " (default and max)."),
        &LLInventoryListener::getSlice,
        llsd::map("result", LLSD::Integer(), "index", LLSD::Integer(),
                  "reply", LLSD::String()));

    add("closeResult",
        "Release resources associated with specified [\"result\"] key,\n"
        "or keys if [\"result\"] is an array.",
        &LLInventoryListener::closeResult,
        llsd::map("result", LLSD()));
}

// This struct captures (possibly large) category results from
// getDirectDescendants() and collectDescendantsIf().
struct CatResultSet: public LL::ResultSet
{
    CatResultSet(): LL::ResultSet("categories") {}
    LLInventoryModel::cat_array_t mCategories;

    int getLength() const override { return narrow(mCategories.size()); }
    LLSD getSingle(int index) const override
    {
        auto cat = mCategories[index];
        return llsd::map("name", cat->getName(),
                         "parent_id", cat->getParentUUID(),
                         "type", LLFolderType::lookup(cat->getPreferredType()));
    }
};

// This struct captures (possibly large) item results from
// getDirectDescendants() and collectDescendantsIf().
struct ItemResultSet: public LL::ResultSet
{
    ItemResultSet(): LL::ResultSet("items") {}
    LLInventoryModel::item_array_t mItems;

    int getLength() const override { return narrow(mItems.size()); }
    LLSD getSingle(int index) const override
    {
        auto item = mItems[index];
        return llsd::map("name", item->getName(),
                         "parent_id", item->getParentUUID(),
                         "desc", item->getDescription(),
                         "inv_type", LLInventoryType::lookup(item->getInventoryType()),
                         "asset_type", LLAssetType::lookup(item->getType()),
                         "creation_date", LLSD::Integer(item->getCreationDate()),
                         "asset_id", item->getAssetUUID(),
                         "is_link", item->getIsLinkType(),
                         "linked_id", item->getLinkedUUID());
    }
};

void LLInventoryListener::getItemsInfo(LLSD const &data)
{
    Response response(LLSD(), data);

    auto catresult = new CatResultSet;
    auto itemresult = new ItemResultSet;

    uuid_vec_t ids = LLSDParam<uuid_vec_t>(data["item_ids"]);
    for (auto &it : ids)
    {
        LLViewerInventoryItem* item = gInventory.getItem(it);
        if (item)
        {
            itemresult->mItems.push_back(item);
        }
        else
        {
            LLViewerInventoryCategory *cat = gInventory.getCategory(it);
            if (cat)
            {
                catresult->mCategories.push_back(cat);
            }
        }
    }
    // Each of categories and items is a { result set key, total length } pair.
    response["categories"] = catresult->getKeyLength();
    response["items"] = itemresult->getKeyLength();
}

void LLInventoryListener::getFolderTypeNames(LLSD const &data)
{
    Response response(llsd::map("names", LLFolderType::getTypeNames()), data);
}

void LLInventoryListener::getAssetTypeNames(LLSD const &data)
{
    Response response(llsd::map("names", LLAssetType::getTypeNames()), data);
}

void LLInventoryListener::getBasicFolderID(LLSD const &data)
{
    Response response(llsd::map("id", gInventory.findCategoryUUIDForType(LLFolderType::lookup(data["ft_name"].asString()))), data);
}


void LLInventoryListener::getDirectDescendants(LLSD const &data)
{
    Response response(LLSD(), data);
    LLInventoryModel::cat_array_t* cats;
    LLInventoryModel::item_array_t* items;
    gInventory.getDirectDescendentsOf(data["folder_id"], cats, items);

    auto catresult = new CatResultSet;
    auto itemresult = new ItemResultSet;

    catresult->mCategories = *cats;
    itemresult->mItems = *items;

    response["categories"] = catresult->getKeyLength();
    response["items"] = itemresult->getKeyLength();
}

struct LLFilteredCollector : public LLInventoryCollectFunctor
{
    enum EFilterLink
    {
        INCLUDE_LINKS,  // show links too
        EXCLUDE_LINKS,  // don't show links
        ONLY_LINKS      // only show links
    };

    LLFilteredCollector(LLSD const &data);
    virtual ~LLFilteredCollector() {}
    virtual bool operator()(LLInventoryCategory *cat, LLInventoryItem *item) override;
    virtual bool exceedsLimit() override
    {
        // mItemLimit == 0 means unlimited
        return (mItemLimit && mItemLimit <= mItemCount);
    }

  protected:
    bool checkagainstType(LLInventoryCategory *cat, LLInventoryItem *item);
    bool checkagainstNameDesc(LLInventoryCategory *cat, LLInventoryItem *item);
    bool checkagainstLinks(LLInventoryCategory *cat, LLInventoryItem *item);

    LLAssetType::EType mType;
    std::string mName;
    std::string mDesc;
    EFilterLink mLinkFilter;

    S32 mItemLimit;
    S32 mItemCount;
};

void LLInventoryListener::collectDescendantsIf(LLSD const &data)
{
    Response response(LLSD(), data);
    LLUUID folder_id(data["folder_id"].asUUID());
    LLViewerInventoryCategory *cat = gInventory.getCategory(folder_id);
    if (!cat)
    {
        return response.error(stringize("Folder ", std::quoted(data["folder_id"].asString()), " was not found"));
    }
    auto catresult = new CatResultSet;
    auto itemresult = new ItemResultSet;

    LLFilteredCollector collector = LLFilteredCollector(data);

    // Populate results directly into the catresult and itemresult arrays.
    // TODO: sprinkle count-based coroutine yields into the real
    // collectDescendentsIf() method so it doesn't steal too many cycles.
    gInventory.collectDescendentsIf(
        folder_id,
        catresult->mCategories,
        itemresult->mItems,
        LLInventoryModel::EXCLUDE_TRASH,
        collector);

    response["categories"] = catresult->getKeyLength();
    response["items"] = itemresult->getKeyLength();
}

/*==========================================================================*|
void LLInventoryListener::getSingle(LLSD const& data)
{
    auto result = LL::ResultSet::getInstance(data["result"]);
    sendReply(llsd::map("single", result->getSingle(data["index"])), data);
}
|*==========================================================================*/

void LLInventoryListener::getSlice(LLSD const& data)
{
    auto result = LL::ResultSet::getInstance(data["result"]);
    int count = data.has("count")? data["count"].asInteger() : MAX_ITEM_LIMIT;
    LL_DEBUGS("Lua") << *result << ".getSlice(" << data["index"].asInteger()
                     << ", " << count << ')' << LL_ENDL;
    auto pair{ result->getSliceStart(data["index"], std::min(count, MAX_ITEM_LIMIT)) };
    sendReply(llsd::map("slice", pair.first, "start", pair.second), data);
}

void LLInventoryListener::closeResult(LLSD const& data)
{
    LLSD results = data["result"];
    if (results.isInteger())
    {
        results = llsd::array(results);
    }
    for (const auto& result : llsd::inArray(results))
    {
        auto ptr = LL::ResultSet::getInstance(result);
        if (ptr)
        {
            delete ptr.get();
        }
    }
}

LLFilteredCollector::LLFilteredCollector(LLSD const &data) :
    mType(LLAssetType::EType::AT_UNKNOWN),
    mLinkFilter(INCLUDE_LINKS),
    mItemLimit(0),
    mItemCount(0)
{

    mName = data["name"].asString();
    mDesc = data["desc"].asString();

    if (data.has("type"))
    {
        mType = LLAssetType::lookup(data["type"]);
    }
    if (data.has("filter_links"))
    {
        if (data["filter_links"] == "EXCLUDE_LINKS")
        {
            mLinkFilter = EXCLUDE_LINKS;
        }
        else if (data["filter_links"] == "ONLY_LINKS")
        {
            mLinkFilter = ONLY_LINKS;
        }
    }
    if (data["limit"].isInteger())
    {
        mItemLimit = std::max(data["limit"].asInteger(), 1);
    }
}

bool LLFilteredCollector::operator()(LLInventoryCategory *cat, LLInventoryItem *item)
{
    bool passed = checkagainstType(cat, item);
    passed = passed && checkagainstNameDesc(cat, item);
    passed = passed && checkagainstLinks(cat, item);

    if (passed)
    {
        ++mItemCount;
    }
    return passed;
}

bool LLFilteredCollector::checkagainstNameDesc(LLInventoryCategory *cat, LLInventoryItem *item)
{
    std::string name, desc;
    bool passed(true);
    if (cat)
    {
        if (!mDesc.empty()) return false;
        name = cat->getName();
    }
    if (item)
    {
        name = item->getName();
        passed = (mDesc.empty() || (item->getDescription().find(mDesc) != std::string::npos));
    }

    return passed && (mName.empty() || name.find(mName) != std::string::npos);
}

bool LLFilteredCollector::checkagainstType(LLInventoryCategory *cat, LLInventoryItem *item)
{
    if (mType == LLAssetType::AT_UNKNOWN)
    {
        return true;
    }
    if (cat && (mType == LLAssetType::AT_CATEGORY))
    {
        return true;
    }
    if (item && item->getType() == mType)
    {
        return true;
    }
    return false;
}

bool LLFilteredCollector::checkagainstLinks(LLInventoryCategory *cat, LLInventoryItem *item)
{
    bool is_link = cat ? cat->getIsLinkType() : item->getIsLinkType();
    if (is_link && (mLinkFilter == EXCLUDE_LINKS))
        return false;
    if (!is_link && (mLinkFilter == ONLY_LINKS))
        return false;
    return true;
}