2010-05-09 01:27:48 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2009-2010, Pier Luigi Fiorini. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
|
|
|
|
*/
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
#include <Alert.h>
|
2010-05-09 01:27:48 -05:00
|
|
|
#include <Button.h>
|
|
|
|
#include <ControlLook.h>
|
|
|
|
#include <GroupLayout.h>
|
|
|
|
#include <GroupLayoutBuilder.h>
|
|
|
|
#include <TextControl.h>
|
|
|
|
#include <String.h>
|
|
|
|
|
|
|
|
#include <libinterface/Divider.h>
|
|
|
|
|
|
|
|
#include "AccountDialog.h"
|
|
|
|
#include "AccountView.h"
|
|
|
|
#include "ProtocolSettings.h"
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
const uint32 kCancel = 'canc';
|
|
|
|
const uint32 kOK = 'save';
|
2010-05-09 01:27:48 -05:00
|
|
|
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
AccountDialog::AccountDialog(const char* title, ProtocolSettings* settings,
|
2010-05-10 16:54:28 -05:00
|
|
|
const char* account)
|
2010-05-09 01:27:48 -05:00
|
|
|
: BWindow(BRect(0, 0, 1, 1), title, B_MODAL_WINDOW, B_NOT_RESIZABLE |
|
2010-05-16 16:02:50 -05:00
|
|
|
B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
|
|
|
|
fSettings(settings),
|
|
|
|
fAccount(account),
|
|
|
|
fTarget(NULL)
|
2010-05-09 01:27:48 -05:00
|
|
|
{
|
|
|
|
fAccountName = new BTextControl("accountName", "Account name:", NULL, NULL);
|
2010-05-16 16:02:50 -05:00
|
|
|
fAccountName->SetExplicitMinSize(BSize(300, B_SIZE_UNSET));
|
2010-05-09 01:27:48 -05:00
|
|
|
fAccountName->SetFont(be_bold_font);
|
2010-05-16 16:02:50 -05:00
|
|
|
|
|
|
|
fAccountName->SetText(fAccount.String());
|
2010-05-09 01:27:48 -05:00
|
|
|
|
|
|
|
Divider* divider = new Divider("divider", B_WILL_DRAW);
|
|
|
|
|
|
|
|
fTop = new AccountView("top");
|
2010-05-16 16:02:50 -05:00
|
|
|
if (fAccount.Length() > 0)
|
|
|
|
fSettings->Load(fAccount.String(), fTop);
|
2010-05-09 01:27:48 -05:00
|
|
|
else
|
|
|
|
fSettings->LoadTemplate(fTop);
|
|
|
|
|
|
|
|
BButton* cancel = new BButton("Cancel", new BMessage(kCancel));
|
|
|
|
BButton* ok = new BButton("OK", new BMessage(kOK));
|
|
|
|
|
|
|
|
const float spacing = be_control_look->DefaultItemSpacing();
|
|
|
|
|
|
|
|
SetLayout(new BGroupLayout(B_VERTICAL, spacing));
|
|
|
|
AddChild(BGroupLayoutBuilder(B_VERTICAL, spacing)
|
|
|
|
.Add(fAccountName)
|
|
|
|
.Add(divider)
|
|
|
|
.Add(fTop)
|
|
|
|
.AddGroup(B_HORIZONTAL, spacing)
|
|
|
|
.AddGlue()
|
|
|
|
.Add(cancel)
|
|
|
|
.Add(ok)
|
|
|
|
.End()
|
|
|
|
.AddGlue()
|
|
|
|
.SetInsets(spacing, spacing, spacing, 0)
|
|
|
|
);
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
fAccountName->MakeFocus(true);
|
|
|
|
|
2010-05-09 01:27:48 -05:00
|
|
|
CenterOnScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
void
|
|
|
|
AccountDialog::SetTarget(BHandler* target)
|
|
|
|
{
|
|
|
|
fTarget = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-09 01:27:48 -05:00
|
|
|
void
|
|
|
|
AccountDialog::MessageReceived(BMessage* msg)
|
|
|
|
{
|
|
|
|
switch (msg->what) {
|
2010-05-16 16:02:50 -05:00
|
|
|
case kOK: {
|
|
|
|
// Are we renaming settings?
|
|
|
|
bool renaming = ((fAccount.Length() > 0)
|
|
|
|
&& (fAccount != fAccountName->Text()));
|
|
|
|
|
|
|
|
// Rename account settings
|
|
|
|
if (renaming) {
|
|
|
|
if (fSettings->Rename(fAccount.String(), fAccountName->Text()) != B_OK) {
|
|
|
|
BString text("An error is occurred renaming the account ");
|
|
|
|
text << fAccount << " to " << fAccountName->Text() << "!";
|
|
|
|
BAlert* alert = new BAlert("", text.String(), "OK", NULL, NULL,
|
|
|
|
B_WIDTH_AS_USUAL, B_STOP_ALERT);
|
|
|
|
alert->Go();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save account settings
|
|
|
|
if (fSettings->Save(fAccountName->Text(), fTop) == B_OK) {
|
|
|
|
if (fTarget) {
|
|
|
|
BMessage* saveMsg = new BMessage(renaming
|
|
|
|
? kAccountRenamed : kAccountSaved);
|
|
|
|
saveMsg->AddPointer("settings", fSettings);
|
|
|
|
if (renaming) {
|
|
|
|
saveMsg->AddString("from", fAccount.String());
|
|
|
|
saveMsg->AddString("to", fAccountName->Text());
|
|
|
|
} else
|
|
|
|
saveMsg->AddString("account", fAccountName->Text());
|
|
|
|
BMessenger(fTarget).SendMessage(saveMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
Close();
|
|
|
|
} else {
|
|
|
|
BAlert* alert = new BAlert("", "An error is occurred saving the settings.\n"
|
|
|
|
"Check if your disk has enough space.", "OK", NULL, NULL, B_WIDTH_AS_USUAL,
|
|
|
|
B_STOP_ALERT);
|
|
|
|
alert->Go();
|
|
|
|
|
2010-05-09 01:27:48 -05:00
|
|
|
Close();
|
2010-05-16 16:02:50 -05:00
|
|
|
}
|
2010-05-09 01:27:48 -05:00
|
|
|
break;
|
2010-05-16 16:02:50 -05:00
|
|
|
}
|
2010-05-09 01:27:48 -05:00
|
|
|
case kCancel:
|
|
|
|
Close();
|
|
|
|
break;
|
|
|
|
case kChanged:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BWindow::MessageReceived(msg);
|
|
|
|
}
|
|
|
|
}
|