2010-05-07 04:47:10 -05:00
|
|
|
/*
|
2011-12-03 16:38:03 -06:00
|
|
|
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
|
|
|
|
* Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
|
2010-05-07 04:47:10 -05:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Andrea Anzani, andrea.anzani@gmail.com
|
|
|
|
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Application.h>
|
|
|
|
#include <Alert.h>
|
2021-05-25 13:57:53 -05:00
|
|
|
#include <LayoutBuilder.h>
|
|
|
|
#include <MenuBar.h>
|
2010-05-09 03:23:53 -05:00
|
|
|
#include <MenuItem.h>
|
2010-05-30 13:56:24 -05:00
|
|
|
#include <Notification.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <ScrollView.h>
|
|
|
|
#include <StringView.h>
|
|
|
|
#include <TextControl.h>
|
|
|
|
#include <TranslationUtils.h>
|
|
|
|
|
2010-05-09 03:23:53 -05:00
|
|
|
#include <libinterface/BitmapUtils.h>
|
|
|
|
|
2011-12-14 17:36:27 -06:00
|
|
|
#include "AccountManager.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
#include "CayaConstants.h"
|
2010-05-16 16:02:50 -05:00
|
|
|
#include "CayaMessages.h"
|
2010-05-19 15:37:26 -05:00
|
|
|
#include "CayaProtocolMessages.h"
|
2010-05-22 09:18:11 -05:00
|
|
|
#include "CayaPreferences.h"
|
2021-05-27 11:15:30 -05:00
|
|
|
#include "ConversationItem.h"
|
|
|
|
#include "ConversationListView.h"
|
2021-05-28 22:26:32 -05:00
|
|
|
#include "ConversationView.h"
|
2021-05-29 15:47:54 -05:00
|
|
|
#include "EditingFilter.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
#include "NotifyMessage.h"
|
|
|
|
#include "MainWindow.h"
|
|
|
|
#include "PreferencesDialog.h"
|
2011-12-14 17:36:27 -06:00
|
|
|
#include "ReplicantStatusView.h"
|
2021-05-26 07:48:25 -05:00
|
|
|
#include "RosterWindow.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
#include "Server.h"
|
|
|
|
#include "StatusView.h"
|
|
|
|
|
2012-09-25 16:45:29 -05:00
|
|
|
|
2010-05-19 17:28:26 -05:00
|
|
|
const uint32 kLogin = 'LOGI';
|
2010-05-12 13:15:21 -05:00
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
MainWindow::MainWindow()
|
2010-05-19 17:28:26 -05:00
|
|
|
:
|
2012-05-15 11:48:53 -05:00
|
|
|
BWindow(BRect(0, 0, 300, 400), "Caya", B_TITLED_WINDOW, 0),
|
2011-12-14 17:36:27 -06:00
|
|
|
fWorkspaceChanged(false)
|
2010-05-22 09:18:11 -05:00
|
|
|
{
|
2010-05-07 04:47:10 -05:00
|
|
|
fStatusView = new StatusView("statusView");
|
|
|
|
|
2021-05-25 13:57:53 -05:00
|
|
|
// Menubar
|
|
|
|
BMenuBar* menuBar = new BMenuBar("MenuBar");
|
|
|
|
|
|
|
|
BMenu* programMenu = new BMenu("Program");
|
|
|
|
programMenu->AddItem(new BMenuItem("About" B_UTF8_ELLIPSIS,
|
2010-05-12 13:15:21 -05:00
|
|
|
new BMessage(B_ABOUT_REQUESTED)));
|
2021-05-25 13:57:53 -05:00
|
|
|
programMenu->AddItem(new BMenuItem("Preferences" B_UTF8_ELLIPSIS,
|
|
|
|
new BMessage(CAYA_SHOW_SETTINGS), ',', B_COMMAND_KEY));
|
|
|
|
programMenu->AddItem(new BSeparatorItem());
|
|
|
|
programMenu->AddItem(new BMenuItem("Quit",
|
|
|
|
new BMessage(B_QUIT_REQUESTED), 'Q', B_COMMAND_KEY));
|
|
|
|
programMenu->SetTargetForItems(this);
|
|
|
|
|
2021-05-26 07:48:25 -05:00
|
|
|
BMenu* chatMenu = new BMenu("Chat");
|
|
|
|
chatMenu->AddItem(new BMenuItem("New chat" B_UTF8_ELLIPSIS,
|
|
|
|
new BMessage(CAYA_NEW_CHAT)));
|
|
|
|
chatMenu->SetTargetForItems(this);
|
|
|
|
|
2021-05-25 13:57:53 -05:00
|
|
|
menuBar->AddItem(programMenu);
|
2021-05-26 07:48:25 -05:00
|
|
|
menuBar->AddItem(chatMenu);
|
2021-05-25 13:57:53 -05:00
|
|
|
|
2021-05-27 11:15:30 -05:00
|
|
|
fListView = new ConversationListView("roomList");
|
|
|
|
|
2021-05-29 15:47:54 -05:00
|
|
|
fChatView = new ConversationView();
|
|
|
|
|
|
|
|
fSendView = new BTextView("fSendView");
|
|
|
|
fSendScroll = new BScrollView("fSendScroll", fSendView,
|
|
|
|
B_WILL_DRAW, false, true);
|
|
|
|
fSendView->SetWordWrap(true);
|
|
|
|
AddCommonFilter(new EditingFilter(fSendView));
|
|
|
|
fSendView->MakeFocus(true);
|
|
|
|
|
|
|
|
fRightView = new BGroupView("rightView", B_VERTICAL);
|
|
|
|
fRightView->AddChild(fChatView);
|
|
|
|
fRightView->AddChild(fSendScroll);
|
|
|
|
|
|
|
|
|
|
|
|
BLayoutBuilder::Group<>(this, B_VERTICAL)
|
2021-05-25 13:57:53 -05:00
|
|
|
.Add(menuBar)
|
2021-05-29 15:47:54 -05:00
|
|
|
|
|
|
|
.AddGroup(B_HORIZONTAL)
|
2021-05-25 13:57:53 -05:00
|
|
|
.SetInsets(5, 5, 5, 10)
|
2021-05-29 15:47:54 -05:00
|
|
|
.AddGroup(B_VERTICAL)
|
|
|
|
.Add(fListView)
|
|
|
|
.Add(fStatusView)
|
|
|
|
.End()
|
|
|
|
|
|
|
|
.Add(fRightView)
|
2021-05-25 13:57:53 -05:00
|
|
|
.End()
|
|
|
|
.End();
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
// Filter messages using Server
|
|
|
|
fServer = new Server();
|
|
|
|
AddFilter(fServer);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
CenterOnScreen();
|
2011-12-14 17:36:27 -06:00
|
|
|
|
|
|
|
//TODO check for errors here
|
2012-03-03 20:27:16 -06:00
|
|
|
ReplicantStatusView::InstallReplicant();
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
void
|
|
|
|
MainWindow::Start()
|
|
|
|
{
|
|
|
|
// Login all accounts
|
|
|
|
fServer->LoginAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
bool
|
|
|
|
MainWindow::QuitRequested()
|
|
|
|
{
|
2015-10-01 23:17:54 -05:00
|
|
|
int32 button_index = 0;
|
|
|
|
if(!CayaPreferences::Item()->DisableQuitConfirm)
|
|
|
|
{
|
2021-05-26 07:48:25 -05:00
|
|
|
BAlert* alert = new BAlert("Closing", "Are you sure you want to quit?",
|
|
|
|
"Yes", "No", NULL, B_WIDTH_AS_USUAL, B_OFFSET_SPACING,
|
|
|
|
B_WARNING_ALERT);
|
2015-10-01 23:17:54 -05:00
|
|
|
alert->SetShortcut(0, B_ESCAPE);
|
|
|
|
button_index = alert->Go();
|
|
|
|
}
|
|
|
|
|
2015-09-29 03:59:40 -05:00
|
|
|
if(button_index == 0) {
|
|
|
|
fServer->Quit();
|
|
|
|
CayaPreferences::Get()->Save();
|
|
|
|
ReplicantStatusView::RemoveReplicant();
|
|
|
|
be_app->PostMessage(B_QUIT_REQUESTED);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
MainWindow::MessageReceived(BMessage* message)
|
|
|
|
{
|
2010-05-16 16:02:50 -05:00
|
|
|
switch (message->what) {
|
2012-06-07 11:46:07 -05:00
|
|
|
case CAYA_SHOW_SETTINGS:
|
|
|
|
{
|
2010-05-07 04:47:10 -05:00
|
|
|
PreferencesDialog* dialog = new PreferencesDialog();
|
|
|
|
dialog->Show();
|
|
|
|
break;
|
|
|
|
}
|
2021-05-26 07:48:25 -05:00
|
|
|
|
|
|
|
case CAYA_NEW_CHAT:
|
2012-06-07 11:46:07 -05:00
|
|
|
{
|
2021-05-26 07:48:25 -05:00
|
|
|
RosterWindow* roster = new RosterWindow("Invite contact to chat"
|
|
|
|
B_UTF8_ELLIPSIS, IM_CREATE_CHAT, new BMessenger(this), fServer);
|
|
|
|
roster->Show();
|
2010-05-16 16:02:50 -05:00
|
|
|
break;
|
|
|
|
}
|
2011-12-14 17:36:27 -06:00
|
|
|
|
|
|
|
case CAYA_REPLICANT_STATUS_SET:
|
|
|
|
{
|
|
|
|
int32 status;
|
|
|
|
message->FindInt32("status", &status);
|
|
|
|
AccountManager* accountManager = AccountManager::Get();
|
|
|
|
accountManager->SetStatus((CayaStatus)status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CAYA_REPLICANT_SHOW_WINDOW:
|
|
|
|
{
|
|
|
|
if (LockLooper()) {
|
|
|
|
SetWorkspaces(B_CURRENT_WORKSPACE);
|
|
|
|
|
|
|
|
if ((IsMinimized() || IsHidden())
|
|
|
|
|| fWorkspaceChanged) {
|
|
|
|
Minimize(false);
|
|
|
|
Show();
|
|
|
|
fWorkspaceChanged = false;
|
|
|
|
} else if ((!IsMinimized() || !IsHidden())
|
|
|
|
|| (!fWorkspaceChanged)) {
|
|
|
|
Minimize(true);
|
|
|
|
}
|
|
|
|
UnlockLooper();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-29 15:47:54 -05:00
|
|
|
case CAYA_CHAT:
|
|
|
|
{
|
|
|
|
message->AddString("body", fSendView->Text());
|
|
|
|
fChatView->MessageReceived(message);
|
|
|
|
fSendView->SetText("");
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
case IM_MESSAGE:
|
|
|
|
ImMessage(message);
|
|
|
|
break;
|
|
|
|
case IM_ERROR:
|
|
|
|
ImError(message);
|
|
|
|
break;
|
2010-05-12 13:57:19 -05:00
|
|
|
case B_ABOUT_REQUESTED:
|
|
|
|
be_app->PostMessage(message);
|
|
|
|
break;
|
2011-12-14 17:36:27 -06:00
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
default:
|
|
|
|
BWindow::MessageReceived(message);
|
2010-05-22 09:18:11 -05:00
|
|
|
}
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
void
|
|
|
|
MainWindow::ImError(BMessage* msg)
|
|
|
|
{
|
2010-05-30 15:50:26 -05:00
|
|
|
const char* error = NULL;
|
|
|
|
const char* detail = msg->FindString("detail");
|
|
|
|
|
|
|
|
if (msg->FindString("error", &error) != B_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Format error message
|
|
|
|
BString errMsg(error);
|
|
|
|
if (detail)
|
|
|
|
errMsg << "\n" << detail;
|
|
|
|
|
|
|
|
BAlert* alert = new BAlert("Error", errMsg.String(), "OK", NULL, NULL,
|
|
|
|
B_WIDTH_AS_USUAL, B_STOP_ALERT);
|
2010-05-07 04:47:10 -05:00
|
|
|
alert->Go();
|
|
|
|
}
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
void
|
|
|
|
MainWindow::ImMessage(BMessage* msg)
|
2010-05-22 09:18:11 -05:00
|
|
|
{
|
2010-05-07 04:47:10 -05:00
|
|
|
int32 im_what = msg->FindInt32("im_what");
|
2010-05-16 16:02:50 -05:00
|
|
|
switch (im_what) {
|
2010-05-07 04:47:10 -05:00
|
|
|
case IM_OWN_CONTACT_INFO:
|
2010-05-19 15:37:26 -05:00
|
|
|
fStatusView->SetName(msg->FindString("name"));
|
2010-05-30 03:51:19 -05:00
|
|
|
break;
|
|
|
|
case IM_OWN_AVATAR_SET:
|
|
|
|
{
|
2010-05-07 04:47:10 -05:00
|
|
|
entry_ref ref;
|
2010-05-30 03:51:19 -05:00
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
if (msg->FindRef("ref", &ref) == B_OK) {
|
|
|
|
BBitmap* bitmap = BTranslationUtils::GetBitmap(&ref);
|
2010-05-30 03:51:19 -05:00
|
|
|
fStatusView->SetAvatarIcon(bitmap);
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-05-27 11:15:30 -05:00
|
|
|
case IM_MESSAGE_RECEIVED:
|
2021-05-28 22:26:32 -05:00
|
|
|
{
|
|
|
|
_EnsureConversationItem(msg);
|
|
|
|
break;
|
|
|
|
}
|
2021-05-27 11:15:30 -05:00
|
|
|
case IM_MESSAGE_SENT:
|
|
|
|
case IM_CHAT_CREATED:
|
|
|
|
{
|
|
|
|
_EnsureConversationItem(msg);
|
|
|
|
break;
|
|
|
|
}
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-29 15:47:54 -05:00
|
|
|
void
|
|
|
|
MainWindow::SetConversation(Conversation* chat)
|
|
|
|
{
|
|
|
|
BView* current = fRightView->FindView("chatView");
|
|
|
|
fRightView->RemoveChild(fRightView->FindView("chatView"));
|
|
|
|
fRightView->RemoveChild(fRightView->FindView("fSendScroll"));
|
|
|
|
|
|
|
|
fChatView = chat->GetView();
|
|
|
|
|
|
|
|
fRightView->AddChild(fChatView);
|
|
|
|
fRightView->AddChild(fSendScroll);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
void
|
|
|
|
MainWindow::ObserveInteger(int32 what, int32 val)
|
|
|
|
{
|
|
|
|
switch (what) {
|
|
|
|
case INT_ACCOUNT_STATUS:
|
|
|
|
fStatusView->SetStatus((CayaStatus)val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-27 11:15:30 -05:00
|
|
|
void
|
|
|
|
MainWindow::UpdateListItem(ConversationItem* item)
|
|
|
|
{
|
|
|
|
if (fListView->HasItem(item) == true)
|
|
|
|
fListView->InvalidateItem(fListView->IndexOf(item));
|
|
|
|
else
|
|
|
|
fListView->AddItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-14 17:36:27 -06:00
|
|
|
void
|
|
|
|
MainWindow::WorkspaceActivated(int32 workspace, bool active)
|
|
|
|
{
|
|
|
|
if (active)
|
|
|
|
fWorkspaceChanged = false;
|
|
|
|
else
|
|
|
|
fWorkspaceChanged = true;
|
|
|
|
}
|
2021-05-26 07:48:25 -05:00
|
|
|
|
|
|
|
|
2021-05-27 11:15:30 -05:00
|
|
|
ConversationItem*
|
|
|
|
MainWindow::_EnsureConversationItem(BMessage* msg)
|
|
|
|
{
|
|
|
|
ChatMap chats = fServer->Conversations();
|
|
|
|
|
|
|
|
BString chat_id = msg->FindString("chat_id");
|
|
|
|
Conversation* chat = fServer->ConversationById(chat_id);
|
|
|
|
|
|
|
|
if (chat != NULL) {
|
|
|
|
ConversationItem* item = chat->GetConversationItem();
|
|
|
|
if (fListView->HasItem(item)) {
|
|
|
|
UpdateListItem(item);
|
|
|
|
}
|
|
|
|
else if (item != NULL) {
|
|
|
|
fListView->AddItem(item);
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|