Chat-O-Matic/application/windows/TemplateWindow.cpp
Jaidyn Ann 7c002d8fcc Room creation window; Catch-all template window
Explicit room-creation using protocol's own "room" template is now
supported.

Two new protocol API messages were added― IM_CREATE_ROOM and
IM_ROOM_CREATED, which are parallels to IM_CREATE_CHAT and
IM_CHAT_CREATED. Rather than the latter two, though, these are wholy
based on the "room" template― their slots are completely determined by
the protocol.

A generic "TemplateWindow" was created, which allows catch-all creation
of windows that leverage protocols' templates. It's used for the
room-creation window (Chat->New Room / Alt+N), for example.

At some point, it ideally should replace even the JoinRoom window, and
maybe others.
2021-06-18 01:13:02 -05:00

165 lines
3.5 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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>
#include "TemplateView.h"
const uint32 kCancel = 'canc';
const uint32 kOK = 'save';
const uint32 kAccSelected = 'JWas';
TemplateWindow::TemplateWindow(const char* title, const char* templateType,
BMessage* msg, Server* server)
:
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)
{
_InitInterface();
_LoadTemplate();
CenterOnScreen();
}
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;
BMessage* settings = new BMessage(*fMessage);
status_t result = fTemplate->Save(fTemplateView, settings);
if (result != B_OK) {
BAlert* alert = new BAlert("", "Invalid settings― make sure "
"each item is filled out.\n", "OK", NULL, NULL);
alert->Go();
break;
}
ProtocolLooper* looper
= fServer->GetProtocolLooper(fAccounts.ValueAt(fSelectedAcc));
if (looper == NULL)
break;
looper->PostMessage(settings);
Close();
break;
}
case kCancel:
Close();
break;
case kChanged:
break;
default:
BWindow::MessageReceived(msg);
}
}
void
TemplateWindow::SetTarget(BHandler* target)
{
fTarget = target;
}
void
TemplateWindow::_InitInterface()
{
fTemplateView = new TemplateView("template");
fMenuField = new BMenuField("accountMenuField", NULL, _CreateAccountMenu());
BButton* cancel = new BButton("Cancel", new BMessage(kCancel));
BButton* fOkButton = new BButton("OK", new BMessage(kOK));
if (fAccounts.CountItems() <= 0)
fOkButton->SetEnabled(false);
const float spacing = be_control_look->DefaultItemSpacing();
BLayoutBuilder::Group<>(this, B_VERTICAL)
.Add(fTemplateView)
.AddGroup(B_HORIZONTAL)
.Add(fMenuField)
.AddGlue()
.Add(cancel)
.Add(fOkButton)
.End()
.AddGlue()
.SetInsets(spacing, spacing, spacing, 0);
}
void
TemplateWindow::_LoadTemplate()
{
if (fAccounts.CountItems() == 0)
return;
// fOkButton->SetEnabled(true);
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);
}
BMenu*
TemplateWindow::_CreateAccountMenu()
{
BMenu* menu = new BMenu("accountMenu");
for (int i = 0; i < fAccounts.CountItems(); i++)
menu->AddItem(new BMenuItem(fAccounts.KeyAt(i).String(),
new BMessage(kAccSelected)));
menu->SetRadioMode(true);
menu->SetLabelFromMarked(true);
menu->ItemAt(fSelectedAcc)->SetMarked(true);
if (fAccounts.CountItems() == 0)
menu->SetEnabled(false);
return menu;
}