aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/newview/llpreviewnotecard.cpp
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/newview/llpreviewnotecard.cpp
parentREADME.txt (diff)
downloadmeta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz
Second Life viewer sources 1.13.2.12
Diffstat (limited to '')
-rw-r--r--linden/indra/newview/llpreviewnotecard.cpp598
1 files changed, 598 insertions, 0 deletions
diff --git a/linden/indra/newview/llpreviewnotecard.cpp b/linden/indra/newview/llpreviewnotecard.cpp
new file mode 100644
index 0000000..da8b9fc
--- /dev/null
+++ b/linden/indra/newview/llpreviewnotecard.cpp
@@ -0,0 +1,598 @@
1/**
2 * @file llpreviewnotecard.cpp
3 * @brief Implementation of the notecard editor
4 *
5 * Copyright (c) 2002-2007, Linden Research, Inc.
6 *
7 * The source code in this file ("Source Code") is provided by Linden Lab
8 * to you under the terms of the GNU General Public License, version 2.0
9 * ("GPL"), unless you have obtained a separate licensing agreement
10 * ("Other License"), formally executed by you and Linden Lab. Terms of
11 * the GPL can be found in doc/GPL-license.txt in this distribution, or
12 * online at http://secondlife.com/developers/opensource/gplv2
13 *
14 * There are special exceptions to the terms and conditions of the GPL as
15 * it is applied to this Source Code. View the full text of the exception
16 * in the file doc/FLOSS-exception.txt in this software distribution, or
17 * online at http://secondlife.com/developers/opensource/flossexception
18 *
19 * By copying, modifying or distributing this software, you acknowledge
20 * that you have read and understood your obligations described above,
21 * and agree to abide by those obligations.
22 *
23 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
24 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
25 * COMPLETENESS OR PERFORMANCE.
26 */
27
28#include "llviewerprecompiledheaders.h"
29
30#include "llpreviewnotecard.h"
31
32#include "llinventory.h"
33
34#include "llagent.h"
35#include "llviewerwindow.h"
36#include "llbutton.h"
37#include "llinventorymodel.h"
38#include "lllineeditor.h"
39#include "llnotify.h"
40#include "llresmgr.h"
41#include "roles_constants.h"
42#include "llscrollbar.h"
43#include "llselectmgr.h"
44#include "llviewertexteditor.h"
45#include "llvfile.h"
46#include "llviewerinventory.h"
47#include "llviewerobject.h"
48#include "llviewerobjectlist.h"
49#include "llviewerregion.h"
50#include "lldir.h"
51//#include "llfloaterchat.h"
52#include "llviewerstats.h"
53#include "viewer.h" // app_abort_quit()
54#include "lllineeditor.h"
55#include "llvieweruictrlfactory.h"
56
57///----------------------------------------------------------------------------
58/// Local function declarations, constants, enums, and typedefs
59///----------------------------------------------------------------------------
60
61const S32 PREVIEW_MIN_WIDTH =
62 2 * PREVIEW_BORDER +
63 2 * PREVIEW_BUTTON_WIDTH +
64 PREVIEW_PAD + RESIZE_HANDLE_WIDTH +
65 PREVIEW_PAD;
66const S32 PREVIEW_MIN_HEIGHT =
67 2 * PREVIEW_BORDER +
68 3*(20 + PREVIEW_PAD) +
69 2 * SCROLLBAR_SIZE + 128;
70
71///----------------------------------------------------------------------------
72/// Class LLPreviewNotecard
73///----------------------------------------------------------------------------
74
75// Default constructor
76LLPreviewNotecard::LLPreviewNotecard(const std::string& name,
77 const LLRect& rect,
78 const std::string& title,
79 const LLUUID& item_id,
80 const LLUUID& object_id,
81 const LLUUID& asset_id,
82 BOOL show_keep_discard) :
83 LLPreview(name, rect, title, item_id, object_id, TRUE,
84 PREVIEW_MIN_WIDTH,
85 PREVIEW_MIN_HEIGHT),
86 mAssetID( asset_id ),
87 mNotecardItemID(item_id),
88 mObjectID(object_id)
89{
90 LLRect curRect = rect;
91
92 if (show_keep_discard)
93 {
94 gUICtrlFactory->buildFloater(this,"floater_preview_notecard_keep_discard.xml");
95 childSetAction("Keep",onKeepBtn,this);
96 childSetAction("Discard",onDiscardBtn,this);
97 }
98 else
99 {
100 gUICtrlFactory->buildFloater(this,"floater_preview_notecard.xml");
101 childSetAction("Save",onClickSave,this);
102
103 if( mAssetID.isNull() )
104 {
105 LLInventoryItem* item = getItem();
106 if( item )
107 {
108 mAssetID = item->getAssetUUID();
109 }
110 }
111 }
112
113 reshape(curRect.getWidth(), curRect.getHeight(), TRUE);
114 setRect(curRect);
115
116 childSetVisible("lock", FALSE);
117
118 LLInventoryItem* item = getItem();
119
120 childSetCommitCallback("desc", LLPreview::onText, this);
121 if (item)
122 childSetText("desc", item->getDescription());
123 childSetPrevalidate("desc", &LLLineEditor::prevalidatePrintableNotPipe);
124
125 setTitle(title);
126
127 LLViewerTextEditor* editor = LLViewerUICtrlFactory::getViewerTextEditorByName(this, "Notecard Editor");
128
129 if (editor)
130 {
131 editor->setWordWrap(TRUE);
132 editor->setSourceID(item_id);
133 editor->setHandleEditKeysDirectly(TRUE);
134 }
135
136 gAgent.changeCameraToDefault();
137}
138
139BOOL LLPreviewNotecard::postBuild()
140{
141 LLViewerTextEditor *ed = (LLViewerTextEditor *)gUICtrlFactory->getTextEditorByName(this, "Notecard Editor");
142 if (ed)
143 {
144 ed->setNotecardInfo(mNotecardItemID, mObjectID);
145 }
146 return TRUE;
147}
148
149bool LLPreviewNotecard::saveItem(LLPointer<LLInventoryItem>* itemptr)
150{
151 LLInventoryItem* item = NULL;
152 if (itemptr && itemptr->notNull())
153 {
154 item = (LLInventoryItem*)(*itemptr);
155 }
156 bool res = saveIfNeeded(item);
157 if (res)
158 {
159 delete itemptr;
160 }
161 return res;
162}
163
164void LLPreviewNotecard::setEnabled( BOOL enabled )
165{
166
167 LLViewerTextEditor* editor = LLViewerUICtrlFactory::getViewerTextEditorByName(this, "Notecard Editor");
168
169 childSetEnabled("Notecard Editor", enabled);
170 childSetVisible("lock", !enabled);
171 childSetEnabled("desc", enabled);
172 childSetEnabled("Save", enabled && editor && (!editor->isPristine()));
173
174}
175
176
177void LLPreviewNotecard::draw()
178{
179
180
181 //childSetFocus("Save", FALSE);
182
183 LLViewerTextEditor* editor = LLViewerUICtrlFactory::getViewerTextEditorByName(this, "Notecard Editor");
184 BOOL script_changed = !editor->isPristine();
185
186 childSetEnabled("Save", script_changed && getEnabled());
187
188 LLPreview::draw();
189}
190
191// virtual
192BOOL LLPreviewNotecard::handleKeyHere(KEY key, MASK mask,
193 BOOL called_from_parent)
194{
195 if(getVisible() && getEnabled())
196 {
197 if(('S' == key) && (MASK_CONTROL == (mask & MASK_CONTROL)))
198 {
199 saveIfNeeded();
200 return TRUE;
201 }
202 }
203 return FALSE;
204}
205
206// virtual
207BOOL LLPreviewNotecard::canClose()
208{
209 LLViewerTextEditor* editor = LLViewerUICtrlFactory::getViewerTextEditorByName(this, "Notecard Editor");
210
211 if(mForceClose || editor->isPristine())
212 {
213 return TRUE;
214 }
215 else
216 {
217 // Bring up view-modal dialog: Save changes? Yes, No, Cancel
218 gViewerWindow->alertXml("SaveChanges",
219 &LLPreviewNotecard::handleSaveChangesDialog,
220 this);
221
222 return FALSE;
223 }
224}
225
226const LLInventoryItem* LLPreviewNotecard::getDragItem()
227{
228 LLViewerTextEditor* editor = LLViewerUICtrlFactory::getViewerTextEditorByName(this, "Notecard Editor");
229
230 if(editor)
231 {
232 return editor->getDragItem();
233 }
234 return NULL;
235}
236
237void LLPreviewNotecard::loadAsset()
238{
239 // request the asset.
240 LLInventoryItem* item = getItem();
241 LLViewerTextEditor* editor = LLViewerUICtrlFactory::getViewerTextEditorByName(this, "Notecard Editor");
242
243 if (!editor)
244 return;
245
246
247 if(item)
248 {
249 if (gAgent.allowOperation(PERM_COPY, item->getPermissions(),
250 GP_OBJECT_MANIPULATE)
251 || gAgent.isGodlike())
252 {
253 mAssetID = item->getAssetUUID();
254 if(mAssetID.isNull())
255 {
256 editor->setText("");
257 editor->makePristine();
258 editor->setEnabled(TRUE);
259 mAssetStatus = PREVIEW_ASSET_LOADED;
260 }
261 else
262 {
263 LLUUID* new_uuid = new LLUUID(mItemUUID);
264 LLHost source_sim = LLHost::invalid;
265 if (mObjectUUID.notNull())
266 {
267 LLViewerObject *objectp = gObjectList.findObject(mObjectUUID);
268 if (objectp && objectp->getRegion())
269 {
270 source_sim = objectp->getRegion()->getHost();
271 }
272 else
273 {
274 // The object that we're trying to look at disappeared, bail.
275 llwarns << "Can't find object " << mObjectUUID << " associated with notecard." << llendl;
276 mAssetID.setNull();
277 editor->setText("Unable to find object containing this note.");
278 editor->makePristine();
279 editor->setEnabled(FALSE);
280 mAssetStatus = PREVIEW_ASSET_LOADED;
281 return;
282 }
283 }
284 gAssetStorage->getInvItemAsset(source_sim,
285 gAgent.getID(),
286 gAgent.getSessionID(),
287 item->getPermissions().getOwner(),
288 mObjectUUID,
289 item->getUUID(),
290 item->getAssetUUID(),
291 item->getType(),
292 &onLoadComplete,
293 (void*)new_uuid,
294 TRUE);
295 mAssetStatus = PREVIEW_ASSET_LOADING;
296 }
297 }
298 else
299 {
300 mAssetID.setNull();
301 editor->setText("You are not allowed to view this note.");
302 editor->makePristine();
303 editor->setEnabled(FALSE);
304 mAssetStatus = PREVIEW_ASSET_LOADED;
305 }
306 if(!gAgent.allowOperation(PERM_MODIFY, item->getPermissions(),
307 GP_OBJECT_MANIPULATE))
308 {
309 editor->setEnabled(FALSE);
310 childSetVisible("lock", TRUE);
311 }
312 }
313 else
314 {
315 editor->setText("");
316 editor->makePristine();
317 editor->setEnabled(TRUE);
318 mAssetStatus = PREVIEW_ASSET_LOADED;
319 }
320}
321
322// static
323void LLPreviewNotecard::onLoadComplete(LLVFS *vfs,
324 const LLUUID& asset_uuid,
325 LLAssetType::EType type,
326 void* user_data, S32 status)
327{
328 llinfos << "LLPreviewNotecard::onLoadComplete()" << llendl;
329 LLUUID* item_id = (LLUUID*)user_data;
330 LLPreviewNotecard* preview = LLPreviewNotecard::getInstance(*item_id);
331 if( preview )
332 {
333 if(0 == status)
334 {
335 LLVFile file(vfs, asset_uuid, type, LLVFile::READ);
336
337 S32 file_length = file.getSize();
338
339 char* buffer = new char[file_length+1];
340 file.read((U8*)buffer, file_length);
341
342 // put a EOS at the end
343 buffer[file_length] = 0;
344
345
346 LLViewerTextEditor* previewEditor = LLViewerUICtrlFactory::getViewerTextEditorByName(preview, "Notecard Editor");
347
348 if( (file_length > 19) && !strncmp( buffer, "Linden text version", 19 ) )
349 {
350 if( !previewEditor->importBuffer( buffer ) )
351 {
352 llwarns << "Problem importing notecard" << llendl;
353 }
354 }
355 else
356 {
357 // Version 0 (just text, doesn't include version number)
358 previewEditor->setText(buffer);
359 }
360
361 previewEditor->makePristine();
362
363 LLInventoryItem* item = preview->getItem();
364 BOOL modifiable = item && gAgent.allowOperation(PERM_MODIFY,
365 item->getPermissions(), GP_OBJECT_MANIPULATE);
366 previewEditor->setEnabled(modifiable);
367 delete[] buffer;
368 preview->mAssetStatus = PREVIEW_ASSET_LOADED;
369 }
370 else
371 {
372 if( gViewerStats )
373 {
374 gViewerStats->incStat( LLViewerStats::ST_DOWNLOAD_FAILED );
375 }
376
377 if( LL_ERR_ASSET_REQUEST_NOT_IN_DATABASE == status ||
378 LL_ERR_FILE_EMPTY == status)
379 {
380 LLNotifyBox::showXml("NotecardMissing");
381 }
382 else if (LL_ERR_INSUFFICIENT_PERMISSIONS == status)
383 {
384 LLNotifyBox::showXml("NotecardNoPermissions");
385 }
386 else
387 {
388 LLNotifyBox::showXml("UnableToLoadNotecard");
389 }
390
391 llwarns << "Problem loading notecard: " << status << llendl;
392 preview->mAssetStatus = PREVIEW_ASSET_ERROR;
393 }
394 }
395 delete item_id;
396}
397
398// static
399LLPreviewNotecard* LLPreviewNotecard::getInstance(const LLUUID& item_id)
400{
401 LLPreview* instance = NULL;
402 preview_map_t::iterator found_it = LLPreview::sInstances.find(item_id);
403 if(found_it != LLPreview::sInstances.end())
404 {
405 instance = found_it->second;
406 }
407 return (LLPreviewNotecard*)instance;
408}
409
410// static
411void LLPreviewNotecard::onClickSave(void* user_data)
412{
413 //llinfos << "LLPreviewNotecard::onBtnSave()" << llendl;
414 LLPreviewNotecard* preview = (LLPreviewNotecard*)user_data;
415 if(preview)
416 {
417 preview->saveIfNeeded();
418 }
419}
420
421struct LLSaveNotecardInfo
422{
423 LLPreviewNotecard* mSelf;
424 LLUUID mItemUUID;
425 LLUUID mObjectUUID;
426 LLTransactionID mTransactionID;
427 LLPointer<LLInventoryItem> mCopyItem;
428 LLSaveNotecardInfo(LLPreviewNotecard* self, const LLUUID& item_id, const LLUUID& object_id,
429 const LLTransactionID& transaction_id, LLInventoryItem* copyitem) :
430 mSelf(self), mItemUUID(item_id), mObjectUUID(object_id), mTransactionID(transaction_id), mCopyItem(copyitem)
431 {
432 }
433};
434
435bool LLPreviewNotecard::saveIfNeeded(LLInventoryItem* copyitem)
436{
437 if(!gAssetStorage)
438 {
439 llwarns << "Not connected to an asset storage system." << llendl;
440 return false;
441 }
442
443
444 LLViewerTextEditor* editor = LLViewerUICtrlFactory::getViewerTextEditorByName(this, "Notecard Editor");
445
446 if(!editor->isPristine())
447 {
448 // We need to update the asset information
449 LLTransactionID tid;
450 LLAssetID asset_id;
451 tid.generate();
452 asset_id = tid.makeAssetID(gAgent.getSecureSessionID());
453
454 LLVFile file(gVFS, asset_id, LLAssetType::AT_NOTECARD, LLVFile::APPEND);
455
456 LLString buffer;
457 if (!editor->exportBuffer(buffer))
458 {
459 return false;
460 }
461
462 editor->makePristine();
463
464 S32 size = buffer.length() + 1;
465 file.setMaxSize(size);
466 file.write((U8*)buffer.c_str(), size);
467
468 LLInventoryItem* item = getItem();
469 // save it out to database
470 if (item)
471 {
472
473 LLSaveNotecardInfo* info = new LLSaveNotecardInfo(this, mItemUUID, mObjectUUID,
474 tid, copyitem);
475 gAssetStorage->storeAssetData(tid, LLAssetType::AT_NOTECARD,
476 &onSaveComplete,
477 (void*)info,
478 FALSE);
479 }
480 }
481 return true;
482}
483
484// static
485void LLPreviewNotecard::onSaveComplete(const LLUUID& asset_uuid, void* user_data, S32 status) // StoreAssetData callback (fixed)
486{
487 LLSaveNotecardInfo* info = (LLSaveNotecardInfo*)user_data;
488 if(info && (0 == status))
489 {
490 if(info->mObjectUUID.isNull())
491 {
492 LLViewerInventoryItem* item;
493 item = (LLViewerInventoryItem*)gInventory.getItem(info->mItemUUID);
494 if(item)
495 {
496 LLPointer<LLViewerInventoryItem> new_item = new LLViewerInventoryItem(item);
497 new_item->setAssetUUID(asset_uuid);
498 new_item->setTransactionID(info->mTransactionID);
499 new_item->updateServer(FALSE);
500 gInventory.updateItem(new_item);
501 gInventory.notifyObservers();
502 }
503 else
504 {
505 llwarns << "Inventory item for script " << info->mItemUUID
506 << " is no longer in agent inventory." << llendl;
507 }
508 }
509 else
510 {
511 LLViewerObject* object = gObjectList.findObject(info->mObjectUUID);
512 LLViewerInventoryItem* item = NULL;
513 if(object)
514 {
515 item = (LLViewerInventoryItem*)object->getInventoryObject(info->mItemUUID);
516 }
517 if(object && item)
518 {
519 item->setAssetUUID(asset_uuid);
520 item->setTransactionID(info->mTransactionID);
521 object->updateInventory(item, TASK_INVENTORY_ITEM_KEY, false);
522 dialog_refresh_all();
523 }
524 else
525 {
526 gViewerWindow->alertXml("SaveNotecardFailObjectNotFound");
527 }
528 }
529 // Perform item copy to inventory
530 if (info->mCopyItem.notNull())
531 {
532 LLViewerTextEditor* editor = LLViewerUICtrlFactory::getViewerTextEditorByName(info->mSelf, "Notecard Editor");
533 if (editor)
534 {
535 editor->copyInventory(info->mCopyItem);
536 }
537 }
538
539 // Find our window and close it if requested.
540 LLPreviewNotecard* previewp = (LLPreviewNotecard*)LLPreview::find(info->mItemUUID);
541 if (previewp && previewp->mCloseAfterSave)
542 {
543 previewp->close();
544 }
545 }
546 else
547 {
548 llwarns << "Problem saving notecard: " << status << llendl;
549 LLStringBase<char>::format_map_t args;
550 args["[REASON]"] = std::string(LLAssetStorage::getErrorString(status));
551 gViewerWindow->alertXml("SaveNotecardFailReason",args);
552 }
553
554 char uuid_string[UUID_STR_LENGTH];
555 asset_uuid.toString(uuid_string);
556 char filename[LL_MAX_PATH];
557 sprintf(filename, "%s.tmp", gDirUtilp->getExpandedFilename(LL_PATH_CACHE,uuid_string).c_str());
558 LLFile::remove(filename);
559 delete info;
560}
561
562// static
563void LLPreviewNotecard::handleSaveChangesDialog(S32 option, void* userdata)
564{
565 LLPreviewNotecard* self = (LLPreviewNotecard*)userdata;
566 switch(option)
567 {
568 case 0: // "Yes"
569 self->mCloseAfterSave = TRUE;
570 LLPreviewNotecard::onClickSave((void*)self);
571 break;
572
573 case 1: // "No"
574 self->mForceClose = TRUE;
575 self->close();
576 break;
577
578 case 2: // "Cancel"
579 default:
580 // If we were quitting, we didn't really mean it.
581 app_abort_quit();
582 break;
583 }
584}
585
586void LLPreviewNotecard::reshape(S32 width, S32 height, BOOL called_from_parent)
587{
588 LLPreview::reshape( width, height, called_from_parent );
589
590 if( !isMinimized() )
591 {
592 // So that next time you open a script it will have the same height and width
593 // (although not the same position).
594 gSavedSettings.setRect("NotecardEditorRect", mRect);
595 }
596}
597
598// EOF