repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / mediaplayer / NetworkStreamWin.cpp
blob714a81e3d134b5749f7ee2d5e859caac22463f39
1 /*
2 * Copyright 2016 Dario Casalinuovo. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 */
8 #include "NetworkStreamWin.h"
10 #include <Alert.h>
11 #include <Button.h>
12 #include <Catalog.h>
13 #include <Clipboard.h>
14 #include <LayoutBuilder.h>
16 #include "MainApp.h"
19 #undef B_TRANSLATION_CONTEXT
20 #define B_TRANSLATION_CONTEXT "MediaPlayer-NetworkStream"
23 enum {
24 M_OPEN_URL = 'NSOP',
25 M_CANCEL = 'NSCN'
29 NetworkStreamWin::NetworkStreamWin(BMessenger target)
31 BWindow(BRect(0, 0, 300, 100), B_TRANSLATE("Open network stream"),
32 B_MODAL_WINDOW, B_NOT_RESIZABLE),
33 fTarget(target)
35 fTextControl = new BTextControl("InputControl",
36 B_TRANSLATE("Stream URL:"), NULL, NULL);
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();
48 _LookIntoClipboardForUrl();
50 CenterOnScreen();
54 NetworkStreamWin::~NetworkStreamWin()
59 void
60 NetworkStreamWin::MessageReceived(BMessage* message)
62 switch(message->what) {
63 case M_OPEN_URL:
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;
74 BMessage archivedUrl;
75 url.Archive(&archivedUrl);
77 BMessage msg(M_URL_RECEIVED);
78 msg.AddMessage("mediaplayer:url", &archivedUrl);
79 fTarget.SendMessage(&msg);
81 Quit();
82 break;
85 case M_CANCEL:
86 Quit();
87 break;
89 case B_KEY_DOWN:
91 int8 key;
92 if (message->FindInt8("byte", &key) == B_OK
93 && key == B_ENTER) {
94 PostMessage(M_OPEN_URL);
95 break;
99 default:
100 BWindow::MessageReceived(message);
105 void
106 NetworkStreamWin::WindowActivated(bool active)
108 if (active)
109 _LookIntoClipboardForUrl();
113 void
114 NetworkStreamWin::_LookIntoClipboardForUrl()
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;
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;
127 BMessage* data = be_clipboard->Data();
128 if (data->FindData("text/plain", B_MIME_TYPE,
129 (const void **)&text, &textLen) == B_OK) {
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';
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);
145 be_clipboard->Unlock();