1/*
2 * Copyright 2009-2010, Philippe Houdoin, phoudoin@haiku-os.org. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include <new>
8
9#include <stdio.h>
10#include <string.h>
11#include <stdarg.h>
12
13#include <Application.h>
14#include <ListView.h>
15#include <ScrollView.h>
16#include <File.h>
17#include <FindDirectory.h>
18#include <Path.h>
19
20#include "MessageCodes.h"
21#include "SettingsManager.h"
22#include "TeamsWindow.h"
23#include "TeamsListView.h"
24
25
26TeamsWindow::TeamsWindow(SettingsManager* settingsManager)
27	:
28	BWindow(BRect(100, 100, 500, 250), "Teams", B_DOCUMENT_WINDOW,
29		B_ASYNCHRONOUS_CONTROLS),
30	fTeamsListView(NULL),
31	fSettingsManager(settingsManager)
32{
33}
34
35
36TeamsWindow::~TeamsWindow()
37{
38}
39
40
41/*static*/ TeamsWindow*
42TeamsWindow::Create(SettingsManager* settingsManager)
43{
44	TeamsWindow* self = new TeamsWindow(settingsManager);
45
46	try {
47		self->_Init();
48	} catch (...) {
49		delete self;
50		throw;
51	}
52
53	return self;
54}
55
56
57void
58TeamsWindow::MessageReceived(BMessage* message)
59{
60	switch (message->what) {
61		case kMsgDebugThisTeam:
62		{
63			TeamRow* row = dynamic_cast<TeamRow*>(fTeamsListView->CurrentSelection());
64			if (row != NULL) {
65				BMessage message(MSG_DEBUG_THIS_TEAM);
66				message.AddInt32("team", row->TeamID());
67				be_app_messenger.SendMessage(&message);
68			}
69			break;
70		}
71
72		default:
73			BWindow::MessageReceived(message);
74			break;
75	}
76}
77
78
79bool
80TeamsWindow::QuitRequested()
81{
82	_SaveSettings();
83
84	be_app_messenger.SendMessage(MSG_TEAMS_WINDOW_CLOSED);
85	return true;
86}
87
88
89// #pragma mark --
90
91
92void
93TeamsWindow::_Init()
94{
95	BMessage settings;
96	_LoadSettings(settings);
97
98	BRect frame;
99	if (settings.FindRect("teams window frame", &frame) == B_OK) {
100		MoveTo(frame.LeftTop());
101		ResizeTo(frame.Width(), frame.Height());
102	}
103
104	// TODO: add button to start a team debugger
105	// TODO: add UI to setup arguments and environ, launch a program
106	//       and start his team debugger
107
108	// Add a teams list view
109	frame = Bounds();
110	frame.InsetBy(-1, -1);
111	fTeamsListView = new TeamsListView(frame, "TeamsList");
112	fTeamsListView->SetInvocationMessage(new BMessage(kMsgDebugThisTeam));
113
114	AddChild(fTeamsListView);
115}
116
117
118status_t
119TeamsWindow::_OpenSettings(BFile& file, uint32 mode)
120{
121	BPath path;
122	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
123		return B_ERROR;
124
125	path.Append("Debugger settings");
126
127	return file.SetTo(path.Path(), mode);
128}
129
130
131status_t
132TeamsWindow::_LoadSettings(BMessage& settings)
133{
134	BFile file;
135	status_t status = _OpenSettings(file, B_READ_ONLY);
136	if (status < B_OK)
137		return status;
138
139	return settings.Unflatten(&file);
140}
141
142
143status_t
144TeamsWindow::_SaveSettings()
145{
146	BFile file;
147	status_t status = _OpenSettings(file,
148		B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
149
150	if (status < B_OK)
151		return status;
152
153	BMessage settings('hdbg');
154	status = settings.AddRect("teams window frame", Frame());
155	if (status != B_OK)
156		return status;
157
158	if (status == B_OK)
159		status = settings.Flatten(&file);
160
161	return status;
162}
163