summaryrefslogtreecommitdiff
path: root/indra/newview/llpanelnearbymedia.cpp
blob: 2dd4866da3aee0f3ff32f0053fb7ad8dc4f8cc06 (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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
/**
 * @file llpanelnearbymedia.cpp
 * @brief Management interface for muting and controlling nearby media
 *
 * $LicenseInfo:firstyear=2005&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, 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 "llpanelnearbymedia.h"

#include "llaudioengine.h"
#include "llbase64.h"
#include "llcheckboxctrl.h"
#include "llclipboard.h"
#include "llcombobox.h"
#include "llresizebar.h"
#include "llresizehandle.h"
#include "llscrolllistctrl.h"
#include "llscrolllistitem.h"
#include "llscrolllistcell.h"
#include "llslider.h"
#include "llsliderctrl.h"
#include "llagent.h"
#include "llagentui.h"
#include "llbutton.h"
#include "lltextbox.h"
#include "llviewermedia.h"
#include "llviewerparcelaskplay.h"
#include "llviewerparcelmedia.h"
#include "llviewerregion.h"
#include "llviewermediafocus.h"
#include "llviewerparcelmgr.h"
#include "llparcel.h"
#include "llpluginclassmedia.h"
#include "llvovolume.h"
#include "llstatusbar.h"
#include "llsdutil.h"
#include "lltoggleablemenu.h"
#include "llvieweraudio.h"
#include "llviewermenu.h"

#include "llfloaterreg.h"
#include "llfloaterpreference.h" // for the gear icon
#include "lltabcontainer.h"

#include <stringize.h>

extern LLControlGroup gSavedSettings;

static const LLUUID PARCEL_MEDIA_LIST_ITEM_UUID = LLUUID("CAB5920F-E484-4233-8621-384CF373A321");
static const LLUUID PARCEL_AUDIO_LIST_ITEM_UUID = LLUUID("DF4B020D-8A24-4B95-AB5D-CA970D694822");

//
// LLPanelNearByMedia
//


LLPanelNearByMedia::LLPanelNearByMedia()
:   mMediaList(NULL),
      mEnableAllCtrl(NULL),
      mDebugInfoVisible(false),
      mParcelMediaItem(NULL),
      mParcelAudioItem(NULL),
      mMoreLessBtn(NULL)
{
    // This is just an initial value, mParcelAudioAutoStart does not affect ParcelMediaAutoPlayEnable
    mParcelAudioAutoStart = gSavedSettings.getS32("ParcelMediaAutoPlayEnable") != 0
                            && gSavedSettings.getBOOL("MediaTentativeAutoPlay");

    gSavedSettings.getControl("ParcelMediaAutoPlayEnable")->getSignal()->connect(boost::bind(&LLPanelNearByMedia::handleMediaAutoPlayChanged, this, _2));

    mCommitCallbackRegistrar.add("MediaListCtrl.EnableAll",     boost::bind(&LLPanelNearByMedia::onClickEnableAll, this));
    mCommitCallbackRegistrar.add("MediaListCtrl.DisableAll",        boost::bind(&LLPanelNearByMedia::onClickDisableAll, this));
    mCommitCallbackRegistrar.add("MediaListCtrl.GoMediaPrefs", boost::bind(&LLPanelNearByMedia::onAdvancedButtonClick, this));
    mCommitCallbackRegistrar.add("MediaListCtrl.MoreLess", boost::bind(&LLPanelNearByMedia::onMoreLess, this));
    mCommitCallbackRegistrar.add("SelectedMediaCtrl.Stop",      boost::bind(&LLPanelNearByMedia::onClickSelectedMediaStop, this));
    mCommitCallbackRegistrar.add("SelectedMediaCtrl.Play",      boost::bind(&LLPanelNearByMedia::onClickSelectedMediaPlay, this));
    mCommitCallbackRegistrar.add("SelectedMediaCtrl.Pause",     boost::bind(&LLPanelNearByMedia::onClickSelectedMediaPause, this));
    mCommitCallbackRegistrar.add("SelectedMediaCtrl.Mute",      boost::bind(&LLPanelNearByMedia::onClickSelectedMediaMute, this));
    mCommitCallbackRegistrar.add("SelectedMediaCtrl.Volume",    boost::bind(&LLPanelNearByMedia::onCommitSelectedMediaVolume, this));
    mCommitCallbackRegistrar.add("SelectedMediaCtrl.Zoom",      boost::bind(&LLPanelNearByMedia::onClickSelectedMediaZoom, this));
    mCommitCallbackRegistrar.add("SelectedMediaCtrl.Unzoom",    boost::bind(&LLPanelNearByMedia::onClickSelectedMediaUnzoom, this));

    // Context menu handler.
    mCommitCallbackRegistrar.add("SelectedMediaCtrl.Action",
                                 [this](LLUICtrl* ctrl, const LLSD& data)
                                 {
                                     onMenuAction(data);
                                 });
    mEnableCallbackRegistrar.add("SelectedMediaCtrl.Visible",
                                 [this](LLUICtrl* ctrl, const LLSD& data)
                                 {
                                     return onMenuVisible(data);
                                 });

    buildFromFile( "panel_nearby_media.xml");
}

LLPanelNearByMedia::~LLPanelNearByMedia()
{
}

bool LLPanelNearByMedia::postBuild()
{
    LLPanelPulldown::postBuild();

    const S32 RESIZE_BAR_THICKNESS = 6;
    LLResizeBar::Params p;
    p.rect = LLRect(0, RESIZE_BAR_THICKNESS, getRect().getWidth(), 0);
    p.name = "resizebar_bottom";
    p.min_size = getRect().getHeight();
    p.side = LLResizeBar::BOTTOM;
    p.resizing_view = this;
    addChild( LLUICtrlFactory::create<LLResizeBar>(p) );

    p.rect = LLRect( 0, getRect().getHeight(), RESIZE_BAR_THICKNESS, 0);
    p.name = "resizebar_left";
    p.min_size = getRect().getWidth();
    p.side = LLResizeBar::LEFT;
    addChild( LLUICtrlFactory::create<LLResizeBar>(p) );

    LLResizeHandle::Params resize_handle_p;
    resize_handle_p.rect = LLRect( 0, RESIZE_HANDLE_HEIGHT, RESIZE_HANDLE_WIDTH, 0 );
    resize_handle_p.mouse_opaque(false);
    resize_handle_p.min_width(getRect().getWidth());
    resize_handle_p.min_height(getRect().getHeight());
    resize_handle_p.corner(LLResizeHandle::LEFT_BOTTOM);
    addChild(LLUICtrlFactory::create<LLResizeHandle>(resize_handle_p));

    mNearbyMediaPanel = getChild<LLUICtrl>("nearby_media_panel");
    mMediaList = getChild<LLScrollListCtrl>("media_list");
    mEnableAllCtrl = getChild<LLUICtrl>("all_nearby_media_enable_btn");
    mDisableAllCtrl = getChild<LLUICtrl>("all_nearby_media_disable_btn");
    mShowCtrl = getChild<LLComboBox>("show_combo");

    // Dynamic (selection-dependent) controls
    mStopCtrl = getChild<LLUICtrl>("stop");
    mPlayCtrl = getChild<LLUICtrl>("play");
    mPauseCtrl = getChild<LLUICtrl>("pause");
    mMuteCtrl = getChild<LLUICtrl>("mute");
    mVolumeSliderCtrl = getChild<LLUICtrl>("volume_slider_ctrl");
    mZoomCtrl = getChild<LLUICtrl>("zoom");
    mUnzoomCtrl = getChild<LLUICtrl>("unzoom");
    mVolumeSlider = getChild<LLSlider>("volume_slider");
    mMuteBtn = getChild<LLButton>("mute_btn");
    mMoreLessBtn = getChild<LLButton>("more_btn");

    mEmptyNameString = getString("empty_item_text");
    mParcelMediaName = getString("parcel_media_name");
    mParcelAudioName = getString("parcel_audio_name");
    mPlayingString = getString("playing_suffix");

    mMediaList->setDoubleClickCallback(onZoomMedia, this);
    mMediaList->sortByColumnIndex(PROXIMITY_COLUMN, true);
    mMediaList->sortByColumnIndex(VISIBILITY_COLUMN, false);

    refreshList();
    updateControls();
    updateColumns();

    LLView* minimized_controls = getChildView("minimized_controls");
    mMoreRect = getRect();
    mLessRect = getRect();
    mLessRect.mBottom = minimized_controls->getRect().mBottom;

    mMoreLessBtn->setVisible(false);
    onMoreLess();

    mContextMenu = LLUICtrlFactory::getInstance()->createFromFile<LLToggleableMenu>(
        "menu_nearby_media.xml",
        gMenuHolder,
        LLViewerMenuHolderGL::child_registry_t::instance());

    return true;
}

void LLPanelNearByMedia::handleMediaAutoPlayChanged(const LLSD& newvalue)
{
    // update mParcelAudioAutoStartMode if "ParcelMediaAutoPlayEnable" changes
    S32 value = gSavedSettings.getS32("ParcelMediaAutoPlayEnable");
    mParcelAudioAutoStart = value != 0
                            && gSavedSettings.getBOOL("MediaTentativeAutoPlay");

    LLViewerParcelAskPlay *inst = LLViewerParcelAskPlay::getInstance();
    if (value == 2 && !inst->hasData())
    {
        // Init if nessesary
        inst->loadSettings();
    }
    inst->cancelNotification();
}

/*virtual*/
void LLPanelNearByMedia::reshape(S32 width, S32 height, bool called_from_parent)
{
    LLPanelPulldown::reshape(width, height, called_from_parent);

    if (mMoreLessBtn && mMoreLessBtn->getValue().asBoolean())
    {
        mMoreRect = getRect();
    }

}

/*virtual*/
void LLPanelNearByMedia::draw()
{
    // keep bottom of panel on screen
    LLRect screen_rect = calcScreenRect();
    if (screen_rect.mBottom < 0)
    {
        LLRect new_rect = getRect();
        new_rect.mBottom += 0 - screen_rect.mBottom;
        setShape(new_rect);
    }

    refreshList();
    updateControls();

    LLPanelPulldown::draw();
}

/*virtual*/
bool LLPanelNearByMedia::handleHover(S32 x, S32 y, MASK mask)
{
    LLPanelPulldown::handleHover(x, y, mask);

    // If we are hovering over this panel, make sure to clear any hovered media
    // ID.  Note that the more general solution would be to clear this ID when
    // the mouse leaves the in-scene view, but that proved to be problematic.
    // See EXT-5517
    LLViewerMediaFocus::getInstance()->clearHover();

    // Always handle
    return true;
}

bool LLPanelNearByMedia::handleRightMouseDown(S32 x, S32 y, MASK mask)
{
    S32 x_list, y_list;
    localPointToOtherView(x, y, &x_list, &y_list, mMediaList);
    if (mMoreLessBtn->getToggleState()
        && mMediaList->pointInView(x_list, y_list))
    {
        LLScrollListItem* hit_item = mMediaList->hitItem(x_list, y_list);
        bool selected = hit_item && hit_item->getSelected();
        if (!selected)
        {
            selected = mMediaList->selectItemAt(x_list, y_list, mask);
        }

        if (selected && mContextMenu)
        {
            mContextMenu->buildDrawLabels();
            mContextMenu->updateParent(LLMenuGL::sMenuContainer);
            LLMenuGL::showPopup(this, mContextMenu, x, y);
            return true;
        }
    }

    return LLPanelPulldown::handleRightMouseDown(x, y, mask);
}


void LLPanelNearByMedia::onVisibilityChange(bool new_visibility)
{
    if (!new_visibility && mContextMenu->getVisible())
    {
        gMenuHolder->hideMenus();
    }
    LLPanelPulldown::onVisibilityChange(new_visibility);
}

bool LLPanelNearByMedia::getParcelAudioAutoStart()
{
    return mParcelAudioAutoStart;
}

LLScrollListItem* LLPanelNearByMedia::addListItem(const LLUUID &id)
{
    if (NULL == mMediaList) return NULL;

    // Just set up the columns -- the values will be filled in by updateListItem().

    LLSD row;
    row["id"] = id;

    LLSD &columns = row["columns"];

    columns[CHECKBOX_COLUMN]["column"] = "media_checkbox_ctrl";
    columns[CHECKBOX_COLUMN]["type"] = "checkbox";
    //if(mDebugInfoVisible)
    {
    columns[PROXIMITY_COLUMN]["column"] = "media_proximity";
    columns[PROXIMITY_COLUMN]["value"] = "";
        columns[VISIBILITY_COLUMN]["column"] = "media_visibility";
        columns[VISIBILITY_COLUMN]["value"] = "";
        columns[CLASS_COLUMN]["column"] = "media_class";
        columns[CLASS_COLUMN]["type"] = "text";
        columns[CLASS_COLUMN]["value"] = "";
    }
    columns[NAME_COLUMN]["column"] = "media_name";
    columns[NAME_COLUMN]["type"] = "text";
    columns[NAME_COLUMN]["value"] = "";
    //if(mDebugInfoVisible)
    {
        columns[DEBUG_COLUMN]["column"] = "media_debug";
        columns[DEBUG_COLUMN]["type"] = "text";
        columns[DEBUG_COLUMN]["value"] = "";
    }

    LLScrollListItem* new_item = mMediaList->addElement(row);
    if (NULL != new_item)
    {
        LLScrollListCheck* scroll_list_check = dynamic_cast<LLScrollListCheck*>(new_item->getColumn(CHECKBOX_COLUMN));
        if (scroll_list_check)
        {
            LLCheckBoxCtrl *check = scroll_list_check->getCheckBox();
            check->setCommitCallback(boost::bind(&LLPanelNearByMedia::onCheckItem, this, _1, id));
        }
    }
    return new_item;
}

void LLPanelNearByMedia::updateListItem(LLScrollListItem* item, LLViewerMediaImpl* impl)
{
    std::string item_name;
    std::string item_tooltip;
    std::string debug_str;
    LLPanelNearByMedia::MediaClass media_class = MEDIA_CLASS_ALL;

    getNameAndUrlHelper(impl, item_name, item_tooltip, mEmptyNameString);
    // Focused
    if (impl->hasFocus())
    {
        media_class = MEDIA_CLASS_FOCUSED;
    }
    // Is attached to another avatar?
    else if (impl->isAttachedToAnotherAvatar())
    {
        media_class = MEDIA_CLASS_ON_OTHERS;
    }
    // Outside agent parcel
    else if (!impl->isInAgentParcel())
    {
        media_class = MEDIA_CLASS_OUTSIDE_PARCEL;
    }
    else {
        // inside parcel
        media_class = MEDIA_CLASS_WITHIN_PARCEL;
    }

    if(mDebugInfoVisible)
    {
        debug_str += llformat("%g/", (float)impl->getInterest());

        // proximity distance is actually distance squared -- display it as straight distance.
        debug_str += llformat("%g/", (F32) sqrt(impl->getProximityDistance()));

        //          s += llformat("%g/", (float)impl->getCPUUsage());
        //          s += llformat("%g/", (float)impl->getApproximateTextureInterest());
        debug_str += llformat("%g/", (float)(NULL == impl->getSomeObject()) ? 0.0 : impl->getSomeObject()->getPixelArea());

        debug_str += LLPluginClassMedia::priorityToString(impl->getPriority());

        if(impl->hasMedia())
        {
            debug_str += '@';
        }
        else if(impl->isPlayable())
        {
            debug_str += '+';
        }
        else if(impl->isForcedUnloaded())
        {
            debug_str += '!';
        }
    }

    updateListItem(item,
                   item_name,
                   item_tooltip,
                   impl->getProximity(),
                   impl->isMediaDisabled(),
                   impl->hasMedia(),
                   impl->isMediaTimeBased() &&  impl->isMediaPlaying(),
                   media_class,
                   debug_str);
}

void LLPanelNearByMedia::updateListItem(LLScrollListItem* item,
                                          const std::string &item_name,
                                          const std::string &item_tooltip,
                                          S32 proximity,
                                          bool is_disabled,
                                          bool has_media,
                                          bool is_time_based_and_playing,
                                          LLPanelNearByMedia::MediaClass media_class,
                                          const std::string &debug_str)
{
    LLScrollListCell* cell = item->getColumn(PROXIMITY_COLUMN);
    if(cell)
    {
        // since we are forced to sort by text, encode sort order as string
        std::string proximity_string = STRINGIZE(proximity);
        std::string old_proximity_string = cell->getValue().asString();
        if(proximity_string != old_proximity_string)
        {
            cell->setValue(proximity_string);
            mMediaList->setNeedsSort(true);
        }
    }

    cell = item->getColumn(CHECKBOX_COLUMN);
    if(cell)
    {
        cell->setValue(!is_disabled);
    }

    cell = item->getColumn(VISIBILITY_COLUMN);
    if(cell)
    {
        S32 old_visibility = cell->getValue();
        // *HACK ALERT: force ordering of Media before Audio before the rest of the list
        S32 new_visibility =
            item->getUUID() == PARCEL_MEDIA_LIST_ITEM_UUID ? 3
            : item->getUUID() == PARCEL_AUDIO_LIST_ITEM_UUID ? 2
            : (has_media) ? 1
            : ((is_disabled) ? 0
            : -1);
        cell->setValue(STRINGIZE(new_visibility));
        if (new_visibility != old_visibility)
        {
            mMediaList->setNeedsSort(true);
        }
    }

    cell = item->getColumn(NAME_COLUMN);
    if(cell)
    {
        std::string name = item_name;
        std::string old_name = cell->getValue().asString();
        if (has_media)
        {
            name += " " + mPlayingString;
        }
        if (name != old_name)
        {
            cell->setValue(name);
        }
        cell->setToolTip(item_tooltip);

        // *TODO: Make these font styles/colors configurable via XUI
        U8 font_style = LLFontGL::NORMAL;
        LLColor4 cell_color = LLColor4::white;

        // Only colorize by class in debug
        if (mDebugInfoVisible)
        {
            switch (media_class) {
                case MEDIA_CLASS_FOCUSED:
                    cell_color = LLColor4::yellow;
                    break;
                case MEDIA_CLASS_ON_OTHERS:
                    cell_color = LLColor4::red;
                    break;
                case MEDIA_CLASS_OUTSIDE_PARCEL:
                    cell_color = LLColor4::orange;
                    break;
                case MEDIA_CLASS_WITHIN_PARCEL:
                default:
                    break;
            }
        }
        if (is_disabled)
        {
            if (mDebugInfoVisible)
            {
                font_style |= LLFontGL::ITALIC;
                cell_color = LLColor4::black;
            }
            else {
                // Dim it if it is disabled
                cell_color.setAlpha(0.25);
            }
        }
        // Dim it if it isn't "showing"
        else if (!has_media)
        {
            cell_color.setAlpha(0.25);
        }
        // Bold it if it is time-based media and it is playing
        else if (is_time_based_and_playing)
        {
            if (mDebugInfoVisible) font_style |= LLFontGL::BOLD;
        }
        cell->setColor(cell_color);
        LLScrollListText *text_cell = dynamic_cast<LLScrollListText*> (cell);
        if (text_cell)
        {
            text_cell->setFontStyle(font_style);
        }
    }

    cell = item->getColumn(CLASS_COLUMN);
    if(cell)
    {
        // TODO: clean this up!
        cell->setValue(STRINGIZE(media_class));
    }

    if(mDebugInfoVisible)
    {
        cell = item->getColumn(DEBUG_COLUMN);
        if(cell)
        {
            cell->setValue(debug_str);
        }
    }
}

void LLPanelNearByMedia::removeListItem(const LLUUID &id)
{
    if (NULL == mMediaList) return;

    mMediaList->deleteSingleItem(mMediaList->getItemIndex(id));
    mMediaList->updateLayout();
}

void LLPanelNearByMedia::refreshParcelItems()
{
    //
    // First add/remove the "fake" items Parcel Media and Parcel Audio.
    // These items will have special UUIDs
    //    PARCEL_MEDIA_LIST_ITEM_UUID
    //    PARCEL_AUDIO_LIST_ITEM_UUID
    //
    // Get the filter choice.
    const LLSD &choice_llsd = mShowCtrl->getSelectedValue();
    MediaClass choice = (MediaClass)choice_llsd.asInteger();
    // Only show "special parcel items" if "All" or "Within" filter
    // (and if media is "enabled")
    bool should_include = (choice == MEDIA_CLASS_ALL || choice == MEDIA_CLASS_WITHIN_PARCEL);
    LLViewerMedia* media_inst = LLViewerMedia::getInstance();

    // First Parcel Media: add or remove it as necessary
    if (gSavedSettings.getBOOL("AudioStreamingMedia") && should_include && media_inst->hasParcelMedia())
    {
        // Yes, there is parcel media.
        if (NULL == mParcelMediaItem)
        {
            mParcelMediaItem = addListItem(PARCEL_MEDIA_LIST_ITEM_UUID);
            mMediaList->setNeedsSort(true);
        }
    }
    else {
        if (NULL != mParcelMediaItem) {
            removeListItem(PARCEL_MEDIA_LIST_ITEM_UUID);
            mParcelMediaItem = NULL;
            mMediaList->setNeedsSort(true);
        }
    }

    // ... then update it
    if (NULL != mParcelMediaItem)
    {
        std::string name, url, tooltip;
        getNameAndUrlHelper(LLViewerParcelMedia::getInstance()->getParcelMedia(), name, url, "");
        if (name.empty() || name == url)
        {
            tooltip = url;
        }
        else
        {
            tooltip = name + " : " + url;
        }
        LLViewerMediaImpl *impl = LLViewerParcelMedia::getInstance()->getParcelMedia();
        updateListItem(mParcelMediaItem,
                       mParcelMediaName,
                       tooltip,
                       -2, // Proximity closer than anything else, before Parcel Audio
                       impl == NULL || impl->isMediaDisabled(),
                       impl != NULL && !LLViewerParcelMedia::getInstance()->getURL().empty(),
                       impl != NULL && impl->isMediaTimeBased() &&  impl->isMediaPlaying(),
                       MEDIA_CLASS_ALL,
                       "parcel media");
    }

    // Next Parcel Audio: add or remove it as necessary (don't show if disabled in prefs)
    if (should_include && media_inst->hasParcelAudio() && gSavedSettings.getBOOL("AudioStreamingMusic"))
    {
        // Yes, there is parcel audio.
        if (NULL == mParcelAudioItem)
        {
            mParcelAudioItem = addListItem(PARCEL_AUDIO_LIST_ITEM_UUID);
            mMediaList->setNeedsSort(true);
        }
    }
    else {
        if (NULL != mParcelAudioItem) {
            removeListItem(PARCEL_AUDIO_LIST_ITEM_UUID);
            mParcelAudioItem = NULL;
            mMediaList->setNeedsSort(true);
        }
    }

    // ... then update it
    if (NULL != mParcelAudioItem)
    {
        bool is_playing = media_inst->isParcelAudioPlaying();

        std::string url;
        url = media_inst->getParcelAudioURL();

        updateListItem(mParcelAudioItem,
                       mParcelAudioName,
                       url,
                       -1, // Proximity after Parcel Media, but closer than anything else
                       (!is_playing),
                       is_playing,
                       is_playing,
                       MEDIA_CLASS_ALL,
                       "parcel audio");
    }
}

void LLPanelNearByMedia::refreshList()
{
    bool all_items_deleted = false;

    if(!mMediaList)
    {
        // None of this makes any sense if the media list isn't there.
        return;
    }

    // Check whether the debug column has been shown/hidden.
    bool debug_info_visible = gSavedSettings.getBOOL("MediaPerformanceManagerDebug");
    if(debug_info_visible != mDebugInfoVisible)
    {
        mDebugInfoVisible = debug_info_visible;

        // Clear all items so the list gets regenerated.
        mMediaList->deleteAllItems();
        mParcelAudioItem = NULL;
        mParcelMediaItem = NULL;
        all_items_deleted = true;

        updateColumns();
    }

    refreshParcelItems();

    // Get the canonical list from LLViewerMedia
    LLViewerMedia* media_inst = LLViewerMedia::getInstance();
    LLViewerMedia::impl_list impls = media_inst->getPriorityList();
    LLViewerMedia::impl_list::iterator priority_iter;

    U32 disabled_count = 0;

    // iterate over the impl list, creating rows as necessary.
    for(priority_iter = impls.begin(); priority_iter != impls.end(); priority_iter++)
    {
        LLViewerMediaImpl *impl = *priority_iter;

        // If we just emptied out the list, every flag needs to be reset.
        if(all_items_deleted)
        {
            impl->setInNearbyMediaList(false);
        }

        if (!impl->isParcelMedia())
        {
            LLUUID media_id = impl->getMediaTextureID();
            S32 proximity = impl->getProximity();
            // This is expensive (i.e. a linear search) -- don't use it here.  We now use mInNearbyMediaList instead.
            //S32 index = mMediaList->getItemIndex(media_id);
            if (proximity < 0 || !shouldShow(impl))
            {
                if (impl->getInNearbyMediaList())
                {
                    // There's a row for this impl -- remove it.
                    removeListItem(media_id);
                    impl->setInNearbyMediaList(false);
                }
            }
            else
            {
                if (!impl->getInNearbyMediaList())
                {
                    // We don't have a row for this impl -- add one.
                    addListItem(media_id);
                    impl->setInNearbyMediaList(true);
                }
            }
            // Update counts
            if (impl->isMediaDisabled())
            {
                disabled_count++;
            }
        }
    }
    mDisableAllCtrl->setEnabled((gSavedSettings.getBOOL("AudioStreamingMusic") ||
                                 gSavedSettings.getBOOL("AudioStreamingMedia")) &&
                                (media_inst->isAnyMediaShowing() ||
                                 media_inst->isParcelMediaPlaying() ||
                                 media_inst->isParcelAudioPlaying()));

    mEnableAllCtrl->setEnabled( (gSavedSettings.getBOOL("AudioStreamingMusic") ||
                                gSavedSettings.getBOOL("AudioStreamingMedia")) &&
                               (disabled_count > 0 ||
                                // parcel media (if we have it, and it isn't playing, enable "start")
                                (media_inst->hasParcelMedia() && ! media_inst->isParcelMediaPlaying()) ||
                                // parcel audio (if we have it, and it isn't playing, enable "start")
                                (media_inst->hasParcelAudio() && ! media_inst->isParcelAudioPlaying())));

    // Iterate over the rows in the control, updating ones whose impl exists, and deleting ones whose impl has gone away.
    std::vector<LLScrollListItem*> items = mMediaList->getAllData();

    for (std::vector<LLScrollListItem*>::iterator item_it = items.begin();
        item_it != items.end();
        ++item_it)
    {
        LLScrollListItem* item = (*item_it);
        LLUUID row_id = item->getUUID();

        if (row_id != PARCEL_MEDIA_LIST_ITEM_UUID &&
            row_id != PARCEL_AUDIO_LIST_ITEM_UUID)
        {
            LLViewerMediaImpl* impl = media_inst->getMediaImplFromTextureID(row_id);
            if(impl)
            {
                updateListItem(item, impl);
            }
            else
            {
                // This item's impl has been deleted -- remove the row.
                // Removing the row won't throw off our iteration, since we have a local copy of the array.
                // We just need to make sure we don't access this item after the delete.
                removeListItem(row_id);
            }
        }
    }

    // Set the selection to whatever media impl the media focus/hover is on.
    // This is an experiment, and can be removed by ifdefing out these 4 lines.
    LLUUID media_target = LLViewerMediaFocus::getInstance()->getControlsMediaID();
    if(media_target.notNull())
    {
        mMediaList->selectByID(media_target);
    }
}

void LLPanelNearByMedia::updateColumns()
{
    if (!mDebugInfoVisible)
    {
        if (mMediaList->getColumn(CHECKBOX_COLUMN)) mMediaList->getColumn(VISIBILITY_COLUMN)->setWidth(-1);
        if (mMediaList->getColumn(VISIBILITY_COLUMN)) mMediaList->getColumn(VISIBILITY_COLUMN)->setWidth(-1);
        if (mMediaList->getColumn(PROXIMITY_COLUMN)) mMediaList->getColumn(PROXIMITY_COLUMN)->setWidth(-1);
        if (mMediaList->getColumn(CLASS_COLUMN)) mMediaList->getColumn(CLASS_COLUMN)->setWidth(-1);
        if (mMediaList->getColumn(DEBUG_COLUMN)) mMediaList->getColumn(DEBUG_COLUMN)->setWidth(-1);
    }
    else {
        if (mMediaList->getColumn(CHECKBOX_COLUMN)) mMediaList->getColumn(VISIBILITY_COLUMN)->setWidth(20);
        if (mMediaList->getColumn(VISIBILITY_COLUMN)) mMediaList->getColumn(VISIBILITY_COLUMN)->setWidth(20);
        if (mMediaList->getColumn(PROXIMITY_COLUMN)) mMediaList->getColumn(PROXIMITY_COLUMN)->setWidth(30);
        if (mMediaList->getColumn(CLASS_COLUMN)) mMediaList->getColumn(CLASS_COLUMN)->setWidth(20);
        if (mMediaList->getColumn(DEBUG_COLUMN)) mMediaList->getColumn(DEBUG_COLUMN)->setWidth(200);
    }
}

void LLPanelNearByMedia::onClickEnableAll()
{
    LLViewerMedia::getInstance()->setAllMediaEnabled(true);
}

void LLPanelNearByMedia::onClickDisableAll()
{
    LLViewerMedia::getInstance()->setAllMediaEnabled(false);
}

void LLPanelNearByMedia::onClickEnableParcelMedia()
{
    if ( ! LLViewerMedia::getInstance()->isParcelMediaPlaying() )
    {
        LLViewerParcelMedia::getInstance()->play(LLViewerParcelMgr::getInstance()->getAgentParcel());
    }
}

void LLPanelNearByMedia::onClickDisableParcelMedia()
{
    // This actually unloads the impl, as opposed to "stop"ping the media
    LLViewerParcelMedia::getInstance()->stop();
}

void LLPanelNearByMedia::onCheckItem(LLUICtrl* ctrl, const LLUUID &row_id)
{
    LLCheckBoxCtrl* check = static_cast<LLCheckBoxCtrl*>(ctrl);

    setDisabled(row_id, ! check->getValue());
}

bool LLPanelNearByMedia::setDisabled(const LLUUID &row_id, bool disabled)
{
    if (row_id == PARCEL_AUDIO_LIST_ITEM_UUID)
    {
        if (disabled)
        {
            onClickParcelAudioStop();
        }
        else
        {
            onClickParcelAudioPlay();
        }
        return true;
    }
    else if (row_id == PARCEL_MEDIA_LIST_ITEM_UUID)
    {
        if (disabled)
        {
            onClickDisableParcelMedia();
        }
        else
        {
            onClickEnableParcelMedia();
        }
        return true;
    }
    else {
        LLViewerMediaImpl* impl = LLViewerMedia::getInstance()->getMediaImplFromTextureID(row_id);
        if(impl)
        {
            impl->setDisabled(disabled, true);
            return true;
        }
    }
    return false;
}

//static
void LLPanelNearByMedia::onZoomMedia(void* user_data)
{
    LLPanelNearByMedia* panelp = (LLPanelNearByMedia*)user_data;
    LLUUID media_id = panelp->mMediaList->getValue().asUUID();

    LLViewerMediaFocus::getInstance()->focusZoomOnMedia(media_id);
}

void LLPanelNearByMedia::onClickParcelMediaPlay()
{
    LLViewerParcelMedia::getInstance()->play(LLViewerParcelMgr::getInstance()->getAgentParcel());
}

void LLPanelNearByMedia::onClickParcelMediaStop()
{
    if (LLViewerParcelMedia::getInstance()->getParcelMedia())
    {
        // This stops the media playing, as opposed to unloading it like
        // LLViewerParcelMedia::stop() does
        LLViewerParcelMedia::getInstance()->getParcelMedia()->stop();
    }
}

void LLPanelNearByMedia::onClickParcelMediaPause()
{
    LLViewerParcelMedia::getInstance()->pause();
}

void LLPanelNearByMedia::onClickParcelAudioPlay()
{
    // User *explicitly* started the internet stream, so keep the stream
    // playing and updated as they cross to other parcels etc.
    mParcelAudioAutoStart = true;
    if (!gAudiop)
    {
        LL_WARNS("AudioEngine") << "LLAudioEngine instance doesn't exist!" << LL_ENDL;
        return;
    }

    if (LLAudioEngine::AUDIO_PAUSED == gAudiop->isInternetStreamPlaying())
    {
        // 'false' means unpause
        gAudiop->pauseInternetStream(false);
    }
    else
    {
        LLViewerAudio::getInstance()->startInternetStreamWithAutoFade(LLViewerMedia::getInstance()->getParcelAudioURL());
    }
}

void LLPanelNearByMedia::onClickParcelAudioStop()
{
    // User *explicitly* stopped the internet stream, so don't
    // re-start audio when i.e. they move to another parcel, until
    // they explicitly start it again.
    mParcelAudioAutoStart = false;
    if (!gAudiop)
    {
        LL_WARNS("AudioEngine") << "LLAudioEngine instance doesn't exist!" << LL_ENDL;
        return;
    }

    LLViewerAudio::getInstance()->stopInternetStreamWithAutoFade();
}

void LLPanelNearByMedia::onClickParcelAudioPause()
{
    if (!gAudiop)
    {
        LL_WARNS("AudioEngine") << "LLAudioEngine instance doesn't exist!" << LL_ENDL;
        return;
    }

    // 'true' means pause
    gAudiop->pauseInternetStream(true);
}

bool LLPanelNearByMedia::shouldShow(LLViewerMediaImpl* impl)
{
    const LLSD &choice_llsd = mShowCtrl->getSelectedValue();
    MediaClass choice = (MediaClass)choice_llsd.asInteger();

    switch (choice)
    {
        case MEDIA_CLASS_ALL:
            return true;
            break;
        case MEDIA_CLASS_WITHIN_PARCEL:
            return impl->isInAgentParcel();
            break;
        case MEDIA_CLASS_OUTSIDE_PARCEL:
            return ! impl->isInAgentParcel();
            break;
        case MEDIA_CLASS_ON_OTHERS:
            return impl->isAttachedToAnotherAvatar();
            break;
        default:
            break;
    }
    return true;
}

void LLPanelNearByMedia::onAdvancedButtonClick()
{
    // bring up the prefs floater
    LLFloaterPreference* prefsfloater = dynamic_cast<LLFloaterPreference*>(LLFloaterReg::showInstance("preferences"));
    if (prefsfloater)
    {
        // grab the 'audio' panel from the preferences floater and
        // bring it the front!
        LLTabContainer* tabcontainer = prefsfloater->getChild<LLTabContainer>("pref core");
        LLPanel* audiopanel = prefsfloater->getChild<LLPanel>("audio");
        if (tabcontainer && audiopanel)
        {
            tabcontainer->selectTabPanel(audiopanel);
        }
    }
}

void LLPanelNearByMedia::onMoreLess()
{
    bool is_more = mMoreLessBtn->getToggleState();
    mNearbyMediaPanel->setVisible(is_more);

    // enable resizing when expanded
    getChildView("resizebar_bottom")->setEnabled(is_more);

    LLRect new_rect = is_more ? mMoreRect : mLessRect;
    new_rect.translate(getRect().mRight - new_rect.mRight, getRect().mTop - new_rect.mTop);

    setShape(new_rect);

    mMoreLessBtn->setVisible(true);
}

void LLPanelNearByMedia::updateControls()
{
    LLUUID selected_media_id = mMediaList->getValue().asUUID();
    LLViewerMedia* media_inst = LLViewerMedia::getInstance();

    if (selected_media_id == PARCEL_AUDIO_LIST_ITEM_UUID)
    {
        if (!media_inst->getInstance()->hasParcelAudio() || !gSavedSettings.getBOOL("AudioStreamingMusic"))
        {
            // disable controls if audio streaming music is disabled from preference
            showDisabledControls();
        }
        else {
            showTimeBasedControls(media_inst->isParcelAudioPlaying(),
                              false, // include_zoom
                              false, // is_zoomed
                              gSavedSettings.getBOOL("MuteMusic"),
                              gSavedSettings.getF32("AudioLevelMusic") );
        }
    }
    else if (selected_media_id == PARCEL_MEDIA_LIST_ITEM_UUID)
    {
        if (!media_inst->hasParcelMedia() || !gSavedSettings.getBOOL("AudioStreamingMedia"))
        {
            // disable controls if audio streaming media is disabled from preference
            showDisabledControls();
        }
        else {
            LLViewerMediaImpl* impl = LLViewerParcelMedia::getInstance()->getParcelMedia();
            if (NULL == impl)
            {
                // Just means it hasn't started yet
                showBasicControls(false, false, false, false, 0);
            }
            else if (impl->isMediaTimeBased())
            {
                showTimeBasedControls(impl->isMediaPlaying(),
                                      false, // include_zoom
                                      false, // is_zoomed
                                      impl->getVolume() == 0.0,
                                      impl->getVolume() );
            }
            else {
                // non-time-based parcel media
                showBasicControls(media_inst->isParcelMediaPlaying(),
                                  false,
                                  false,
                                  impl->getVolume() == 0.0,
                                  impl->getVolume());
            }
        }
    }
    else {
        LLViewerMediaImpl* impl = media_inst->getMediaImplFromTextureID(selected_media_id);

        if (NULL == impl || !gSavedSettings.getBOOL("AudioStreamingMedia"))
        {
            showDisabledControls();
        }
        else {
            if (impl->isMediaTimeBased())
            {
                showTimeBasedControls(impl->isMediaPlaying(),
                                      ! impl->isParcelMedia(),  // include_zoom
                                      LLViewerMediaFocus::getInstance()->isZoomed(),
                                      impl->getVolume() == 0.0,
                                      impl->getVolume());
            }
            else {
                showBasicControls(!impl->isMediaDisabled(),
                                  ! impl->isParcelMedia(),  // include_zoom
                                  LLViewerMediaFocus::getInstance()->isZoomedOnMedia(impl->getMediaTextureID()),
                                  impl->getVolume() == 0.0,
                                  impl->getVolume());
            }
        }
    }
}

void LLPanelNearByMedia::showBasicControls(bool playing, bool include_zoom, bool is_zoomed, bool muted, F32 volume)
{
    mStopCtrl->setVisible(playing);
    mPlayCtrl->setVisible(!playing);
    mPauseCtrl->setVisible(false);
    mVolumeSliderCtrl->setVisible(true);
    mMuteCtrl->setVisible(true);
    mMuteBtn->setValue(muted);
    mVolumeSlider->setValue(volume);
    mZoomCtrl->setVisible(include_zoom && !is_zoomed);
    mUnzoomCtrl->setVisible(include_zoom && is_zoomed);
    mStopCtrl->setEnabled(true);
    mZoomCtrl->setEnabled(true);
}

void LLPanelNearByMedia::showTimeBasedControls(bool playing, bool include_zoom, bool is_zoomed, bool muted, F32 volume)
{
    mStopCtrl->setVisible(true);
    mPlayCtrl->setVisible(!playing);
    mPauseCtrl->setVisible(playing);
    mMuteCtrl->setVisible(true);
    mVolumeSliderCtrl->setVisible(true);
    mZoomCtrl->setVisible(include_zoom);
    mZoomCtrl->setVisible(include_zoom && !is_zoomed);
    mUnzoomCtrl->setVisible(include_zoom && is_zoomed);
    mStopCtrl->setEnabled(true);
    mZoomCtrl->setEnabled(true);
    mMuteBtn->setValue(muted);
    mVolumeSlider->setValue(volume);
}

void LLPanelNearByMedia::showDisabledControls()
{
    mStopCtrl->setVisible(true);
    mPlayCtrl->setVisible(false);
    mPauseCtrl->setVisible(false);
    mMuteCtrl->setVisible(false);
    mVolumeSliderCtrl->setVisible(false);
    mZoomCtrl->setVisible(true);
    mUnzoomCtrl->setVisible(false);
    mStopCtrl->setEnabled(false);
    mZoomCtrl->setEnabled(false);
}

void LLPanelNearByMedia::onClickSelectedMediaStop()
{
    setDisabled(mMediaList->getValue().asUUID(), true);
}

void LLPanelNearByMedia::onClickSelectedMediaPlay()
{
    LLUUID selected_media_id = mMediaList->getValue().asUUID();

    // First enable it
    setDisabled(selected_media_id, false);

    // Special code to make play "unpause" if time-based and playing
    if (selected_media_id != PARCEL_AUDIO_LIST_ITEM_UUID)
    {
        LLViewerMediaImpl *impl = (selected_media_id == PARCEL_MEDIA_LIST_ITEM_UUID) ?
            ((LLViewerMediaImpl*)LLViewerParcelMedia::getInstance()->getParcelMedia()) : LLViewerMedia::getInstance()->getMediaImplFromTextureID(selected_media_id);
        if (NULL != impl)
        {
            if (impl->isMediaTimeBased() && impl->isMediaPaused())
            {
                // Aha!  It's really time-based media that's paused, so unpause
                impl->play();
                return;
            }
            else if (impl->isParcelMedia())
            {
                LLViewerParcelMedia::getInstance()->play(LLViewerParcelMgr::getInstance()->getAgentParcel());
            }
        }
    }
}

void LLPanelNearByMedia::onClickSelectedMediaPause()
{
    LLUUID selected_media_id = mMediaList->getValue().asUUID();
    if (selected_media_id == PARCEL_AUDIO_LIST_ITEM_UUID)
    {
        onClickParcelAudioPause();
    }
    else if (selected_media_id == PARCEL_MEDIA_LIST_ITEM_UUID)
    {
        onClickParcelMediaPause();
    }
    else {
        LLViewerMediaImpl* impl = LLViewerMedia::getInstance()->getMediaImplFromTextureID(selected_media_id);
        if (NULL != impl && impl->isMediaTimeBased() && impl->isMediaPlaying())
        {
            impl->pause();
        }
    }
}

void LLPanelNearByMedia::onClickSelectedMediaMute()
{
    LLUUID selected_media_id = mMediaList->getValue().asUUID();
    if (selected_media_id == PARCEL_AUDIO_LIST_ITEM_UUID)
    {
        gSavedSettings.setBOOL("MuteMusic", mMuteBtn->getValue());
    }
    else {
        LLViewerMediaImpl* impl = (selected_media_id == PARCEL_MEDIA_LIST_ITEM_UUID) ?
            ((LLViewerMediaImpl*)LLViewerParcelMedia::getInstance()->getParcelMedia()) : LLViewerMedia::getInstance()->getMediaImplFromTextureID(selected_media_id);
        if (NULL != impl)
        {
            F32 volume = impl->getVolume();
            if(volume > 0.0)
            {
                impl->setMute(true);
            }
            else if (mVolumeSlider->getValueF32() == 0.0)
            {
                impl->setMute(false);
                mVolumeSlider->setValue(impl->getVolume());
            }
            else
            {
                impl->setVolume(mVolumeSlider->getValueF32());
            }
        }
    }
}

void LLPanelNearByMedia::onCommitSelectedMediaVolume()
{
    LLUUID selected_media_id = mMediaList->getValue().asUUID();
    if (selected_media_id == PARCEL_AUDIO_LIST_ITEM_UUID)
    {
        F32 vol = mVolumeSlider->getValueF32();
        gSavedSettings.setF32("AudioLevelMusic", vol);
    }
    else {
        LLViewerMediaImpl* impl = (selected_media_id == PARCEL_MEDIA_LIST_ITEM_UUID) ?
            ((LLViewerMediaImpl*)LLViewerParcelMedia::getInstance()->getParcelMedia()) : LLViewerMedia::getInstance()->getMediaImplFromTextureID(selected_media_id);
        if (NULL != impl)
        {
            impl->setVolume(mVolumeSlider->getValueF32());
        }
    }
}

void LLPanelNearByMedia::onClickSelectedMediaZoom()
{
    LLUUID selected_media_id = mMediaList->getValue().asUUID();
    if (selected_media_id == PARCEL_AUDIO_LIST_ITEM_UUID || selected_media_id == PARCEL_MEDIA_LIST_ITEM_UUID)
        return;
    LLViewerMediaFocus::getInstance()->focusZoomOnMedia(selected_media_id);
}

void LLPanelNearByMedia::onClickSelectedMediaUnzoom()
{
    LLViewerMediaFocus::getInstance()->unZoom();
}

void LLPanelNearByMedia::onMenuAction(const LLSD& userdata)
{
    const std::string command_name = userdata.asString();
    if ("copy_url" == command_name)
    {
        LLClipboard::instance().reset();
        std::string url = getSelectedUrl();

        if (!url.empty())
        {
            LLClipboard::instance().copyToClipboard(utf8str_to_wstring(url), 0, static_cast<S32>(url.size()));
        }
    }
    else if ("copy_data" == command_name)
    {
        LLClipboard::instance().reset();
        std::string url = getSelectedUrl();
        static const std::string encoding_specifier = "base64,";
        size_t pos = url.find(encoding_specifier);
        if (pos != std::string::npos)
        {
            pos += encoding_specifier.size();
            std::string res = LLBase64::decodeAsString(url.substr(pos));
            LLClipboard::instance().copyToClipboard(utf8str_to_wstring(res), 0, static_cast<S32>(res.size()));
        }
        else
        {
            url = LLURI::unescape(url);
            LLClipboard::instance().copyToClipboard(utf8str_to_wstring(url), 0, static_cast<S32>(url.size()));
        }
    }
}

bool LLPanelNearByMedia::onMenuVisible(const LLSD& userdata)
{
    const std::string command_name = userdata.asString();
    if ("copy_data" == command_name)
    {
        std::string url = getSelectedUrl();
        if (url.rfind("data:", 0) == 0)
        {
            // might be a a good idea to permit text/html only
            return true;
        }
    }
    return false;
}

// static
void LLPanelNearByMedia::getNameAndUrlHelper(LLViewerMediaImpl* impl, std::string& name, std::string & url, const std::string &defaultName)
{
    if (NULL == impl) return;

    name = impl->getName();
    url = impl->getCurrentMediaURL();   // This is the URL the media impl actually has loaded
    if (url.empty())
    {
        url = impl->getMediaEntryURL(); // This is the current URL from the media data
    }
    if (url.empty())
    {
        url = impl->getHomeURL();       // This is the home URL from the media data
    }
    if (name.empty())
    {
        name = url;
    }
    if (name.empty())
    {
        name = defaultName;
    }
}

std::string LLPanelNearByMedia::getSelectedUrl()
{
    std::string url;
    LLUUID selected_media_id = mMediaList->getValue().asUUID();
    if (selected_media_id == PARCEL_AUDIO_LIST_ITEM_UUID)
    {
        url = LLViewerMedia::getInstance()->getParcelAudioURL();
    }
    else if (selected_media_id == PARCEL_MEDIA_LIST_ITEM_UUID)
    {
        url = LLViewerParcelMedia::getInstance()->getURL();
    }
    else
    {
        LLViewerMediaImpl* impl = LLViewerMedia::getInstance()->getMediaImplFromTextureID(selected_media_id);
        if (NULL != impl)
        {
            std::string name;
            getNameAndUrlHelper(impl, name, url, mEmptyNameString);
        }
    }
    return url;
}