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>
|
2010-05-09 03:23:53 -05:00
|
|
|
#include <Button.h>
|
|
|
|
#include <CardLayout.h>
|
2011-12-14 17:36:27 -06:00
|
|
|
#include <Deskbar.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <ListView.h>
|
|
|
|
#include <Box.h>
|
|
|
|
#include <CheckBox.h>
|
|
|
|
#include <Entry.h>
|
2010-05-09 03:23:53 -05:00
|
|
|
#include <GridLayout.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <GridLayoutBuilder.h>
|
|
|
|
#include <GroupLayout.h>
|
|
|
|
#include <GroupLayoutBuilder.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-09 03:23:53 -05:00
|
|
|
#include <PopUpMenu.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <SpaceLayoutItem.h>
|
|
|
|
#include <ScrollView.h>
|
|
|
|
#include <StringView.h>
|
|
|
|
#include <TextControl.h>
|
|
|
|
#include <TranslationUtils.h>
|
|
|
|
|
2010-05-09 03:23:53 -05:00
|
|
|
#include <libinterface/BitmapUtils.h>
|
|
|
|
#include <libinterface/ToolButton.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"
|
2010-05-09 03:23:53 -05:00
|
|
|
#include "CayaResources.h"
|
|
|
|
#include "CayaUtils.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"
|
2010-05-07 04:47:10 -05:00
|
|
|
#include "RosterItem.h"
|
|
|
|
#include "RosterListView.h"
|
|
|
|
#include "Server.h"
|
|
|
|
#include "StatusView.h"
|
|
|
|
|
2010-05-19 17:28:26 -05:00
|
|
|
const uint32 kLogin = 'LOGI';
|
2010-05-12 13:15:21 -05:00
|
|
|
const uint32 kSearchContact = 'SRCH';
|
|
|
|
|
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
|
|
|
:
|
2011-12-14 17:36:27 -06:00
|
|
|
BWindow(BRect(0, 0, 300, 400), "Caya", B_DOCUMENT_WINDOW, 0),
|
|
|
|
fWorkspaceChanged(false)
|
2010-05-22 09:18:11 -05:00
|
|
|
{
|
2010-05-07 04:47:10 -05:00
|
|
|
fStatusView = new StatusView("statusView");
|
|
|
|
|
|
|
|
BTextControl* searchBox = new BTextControl("searchBox", NULL, NULL,
|
|
|
|
new BMessage(kSearchContact));
|
|
|
|
|
|
|
|
fListView = new RosterListView("buddyView");
|
2011-12-14 17:36:27 -06:00
|
|
|
fListView->SetInvocationMessage(new BMessage(CAYA_OPEN_CHAT_WINDOW));
|
2010-05-07 04:47:10 -05:00
|
|
|
BScrollView* scrollView = new BScrollView("scrollview", fListView,
|
|
|
|
B_WILL_DRAW, false, true);
|
|
|
|
|
2010-05-09 03:23:53 -05:00
|
|
|
// Wrench menu
|
|
|
|
BPopUpMenu* wrenchMenu = new BPopUpMenu("Wrench");
|
2010-05-12 13:57:19 -05:00
|
|
|
(void)wrenchMenu->AddItem(new BMenuItem("About" B_UTF8_ELLIPSIS,
|
2010-05-12 13:15:21 -05:00
|
|
|
new BMessage(B_ABOUT_REQUESTED)));
|
|
|
|
(void)wrenchMenu->AddItem(new BSeparatorItem());
|
2010-05-12 13:57:19 -05:00
|
|
|
(void)wrenchMenu->AddItem(new BMenuItem("Preferences" B_UTF8_ELLIPSIS,
|
2011-12-14 17:36:27 -06:00
|
|
|
new BMessage(CAYA_SHOW_SETTINGS)));
|
2010-05-12 13:15:21 -05:00
|
|
|
(void)wrenchMenu->AddItem(new BSeparatorItem());
|
|
|
|
(void)wrenchMenu->AddItem(new BMenuItem("Quit",
|
|
|
|
new BMessage(B_QUIT_REQUESTED)));
|
2010-05-09 03:23:53 -05:00
|
|
|
wrenchMenu->SetTargetForItems(this);
|
|
|
|
|
|
|
|
// Tool icon
|
|
|
|
BResources* res = CayaResources();
|
|
|
|
BBitmap* toolIcon = IconFromResources(res, kToolIcon);
|
|
|
|
delete res;
|
|
|
|
|
|
|
|
// Wrench tool button
|
|
|
|
ToolButton* wrench = new ToolButton(NULL, NULL);
|
|
|
|
wrench->SetBitmap(toolIcon);
|
|
|
|
wrench->SetMenu(wrenchMenu);
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
SetLayout(new BGridLayout(5, 5));
|
|
|
|
AddChild(BGridLayoutBuilder(5, 5)
|
2010-05-09 03:23:53 -05:00
|
|
|
.Add(fStatusView, 0, 0)
|
|
|
|
.Add(wrench, 1, 0)
|
|
|
|
.Add(searchBox, 0, 1)
|
|
|
|
.Add(scrollView, 0, 2, 2)
|
2010-05-07 04:47:10 -05:00
|
|
|
.SetInsets(5, 5, 5, 10)
|
|
|
|
);
|
|
|
|
|
|
|
|
AddShortcut('a', B_COMMAND_KEY, new BMessage(B_ABOUT_REQUESTED));
|
|
|
|
MoveTo(BAlert::AlertPosition(Bounds().Width(), Bounds().Height() / 2));
|
|
|
|
|
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
|
|
|
|
_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()
|
|
|
|
{
|
|
|
|
fListView->MakeEmpty();
|
2010-05-16 16:02:50 -05:00
|
|
|
fServer->Quit();
|
2010-05-22 09:18:11 -05:00
|
|
|
CayaPreferences::Get()->Save();
|
2011-12-14 17:36:27 -06:00
|
|
|
_RemoveReplicant();
|
2010-05-07 04:47:10 -05:00
|
|
|
be_app->PostMessage(B_QUIT_REQUESTED);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
MainWindow::MessageReceived(BMessage* message)
|
|
|
|
{
|
2010-05-16 16:02:50 -05:00
|
|
|
switch (message->what) {
|
|
|
|
case kSearchContact: {
|
2010-05-07 04:47:10 -05:00
|
|
|
void* control = NULL;
|
|
|
|
if (message->FindPointer("source", &control) != B_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
BTextControl* searchBox = static_cast<BTextControl*>(control);
|
|
|
|
if (searchBox == NULL)
|
|
|
|
return;
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
RosterMap map = fServer->RosterItems();
|
2010-05-07 04:47:10 -05:00
|
|
|
for (uint32 i = 0; i < map.CountItems(); i++) {
|
|
|
|
ContactLinker* linker = map.ValueAt(i);
|
|
|
|
RosterItem* item = linker->GetRosterItem();
|
|
|
|
|
|
|
|
// If the search filter has been deleted show all the items,
|
|
|
|
// otherwise remove the item in order to show only items
|
|
|
|
// that matches the search criteria
|
|
|
|
if (strcmp(searchBox->Text(), "") == 0)
|
|
|
|
AddItem(item);
|
|
|
|
else if (linker->GetName().IFindFirst(searchBox->Text()) == B_ERROR)
|
|
|
|
RemoveItem(item);
|
|
|
|
else
|
|
|
|
AddItem(item);
|
|
|
|
UpdateListItem(item);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-12-14 17:36:27 -06:00
|
|
|
case CAYA_SHOW_SETTINGS: {
|
2010-05-07 04:47:10 -05:00
|
|
|
PreferencesDialog* dialog = new PreferencesDialog();
|
|
|
|
dialog->Show();
|
|
|
|
break;
|
|
|
|
}
|
2011-12-14 17:36:27 -06:00
|
|
|
case CAYA_OPEN_CHAT_WINDOW: {
|
2010-05-16 16:02:50 -05:00
|
|
|
int index = message->FindInt32("index");
|
|
|
|
RosterItem* ritem = ItemAt(index);
|
|
|
|
if (ritem != NULL)
|
|
|
|
ritem->GetContactLinker()->ShowWindow();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2010-05-19 15:37:26 -05:00
|
|
|
case IM_STATUS_SET:
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
int32 status;
|
|
|
|
|
|
|
|
if (msg->FindInt32("status", &status) != B_OK)
|
|
|
|
return;
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
RosterItem* rosterItem = fServer->RosterItemForId(msg->FindString("id"));
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
if (rosterItem) {
|
|
|
|
UpdateListItem(rosterItem);
|
2010-05-30 13:56:24 -05:00
|
|
|
|
|
|
|
// Add or remove item
|
2010-05-07 04:47:10 -05:00
|
|
|
switch (status) {
|
|
|
|
case CAYA_OFFLINE:
|
2010-05-30 13:56:24 -05:00
|
|
|
// By default offline contacts are hidden
|
2010-05-07 04:47:10 -05:00
|
|
|
if (HasItem(rosterItem))
|
|
|
|
RemoveItem(rosterItem);
|
|
|
|
return;
|
|
|
|
default:
|
2010-05-30 13:56:24 -05:00
|
|
|
// Add item because it has a non-offline status
|
2010-05-07 04:47:10 -05:00
|
|
|
if (!HasItem(rosterItem))
|
|
|
|
AddItem(rosterItem);
|
|
|
|
break;
|
|
|
|
}
|
2010-05-30 13:56:24 -05:00
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
UpdateListItem(rosterItem);
|
|
|
|
|
|
|
|
// Sort list view again
|
|
|
|
fListView->Sort();
|
2010-05-30 13:56:24 -05:00
|
|
|
|
2010-07-03 02:55:23 -05:00
|
|
|
#if 0
|
2010-05-30 13:56:24 -05:00
|
|
|
switch (status) {
|
|
|
|
case CAYA_ONLINE:
|
|
|
|
case CAYA_OFFLINE:
|
|
|
|
// Notify when contact is online or offline
|
|
|
|
if (status == CAYA_ONLINE) {
|
|
|
|
BString message;
|
|
|
|
message << rosterItem->GetContactLinker()->GetName();
|
|
|
|
|
|
|
|
if (status == CAYA_ONLINE)
|
|
|
|
message << " is available!";
|
|
|
|
else
|
|
|
|
message << " is offline!";
|
|
|
|
|
|
|
|
BNotification notification(B_INFORMATION_NOTIFICATION);
|
2010-07-03 02:55:23 -05:00
|
|
|
notification.SetGroup(BString("Caya"));
|
|
|
|
notification.SetTitle(BString("Presence"));
|
2010-05-30 13:56:24 -05:00
|
|
|
notification.SetIcon(rosterItem->Bitmap());
|
2010-07-03 02:55:23 -05:00
|
|
|
notification.SetContent(message);
|
|
|
|
notification.Send();
|
2010-05-30 13:56:24 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-07-03 02:55:23 -05:00
|
|
|
#endif
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2010-05-19 15:37:26 -05:00
|
|
|
case IM_AVATAR_SET:
|
2010-05-07 04:47:10 -05:00
|
|
|
case IM_CONTACT_INFO:
|
2010-05-29 23:28:04 -05:00
|
|
|
case IM_EXTENDED_CONTACT_INFO:
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2010-05-19 15:37:26 -05:00
|
|
|
RosterItem* rosterItem
|
|
|
|
= fServer->RosterItemForId(msg->FindString("id"));
|
2010-05-07 04:47:10 -05:00
|
|
|
if (rosterItem)
|
|
|
|
UpdateListItem(rosterItem);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
MainWindow::ObserveInteger(int32 what, int32 val)
|
|
|
|
{
|
|
|
|
switch (what) {
|
|
|
|
case INT_ACCOUNT_STATUS:
|
|
|
|
fStatusView->SetStatus((CayaStatus)val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-22 09:18:11 -05:00
|
|
|
void
|
2010-05-07 04:47:10 -05:00
|
|
|
MainWindow::UpdateListItem(RosterItem* item)
|
|
|
|
{
|
|
|
|
if (fListView->HasItem(item))
|
|
|
|
fListView->InvalidateItem(fListView->IndexOf(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32
|
|
|
|
MainWindow::CountItems() const
|
|
|
|
{
|
|
|
|
return fListView->CountItems();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-22 09:18:11 -05:00
|
|
|
RosterItem*
|
2010-05-07 04:47:10 -05:00
|
|
|
MainWindow::ItemAt(int index)
|
|
|
|
{
|
|
|
|
return dynamic_cast<RosterItem*>(fListView->ItemAt(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-22 09:18:11 -05:00
|
|
|
void
|
2010-05-07 04:47:10 -05:00
|
|
|
MainWindow::AddItem(RosterItem* item)
|
|
|
|
{
|
|
|
|
// Don't add offline items and avoid duplicates
|
|
|
|
if ((item->Status() == CAYA_OFFLINE) || HasItem(item))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Add item and sort
|
|
|
|
fListView->AddItem(item);
|
|
|
|
fListView->Sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-22 09:18:11 -05:00
|
|
|
bool
|
|
|
|
MainWindow::HasItem(RosterItem* item)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
return fListView->HasItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-22 09:18:11 -05:00
|
|
|
void
|
|
|
|
MainWindow::RemoveItem(RosterItem* item)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
// Remove item and sort
|
|
|
|
fListView->RemoveItem(item);
|
|
|
|
fListView->Sort();
|
|
|
|
}
|
2011-12-14 17:36:27 -06:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
MainWindow::WorkspaceActivated(int32 workspace, bool active)
|
|
|
|
{
|
|
|
|
if (active)
|
|
|
|
fWorkspaceChanged = false;
|
|
|
|
else
|
|
|
|
fWorkspaceChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// The following methods install
|
|
|
|
// and remove the Caya's replicant
|
|
|
|
// from Deskbar.
|
|
|
|
status_t
|
|
|
|
MainWindow::_InstallReplicant()
|
|
|
|
{
|
|
|
|
BDeskbar deskbar;
|
|
|
|
if (deskbar.HasItem("ReplicantStatusView")) {
|
|
|
|
_RemoveReplicant();
|
|
|
|
}
|
|
|
|
ReplicantStatusView* view = new ReplicantStatusView();
|
|
|
|
return deskbar.AddItem(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
MainWindow::_RemoveReplicant()
|
|
|
|
{
|
|
|
|
BDeskbar deskbar;
|
|
|
|
return deskbar.RemoveItem("ReplicantStatusView");
|
|
|
|
}
|