xref: /haiku/src/preferences/repositories/AddRepoWindow.cpp (revision 48d27198d0c684ad918733a087ea94897d7d6a4f)
1 /*
2  * Copyright 2017 Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Brian Hill
7  */
8 
9 
10 #include "AddRepoWindow.h"
11 
12 #include <Alert.h>
13 #include <Application.h>
14 #include <Catalog.h>
15 #include <Clipboard.h>
16 #include <LayoutBuilder.h>
17 #include <Url.h>
18 
19 #include "constants.h"
20 
21 #undef B_TRANSLATION_CONTEXT
22 #define B_TRANSLATION_CONTEXT "AddRepoWindow"
23 
24 static float sAddWindowWidth = 500.0;
25 
26 
27 AddRepoWindow::AddRepoWindow(BRect size, const BMessenger& messenger)
28 	:
29 	BWindow(BRect(0, 0, sAddWindowWidth, 10), "AddWindow", B_MODAL_WINDOW,
30 		B_ASYNCHRONOUS_CONTROLS	| B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
31 	fReplyMessenger(messenger)
32 {
33 	fText = new BTextControl("text", B_TRANSLATE_COMMENT("Repository URL:",
34 		"Text box label"), "", new BMessage(ADD_BUTTON_PRESSED));
35 	fAddButton = new BButton(B_TRANSLATE_COMMENT("Add", "Button label"),
36 		new BMessage(ADD_BUTTON_PRESSED));
37 	fAddButton->MakeDefault(true);
38 	fCancelButton = new BButton(kCancelLabel,
39 		new BMessage(CANCEL_BUTTON_PRESSED));
40 
41 	BLayoutBuilder::Group<>(this, B_VERTICAL)
42 		.SetInsets(B_USE_WINDOW_SPACING)
43 		.Add(fText)
44 		.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
45 			.AddGlue()
46 			.Add(fCancelButton)
47 			.Add(fAddButton)
48 		.End()
49 	.End();
50 	_GetClipboardData();
51 	fText->MakeFocus();
52 
53 	// Move to the center of the preflet window
54 	CenterIn(size);
55 	float widthDifference = size.Width() - Frame().Width();
56 	if (widthDifference < 0)
57 		MoveBy(widthDifference / 2.0, 0);
58 	Show();
59 }
60 
61 
62 void
63 AddRepoWindow::Quit()
64 {
65 	fReplyMessenger.SendMessage(ADD_WINDOW_CLOSED);
66 	BWindow::Quit();
67 }
68 
69 
70 void
71 AddRepoWindow::MessageReceived(BMessage* message)
72 {
73 	switch (message->what)
74 	{
75 		case CANCEL_BUTTON_PRESSED:
76 			if (QuitRequested())
77 				Quit();
78 			break;
79 
80 		case ADD_BUTTON_PRESSED:
81 		{
82 			BString url(fText->Text());
83 			if (url != "") {
84 				// URL must have a protocol
85 				BUrl newRepoUrl(url);
86 				if (!newRepoUrl.IsValid()) {
87 					BAlert* alert = new BAlert("error",
88 						B_TRANSLATE_COMMENT("This is not a valid URL.",
89 							"Add URL error message"),
90 						kOKLabel, NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
91 					alert->SetFeel(B_MODAL_APP_WINDOW_FEEL);
92 					alert->Go(NULL);
93 					// Center the alert to this window and move down some
94 					alert->CenterIn(Frame());
95 					alert->MoveBy(0, kAddWindowOffset);
96 				} else {
97 					BMessage* addMessage = new BMessage(ADD_REPO_URL);
98 					addMessage->AddString(key_url, url);
99 					fReplyMessenger.SendMessage(addMessage);
100 					Quit();
101 				}
102 			}
103 			break;
104 		}
105 
106 		default:
107 			BWindow::MessageReceived(message);
108 	}
109 }
110 
111 
112 void
113 AddRepoWindow::FrameResized(float newWidth, float newHeight)
114 {
115 	sAddWindowWidth = newWidth;
116 }
117 
118 
119 status_t
120 AddRepoWindow::_GetClipboardData()
121 {
122 	if (be_clipboard->Lock()) {
123 		const char* string;
124 		ssize_t stringLen;
125 		BMessage* clip = be_clipboard->Data();
126 		clip->FindData("text/plain", B_MIME_TYPE, (const void **)&string,
127 			&stringLen);
128 		be_clipboard->Unlock();
129 
130 		// The string must be a valid url
131 		BString clipString(string, stringLen);
132 		BUrl testUrl(clipString.String());
133 		if (!testUrl.IsValid())
134 			return B_ERROR;
135 		else
136 			fText->SetText(clipString);
137 	}
138 	return B_OK;
139 }
140