2021-06-18 01:13:02 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2009-2010, Pier Luigi Fiorini. All rights reserved.
|
|
|
|
* Copyright 2021, Jaidyn Levesque. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
|
|
|
|
* Jaidyn Levesque, jadedctrl@teknik.io
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "TemplateWindow.h"
|
|
|
|
|
|
|
|
#include <Alert.h>
|
|
|
|
#include <Button.h>
|
|
|
|
#include <ControlLook.h>
|
|
|
|
#include <LayoutBuilder.h>
|
|
|
|
#include <TextControl.h>
|
|
|
|
#include <String.h>
|
|
|
|
|
2021-06-19 18:25:58 -05:00
|
|
|
#include "CayaUtils.h"
|
2021-06-18 01:13:02 -05:00
|
|
|
#include "TemplateView.h"
|
|
|
|
|
|
|
|
|
|
|
|
const uint32 kOK = 'save';
|
|
|
|
const uint32 kAccSelected = 'JWas';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TemplateWindow::TemplateWindow(const char* title, const char* templateType,
|
2021-06-19 18:25:58 -05:00
|
|
|
BMessage* msg, Server* server, bigtime_t instance)
|
2021-06-18 01:13:02 -05:00
|
|
|
:
|
|
|
|
BWindow(BRect(0, 0, 400, 100), title, B_FLOATING_WINDOW,
|
|
|
|
B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
|
|
|
|
fServer(server),
|
|
|
|
fAccounts(server->GetAccounts()),
|
|
|
|
fSelectedAcc(0),
|
|
|
|
fTemplate(NULL),
|
|
|
|
fTemplateType(templateType),
|
|
|
|
fMessage(msg),
|
|
|
|
fTarget(NULL)
|
|
|
|
{
|
2021-06-19 18:25:58 -05:00
|
|
|
_InitInterface(instance);
|
2021-06-18 01:13:02 -05:00
|
|
|
_LoadTemplate();
|
|
|
|
CenterOnScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-19 18:25:58 -05:00
|
|
|
TemplateWindow::TemplateWindow(const char* title, ProtocolTemplate* temp,
|
|
|
|
BMessage* msg, Server* server, bigtime_t instance)
|
|
|
|
:
|
|
|
|
BWindow(BRect(0, 0, 400, 100), title, B_FLOATING_WINDOW,
|
|
|
|
B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
|
|
|
|
fServer(server),
|
|
|
|
fAccounts(server->GetAccounts()),
|
|
|
|
fSelectedAcc(0),
|
|
|
|
fTemplate(temp),
|
|
|
|
fMessage(msg)
|
|
|
|
{
|
|
|
|
_InitInterface(instance);
|
|
|
|
CenterOnScreen();
|
|
|
|
|
|
|
|
fTemplate->Load(fTemplateView);
|
|
|
|
fTemplateView->AttachedToWindow();
|
|
|
|
fTemplateView->MakeFocus(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-18 01:13:02 -05:00
|
|
|
void
|
|
|
|
TemplateWindow::MessageReceived(BMessage* msg)
|
|
|
|
{
|
|
|
|
switch (msg->what) {
|
|
|
|
case kAccSelected:
|
|
|
|
{
|
|
|
|
int32 index;
|
|
|
|
if (msg->FindInt32("index", &index) == B_OK)
|
|
|
|
fSelectedAcc = index;
|
|
|
|
_LoadTemplate();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kOK: {
|
|
|
|
// Save account settings
|
|
|
|
if (fTemplate == NULL || fTemplateView == NULL)
|
|
|
|
break;
|
2021-06-18 12:49:30 -05:00
|
|
|
BString error = "Some items are empty. Please make sure to fill "
|
|
|
|
"out every item.";
|
2021-06-18 01:13:02 -05:00
|
|
|
BMessage* settings = new BMessage(*fMessage);
|
2021-06-18 12:49:30 -05:00
|
|
|
status_t result = fTemplate->Save(fTemplateView, settings, &error);
|
2021-06-18 01:13:02 -05:00
|
|
|
|
|
|
|
if (result != B_OK) {
|
2021-06-18 12:49:30 -05:00
|
|
|
BAlert* alert = new BAlert("", error.String(), "OK", NULL, NULL,
|
|
|
|
B_WIDTH_AS_USUAL, B_WARNING_ALERT);
|
2021-06-18 01:13:02 -05:00
|
|
|
alert->Go();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ProtocolLooper* looper
|
|
|
|
= fServer->GetProtocolLooper(fAccounts.ValueAt(fSelectedAcc));
|
|
|
|
if (looper == NULL)
|
|
|
|
break;
|
|
|
|
looper->PostMessage(settings);
|
|
|
|
Close();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kChanged:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BWindow::MessageReceived(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
TemplateWindow::SetTarget(BHandler* target)
|
|
|
|
{
|
|
|
|
fTarget = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-06-19 18:25:58 -05:00
|
|
|
TemplateWindow::_InitInterface(bigtime_t instance)
|
2021-06-18 01:13:02 -05:00
|
|
|
{
|
|
|
|
fTemplateView = new TemplateView("template");
|
2021-06-19 18:25:58 -05:00
|
|
|
BMenu* menu = CreateAccountMenu(fAccounts, BMessage(kAccSelected));
|
|
|
|
fMenuField = new BMenuField("accountMenuField", NULL, menu);
|
|
|
|
|
|
|
|
if (instance > -1) {
|
|
|
|
for (int i = 0; i < fAccounts.CountItems(); i++)
|
|
|
|
if (fAccounts.ValueAt(i) == instance) {
|
|
|
|
menu->ItemAt(i)->SetMarked(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fMenuField->SetEnabled(false);
|
|
|
|
}
|
2021-06-18 01:13:02 -05:00
|
|
|
|
|
|
|
BButton* fOkButton = new BButton("OK", new BMessage(kOK));
|
|
|
|
if (fAccounts.CountItems() <= 0)
|
|
|
|
fOkButton->SetEnabled(false);
|
2021-06-18 14:18:45 -05:00
|
|
|
fOkButton->MakeDefault(true);
|
2021-06-18 01:13:02 -05:00
|
|
|
|
|
|
|
const float spacing = be_control_look->DefaultItemSpacing();
|
2021-06-18 14:18:45 -05:00
|
|
|
|
2021-06-18 01:13:02 -05:00
|
|
|
BLayoutBuilder::Group<>(this, B_VERTICAL)
|
2021-06-18 14:18:45 -05:00
|
|
|
.SetInsets(B_USE_DEFAULT_SPACING)
|
2021-06-18 01:13:02 -05:00
|
|
|
.Add(fTemplateView)
|
|
|
|
.AddGroup(B_HORIZONTAL)
|
|
|
|
.Add(fMenuField)
|
|
|
|
.AddGlue()
|
2021-06-18 14:18:45 -05:00
|
|
|
.Add(new BButton("Cancel", new BMessage(B_QUIT_REQUESTED)))
|
2021-06-18 01:13:02 -05:00
|
|
|
.Add(fOkButton)
|
|
|
|
.End()
|
2021-06-18 14:18:45 -05:00
|
|
|
.End();
|
2021-06-18 01:13:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
TemplateWindow::_LoadTemplate()
|
|
|
|
{
|
2021-06-19 18:25:58 -05:00
|
|
|
if (fAccounts.CountItems() == 0 || fTemplateType.IsEmpty() == true)
|
2021-06-18 01:13:02 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
ProtocolLooper* looper
|
|
|
|
= fServer->GetProtocolLooper(fAccounts.ValueAt(fSelectedAcc));
|
|
|
|
if (looper == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fTemplate = new ProtocolTemplate(looper->Protocol(), fTemplateType.String());
|
|
|
|
|
|
|
|
for (int i = 0; fTemplateView->CountChildren(); i++)
|
|
|
|
fTemplateView->RemoveChild(fTemplateView->ChildAt(i));
|
|
|
|
|
|
|
|
fTemplate->Load(fTemplateView);
|
2021-06-18 14:18:45 -05:00
|
|
|
fTemplateView->AttachedToWindow();
|
|
|
|
fTemplateView->MakeFocus(true);
|
2021-06-18 01:13:02 -05:00
|
|
|
}
|