1 //-------------------------------------------------------------------- 2 // 3 // PeopleApp.cpp 4 // 5 // Written by: Robert Polic 6 // 7 //-------------------------------------------------------------------- 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 <Bitmap.h> 14 #include <Directory.h> 15 #include <Volume.h> 16 #include <VolumeRoster.h> 17 #include <Path.h> 18 #include <FindDirectory.h> 19 #include <Screen.h> 20 #include <fs_index.h> 21 #include <string.h> 22 #include <Alert.h> 23 24 25 #include "PeopleApp.h" 26 #include "PeopleWindow.h" 27 #include "PersonIcons.h" 28 29 //==================================================================== 30 31 TPeopleApp::TPeopleApp(void) 32 :BApplication(APP_SIG) 33 { 34 bool valid = FALSE; 35 const char *str; 36 int32 index = 0; 37 BBitmap large_icon(BRect(0, 0, B_LARGE_ICON - 1, B_LARGE_ICON - 1), B_COLOR_8_BIT); 38 BBitmap mini_icon(BRect(0, 0, B_MINI_ICON - 1, B_MINI_ICON - 1), B_COLOR_8_BIT); 39 BDirectory dir; 40 BEntry entry; 41 BMessage msg; 42 BMessage info; 43 BMimeType mime; 44 BPath path; 45 BPoint pos; 46 BVolume vol; 47 BVolumeRoster roster; 48 // TPeopleWindow *window; 49 50 fHaveWindow = FALSE; 51 52 fPosition.Set(6, TITLE_BAR_HEIGHT, 6 + WIND_WIDTH, TITLE_BAR_HEIGHT + WIND_HEIGHT); 53 pos = fPosition.LeftTop(); 54 55 find_directory(B_USER_SETTINGS_DIRECTORY, &path, true); 56 dir.SetTo(path.Path()); 57 if (dir.FindEntry("People_data", &entry) == B_NO_ERROR) { 58 fPrefs = new BFile(&entry, O_RDWR); 59 if (fPrefs->InitCheck() == B_NO_ERROR) { 60 fPrefs->Read(&pos, sizeof(BPoint)); 61 if (BScreen(B_MAIN_SCREEN_ID).Frame().Contains(pos)) 62 fPosition.OffsetTo(pos); 63 } 64 } 65 else { 66 fPrefs = new BFile(); 67 if (dir.CreateFile("People_data", fPrefs) != B_NO_ERROR) { 68 delete fPrefs; 69 fPrefs = NULL; 70 } 71 } 72 73 roster.GetBootVolume(&vol); 74 fs_create_index(vol.Device(), P_NAME, B_STRING_TYPE, 0); 75 fs_create_index(vol.Device(), P_COMPANY, B_STRING_TYPE, 0); 76 fs_create_index(vol.Device(), P_ADDRESS, B_STRING_TYPE, 0); 77 fs_create_index(vol.Device(), P_CITY, B_STRING_TYPE, 0); 78 fs_create_index(vol.Device(), P_STATE, B_STRING_TYPE, 0); 79 fs_create_index(vol.Device(), P_ZIP, B_STRING_TYPE, 0); 80 fs_create_index(vol.Device(), P_COUNTRY, B_STRING_TYPE, 0); 81 fs_create_index(vol.Device(), P_HPHONE, B_STRING_TYPE, 0); 82 fs_create_index(vol.Device(), P_WPHONE, B_STRING_TYPE, 0); 83 fs_create_index(vol.Device(), P_FAX, B_STRING_TYPE, 0); 84 fs_create_index(vol.Device(), P_EMAIL, B_STRING_TYPE, 0); 85 fs_create_index(vol.Device(), P_URL, B_STRING_TYPE, 0); 86 fs_create_index(vol.Device(), P_GROUP, B_STRING_TYPE, 0); 87 fs_create_index(vol.Device(), P_NICKNAME, B_STRING_TYPE, 0); 88 89 // install person mime type 90 mime.SetType(B_PERSON_MIMETYPE); 91 if (mime.IsInstalled()) { 92 if (mime.GetAttrInfo(&info) == B_NO_ERROR) { 93 while (info.FindString("attr:name", index++, &str) == B_NO_ERROR) { 94 if (!strcmp(str, P_NAME)) { 95 valid = TRUE; 96 break; 97 } 98 } 99 if (!valid) 100 mime.Delete(); 101 } 102 } 103 if (!valid) { 104 mime.Install(); 105 large_icon.SetBits(kLargePersonIcon, large_icon.BitsLength(), 0, B_COLOR_8_BIT); 106 mini_icon.SetBits(kSmallPersonIcon, mini_icon.BitsLength(), 0, B_COLOR_8_BIT); 107 mime.SetShortDescription("Person"); 108 mime.SetLongDescription("Contact information for a person."); 109 mime.SetIcon(&large_icon, B_LARGE_ICON); 110 mime.SetIcon(&mini_icon, B_MINI_ICON); 111 mime.SetPreferredApp(APP_SIG); 112 113 // add relevant person fields to meta-mime type 114 msg.AddString("attr:public_name", "Contact Name"); 115 msg.AddString("attr:name", P_NAME); 116 msg.AddInt32("attr:type", B_STRING_TYPE); 117 msg.AddBool("attr:viewable", true); 118 msg.AddBool("attr:editable", true); 119 msg.AddInt32("attr:width", 120); 120 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 121 msg.AddBool("attr:extra", false); 122 123 msg.AddString("attr:public_name", "Company"); 124 msg.AddString("attr:name", P_COMPANY); 125 msg.AddInt32("attr:type", B_STRING_TYPE); 126 msg.AddBool("attr:viewable", true); 127 msg.AddBool("attr:editable", true); 128 msg.AddInt32("attr:width", 120); 129 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 130 msg.AddBool("attr:extra", false); 131 132 msg.AddString("attr:public_name", "Address"); 133 msg.AddString("attr:name", P_ADDRESS); 134 msg.AddInt32("attr:type", B_STRING_TYPE); 135 msg.AddBool("attr:viewable", true); 136 msg.AddBool("attr:editable", true); 137 msg.AddInt32("attr:width", 120); 138 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 139 msg.AddBool("attr:extra", false); 140 141 msg.AddString("attr:public_name", "City"); 142 msg.AddString("attr:name", P_CITY); 143 msg.AddInt32("attr:type", B_STRING_TYPE); 144 msg.AddBool("attr:viewable", true); 145 msg.AddBool("attr:editable", true); 146 msg.AddInt32("attr:width", 90); 147 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 148 msg.AddBool("attr:extra", false); 149 150 msg.AddString("attr:public_name", "State"); 151 msg.AddString("attr:name", P_STATE); 152 msg.AddInt32("attr:type", B_STRING_TYPE); 153 msg.AddBool("attr:viewable", true); 154 msg.AddBool("attr:editable", true); 155 msg.AddInt32("attr:width", 50); 156 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 157 msg.AddBool("attr:extra", false); 158 159 msg.AddString("attr:public_name", "Zip"); 160 msg.AddString("attr:name", P_ZIP); 161 msg.AddInt32("attr:type", B_STRING_TYPE); 162 msg.AddBool("attr:viewable", true); 163 msg.AddBool("attr:editable", true); 164 msg.AddInt32("attr:width", 50); 165 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 166 msg.AddBool("attr:extra", false); 167 168 msg.AddString("attr:public_name", "Country"); 169 msg.AddString("attr:name", P_COUNTRY); 170 msg.AddInt32("attr:type", B_STRING_TYPE); 171 msg.AddBool("attr:viewable", true); 172 msg.AddBool("attr:editable", true); 173 msg.AddInt32("attr:width", 120); 174 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 175 msg.AddBool("attr:extra", false); 176 177 msg.AddString("attr:public_name", "Home Phone"); 178 msg.AddString("attr:name", P_HPHONE); 179 msg.AddInt32("attr:type", B_STRING_TYPE); 180 msg.AddBool("attr:viewable", true); 181 msg.AddBool("attr:editable", true); 182 msg.AddInt32("attr:width", 90); 183 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 184 msg.AddBool("attr:extra", false); 185 186 msg.AddString("attr:public_name", "Work Phone"); 187 msg.AddString("attr:name", P_WPHONE); 188 msg.AddInt32("attr:type", B_STRING_TYPE); 189 msg.AddBool("attr:viewable", true); 190 msg.AddBool("attr:editable", true); 191 msg.AddInt32("attr:width", 90); 192 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 193 msg.AddBool("attr:extra", false); 194 195 msg.AddString("attr:public_name", "Fax"); 196 msg.AddString("attr:name", P_FAX); 197 msg.AddInt32("attr:type", B_STRING_TYPE); 198 msg.AddBool("attr:viewable", true); 199 msg.AddBool("attr:editable", true); 200 msg.AddInt32("attr:width", 90); 201 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 202 msg.AddBool("attr:extra", false); 203 204 msg.AddString("attr:public_name", "E-mail"); 205 msg.AddString("attr:name", P_EMAIL); 206 msg.AddInt32("attr:type", B_STRING_TYPE); 207 msg.AddBool("attr:viewable", true); 208 msg.AddBool("attr:editable", true); 209 msg.AddInt32("attr:width", 120); 210 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 211 msg.AddBool("attr:extra", false); 212 213 msg.AddString("attr:public_name", "URL"); 214 msg.AddString("attr:name", P_URL); 215 msg.AddInt32("attr:type", B_STRING_TYPE); 216 msg.AddBool("attr:viewable", true); 217 msg.AddBool("attr:editable", true); 218 msg.AddInt32("attr:width", 120); 219 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 220 msg.AddBool("attr:extra", false); 221 222 msg.AddString("attr:public_name", "Group"); 223 msg.AddString("attr:name", P_GROUP); 224 msg.AddInt32("attr:type", B_STRING_TYPE); 225 msg.AddBool("attr:viewable", true); 226 msg.AddBool("attr:editable", true); 227 msg.AddInt32("attr:width", 120); 228 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 229 msg.AddBool("attr:extra", false); 230 231 msg.AddString("attr:public_name", "Nickname"); 232 msg.AddString("attr:name", P_NICKNAME); 233 msg.AddInt32("attr:type", B_STRING_TYPE); 234 msg.AddBool("attr:viewable", true); 235 msg.AddBool("attr:editable", true); 236 msg.AddInt32("attr:width", 120); 237 msg.AddInt32("attr:alignment", B_ALIGN_LEFT); 238 msg.AddBool("attr:extra", false); 239 240 mime.SetAttrInfo(&msg); 241 } 242 } 243 244 //-------------------------------------------------------------------- 245 246 TPeopleApp::~TPeopleApp(void) 247 { 248 if (fPrefs) 249 delete fPrefs; 250 } 251 252 //-------------------------------------------------------------------- 253 254 void TPeopleApp::AboutRequested(void) 255 { 256 (new BAlert("", "...by Robert Polic", "Big Deal"))->Go(); 257 } 258 259 //-------------------------------------------------------------------- 260 261 void TPeopleApp::ArgvReceived(int32 argc, char **argv) 262 { 263 char *arg; 264 int32 index; 265 int32 loop; 266 TPeopleWindow *window = NULL; 267 268 for (loop = 1; loop < argc; loop++) { 269 arg = argv[loop]; 270 if (!strncmp(P_NAME, arg, strlen(P_NAME))) 271 index = F_NAME; 272 else if (!strncmp(P_COMPANY, arg, strlen(P_COMPANY))) 273 index = F_COMPANY; 274 else if (!strncmp(P_ADDRESS, arg, strlen(P_ADDRESS))) 275 index = F_ADDRESS; 276 else if (!strncmp(P_CITY, arg, strlen(P_CITY))) 277 index = F_CITY; 278 else if (!strncmp(P_STATE, arg, strlen(P_STATE))) 279 index = F_STATE; 280 else if (!strncmp(P_ZIP, arg, strlen(P_ZIP))) 281 index = F_ZIP; 282 else if (!strncmp(P_COUNTRY, arg, strlen(P_COUNTRY))) 283 index = F_COUNTRY; 284 else if (!strncmp(P_HPHONE, arg, strlen(P_HPHONE))) 285 index = F_HPHONE; 286 else if (!strncmp(P_WPHONE, arg, strlen(P_WPHONE))) 287 index = F_WPHONE; 288 else if (!strncmp(P_FAX, arg, strlen(P_FAX))) 289 index = F_FAX; 290 else if (!strncmp(P_EMAIL, arg, strlen(P_EMAIL))) 291 index = F_EMAIL; 292 else if (!strncmp(P_URL, arg, strlen(P_URL))) 293 index = F_URL; 294 else if (!strncmp(P_GROUP, arg, strlen(P_GROUP))) 295 index = F_GROUP; 296 else if (!strncmp(P_NICKNAME, arg, strlen(P_NICKNAME))) 297 index = F_NICKNAME; 298 else 299 index = F_END; 300 301 if (index != F_END) { 302 if (!window) 303 window = NewWindow(); 304 while(*arg != ' ') 305 arg++; 306 arg++; 307 window->SetField(index, arg); 308 } 309 } 310 } 311 312 //-------------------------------------------------------------------- 313 314 void TPeopleApp::MessageReceived(BMessage *msg) 315 { 316 switch (msg->what) { 317 case M_NEW: 318 NewWindow(); 319 break; 320 321 default: 322 BApplication::MessageReceived(msg); 323 } 324 } 325 326 //-------------------------------------------------------------------- 327 328 void TPeopleApp::RefsReceived(BMessage *msg) 329 { 330 int32 item = 0; 331 BFile file; 332 entry_ref ref; 333 TPeopleWindow *window; 334 335 while (msg->HasRef("refs", item)) { 336 msg->FindRef("refs", item++, &ref); 337 if ((window = FindWindow(ref))) 338 window->Activate(TRUE); 339 else { 340 file.SetTo(&ref, O_RDONLY); 341 if (file.InitCheck() == B_NO_ERROR) 342 NewWindow(&ref); 343 } 344 } 345 } 346 347 //-------------------------------------------------------------------- 348 349 void TPeopleApp::ReadyToRun(void) 350 { 351 if (!fHaveWindow) 352 NewWindow(); 353 } 354 355 //-------------------------------------------------------------------- 356 357 TPeopleWindow* TPeopleApp::NewWindow(entry_ref *ref) 358 { 359 TPeopleWindow *window; 360 361 window = new TPeopleWindow(fPosition, "New Person", ref); 362 window->Show(); 363 fHaveWindow = TRUE; 364 fPosition.OffsetBy(20, 20); 365 366 if (fPosition.bottom > BScreen(B_MAIN_SCREEN_ID).Frame().bottom) 367 fPosition.OffsetTo(fPosition.left, TITLE_BAR_HEIGHT); 368 if (fPosition.right > BScreen(B_MAIN_SCREEN_ID).Frame().right) 369 fPosition.OffsetTo(6, fPosition.top); 370 371 return window; 372 } 373 374 //-------------------------------------------------------------------- 375 376 TPeopleWindow* TPeopleApp::FindWindow(entry_ref ref) 377 { 378 int32 index = 0; 379 TPeopleWindow *window; 380 381 while ((window = (TPeopleWindow *)WindowAt(index++))) { 382 if ((window->FindView("PeopleView")) && (window->fRef) && (*(window->fRef) == ref)) 383 return window; 384 } 385 return NULL; 386 } 387