xref: /haiku/src/apps/launchbox/App.cpp (revision cb9c3e9cede30851ba29be2e955fb8cfcecef7ac)
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 <AboutWindow.h>
11 #include <Catalog.h>
12 #include <Entry.h>
13 #include <Message.h>
14 #include <String.h>
15 
16 #include "support.h"
17 
18 #include "MainWindow.h"
19 
20 #undef B_TRANSLATE_CONTEXT
21 #define B_TRANSLATE_CONTEXT "LaunchBox"
22 
23 App::App()
24 	:
25 	BApplication("application/x-vnd.Haiku-LaunchBox"),
26 	fSettingsChanged(false)
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('sett');
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 	}
70 
71 	if (!windowAdded) {
72 		MainWindow* window = new MainWindow(B_TRANSLATE("Pad 1"), frame, true);
73 		window->Show();
74 	}
75 }
76 
77 
78 void
79 App::MessageReceived(BMessage* message)
80 {
81 	switch (message->what) {
82 		case MSG_ADD_WINDOW: {
83 			BMessage* settings = new BMessage('sett');
84 			bool wasCloned = message->FindMessage("window", settings) == B_OK;
85 			BString string;
86 			string << CountWindows() + 1;
87 			BString name(B_TRANSLATE("Pad %1"));
88 			name.ReplaceFirst("%1", string);
89 			MainWindow* window = new MainWindow(name.String(),
90 				BRect(50.0, 50.0, 65.0, 100.0), settings);
91 			if (wasCloned)
92 				window->MoveBy(10, 10);
93 			window->Show();
94 			fSettingsChanged = true;
95 			break;
96 		}
97 		case MSG_SETTINGS_CHANGED:
98 			fSettingsChanged = true;
99 			break;
100 		default:
101 			BApplication::MessageReceived(message);
102 			break;
103 	}
104 }
105 
106 
107 void
108 App::AboutRequested()
109 {
110 	const char* authors[2];
111 	authors[0] = B_TRANSLATE("Stephan Aßmus (aka stippi)");
112 	authors[1] = NULL;
113 	BString appName = B_TRANSLATE("LaunchBox");
114 	BAboutWindow(appName, 2004, authors).Show();
115 }
116 
117 
118 void
119 App::Pulse()
120 {
121 	_StoreSettingsIfNeeded();
122 }
123 
124 
125 void
126 App::_StoreSettingsIfNeeded()
127 {
128 	if (!fSettingsChanged)
129 		return;
130 
131 	BMessage settings('sett');
132 	for (int32 i = 0; BWindow* window = WindowAt(i); i++) {
133 		if (MainWindow* padWindow = dynamic_cast<MainWindow*>(window)) {
134 			BMessage* windowSettings = padWindow->Settings();
135 			if (windowSettings && padWindow->Lock()) {
136 				padWindow->SaveSettings(windowSettings);
137 				padWindow->Unlock();
138 				settings.AddMessage("window", windowSettings);
139 			}
140 		}
141 	}
142 	save_settings(&settings, "main_settings", "LaunchBox");
143 
144 	fSettingsChanged = false;
145 }
146