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("", B_TRANSLATE("Save changes before quitting?"), 300 B_TRANSLATE("Cancel"), B_TRANSLATE("Quit"), 301 B_TRANSLATE("Save")); 302 alert->SetShortcut(0, B_ESCAPE); 303 result = alert->Go(); 304 305 if (result == 2) { 306 if (fRef) 307 fView->Save(); 308 else { 309 SaveAs(); 310 return false; 311 } 312 } else if (result == 0) 313 return false; 314 } 315 316 delete fPanel; 317 318 BMessage message(M_WINDOW_QUITS); 319 message.AddRect("frame", Frame()); 320 if (be_app->Lock()) { 321 be_app->PostMessage(&message); 322 be_app->Unlock(); 323 } 324 325 return true; 326 } 327 328 329 void 330 PersonWindow::Show() 331 { 332 fView->MakeFocus(); 333 BWindow::Show(); 334 } 335 336 337 void 338 PersonWindow::AddAttribute(const char* label, const char* attribute) 339 { 340 fView->AddAttribute(label, attribute); 341 } 342 343 344 void 345 PersonWindow::SaveAs() 346 { 347 char name[B_FILE_NAME_LENGTH]; 348 _GetDefaultFileName(name); 349 350 if (fPanel == NULL) { 351 BMessenger target(this); 352 fPanel = new BFilePanel(B_SAVE_PANEL, &target); 353 354 BPath path; 355 find_directory(B_USER_DIRECTORY, &path, true); 356 357 BDirectory dir; 358 dir.SetTo(path.Path()); 359 360 BEntry entry; 361 if (dir.FindEntry("people", &entry) == B_OK 362 || (dir.CreateDirectory("people", &dir) == B_OK 363 && dir.GetEntry(&entry) == B_OK)) { 364 fPanel->SetPanelDirectory(&entry); 365 } 366 } 367 368 if (fPanel->Window()->Lock()) { 369 fPanel->SetSaveText(name); 370 if (fPanel->Window()->IsHidden()) 371 fPanel->Window()->Show(); 372 else 373 fPanel->Window()->Activate(); 374 fPanel->Window()->Unlock(); 375 } 376 } 377 378 379 bool 380 PersonWindow::RefersPersonFile(const entry_ref& ref) const 381 { 382 if (fRef == NULL) 383 return false; 384 return *fRef == ref; 385 } 386 387 388 void 389 PersonWindow::_GetDefaultFileName(char* name) 390 { 391 strncpy(name, fView->AttributeValue(fNameAttribute), B_FILE_NAME_LENGTH); 392 while (*name) { 393 if (*name == '/') 394 *name = '-'; 395 name++; 396 } 397 } 398 399 400 void 401 PersonWindow::_SetToRef(entry_ref* ref) 402 { 403 if (fRef != NULL) { 404 _WatchChanges(false); 405 delete fRef; 406 } 407 408 fRef = ref; 409 410 _WatchChanges(true); 411 } 412 413 414 void 415 PersonWindow::_WatchChanges(bool enable) 416 { 417 if (fRef == NULL) 418 return; 419 420 node_ref nodeRef; 421 422 BNode node(fRef); 423 node.GetNodeRef(&nodeRef); 424 425 uint32 flags; 426 BString action; 427 428 if (enable) { 429 // Start watching. 430 flags = B_WATCH_ALL; 431 action = "starting"; 432 } else { 433 // Stop watching. 434 flags = B_STOP_WATCHING; 435 action = "stoping"; 436 } 437 438 if (watch_node(&nodeRef, flags, this) != B_OK) { 439 printf("Error %s node monitor.\n", action.String()); 440 } 441 } 442