1 /* 2 * Copyright 2016 Dario Casalinuovo. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 */ 6 7 8 #include "NetworkStreamWin.h" 9 10 #include <Alert.h> 11 #include <Button.h> 12 #include <Catalog.h> 13 #include <Clipboard.h> 14 #include <LayoutBuilder.h> 15 16 #include "MainApp.h" 17 18 19 #undef B_TRANSLATION_CONTEXT 20 #define B_TRANSLATION_CONTEXT "MediaPlayer-NetworkStream" 21 22 23 enum { 24 M_OPEN_URL = 'NSOP', 25 M_CANCEL = 'NSCN' 26 }; 27 28 29 NetworkStreamWin::NetworkStreamWin(BMessenger target) 30 : 31 BWindow(BRect(0, 0, 300, 100), B_TRANSLATE("Open network stream"), 32 B_MODAL_WINDOW, B_NOT_RESIZABLE), 33 fTarget(target) 34 { 35 fTextControl = new BTextControl("InputControl", 36 B_TRANSLATE("Stream URL:"), NULL, NULL); 37 38 BLayoutBuilder::Group<>(this, B_VERTICAL) 39 .SetInsets(B_USE_HALF_ITEM_INSETS) 40 .Add(fTextControl) 41 .AddGroup(B_HORIZONTAL) 42 .AddGlue() 43 .Add(new BButton(B_TRANSLATE("OK"), new BMessage(M_OPEN_URL))) 44 .Add(new BButton(B_TRANSLATE("Cancel"), new BMessage(M_CANCEL))) 45 .End() 46 .End(); 47 48 _LookIntoClipboardForUrl(); 49 50 CenterOnScreen(); 51 } 52 53 54 NetworkStreamWin::~NetworkStreamWin() 55 { 56 } 57 58 59 void 60 NetworkStreamWin::MessageReceived(BMessage* message) 61 { 62 switch(message->what) { 63 case M_OPEN_URL: 64 { 65 BUrl url(fTextControl->Text()); 66 if (!url.IsValid()) { 67 BAlert* alert = new BAlert(B_TRANSLATE("Bad URL"), 68 B_TRANSLATE("Invalid URL inserted!"), 69 B_TRANSLATE("OK")); 70 alert->Go(); 71 return; 72 } 73 74 BMessage archivedUrl; 75 url.Archive(&archivedUrl); 76 77 BMessage msg(M_URL_RECEIVED); 78 msg.AddMessage("mediaplayer:url", &archivedUrl); 79 fTarget.SendMessage(&msg); 80 81 Quit(); 82 break; 83 } 84 85 case M_CANCEL: 86 Quit(); 87 break; 88 89 case B_KEY_DOWN: 90 { 91 int8 key; 92 if (message->FindInt8("byte", &key) == B_OK 93 && key == B_ENTER) { 94 PostMessage(M_OPEN_URL); 95 break; 96 } 97 } 98 99 default: 100 BWindow::MessageReceived(message); 101 } 102 } 103 104 105 void 106 NetworkStreamWin::WindowActivated(bool active) 107 { 108 if (active) 109 _LookIntoClipboardForUrl(); 110 } 111 112 113 void 114 NetworkStreamWin::_LookIntoClipboardForUrl() 115 { 116 // Don't do anything if we already have something 117 // set to avoid annoying the user. 118 if (fTextControl->TextView()->TextLength() > 0) 119 return; 120 121 // Let's see if we already have an url in the clipboard 122 // in that case avoid the user to paste it. 123 if (be_clipboard->Lock()) { 124 char* text = NULL; 125 ssize_t textLen = 0; 126 127 BMessage* data = be_clipboard->Data(); 128 if (data->FindData("text/plain", B_MIME_TYPE, 129 (const void **)&text, &textLen) == B_OK) { 130 131 // NOTE: This is done because urls copied 132 // from WebPositive have the mime string at 133 // the end. 134 // TODO: Looks like a problem in Web+, should 135 // be fixed. 136 text[textLen] = '\0'; 137 138 // Before to set the text let's see if it's really 139 // a valid URL. 140 BUrl url(text); 141 if (url.IsValid()) 142 fTextControl->SetText(text); 143 } 144 145 be_clipboard->Unlock(); 146 } 147 } 148