summaryrefslogtreecommitdiff
path: root/indra/newview/llfloatermyscripts.cpp
blob: e4ecb934ee3ff945162abf5a8bf800e9eb586f4b (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
/**
 * @file llfloatermyscripts.cpp
 * @brief LLFloaterMyScripts class implementation.
 *
 * $LicenseInfo:firstyear=2004&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2019, 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 "llfloatermyscripts.h"

#include "llagent.h"
#include "llcorehttputil.h"
#include "llcoros.h"
#include "lleventcoro.h"
#include "llfloaterreg.h"
#include "llscrolllistctrl.h"
#include "lltrans.h"
#include "llviewerregion.h"

constexpr S32 SIZE_OF_ONE_KB = 1024;

LLFloaterMyScripts::LLFloaterMyScripts(const LLSD& seed)
    : LLFloater(seed),
    mGotAttachmentMemoryUsed(false),
    mAttachmentDetailsRequested(false),
    mAttachmentMemoryMax(0),
    mAttachmentMemoryUsed(0),
    mGotAttachmentURLsUsed(false),
    mAttachmentURLsMax(0),
    mAttachmentURLsUsed(0)
{
}

bool LLFloaterMyScripts::postBuild()
{
    childSetAction("refresh_list_btn", onClickRefresh, this);

    std::string msg_waiting = LLTrans::getString("ScriptLimitsRequestWaiting");
    getChild<LLUICtrl>("loading_text")->setValue(LLSD(msg_waiting));
    mAttachmentDetailsRequested = requestAttachmentDetails();
    return true;
}

// virtual
void LLFloaterMyScripts::onOpen(const LLSD& key)
{
    if (!mAttachmentDetailsRequested)
    {
        mAttachmentDetailsRequested = requestAttachmentDetails();
    }

    LLFloater::onOpen(key);
}

bool LLFloaterMyScripts::requestAttachmentDetails()
{
    if (!gAgent.getRegion()) return false;

    LLSD body;
    std::string url = gAgent.getRegion()->getCapability("AttachmentResources");
    if (!url.empty())
    {
        LLCoros::instance().launch("LLFloaterMyScripts::getAttachmentLimitsCoro",
            boost::bind(&LLFloaterMyScripts::getAttachmentLimitsCoro, this, url));
        return true;
    }
    else
    {
        return false;
    }
}

void LLFloaterMyScripts::getAttachmentLimitsCoro(std::string url)
{
    LLCore::HttpRequest::policy_t httpPolicy(LLCore::HttpRequest::DEFAULT_POLICY_ID);
    LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t
        httpAdapter(new LLCoreHttpUtil::HttpCoroutineAdapter("getAttachmentLimitsCoro", httpPolicy));
    LLCore::HttpRequest::ptr_t httpRequest(new LLCore::HttpRequest);

    LLSD result = httpAdapter->getAndSuspend(httpRequest, url);

    LLSD httpResults = result[LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS];
    LLCore::HttpStatus status = LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(httpResults);

    if (!status)
    {
        LL_WARNS() << "Unable to retrieve attachment limits." << LL_ENDL;
        return;
    }

    LLFloaterMyScripts* instance = LLFloaterReg::getTypedInstance<LLFloaterMyScripts>("my_scripts");

    if (!instance)
    {
        LL_WARNS() << "Failed to get LLFloaterMyScripts instance" << LL_ENDL;
        return;
    }

    instance->getChild<LLUICtrl>("loading_text")->setValue(LLSD(std::string("")));

    LLButton* btn = instance->getChild<LLButton>("refresh_list_btn");
    if (btn)
    {
        btn->setEnabled(true);
    }

    result.erase(LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS);
    instance->setAttachmentDetails(result);
}


void LLFloaterMyScripts::setAttachmentDetails(LLSD content)
{
    LLScrollListCtrl *list = getChild<LLScrollListCtrl>("scripts_list");

    if(!list)
    {
        return;
    }

    S32 number_attachments = content["attachments"].size();

    for(int i = 0; i < number_attachments; i++)
    {
        std::string humanReadableLocation = "";
        if(content["attachments"][i].has("location"))
        {
            std::string actualLocation = content["attachments"][i]["location"];
            humanReadableLocation = LLTrans::getString(actualLocation.c_str());
        }

        S32 number_objects = content["attachments"][i]["objects"].size();
        for(int j = 0; j < number_objects; j++)
        {
            LLUUID task_id = content["attachments"][i]["objects"][j]["id"].asUUID();
            S32 size = 0;
            if(content["attachments"][i]["objects"][j]["resources"].has("memory"))
            {
                size = content["attachments"][i]["objects"][j]["resources"]["memory"].asInteger() / SIZE_OF_ONE_KB;
            }
            S32 urls = 0;
            if(content["attachments"][i]["objects"][j]["resources"].has("urls"))
            {
                urls = content["attachments"][i]["objects"][j]["resources"]["urls"].asInteger();
            }
            std::string name = content["attachments"][i]["objects"][j]["name"].asString();

            LLSD element;

            element["id"] = task_id;
            element["columns"][0]["column"] = "size";
            element["columns"][0]["value"] = llformat("%d", size);
            element["columns"][0]["font"] = "SANSSERIF";
            element["columns"][0]["halign"] = LLFontGL::RIGHT;

            element["columns"][1]["column"] = "urls";
            element["columns"][1]["value"] = llformat("%d", urls);
            element["columns"][1]["font"] = "SANSSERIF";
            element["columns"][1]["halign"] = LLFontGL::RIGHT;

            element["columns"][2]["column"] = "name";
            element["columns"][2]["value"] = name;
            element["columns"][2]["font"] = "SANSSERIF";

            element["columns"][3]["column"] = "location";
            element["columns"][3]["value"] = humanReadableLocation;
            element["columns"][3]["font"] = "SANSSERIF";

            list->addElement(element);
        }
    }

    setAttachmentSummary(content);

    getChild<LLUICtrl>("loading_text")->setValue(LLSD(std::string("")));

    LLButton* btn = getChild<LLButton>("refresh_list_btn");
    if(btn)
    {
        btn->setEnabled(true);
    }
}

void LLFloaterMyScripts::clearList()
{
    LLCtrlListInterface *list = childGetListInterface("scripts_list");

    if (list)
    {
        list->operateOnAll(LLCtrlListInterface::OP_DELETE);
    }

    std::string msg_waiting = LLTrans::getString("ScriptLimitsRequestWaiting");
    getChild<LLUICtrl>("loading_text")->setValue(LLSD(msg_waiting));
}

void LLFloaterMyScripts::setAttachmentSummary(LLSD content)
{
    if(content["summary"]["used"][0]["type"].asString() == std::string("memory"))
    {
        mAttachmentMemoryUsed = content["summary"]["used"][0]["amount"].asInteger() / SIZE_OF_ONE_KB;
        mAttachmentMemoryMax = content["summary"]["available"][0]["amount"].asInteger() / SIZE_OF_ONE_KB;
        mGotAttachmentMemoryUsed = true;
    }
    else if(content["summary"]["used"][1]["type"].asString() == std::string("memory"))
    {
        mAttachmentMemoryUsed = content["summary"]["used"][1]["amount"].asInteger() / SIZE_OF_ONE_KB;
        mAttachmentMemoryMax = content["summary"]["available"][1]["amount"].asInteger() / SIZE_OF_ONE_KB;
        mGotAttachmentMemoryUsed = true;
    }
    else
    {
        LL_WARNS() << "attachment details don't contain memory summary info" << LL_ENDL;
        return;
    }

    if(content["summary"]["used"][0]["type"].asString() == std::string("urls"))
    {
        mAttachmentURLsUsed = content["summary"]["used"][0]["amount"].asInteger();
        mAttachmentURLsMax = content["summary"]["available"][0]["amount"].asInteger();
        mGotAttachmentURLsUsed = true;
    }
    else if(content["summary"]["used"][1]["type"].asString() == std::string("urls"))
    {
        mAttachmentURLsUsed = content["summary"]["used"][1]["amount"].asInteger();
        mAttachmentURLsMax = content["summary"]["available"][1]["amount"].asInteger();
        mGotAttachmentURLsUsed = true;
    }
    else
    {
        LL_WARNS() << "attachment details don't contain urls summary info" << LL_ENDL;
        return;
    }

    if((mAttachmentMemoryUsed >= 0) && (mAttachmentMemoryMax >= 0))
    {
        LLStringUtil::format_map_t args_attachment_memory;
        args_attachment_memory["[COUNT]"] = llformat ("%d", mAttachmentMemoryUsed);
        std::string translate_message = "ScriptLimitsMemoryUsedSimple";

        if (0 < mAttachmentMemoryMax)
        {
            S32 attachment_memory_available = mAttachmentMemoryMax - mAttachmentMemoryUsed;

            args_attachment_memory["[MAX]"] = llformat ("%d", mAttachmentMemoryMax);
            args_attachment_memory["[AVAILABLE]"] = llformat ("%d", attachment_memory_available);
            translate_message = "ScriptLimitsMemoryUsed";
        }

        getChild<LLUICtrl>("memory_used")->setValue(LLTrans::getString(translate_message, args_attachment_memory));
    }

    if((mAttachmentURLsUsed >= 0) && (mAttachmentURLsMax >= 0))
    {
        S32 attachment_urls_available = mAttachmentURLsMax - mAttachmentURLsUsed;

        LLStringUtil::format_map_t args_attachment_urls;
        args_attachment_urls["[COUNT]"] = llformat ("%d", mAttachmentURLsUsed);
        args_attachment_urls["[MAX]"] = llformat ("%d", mAttachmentURLsMax);
        args_attachment_urls["[AVAILABLE]"] = llformat ("%d", attachment_urls_available);
        std::string msg_attachment_urls = LLTrans::getString("ScriptLimitsURLsUsed", args_attachment_urls);
        getChild<LLUICtrl>("urls_used")->setValue(LLSD(msg_attachment_urls));
    }
}

// static
void LLFloaterMyScripts::onClickRefresh(void* userdata)
{
    LLFloaterMyScripts* instance = LLFloaterReg::getTypedInstance<LLFloaterMyScripts>("my_scripts");
    if(instance)
    {
        LLButton* btn = instance->getChild<LLButton>("refresh_list_btn");

        //To stop people from hammering the refesh button and accidentally dosing themselves - enough requests can crash the viewer!
        //turn the button off, then turn it on when we get a response
        if(btn)
        {
            btn->setEnabled(false);
        }
        instance->clearList();
        instance->mAttachmentDetailsRequested = instance->requestAttachmentDetails();
    }
    else
    {
        LL_WARNS() << "could not find LLFloaterMyScripts instance after refresh button clicked" << LL_ENDL;
    }
}