Renga/jabber/CustomStatusWindow.cpp

164 lines
4.3 KiB
C++

//////////////////////////////////////////////////
// Blabber [SendTalkWindow.cpp]
//////////////////////////////////////////////////
#include "CustomStatusWindow.h"
#include <cstdio>
#include <Application.h>
#include <Box.h>
#include <Button.h>
#include <LayoutBuilder.h>
#include <StringView.h>
#include "support/AppLocation.h"
#include "ui/PictureView.h"
#include "ui/MainWindow.h"
#include "BlabberSettings.h"
#include "GenericFunctions.h"
#include "JabberSpeak.h"
#include "Messages.h"
CustomStatusWindow *CustomStatusWindow::fInstance = NULL;
CustomStatusWindow *CustomStatusWindow::Instance() {
if (fInstance == NULL) {
float main_window_width = 410;
float main_window_height = 100;
BRect frame(GenericFunctions::CenteredFrame(main_window_width, main_window_height));
fInstance = new CustomStatusWindow(frame);
}
return fInstance;
}
CustomStatusWindow::CustomStatusWindow(BRect frame)
: BWindow(frame, "Create a Custom Status",
B_TITLED_WINDOW,
B_AUTO_UPDATE_SIZE_LIMITS |
B_NOT_ZOOMABLE |
B_NOT_RESIZABLE)
{
fChatRadio = new BRadioButton("status", "Chat", NULL);
fAwayRadio = new BRadioButton("status", "Away", NULL);
fExtendedAwayRadio = new BRadioButton("status", "Extended Away", NULL);
fDoNotDisturbRadio = new BRadioButton("status", "Do Not Disturb", NULL);
BStringView *queryString = new BStringView(NULL, "Please provide your detailed status:");
// handle
fStatus = new BTextControl(NULL, NULL, "", NULL);
fStatus->SetDivider(0);
if (BlabberSettings::Instance()->Data("last-custom-more-exact-status")) {
fStatus->SetText(BlabberSettings::Instance()->Data("last-custom-more-exact-status"));
} else {
fStatus->SetText("I'm at my computer.");
}
BButton *cancelButton = new BButton("cancel", "Nevermind", new BMessage(JAB_CANCEL));
cancelButton->SetTarget(this);
BButton *okButton = new BButton("ok", "OK", new BMessage(JAB_OK));
okButton->MakeDefault(true);
okButton->SetTarget(this);
SetLayout(new BGroupLayout(B_HORIZONTAL));
BLayoutBuilder::Group<>(this)
.SetInsets(B_USE_WINDOW_SPACING)
.AddGroup(B_VERTICAL, B_USE_HALF_ITEM_SPACING, 0)
.Add(fChatRadio)
.Add(fAwayRadio)
.Add(fExtendedAwayRadio)
.Add(fDoNotDisturbRadio)
.End()
.AddGroup(B_VERTICAL, B_USE_ITEM_SPACING, 2)
.Add(queryString)
.Add(fStatus)
.AddGroup(B_HORIZONTAL, B_USE_ITEM_SPACING, 0)
.AddGlue()
.Add(cancelButton)
.Add(okButton)
.End()
.End()
.End();
// set defaults
string exact_status;
if (BlabberSettings::Instance()->Data("last-custom-exact-status")) {
// get last status
exact_status = BlabberSettings::Instance()->Data("last-custom-exact-status");
// map to radio buttons
if (exact_status == "away") {
fAwayRadio->SetValue(1);
} else if (exact_status == "xa") {
fExtendedAwayRadio->SetValue(1);
} else if (exact_status == "dnd") {
fDoNotDisturbRadio->SetValue(1);
} else {
fChatRadio->SetValue(1);
}
} else {
fChatRadio->SetValue(1);
}
// focus
fStatus->MakeFocus(true);
}
CustomStatusWindow::~CustomStatusWindow() {
fInstance = NULL;
}
void CustomStatusWindow::MessageReceived(BMessage *msg) {
switch (msg->what) {
//// JAB_OK
case JAB_OK: {
if (fChatRadio->Value()) {
JabberSpeak::Instance()->SendPresence(gloox::Presence::Chat, fStatus->Text());
BlabberSettings::Instance()->SetData("last-custom-exact-status", "chat");
} else if (fAwayRadio->Value()) {
JabberSpeak::Instance()->SendPresence(gloox::Presence::Away, fStatus->Text());
BlabberSettings::Instance()->SetData("last-custom-exact-status", "away");
} else if (fExtendedAwayRadio->Value()) {
JabberSpeak::Instance()->SendPresence(gloox::Presence::XA, fStatus->Text());
BlabberSettings::Instance()->SetData("last-custom-exact-status", "xa");
} else if (fDoNotDisturbRadio->Value()) {
JabberSpeak::Instance()->SendPresence(gloox::Presence::DND, fStatus->Text());
BlabberSettings::Instance()->SetData("last-custom-exact-status", "dnd");
}
BlabberSettings::Instance()->SetTag("last-used-custom-status", true);
BlabberSettings::Instance()->SetData("last-custom-more-exact-status", fStatus->Text());
BlabberSettings::Instance()->WriteToFile();
// update menu
BlabberMainWindow::Instance()->SetCustomStatus(fStatus->Text());
PostMessage(B_QUIT_REQUESTED);
break;
}
//// JAB_CANCEL
case JAB_CANCEL: {
PostMessage(B_QUIT_REQUESTED);
break;
}
}
}