xref: /haiku/src/apps/people/PersonWindow.cpp (revision 84627497fb50f0217c24d3c8af4b68f5566b4e6d)
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 					case B_STAT_CHANGED:
259 						fView->UpdatePicture(fRef);
260 						break;
261 				}
262 			}
263 			break;
264 		}
265 
266 		default:
267 			BWindow::MessageReceived(msg);
268 	}
269 }
270 
271 
272 bool
273 PersonWindow::QuitRequested()
274 {
275 	status_t result;
276 
277 	if (!fView->IsSaved()) {
278 		result = (new BAlert("", B_TRANSLATE("Save changes before quitting?"),
279 							B_TRANSLATE("Cancel"), B_TRANSLATE("Quit"),
280 							B_TRANSLATE("Save")))->Go();
281 		if (result == 2) {
282 			if (fRef)
283 				fView->Save();
284 			else {
285 				SaveAs();
286 				return false;
287 			}
288 		} else if (result == 0)
289 			return false;
290 	}
291 
292 	delete fPanel;
293 
294 	BMessage message(M_WINDOW_QUITS);
295 	message.AddRect("frame", Frame());
296 	if (be_app->Lock()) {
297 		be_app->PostMessage(&message);
298 		be_app->Unlock();
299 	}
300 
301 	return true;
302 }
303 
304 
305 void
306 PersonWindow::Show()
307 {
308 	fView->MakeFocus();
309 	BWindow::Show();
310 }
311 
312 
313 void
314 PersonWindow::AddAttribute(const char* label, const char* attribute)
315 {
316 	fView->AddAttribute(label, attribute);
317 }
318 
319 
320 void
321 PersonWindow::SaveAs()
322 {
323 	char name[B_FILE_NAME_LENGTH];
324 	_GetDefaultFileName(name);
325 
326 	if (fPanel == NULL) {
327 		BMessenger target(this);
328 		fPanel = new BFilePanel(B_SAVE_PANEL, &target);
329 
330 		BPath path;
331 		find_directory(B_USER_DIRECTORY, &path, true);
332 
333 		BDirectory dir;
334 		dir.SetTo(path.Path());
335 
336 		BEntry entry;
337 		if (dir.FindEntry("people", &entry) == B_OK
338 			|| (dir.CreateDirectory("people", &dir) == B_OK
339 					&& dir.GetEntry(&entry) == B_OK)) {
340 			fPanel->SetPanelDirectory(&entry);
341 		}
342 	}
343 
344 	if (fPanel->Window()->Lock()) {
345 		fPanel->SetSaveText(name);
346 		if (fPanel->Window()->IsHidden())
347 			fPanel->Window()->Show();
348 		else
349 			fPanel->Window()->Activate();
350 		fPanel->Window()->Unlock();
351 	}
352 }
353 
354 
355 bool
356 PersonWindow::RefersPersonFile(const entry_ref& ref) const
357 {
358 	if (fRef == NULL)
359 		return false;
360 	return *fRef == ref;
361 }
362 
363 
364 void
365 PersonWindow::_GetDefaultFileName(char* name)
366 {
367 	strncpy(name, fView->AttributeValue(fNameAttribute), B_FILE_NAME_LENGTH);
368 	while (*name) {
369 		if (*name == '/')
370 			*name = '-';
371 		name++;
372 	}
373 }
374 
375 
376 void
377 PersonWindow::_SetToRef(entry_ref* ref)
378 {
379 	if (fRef != NULL) {
380 		_WatchChanges(false);
381 		delete fRef;
382 	}
383 
384 	fRef = ref;
385 
386 	_WatchChanges(true);
387 }
388 
389 
390 void
391 PersonWindow::_WatchChanges(bool enable)
392 {
393 	if (fRef == NULL)
394 		return;
395 
396 	node_ref nodeRef;
397 
398 	BNode node(fRef);
399 	node.GetNodeRef(&nodeRef);
400 
401 	uint32 flags;
402 	BString action;
403 
404 	if (enable) {
405 		// Start watching.
406 		flags = B_WATCH_ALL;
407 		action = "starting";
408 	} else {
409 		// Stop watching.
410 		flags = B_STOP_WATCHING;
411 		action = "stoping";
412 	}
413 
414 	if (watch_node(&nodeRef, flags, this) != B_OK) {
415 		printf("Error %s node monitor.\n", action.String());
416 	}
417 }
418