2 * Copyright 2012, Michael Lotz, mmlr@mlotz.ch. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
7 #include "KeyRequestWindow.h"
12 #include <GridLayout.h>
14 #include <GroupLayout.h>
15 #include <GroupView.h>
17 #include <MenuField.h>
19 #include <NetworkDevice.h>
20 #include <PopUpMenu.h>
21 #include <SpaceLayoutItem.h>
22 #include <StringView.h>
23 #include <TextControl.h>
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "KeyRequestWindow"
34 static const uint32 kMessageCancel
= 'btcl';
35 static const uint32 kMessageUnlock
= 'btul';
38 class KeyRequestView
: public BView
{
42 BView("KeyRequestView", B_WILL_DRAW
),
45 SetViewUIColor(B_PANEL_BACKGROUND_COLOR
);
47 BGroupLayout
* rootLayout
= new(std::nothrow
) BGroupLayout(B_VERTICAL
);
48 if (rootLayout
== NULL
)
51 SetLayout(rootLayout
);
53 BGridView
* controls
= new(std::nothrow
) BGridView();
57 BGridLayout
* layout
= controls
->GridLayout();
59 float inset
= ceilf(be_plain_font
->Size() * 0.7);
60 rootLayout
->SetInsets(inset
, inset
, inset
, inset
);
61 rootLayout
->SetSpacing(inset
);
62 layout
->SetSpacing(inset
, inset
);
64 BStringView
* label
= new(std::nothrow
) BStringView("keyringLabel",
65 B_TRANSLATE("Keyring:"));
70 layout
->AddView(label
, 0, row
);
72 fKeyringName
= new(std::nothrow
) BStringView("keyringName", "");
73 if (fKeyringName
== NULL
)
76 layout
->AddView(fKeyringName
, 1, row
++);
78 fPassword
= new(std::nothrow
) BTextControl(B_TRANSLATE("Password:"), "", NULL
);
79 if (fPassword
== NULL
)
82 BLayoutItem
* layoutItem
= fPassword
->CreateTextViewLayoutItem();
83 layoutItem
->SetExplicitMinSize(BSize(fPassword
->StringWidth(
84 "0123456789012345678901234567890123456789") + inset
,
87 layout
->AddItem(fPassword
->CreateLabelLayoutItem(), 0, row
);
88 layout
->AddItem(layoutItem
, 1, row
++);
90 BGroupView
* buttons
= new(std::nothrow
) BGroupView(B_HORIZONTAL
);
94 fCancelButton
= new(std::nothrow
) BButton(B_TRANSLATE("Cancel"),
95 new BMessage(kMessageCancel
));
96 buttons
->GroupLayout()->AddView(fCancelButton
);
98 buttons
->GroupLayout()->AddItem(BSpaceLayoutItem::CreateGlue());
100 fUnlockButton
= new(std::nothrow
) BButton(B_TRANSLATE("Unlock"),
101 new BMessage(kMessageUnlock
));
102 buttons
->GroupLayout()->AddView(fUnlockButton
);
104 BTextView
* message
= new(std::nothrow
) BTextView("message");
105 message
->SetText(B_TRANSLATE("An application wants to access the "
106 "keyring below, but it is locked with a passphrase. Please enter "
107 "the passphrase to unlock the keyring.\n"
108 "If you unlock the keyring, it stays unlocked until the system is "
109 "shut down or the keyring is manually locked again.\n"
110 "If you cancel this dialog the keyring will remain locked."));
111 message
->SetViewUIColor(B_PANEL_BACKGROUND_COLOR
);
112 rgb_color textColor
= ui_color(B_PANEL_TEXT_COLOR
);
113 message
->SetFontAndColor(be_plain_font
, B_FONT_ALL
, &textColor
);
114 message
->MakeEditable(false);
115 message
->MakeSelectable(false);
116 message
->SetWordWrap(true);
118 rootLayout
->AddView(message
);
119 rootLayout
->AddView(controls
);
120 rootLayout
->AddView(buttons
);
126 fCancelButton
->SetTarget(Window());
127 fUnlockButton
->SetTarget(Window());
128 fUnlockButton
->MakeDefault(true);
129 fPassword
->MakeFocus();
133 SetUp(const BString
& keyringName
)
135 fKeyringName
->SetText(keyringName
);
139 Complete(BMessage
& keyMessage
)
141 BPasswordKey
password(fPassword
->Text(), B_KEY_PURPOSE_KEYRING
, "");
142 return password
.Flatten(keyMessage
);
146 BStringView
* fKeyringName
;
147 BTextControl
* fPassword
;
148 BButton
* fCancelButton
;
149 BButton
* fUnlockButton
;
153 KeyRequestWindow::KeyRequestWindow()
155 BWindow(BRect(50, 50, 100, 100), B_TRANSLATE("Unlock keyring"),
156 B_TITLED_WINDOW
, B_NOT_RESIZABLE
| B_ASYNCHRONOUS_CONTROLS
157 | B_NOT_ZOOMABLE
| B_NOT_MINIMIZABLE
| B_AUTO_UPDATE_SIZE_LIMITS
158 | B_CLOSE_ON_ESCAPE
),
163 fDoneSem
= create_sem(0, "keyring unlock dialog");
167 BLayout
* layout
= new(std::nothrow
) BGroupLayout(B_HORIZONTAL
);
173 fRequestView
= new(std::nothrow
) KeyRequestView();
174 if (fRequestView
== NULL
)
177 layout
->AddView(fRequestView
);
181 KeyRequestWindow::~KeyRequestWindow()
184 delete_sem(fDoneSem
);
189 KeyRequestWindow::QuitRequested()
191 fResult
= B_CANCELED
;
192 release_sem(fDoneSem
);
198 KeyRequestWindow::MessageReceived(BMessage
* message
)
200 switch (message
->what
) {
203 fResult
= message
->what
== kMessageUnlock
? B_OK
: B_CANCELED
;
204 release_sem(fDoneSem
);
208 BWindow::MessageReceived(message
);
213 KeyRequestWindow::RequestKey(const BString
& keyringName
, BMessage
& keyMessage
)
215 fRequestView
->SetUp(keyringName
);
221 while (acquire_sem(fDoneSem
) == B_INTERRUPTED
)
224 status_t result
= fResult
;
226 result
= fRequestView
->Complete(keyMessage
);