xref: /haiku/src/apps/people/PersonWindow.cpp (revision fce97ba360ff70fb19f9cd2a57a16152f0925c06)
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 						(new BAlert("", str, B_TRANSLATE("Sorry")))->Go();
220 					}
221 				}
222 			}
223 			break;
224 		}
225 
226 		case B_NODE_MONITOR:
227 		{
228 			int32 opcode;
229 			if (msg->FindInt32("opcode", &opcode) == B_OK) {
230 				switch (opcode) {
231 					case B_ENTRY_REMOVED:
232 						// We lost our file. Close the window.
233 						PostMessage(B_QUIT_REQUESTED);
234 						break;
235 
236 					case B_ENTRY_MOVED:
237 					{
238 						// We may have renamed our entry. Obtain relevant data
239 						// from message.
240 						BString name;
241 						msg->FindString("name", &name);
242 
243 						int64 directory;
244 						msg->FindInt64("to directory", &directory);
245 
246 						int32 device;
247 						msg->FindInt32("device", &device);
248 
249 						// Update our ref.
250 						delete fRef;
251 						fRef = new entry_ref(device, directory, name.String());
252 
253 						// And our window title.
254 						SetTitle(name);
255 
256 						// If moved to Trash, close window.
257 						BVolume volume(device);
258 						BPath trash;
259 						find_directory(B_TRASH_DIRECTORY, &trash, false,
260 							&volume);
261 						BPath folder(fRef);
262 						folder.GetParent(&folder);
263 						if (folder == trash)
264 							PostMessage(B_QUIT_REQUESTED);
265 
266 						break;
267 					}
268 
269 					case B_ATTR_CHANGED:
270 					{
271 						// An attribute was updated.
272 						BString attr;
273 						if (msg->FindString("attr", &attr) == B_OK)
274 							fView->SetAttribute(attr.String(), true);
275 						break;
276 					}
277 					case B_STAT_CHANGED:
278 						fView->UpdatePicture(fRef);
279 						break;
280 				}
281 			}
282 			break;
283 		}
284 
285 		default:
286 			BWindow::MessageReceived(msg);
287 	}
288 }
289 
290 
291 bool
292 PersonWindow::QuitRequested()
293 {
294 	status_t result;
295 
296 	if (!fView->IsSaved()) {
297 		result = (new BAlert("", B_TRANSLATE("Save changes before quitting?"),
298 							B_TRANSLATE("Cancel"), B_TRANSLATE("Quit"),
299 							B_TRANSLATE("Save")))->Go();
300 		if (result == 2) {
301 			if (fRef)
302 				fView->Save();
303 			else {
304 				SaveAs();
305 				return false;
306 			}
307 		} else if (result == 0)
308 			return false;
309 	}
310 
311 	delete fPanel;
312 
313 	BMessage message(M_WINDOW_QUITS);
314 	message.AddRect("frame", Frame());
315 	if (be_app->Lock()) {
316 		be_app->PostMessage(&message);
317 		be_app->Unlock();
318 	}
319 
320 	return true;
321 }
322 
323 
324 void
325 PersonWindow::Show()
326 {
327 	fView->MakeFocus();
328 	BWindow::Show();
329 }
330 
331 
332 void
333 PersonWindow::AddAttribute(const char* label, const char* attribute)
334 {
335 	fView->AddAttribute(label, attribute);
336 }
337 
338 
339 void
340 PersonWindow::SaveAs()
341 {
342 	char name[B_FILE_NAME_LENGTH];
343 	_GetDefaultFileName(name);
344 
345 	if (fPanel == NULL) {
346 		BMessenger target(this);
347 		fPanel = new BFilePanel(B_SAVE_PANEL, &target);
348 
349 		BPath path;
350 		find_directory(B_USER_DIRECTORY, &path, true);
351 
352 		BDirectory dir;
353 		dir.SetTo(path.Path());
354 
355 		BEntry entry;
356 		if (dir.FindEntry("people", &entry) == B_OK
357 			|| (dir.CreateDirectory("people", &dir) == B_OK
358 					&& dir.GetEntry(&entry) == B_OK)) {
359 			fPanel->SetPanelDirectory(&entry);
360 		}
361 	}
362 
363 	if (fPanel->Window()->Lock()) {
364 		fPanel->SetSaveText(name);
365 		if (fPanel->Window()->IsHidden())
366 			fPanel->Window()->Show();
367 		else
368 			fPanel->Window()->Activate();
369 		fPanel->Window()->Unlock();
370 	}
371 }
372 
373 
374 bool
375 PersonWindow::RefersPersonFile(const entry_ref& ref) const
376 {
377 	if (fRef == NULL)
378 		return false;
379 	return *fRef == ref;
380 }
381 
382 
383 void
384 PersonWindow::_GetDefaultFileName(char* name)
385 {
386 	strncpy(name, fView->AttributeValue(fNameAttribute), B_FILE_NAME_LENGTH);
387 	while (*name) {
388 		if (*name == '/')
389 			*name = '-';
390 		name++;
391 	}
392 }
393 
394 
395 void
396 PersonWindow::_SetToRef(entry_ref* ref)
397 {
398 	if (fRef != NULL) {
399 		_WatchChanges(false);
400 		delete fRef;
401 	}
402 
403 	fRef = ref;
404 
405 	_WatchChanges(true);
406 }
407 
408 
409 void
410 PersonWindow::_WatchChanges(bool enable)
411 {
412 	if (fRef == NULL)
413 		return;
414 
415 	node_ref nodeRef;
416 
417 	BNode node(fRef);
418 	node.GetNodeRef(&nodeRef);
419 
420 	uint32 flags;
421 	BString action;
422 
423 	if (enable) {
424 		// Start watching.
425 		flags = B_WATCH_ALL;
426 		action = "starting";
427 	} else {
428 		// Stop watching.
429 		flags = B_STOP_WATCHING;
430 		action = "stoping";
431 	}
432 
433 	if (watch_node(&nodeRef, flags, this) != B_OK) {
434 		printf("Error %s node monitor.\n", action.String());
435 	}
436 }
437