docs/ikteam: Delete most files.
[haiku.git] / src / preferences / repositories / AddRepoWindow.cpp
blobb163ce57b15656b03881e4a718f16e11234e66bc
1 /*
2 * Copyright 2017 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Brian Hill
7 */
10 #include "AddRepoWindow.h"
12 #include <Alert.h>
13 #include <Application.h>
14 #include <Catalog.h>
15 #include <Clipboard.h>
16 #include <LayoutBuilder.h>
17 #include <Url.h>
19 #include "constants.h"
21 #undef B_TRANSLATION_CONTEXT
22 #define B_TRANSLATION_CONTEXT "AddRepoWindow"
24 static float sAddWindowWidth = 500.0;
27 AddRepoWindow::AddRepoWindow(BRect size, const BMessenger& messenger)
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)
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));
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();
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();
62 void
63 AddRepoWindow::Quit()
65 fReplyMessenger.SendMessage(ADD_WINDOW_CLOSED);
66 BWindow::Quit();
70 void
71 AddRepoWindow::MessageReceived(BMessage* message)
73 switch (message->what)
75 case CANCEL_BUTTON_PRESSED:
76 if (QuitRequested())
77 Quit();
78 break;
80 case ADD_BUTTON_PRESSED:
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();
103 break;
106 default:
107 BWindow::MessageReceived(message);
112 void
113 AddRepoWindow::FrameResized(float newWidth, float newHeight)
115 sAddWindowWidth = newWidth;
119 status_t
120 AddRepoWindow::_GetClipboardData()
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();
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);
138 return B_OK;