xref: /haiku/src/apps/launchbox/App.cpp (revision f73f5d4c42a01ece688cbb57b5d332cc0f68b2c6)
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 
15 #include "support.h"
16 
17 #include "MainWindow.h"
18 
19 #undef B_TRANSLATION_CONTEXT
20 #define B_TRANSLATION_CONTEXT "LaunchBox"
21 
22 App::App()
23 	:
24 	BApplication("application/x-vnd.Haiku-LaunchBox"),
25 	fSettingsChanged(false),
26 	fNamePanelSize(200, 50)
27 {
28 	SetPulseRate(3000000);
29 }
30 
31 
32 App::~App()
33 {
34 }
35 
36 
37 bool
38 App::QuitRequested()
39 {
40 	_StoreSettingsIfNeeded();
41 	return true;
42 }
43 
44 
45 void
46 App::ReadyToRun()
47 {
48 	bool windowAdded = false;
49 	BRect frame(50.0, 50.0, 65.0, 100.0);
50 
51 	BMessage settings;
52 	status_t status = load_settings(&settings, "main_settings", "LaunchBox");
53 	if (status >= B_OK) {
54 		BMessage windowMessage;
55 		for (int32 i = 0; settings.FindMessage("window", i, &windowMessage)
56 				>= B_OK; i++) {
57 			BString string;
58 			string << i + 1;
59 			BString name(B_TRANSLATE("Pad %1"));
60 			name.ReplaceFirst("%1", string);
61 			BMessage* windowSettings = new BMessage(windowMessage);
62 			MainWindow* window = new MainWindow(name.String(), frame,
63 				windowSettings);
64 			window->Show();
65 			windowAdded = true;
66 			frame.OffsetBy(10.0, 10.0);
67 			windowMessage.MakeEmpty();
68 		}
69 		BSize size;
70 		if (settings.FindSize("name panel size", &size) == B_OK)
71 			fNamePanelSize = size;
72 	}
73 
74 	if (!windowAdded) {
75 		MainWindow* window = new MainWindow(B_TRANSLATE("Pad 1"), frame, true);
76 		window->Show();
77 	}
78 }
79 
80 
81 void
82 App::MessageReceived(BMessage* message)
83 {
84 	switch (message->what) {
85 		case MSG_ADD_WINDOW: {
86 			BMessage* settings = new BMessage('sett');
87 			bool wasCloned = message->FindMessage("window", settings) == B_OK;
88 			BString string;
89 			string << CountWindows() + 1;
90 			BString name(B_TRANSLATE("Pad %1"));
91 			name.ReplaceFirst("%1", string);
92 			MainWindow* window = new MainWindow(name.String(),
93 				BRect(50.0, 50.0, 65.0, 100.0), settings);
94 			if (wasCloned)
95 				window->MoveBy(10, 10);
96 			window->Show();
97 			fSettingsChanged = true;
98 			break;
99 		}
100 		case MSG_SETTINGS_CHANGED:
101 			fSettingsChanged = true;
102 			break;
103 		default:
104 			BApplication::MessageReceived(message);
105 			break;
106 	}
107 }
108 
109 
110 void
111 App::Pulse()
112 {
113 	_StoreSettingsIfNeeded();
114 }
115 
116 
117 void
118 App::SetNamePanelSize(const BSize& size)
119 {
120 	if (Lock()) {
121 		fNamePanelSize = size;
122 		Unlock();
123 	}
124 }
125 
126 
127 BSize
128 App::NamePanelSize()
129 {
130 	BSize size;
131 	if (Lock()) {
132 		size = fNamePanelSize;
133 		Unlock();
134 	}
135 	return size;
136 }
137 
138 
139 void
140 App::_StoreSettingsIfNeeded()
141 {
142 	if (!fSettingsChanged)
143 		return;
144 
145 	BMessage settings('sett');
146 	for (int32 i = 0; BWindow* window = WindowAt(i); i++) {
147 		if (MainWindow* padWindow = dynamic_cast<MainWindow*>(window)) {
148 			if (padWindow->Lock()) {
149 				BMessage* windowSettings = padWindow->Settings();
150 				if (windowSettings) {
151 					padWindow->SaveSettings(windowSettings);
152 					settings.AddMessage("window", windowSettings);
153 				}
154 				padWindow->Unlock();
155 			}
156 		}
157 	}
158 	settings.AddSize("name panel size", fNamePanelSize);
159 
160 	save_settings(&settings, "main_settings", "LaunchBox");
161 
162 	fSettingsChanged = false;
163 }
164