1/*
2 * Copyright 2006-2011, Stephan A��mus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6#include "App.h"
7
8#include <stdio.h>
9
10#include <Catalog.h>
11#include <Entry.h>
12#include <Message.h>
13#include <String.h>
14#include <Directory.h>
15#include <File.h>
16
17#include "support.h"
18
19#include "MainWindow.h"
20
21#undef B_TRANSLATION_CONTEXT
22#define B_TRANSLATION_CONTEXT "LaunchBox"
23
24
25App::App()
26	:
27	BApplication("application/x-vnd.Haiku-LaunchBox"),
28	fSettingsChanged(false),
29	fNamePanelSize(200, 50),
30	fAutoStart(false)
31{
32	SetPulseRate(3000000);
33}
34
35
36App::~App()
37{
38}
39
40
41bool
42App::QuitRequested()
43{
44	_StoreSettingsIfNeeded();
45	return true;
46}
47
48
49void
50App::ReadyToRun()
51{
52	bool windowAdded = false;
53	BRect frame(50.0, 50.0, 65.0, 100.0);
54
55	BMessage settings;
56	status_t status = load_settings(&settings, "main_settings", "LaunchBox");
57	if (status >= B_OK) {
58		BMessage windowMessage;
59		for (int32 i = 0; settings.FindMessage("window", i, &windowMessage)
60				>= B_OK; i++) {
61			BString string;
62			string << i + 1;
63			BString name(B_TRANSLATE("Pad %1"));
64			name.ReplaceFirst("%1", string);
65			BMessage* windowSettings = new BMessage(windowMessage);
66			MainWindow* window = new MainWindow(name.String(), frame,
67				windowSettings);
68			window->Show();
69			windowAdded = true;
70			frame.OffsetBy(10.0, 10.0);
71			windowMessage.MakeEmpty();
72		}
73		BSize size;
74		if (settings.FindSize("name panel size", &size) == B_OK)
75			fNamePanelSize = size;
76		bool auto_start;
77		if (settings.FindBool("autostart", &auto_start) == B_OK)
78			fAutoStart = auto_start;
79	}
80
81	if (!windowAdded) {
82		MainWindow* window = new MainWindow(B_TRANSLATE("Pad 1"), frame, true);
83		window->Show();
84	}
85}
86
87
88void
89App::MessageReceived(BMessage* message)
90{
91	switch (message->what) {
92		case MSG_ADD_WINDOW: {
93			BMessage* settings = new BMessage('sett');
94			bool wasCloned = message->FindMessage("window", settings) == B_OK;
95			BString string;
96			string << CountWindows() + 1;
97			BString name(B_TRANSLATE("Pad %1"));
98			name.ReplaceFirst("%1", string);
99			MainWindow* window = new MainWindow(name.String(),
100				BRect(50.0, 50.0, 65.0, 100.0), settings);
101			if (wasCloned)
102				window->MoveBy(10, 10);
103			window->Show();
104			fSettingsChanged = true;
105			break;
106		}
107		case MSG_TOGGLE_AUTOSTART:
108			ToggleAutoStart();
109			break;
110		case MSG_SETTINGS_CHANGED:
111			fSettingsChanged = true;
112			break;
113		default:
114			BApplication::MessageReceived(message);
115			break;
116	}
117}
118
119
120void
121App::Pulse()
122{
123	_StoreSettingsIfNeeded();
124}
125
126
127void
128App::SetNamePanelSize(const BSize& size)
129{
130	if (Lock()) {
131		fNamePanelSize = size;
132		Unlock();
133	}
134}
135
136
137void
138App::ToggleAutoStart()
139{
140	fSettingsChanged = true;
141	fAutoStart = !AutoStart();
142}
143
144
145BSize
146App::NamePanelSize()
147{
148	BSize size;
149	if (Lock()) {
150		size = fNamePanelSize;
151		Unlock();
152	}
153	return size;
154}
155
156
157void
158App::_StoreSettingsIfNeeded()
159{
160	if (!fSettingsChanged)
161		return;
162
163	BMessage settings('sett');
164	for (int32 i = 0; BWindow* window = WindowAt(i); i++) {
165		if (MainWindow* padWindow = dynamic_cast<MainWindow*>(window)) {
166			if (padWindow->Lock()) {
167				BMessage* windowSettings = padWindow->Settings();
168				if (windowSettings) {
169					padWindow->SaveSettings(windowSettings);
170					settings.AddMessage("window", windowSettings);
171				}
172				padWindow->Unlock();
173			}
174		}
175	}
176	settings.AddSize("name panel size", fNamePanelSize);
177	settings.AddBool("autostart", AutoStart());
178
179	save_settings(&settings, "main_settings", "LaunchBox");
180
181	fSettingsChanged = false;
182}