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>
|
2010-10-24 01:27:09 -05:00
|
|
|
#include <LayoutBuilder.h>
|
2010-05-09 01:27:48 -05:00
|
|
|
#include <TextControl.h>
|
|
|
|
#include <String.h>
|
|
|
|
|
|
|
|
#include <libinterface/Divider.h>
|
|
|
|
|
|
|
|
#include "AccountDialog.h"
|
|
|
|
#include "AccountView.h"
|
|
|
|
#include "ProtocolSettings.h"
|
|
|
|
|
2010-05-20 16:31:55 -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-20 16:31:55 -05:00
|
|
|
const char* account)
|
|
|
|
:
|
|
|
|
BWindow(BRect(0, 0, 1, 1), title, B_MODAL_WINDOW, B_NOT_RESIZABLE
|
|
|
|
| B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
|
2010-05-16 16:02:50 -05:00
|
|
|
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();
|
2010-10-24 01:27:09 -05:00
|
|
|
BLayoutBuilder::Group<>(this, B_VERTICAL)
|
2010-05-09 01:27:48 -05:00
|
|
|
.Add(fAccountName)
|
|
|
|
.Add(divider)
|
|
|
|
.Add(fTop)
|
|
|
|
.AddGroup(B_HORIZONTAL, spacing)
|
|
|
|
.AddGlue()
|
|
|
|
.Add(cancel)
|
|
|
|
.Add(ok)
|
|
|
|
.End()
|
|
|
|
.AddGlue()
|
2010-10-24 01:27:09 -05:00
|
|
|
.SetInsets(spacing, spacing, spacing, 0);
|
2010-05-09 01:27:48 -05:00
|
|
|
|
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: {
|
2010-05-29 23:28:04 -05:00
|
|
|
// Are we renaming or adding?
|
2010-05-16 16:02:50 -05:00
|
|
|
bool renaming = ((fAccount.Length() > 0)
|
|
|
|
&& (fAccount != fAccountName->Text()));
|
2010-05-29 23:28:04 -05:00
|
|
|
bool adding = fAccount.Length() == 0;
|
2010-05-16 16:02:50 -05:00
|
|
|
|
|
|
|
// 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) {
|
2010-05-29 23:28:04 -05:00
|
|
|
if (fTarget && (adding || renaming)) {
|
2010-05-16 16:02:50 -05:00
|
|
|
BMessage* saveMsg = new BMessage(renaming
|
2010-05-29 23:28:04 -05:00
|
|
|
? kAccountRenamed : kAccountAdded);
|
2010-05-16 16:02:50 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|