xref: /haiku/src/apps/people/PersonWindow.cpp (revision dcd112f767b89f0cb5a1e93e0515361c1742c635)
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_TRANSLATION_CONTEXT
41 #define B_TRANSLATION_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 	fRef(NULL),
51 	fPanel(NULL),
52 	fNameAttribute(nameAttribute)
53 {
54 	BMenu* menu;
55 	BMenuItem* item;
56 
57 	BMenuBar* menuBar = new BMenuBar("");
58 	menu = new BMenu(B_TRANSLATE("File"));
59 	menu->AddItem(item = new BMenuItem(
60 		B_TRANSLATE("New person" B_UTF8_ELLIPSIS),
61 		new BMessage(M_NEW), 'N'));
62 	item->SetTarget(be_app);
63 	menu->AddItem(new BMenuItem(B_TRANSLATE("Close"),
64 		new BMessage(B_QUIT_REQUESTED), 'W'));
65 	menu->AddSeparatorItem();
66 	menu->AddItem(fSave = new BMenuItem(B_TRANSLATE("Save"),
67 		new BMessage(M_SAVE), 'S'));
68 	fSave->SetEnabled(FALSE);
69 	menu->AddItem(new BMenuItem(
70 		B_TRANSLATE("Save as"B_UTF8_ELLIPSIS),
71 		new BMessage(M_SAVE_AS)));
72 	menu->AddItem(fRevert = new BMenuItem(B_TRANSLATE("Revert"),
73 		new BMessage(M_REVERT), 'R'));
74 	fRevert->SetEnabled(FALSE);
75 	menu->AddSeparatorItem();
76 	item = new BMenuItem(B_TRANSLATE("Quit"),
77 		new BMessage(B_QUIT_REQUESTED), 'Q');
78 	item->SetTarget(be_app);
79 	menu->AddItem(item);
80 	menuBar->AddItem(menu);
81 
82 	menu = new BMenu(B_TRANSLATE("Edit"));
83 	menu->AddItem(fUndo = new BMenuItem(B_TRANSLATE("Undo"),
84 		new BMessage(B_UNDO), 'Z'));
85 	fUndo->SetEnabled(false);
86 	menu->AddSeparatorItem();
87 	menu->AddItem(fCut = new BMenuItem(B_TRANSLATE("Cut"),
88 		new BMessage(B_CUT), 'X'));
89 	menu->AddItem(fCopy = new BMenuItem(B_TRANSLATE("Copy"),
90 		new BMessage(B_COPY), 'C'));
91 	menu->AddItem(fPaste = new BMenuItem(B_TRANSLATE("Paste"),
92 		new BMessage(B_PASTE), 'V'));
93 	BMenuItem* selectAllItem = new BMenuItem(B_TRANSLATE("Select all"),
94 		new BMessage(M_SELECT), 'A');
95 	menu->AddItem(selectAllItem);
96 	menu->AddSeparatorItem();
97 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Configure attributes"),
98 		new BMessage(M_CONFIGURE_ATTRIBUTES), 'F'));
99 	item->SetTarget(be_app);
100 	menuBar->AddItem(menu);
101 
102 	if (ref != NULL) {
103 		SetTitle(ref->name);
104 		_SetToRef(new entry_ref(*ref));
105 	} else
106 		_SetToRef(NULL);
107 
108 	fView = new PersonView("PeopleView", categoryAttribute, fRef);
109 
110 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
111 		.SetInsets(0, 0, 0, 0)
112 		.Add(menuBar)
113 		.Add(fView);
114 
115 	fRevert->SetTarget(fView);
116 	selectAllItem->SetTarget(fView);
117 }
118 
119 
120 PersonWindow::~PersonWindow()
121 {
122 	_SetToRef(NULL);
123 }
124 
125 
126 void
127 PersonWindow::MenusBeginning()
128 {
129 	bool enabled = !fView->IsSaved();
130 	fSave->SetEnabled(enabled);
131 	fRevert->SetEnabled(enabled);
132 
133 	bool isRedo = false;
134 	bool undoEnabled = false;
135 	bool cutAndCopyEnabled = false;
136 
137 	BTextView* textView = dynamic_cast<BTextView*>(CurrentFocus());
138 	if (textView != NULL) {
139 		undo_state state = textView->UndoState(&isRedo);
140 		undoEnabled = state != B_UNDO_UNAVAILABLE;
141 
142 		cutAndCopyEnabled = fView->IsTextSelected();
143 	}
144 
145 	if (isRedo)
146 		fUndo->SetLabel(B_TRANSLATE("Redo"));
147 	else
148 		fUndo->SetLabel(B_TRANSLATE("Undo"));
149 	fUndo->SetEnabled(undoEnabled);
150 	fCut->SetEnabled(cutAndCopyEnabled);
151 	fCopy->SetEnabled(cutAndCopyEnabled);
152 
153 	be_clipboard->Lock();
154 	fPaste->SetEnabled(be_clipboard->Data()->HasData("text/plain", B_MIME_TYPE));
155 	be_clipboard->Unlock();
156 
157 	fView->BuildGroupMenu();
158 }
159 
160 
161 void
162 PersonWindow::MessageReceived(BMessage* msg)
163 {
164 	char			str[256];
165 	BDirectory		directory;
166 	BEntry			entry;
167 	BFile			file;
168 	BNodeInfo		*node;
169 
170 	switch (msg->what) {
171 		case M_SAVE:
172 			if (!fRef) {
173 				SaveAs();
174 				break;
175 			}
176 			// supposed to fall through
177 		case M_REVERT:
178 		case M_SELECT:
179 			fView->MessageReceived(msg);
180 			break;
181 
182 		case M_SAVE_AS:
183 			SaveAs();
184 			break;
185 
186 		case B_UNDO: // fall through
187 		case B_CUT:
188 		case B_COPY:
189 		case B_PASTE:
190 		{
191 			BView* view = CurrentFocus();
192 			if (view != NULL)
193 				view->MessageReceived(msg);
194 			break;
195 		}
196 
197 		case B_SAVE_REQUESTED:
198 		{
199 			entry_ref dir;
200 			if (msg->FindRef("directory", &dir) == B_OK) {
201 				const char* name = NULL;
202 				msg->FindString("name", &name);
203 				directory.SetTo(&dir);
204 				if (directory.InitCheck() == B_NO_ERROR) {
205 					directory.CreateFile(name, &file);
206 					if (file.InitCheck() == B_NO_ERROR) {
207 						node = new BNodeInfo(&file);
208 						node->SetType("application/x-person");
209 						delete node;
210 
211 						directory.FindEntry(name, &entry);
212 						entry.GetRef(&dir);
213 						_SetToRef(new entry_ref(dir));
214 						SetTitle(fRef->name);
215 						fView->CreateFile(fRef);
216 					}
217 					else {
218 						sprintf(str, B_TRANSLATE("Could not create %s."), name);
219 						BAlert* alert = new BAlert("", str, B_TRANSLATE("Sorry"));
220 						alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
221 						alert->Go();
222 					}
223 				}
224 			}
225 			break;
226 		}
227 
228 		case B_NODE_MONITOR:
229 		{
230 			int32 opcode;
231 			if (msg->FindInt32("opcode", &opcode) == B_OK) {
232 				switch (opcode) {
233 					case B_ENTRY_REMOVED:
234 						// We lost our file. Close the window.
235 						PostMessage(B_QUIT_REQUESTED);
236 						break;
237 
238 					case B_ENTRY_MOVED:
239 					{
240 						// We may have renamed our entry. Obtain relevant data
241 						// from message.
242 						BString name;
243 						msg->FindString("name", &name);
244 
245 						int64 directory;
246 						msg->FindInt64("to directory", &directory);
247 
248 						int32 device;
249 						msg->FindInt32("device", &device);
250 
251 						// Update our ref.
252 						delete fRef;
253 						fRef = new entry_ref(device, directory, name.String());
254 
255 						// And our window title.
256 						SetTitle(name);
257 
258 						// If moved to Trash, close window.
259 						BVolume volume(device);
260 						BPath trash;
261 						find_directory(B_TRASH_DIRECTORY, &trash, false,
262 							&volume);
263 						BPath folder(fRef);
264 						folder.GetParent(&folder);
265 						if (folder == trash)
266 							PostMessage(B_QUIT_REQUESTED);
267 
268 						break;
269 					}
270 
271 					case B_ATTR_CHANGED:
272 					{
273 						// An attribute was updated.
274 						BString attr;
275 						if (msg->FindString("attr", &attr) == B_OK)
276 							fView->SetAttribute(attr.String(), true);
277 						break;
278 					}
279 					case B_STAT_CHANGED:
280 						fView->UpdatePicture(fRef);
281 						break;
282 				}
283 			}
284 			break;
285 		}
286 
287 		default:
288 			BWindow::MessageReceived(msg);
289 	}
290 }
291 
292 
293 bool
294 PersonWindow::QuitRequested()
295 {
296 	status_t result;
297 
298 	if (!fView->IsSaved()) {
299 		BAlert* alert = new BAlert("",
300 			B_TRANSLATE("Save changes before closing?"), B_TRANSLATE("Cancel"),
301 			B_TRANSLATE("Don't save"), B_TRANSLATE("Save"),
302 			B_WIDTH_AS_USUAL, B_OFFSET_SPACING, B_WARNING_ALERT);
303 		alert->SetShortcut(0, B_ESCAPE);
304 		alert->SetShortcut(1, 'd');
305 		alert->SetShortcut(2, 's');
306 		result = alert->Go();
307 
308 		if (result == 2) {
309 			if (fRef)
310 				fView->Save();
311 			else {
312 				SaveAs();
313 				return false;
314 			}
315 		} else if (result == 0)
316 			return false;
317 	}
318 
319 	delete fPanel;
320 
321 	BMessage message(M_WINDOW_QUITS);
322 	message.AddRect("frame", Frame());
323 	if (be_app->Lock()) {
324 		be_app->PostMessage(&message);
325 		be_app->Unlock();
326 	}
327 
328 	return true;
329 }
330 
331 
332 void
333 PersonWindow::Show()
334 {
335 	fView->MakeFocus();
336 	BWindow::Show();
337 }
338 
339 
340 void
341 PersonWindow::AddAttribute(const char* label, const char* attribute)
342 {
343 	fView->AddAttribute(label, attribute);
344 }
345 
346 
347 void
348 PersonWindow::SaveAs()
349 {
350 	char name[B_FILE_NAME_LENGTH];
351 	_GetDefaultFileName(name);
352 
353 	if (fPanel == NULL) {
354 		BMessenger target(this);
355 		fPanel = new BFilePanel(B_SAVE_PANEL, &target);
356 
357 		BPath path;
358 		find_directory(B_USER_DIRECTORY, &path, true);
359 
360 		BDirectory dir;
361 		dir.SetTo(path.Path());
362 
363 		BEntry entry;
364 		if (dir.FindEntry("people", &entry) == B_OK
365 			|| (dir.CreateDirectory("people", &dir) == B_OK
366 					&& dir.GetEntry(&entry) == B_OK)) {
367 			fPanel->SetPanelDirectory(&entry);
368 		}
369 	}
370 
371 	if (fPanel->Window()->Lock()) {
372 		fPanel->SetSaveText(name);
373 		if (fPanel->Window()->IsHidden())
374 			fPanel->Window()->Show();
375 		else
376 			fPanel->Window()->Activate();
377 		fPanel->Window()->Unlock();
378 	}
379 }
380 
381 
382 bool
383 PersonWindow::RefersPersonFile(const entry_ref& ref) const
384 {
385 	if (fRef == NULL)
386 		return false;
387 	return *fRef == ref;
388 }
389 
390 
391 void
392 PersonWindow::_GetDefaultFileName(char* name)
393 {
394 	strncpy(name, fView->AttributeValue(fNameAttribute), B_FILE_NAME_LENGTH);
395 	while (*name) {
396 		if (*name == '/')
397 			*name = '-';
398 		name++;
399 	}
400 }
401 
402 
403 void
404 PersonWindow::_SetToRef(entry_ref* ref)
405 {
406 	if (fRef != NULL) {
407 		_WatchChanges(false);
408 		delete fRef;
409 	}
410 
411 	fRef = ref;
412 
413 	_WatchChanges(true);
414 }
415 
416 
417 void
418 PersonWindow::_WatchChanges(bool enable)
419 {
420 	if (fRef == NULL)
421 		return;
422 
423 	node_ref nodeRef;
424 
425 	BNode node(fRef);
426 	node.GetNodeRef(&nodeRef);
427 
428 	uint32 flags;
429 	BString action;
430 
431 	if (enable) {
432 		// Start watching.
433 		flags = B_WATCH_ALL;
434 		action = "starting";
435 	} else {
436 		// Stop watching.
437 		flags = B_STOP_WATCHING;
438 		action = "stoping";
439 	}
440 
441 	if (watch_node(&nodeRef, flags, this) != B_OK) {
442 		printf("Error %s node monitor.\n", action.String());
443 	}
444 }
445