xref: /haiku/src/apps/launchbox/App.cpp (revision 922e7ba1f3228e6f28db69b0ded8f86eb32dea17)
1 /*
2  * Copyright 2006-2009, 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_TRANSLATE_CONTEXT
20 #define B_TRANSLATE_CONTEXT "LaunchBox"
21 
22 App::App()
23 	:
24 	BApplication("application/x-vnd.Haiku-LaunchBox"),
25 	fSettingsChanged(false)
26 {
27 	SetPulseRate(3000000);
28 }
29 
30 
31 App::~App()
32 {
33 }
34 
35 
36 bool
37 App::QuitRequested()
38 {
39 	_StoreSettingsIfNeeded();
40 	return true;
41 }
42 
43 
44 void
45 App::ReadyToRun()
46 {
47 	bool windowAdded = false;
48 	BRect frame(50.0, 50.0, 65.0, 100.0);
49 
50 	BMessage settings('sett');
51 	status_t status = load_settings(&settings, "main_settings", "LaunchBox");
52 	if (status >= B_OK) {
53 		BMessage windowMessage;
54 		for (int32 i = 0; settings.FindMessage("window", i, &windowMessage)
55 				>= B_OK; i++) {
56 			BString string;
57 			string << i + 1;
58 			BString name(B_TRANSLATE("Pad %1"));
59 			name.ReplaceFirst("%1", string);
60 			BMessage* windowSettings = new BMessage(windowMessage);
61 			MainWindow* window = new MainWindow(name.String(), frame,
62 				windowSettings);
63 			window->Show();
64 			windowAdded = true;
65 			frame.OffsetBy(10.0, 10.0);
66 			windowMessage.MakeEmpty();
67 		}
68 	}
69 
70 	if (!windowAdded) {
71 		MainWindow* window = new MainWindow(B_TRANSLATE("Pad 1"), frame, true);
72 		window->Show();
73 	}
74 }
75 
76 
77 void
78 App::MessageReceived(BMessage* message)
79 {
80 	switch (message->what) {
81 		case MSG_ADD_WINDOW: {
82 			BMessage* settings = new BMessage('sett');
83 			bool wasCloned = message->FindMessage("window", settings) == B_OK;
84 			BString string;
85 			string << CountWindows() + 1;
86 			BString name(B_TRANSLATE("Pad %1"));
87 			name.ReplaceFirst("%1", string);
88 			MainWindow* window = new MainWindow(name.String(),
89 				BRect(50.0, 50.0, 65.0, 100.0), settings);
90 			if (wasCloned)
91 				window->MoveBy(10, 10);
92 			window->Show();
93 			fSettingsChanged = true;
94 			break;
95 		}
96 		case MSG_SETTINGS_CHANGED:
97 			fSettingsChanged = true;
98 			break;
99 		default:
100 			BApplication::MessageReceived(message);
101 			break;
102 	}
103 }
104 
105 
106 void
107 App::Pulse()
108 {
109 	_StoreSettingsIfNeeded();
110 }
111 
112 
113 void
114 App::_StoreSettingsIfNeeded()
115 {
116 	if (!fSettingsChanged)
117 		return;
118 
119 	BMessage settings('sett');
120 	for (int32 i = 0; BWindow* window = WindowAt(i); i++) {
121 		if (MainWindow* padWindow = dynamic_cast<MainWindow*>(window)) {
122 			BMessage* windowSettings = padWindow->Settings();
123 			if (windowSettings && padWindow->Lock()) {
124 				padWindow->SaveSettings(windowSettings);
125 				padWindow->Unlock();
126 				settings.AddMessage("window", windowSettings);
127 			}
128 		}
129 	}
130 	save_settings(&settings, "main_settings", "LaunchBox");
131 
132 	fSettingsChanged = false;
133 }
134