xref: /haiku/src/apps/people/PersonWindow.cpp (revision 6df0729855318b8c5b87d10feb32781c5562227e)
1 /*
2  * Copyright 2005-2010, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Robert Polic
7  *		Stephan Aßmus <superstippi@gmx.de>
8  *
9  * Copyright 1999, Be Incorporated.   All Rights Reserved.
10  * This file may be used under the terms of the Be Sample Code License.
11  */
12 
13 #include "PersonWindow.h"
14 
15 #include <stdio.h>
16 #include <string.h>
17 
18 #include <Alert.h>
19 #include <Catalog.h>
20 #include <Clipboard.h>
21 #include <ControlLook.h>
22 #include <FilePanel.h>
23 #include <FindDirectory.h>
24 #include <Font.h>
25 #include <LayoutBuilder.h>
26 #include <Locale.h>
27 #include <MenuBar.h>
28 #include <MenuItem.h>
29 #include <NodeInfo.h>
30 #include <NodeMonitor.h>
31 #include <Path.h>
32 #include <String.h>
33 #include <TextView.h>
34 #include <Volume.h>
35 
36 #include "PeopleApp.h"
37 #include "PersonView.h"
38 
39 
40 #undef B_TRANSLATE_CONTEXT
41 #define B_TRANSLATE_CONTEXT "People"
42 
43 
44 PersonWindow::PersonWindow(BRect frame, const char* title,
45 		const char* nameAttribute, const char* categoryAttribute,
46 		const entry_ref* ref)
47 	:
48 	BWindow(frame, title, B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE
49 		| B_AUTO_UPDATE_SIZE_LIMITS),
50 	fPanel(NULL),
51 	fNameAttribute(nameAttribute)
52 {
53 	BMenu* menu;
54 	BMenuItem* item;
55 
56 	BMenuBar* menuBar = new BMenuBar("");
57 	menu = new BMenu(B_TRANSLATE("File"));
58 	menu->AddItem(item = new BMenuItem(
59 		B_TRANSLATE("New person" B_UTF8_ELLIPSIS),
60 		new BMessage(M_NEW), 'N'));
61 	item->SetTarget(be_app);
62 	menu->AddItem(new BMenuItem(B_TRANSLATE("Close"),
63 		new BMessage(B_QUIT_REQUESTED), 'W'));
64 	menu->AddSeparatorItem();
65 	menu->AddItem(fSave = new BMenuItem(B_TRANSLATE("Save"),
66 		new BMessage(M_SAVE), 'S'));
67 	fSave->SetEnabled(FALSE);
68 	menu->AddItem(new BMenuItem(
69 		B_TRANSLATE("Save as"B_UTF8_ELLIPSIS),
70 		new BMessage(M_SAVE_AS)));
71 	menu->AddItem(fRevert = new BMenuItem(B_TRANSLATE("Revert"),
72 		new BMessage(M_REVERT), 'R'));
73 	fRevert->SetEnabled(FALSE);
74 	menu->AddSeparatorItem();
75 	item = new BMenuItem(B_TRANSLATE("Quit"),
76 		new BMessage(B_QUIT_REQUESTED), 'Q');
77 	item->SetTarget(be_app);
78 	menu->AddItem(item);
79 	menuBar->AddItem(menu);
80 
81 	menu = new BMenu(B_TRANSLATE("Edit"));
82 	menu->AddItem(fUndo = new BMenuItem(B_TRANSLATE("Undo"),
83 		new BMessage(B_UNDO), 'Z'));
84 	fUndo->SetEnabled(false);
85 	menu->AddSeparatorItem();
86 	menu->AddItem(fCut = new BMenuItem(B_TRANSLATE("Cut"),
87 		new BMessage(B_CUT), 'X'));
88 	menu->AddItem(fCopy = new BMenuItem(B_TRANSLATE("Copy"),
89 		new BMessage(B_COPY), 'C'));
90 	menu->AddItem(fPaste = new BMenuItem(B_TRANSLATE("Paste"),
91 		new BMessage(B_PASTE), 'V'));
92 	BMenuItem* selectAllItem = new BMenuItem(B_TRANSLATE("Select all"),
93 		new BMessage(M_SELECT), 'A');
94 	menu->AddItem(selectAllItem);
95 	menu->AddSeparatorItem();
96 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Configure attributes"),
97 		new BMessage(M_CONFIGURE_ATTRIBUTES), 'F'));
98 	item->SetTarget(be_app);
99 	menuBar->AddItem(menu);
100 
101 	if (ref != NULL) {
102 		SetTitle(ref->name);
103 		_SetToRef(new entry_ref(*ref));
104 	} else
105 		_SetToRef(NULL);
106 
107 	fView = new PersonView("PeopleView", categoryAttribute, fRef);
108 
109 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
110 		.SetInsets(0, 0, 0, 0)
111 		.Add(menuBar)
112 		.Add(fView);
113 
114 	fRevert->SetTarget(fView);
115 	selectAllItem->SetTarget(fView);
116 }
117 
118 
119 PersonWindow::~PersonWindow()
120 {
121 	_SetToRef(NULL);
122 }
123 
124 
125 void
126 PersonWindow::MenusBeginning()
127 {
128 	bool enabled = !fView->IsSaved();
129 	fSave->SetEnabled(enabled);
130 	fRevert->SetEnabled(enabled);
131 
132 	bool isRedo = false;
133 	undo_state state = ((BTextView*)CurrentFocus())->UndoState(&isRedo);
134 	fUndo->SetEnabled(state != B_UNDO_UNAVAILABLE);
135 
136 	if (isRedo)
137 		fUndo->SetLabel(B_TRANSLATE("Redo"));
138 	else
139 		fUndo->SetLabel(B_TRANSLATE("Undo"));
140 
141 	enabled = fView->IsTextSelected();
142 	fCut->SetEnabled(enabled);
143 	fCopy->SetEnabled(enabled);
144 
145 	be_clipboard->Lock();
146 	fPaste->SetEnabled(be_clipboard->Data()->HasData("text/plain", B_MIME_TYPE));
147 	be_clipboard->Unlock();
148 
149 	fView->BuildGroupMenu();
150 }
151 
152 
153 void
154 PersonWindow::MessageReceived(BMessage* msg)
155 {
156 	char			str[256];
157 	BDirectory		directory;
158 	BEntry			entry;
159 	BFile			file;
160 	BNodeInfo		*node;
161 
162 	switch (msg->what) {
163 		case M_SAVE:
164 			if (!fRef) {
165 				SaveAs();
166 				break;
167 			}
168 			// supposed to fall through
169 		case M_REVERT:
170 		case M_SELECT:
171 			fView->MessageReceived(msg);
172 			break;
173 
174 		case M_SAVE_AS:
175 			SaveAs();
176 			break;
177 
178 		case B_SAVE_REQUESTED:
179 		{
180 			entry_ref dir;
181 			if (msg->FindRef("directory", &dir) == B_OK) {
182 				const char* name = NULL;
183 				msg->FindString("name", &name);
184 				directory.SetTo(&dir);
185 				if (directory.InitCheck() == B_NO_ERROR) {
186 					directory.CreateFile(name, &file);
187 					if (file.InitCheck() == B_NO_ERROR) {
188 						node = new BNodeInfo(&file);
189 						node->SetType("application/x-person");
190 						delete node;
191 
192 						directory.FindEntry(name, &entry);
193 						entry.GetRef(&dir);
194 						_SetToRef(new entry_ref(dir));
195 						SetTitle(fRef->name);
196 						fView->CreateFile(fRef);
197 					}
198 					else {
199 						sprintf(str, B_TRANSLATE("Could not create %s."), name);
200 						(new BAlert("", str, B_TRANSLATE("Sorry")))->Go();
201 					}
202 				}
203 			}
204 			break;
205 		}
206 
207 		case B_NODE_MONITOR:
208 		{
209 			int32 opcode;
210 			if (msg->FindInt32("opcode", &opcode) == B_OK) {
211 				switch (opcode) {
212 					case B_ENTRY_REMOVED:
213 						// We lost our file. Close the window.
214 						PostMessage(B_QUIT_REQUESTED);
215 						break;
216 
217 					case B_ENTRY_MOVED:
218 					{
219 						// We may have renamed our entry. Obtain relevant data
220 						// from message.
221 						BString name;
222 						msg->FindString("name", &name);
223 
224 						int64 directory;
225 						msg->FindInt64("to directory", &directory);
226 
227 						int32 device;
228 						msg->FindInt32("device", &device);
229 
230 						// Update our ref.
231 						delete fRef;
232 						fRef = new entry_ref(device, directory, name.String());
233 
234 						// And our window title.
235 						SetTitle(name);
236 
237 						// If moved to Trash, close window.
238 						BVolume volume(device);
239 						BPath trash;
240 						find_directory(B_TRASH_DIRECTORY, &trash, false,
241 							&volume);
242 						BPath folder(fRef);
243 						folder.GetParent(&folder);
244 						if (folder == trash)
245 							PostMessage(B_QUIT_REQUESTED);
246 
247 						break;
248 					}
249 
250 					case B_ATTR_CHANGED:
251 					{
252 						// An attribute was updated.
253 						BString attr;
254 						if (msg->FindString("attr", &attr) == B_OK)
255 							fView->SetAttribute(attr.String(), true);
256 						break;
257 					}
258 				}
259 			}
260 			break;
261 		}
262 
263 		default:
264 			BWindow::MessageReceived(msg);
265 	}
266 }
267 
268 
269 bool
270 PersonWindow::QuitRequested()
271 {
272 	status_t result;
273 
274 	if (!fView->IsSaved()) {
275 		result = (new BAlert("", B_TRANSLATE("Save changes before quitting?"),
276 							B_TRANSLATE("Cancel"), B_TRANSLATE("Quit"),
277 							B_TRANSLATE("Save")))->Go();
278 		if (result == 2) {
279 			if (fRef)
280 				fView->Save();
281 			else {
282 				SaveAs();
283 				return false;
284 			}
285 		} else if (result == 0)
286 			return false;
287 	}
288 
289 	delete fPanel;
290 
291 	BMessage message(M_WINDOW_QUITS);
292 	message.AddRect("frame", Frame());
293 	if (be_app->Lock()) {
294 		be_app->PostMessage(&message);
295 		be_app->Unlock();
296 	}
297 
298 	return true;
299 }
300 
301 
302 void
303 PersonWindow::Show()
304 {
305 	fView->MakeFocus();
306 	BWindow::Show();
307 }
308 
309 
310 void
311 PersonWindow::AddAttribute(const char* label, const char* attribute)
312 {
313 	fView->AddAttribute(label, attribute);
314 }
315 
316 
317 void
318 PersonWindow::SaveAs()
319 {
320 	char name[B_FILE_NAME_LENGTH];
321 	_GetDefaultFileName(name);
322 
323 	if (fPanel == NULL) {
324 		BMessenger target(this);
325 		fPanel = new BFilePanel(B_SAVE_PANEL, &target);
326 
327 		BPath path;
328 		find_directory(B_USER_DIRECTORY, &path, true);
329 
330 		BDirectory dir;
331 		dir.SetTo(path.Path());
332 
333 		BEntry entry;
334 		if (dir.FindEntry("people", &entry) == B_OK
335 			|| (dir.CreateDirectory("people", &dir) == B_OK
336 					&& dir.GetEntry(&entry) == B_OK)) {
337 			fPanel->SetPanelDirectory(&entry);
338 		}
339 	}
340 
341 	if (fPanel->Window()->Lock()) {
342 		fPanel->SetSaveText(name);
343 		if (fPanel->Window()->IsHidden())
344 			fPanel->Window()->Show();
345 		else
346 			fPanel->Window()->Activate();
347 		fPanel->Window()->Unlock();
348 	}
349 }
350 
351 
352 bool
353 PersonWindow::RefersPersonFile(const entry_ref& ref) const
354 {
355 	if (fRef == NULL)
356 		return false;
357 	return *fRef == ref;
358 }
359 
360 
361 void
362 PersonWindow::_GetDefaultFileName(char* name)
363 {
364 	strncpy(name, fView->AttributeValue(fNameAttribute), B_FILE_NAME_LENGTH);
365 	while (*name) {
366 		if (*name == '/')
367 			*name = '-';
368 		name++;
369 	}
370 }
371 
372 
373 void
374 PersonWindow::_SetToRef(entry_ref* ref)
375 {
376 	if (fRef != NULL) {
377 		_WatchChanges(false);
378 		delete fRef;
379 	}
380 
381 	fRef = ref;
382 
383 	_WatchChanges(true);
384 }
385 
386 
387 void
388 PersonWindow::_WatchChanges(bool enable)
389 {
390 	if (fRef == NULL)
391 		return;
392 
393 	node_ref nodeRef;
394 
395 	BNode node(fRef);
396 	node.GetNodeRef(&nodeRef);
397 
398 	uint32 flags;
399 	BString action;
400 
401 	if (enable) {
402 		// Start watching.
403 		flags = B_WATCH_ALL;
404 		action = "starting";
405 	} else {
406 		// Stop watching.
407 		flags = B_STOP_WATCHING;
408 		action = "stoping";
409 	}
410 
411 	if (watch_node(&nodeRef, flags, this) != B_OK) {
412 		printf("Error %s node monitor.\n", action.String());
413 	}
414 }
415