xref: /haiku/src/apps/mediaplayer/NetworkStreamWin.cpp (revision 05fc1277c47440dc36134816d70e5723c99cfcd2)
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 <LayoutBuilder.h>
14 
15 #include "MainApp.h"
16 
17 
18 #undef B_TRANSLATION_CONTEXT
19 #define B_TRANSLATION_CONTEXT "MediaPlayer-NetworkStream"
20 
21 
22 enum  {
23 	M_OPEN_URL = 0,
24 	M_CANCEL
25 };
26 
27 
28 NetworkStreamWin::NetworkStreamWin(BMessenger target)
29 	:
30 	BWindow(BRect(0, 0, 300, 100), "Open Network Stream",
31 		B_MODAL_WINDOW, B_NOT_RESIZABLE),
32 	fTarget(target)
33 {
34 	fTextControl = new BTextControl("InputControl",
35 		"Insert URL", NULL, NULL);
36 
37 	BLayoutBuilder::Group<>(this, B_VERTICAL)
38 		.SetInsets(0, 0, 0, 0)
39 		.Add(fTextControl)
40 			.AddGroup(B_HORIZONTAL)
41 				.Add(new BButton("Ok", new BMessage(M_OPEN_URL)))
42 				.Add(new BButton("Cancel", new BMessage(M_CANCEL)))
43 			.End()
44 		.End();
45 
46 	CenterOnScreen();
47 }
48 
49 
50 NetworkStreamWin::~NetworkStreamWin()
51 {
52 }
53 
54 
55 void
56 NetworkStreamWin::MessageReceived(BMessage* message)
57 {
58 	switch(message->what) {
59 		case M_OPEN_URL:
60 		{
61 			BUrl url(fTextControl->Text());
62 			if (!url.IsValid()) {
63 				BAlert* alert = new BAlert(B_TRANSLATE("Bad URL"),
64 					B_TRANSLATE("Invalid URL inserted!"),
65 						B_TRANSLATE("OK"));
66 					alert->Go();
67 				return;
68 			}
69 
70 			BMessage archivedUrl;
71 			url.Archive(&archivedUrl);
72 
73 			BMessage msg(M_URL_RECEIVED);
74 			msg.AddMessage("mediaplayer:url", &archivedUrl);
75 			fTarget.SendMessage(&msg);
76 
77 			Quit();
78 			break;
79 		}
80 
81 		case M_CANCEL:
82 			Quit();
83 			break;
84 
85 		default:
86 			BWindow::MessageReceived(message);
87 	}
88 }
89