void DriverSettingsMessageAdapterTest::TestMessage() { const settings_template kSubTemplate[] = { {B_BOOL_TYPE, "bool", NULL}, {} }; const settings_template kTemplate[] = { {B_MESSAGE_TYPE, "message", kSubTemplate}, {} }; Settings settingsA("message {\n" " bool\n" "}\n"); BMessage message; CPPUNIT_ASSERT_EQUAL(B_OK, settingsA.ToMessage(kTemplate, message)); BMessage subMessage; CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("message", &subMessage)); CPPUNIT_ASSERT_EQUAL_MESSAGE("bool", true, subMessage.GetBool("bool")); CPPUNIT_ASSERT_EQUAL(1, message.CountNames(B_ANY_TYPE)); Settings settingsB("message {\n" "}\n"); CPPUNIT_ASSERT_EQUAL(B_OK, settingsB.ToMessage(kTemplate, message)); CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("message", &subMessage)); CPPUNIT_ASSERT_EQUAL(1, message.CountNames(B_ANY_TYPE)); Settings settingsC("\n"); CPPUNIT_ASSERT_EQUAL(B_OK, settingsC.ToMessage(kTemplate, message)); CPPUNIT_ASSERT(message.IsEmpty()); }
int PootleProject::CountTranslationProjects() { _EnsureData(); BMessage projects; mData.FindMessage("translation_projects", &projects); return projects.CountNames(B_ANY_TYPE); }
BObjectList<PootleTranslationProject> PootleProject::TranslationProjects() { BMessage tprojects; mData.FindMessage("translation_projects", &tprojects); BObjectList<BString> urls(20, true); int32 count = tprojects.CountNames(B_ANY_TYPE); char buffer[33]; for (int32 i = 0; i < count; i++) { sprintf(buffer, "%d", i); urls.AddItem(new BString(tprojects.GetString(buffer, ""))); } return mEndpoint->API()->TranslationProjects()->GetByList(urls); }
void DriverSettingsMessageAdapterTest::TestConverter() { class InterfaceAddressFamilyConverter : public DriverSettingsConverter { public: status_t ConvertFromDriverSettings(const driver_parameter& parameter, const char* name, int32 index, uint32 type, BMessage& target) { const char* value = parameter.values[index]; if (value[0] == '0' && value[1] == 'x') return target.AddInt32(name, (int32)strtol(value, NULL, 0)); return B_NOT_SUPPORTED; } status_t ConvertToDriverSettings(const BMessage& source, const char* name, int32 index, uint32 type, BString& value) { int32 intValue; if (index == 0 && source.FindInt32(name, 0, &intValue) == B_OK) { BString string; string.SetToFormat("0x%" B_PRIu32, intValue); value << string; return B_OK; } return B_NOT_SUPPORTED; } } converter; const settings_template kTemplate[] = { {B_INT32_TYPE, "test", NULL, false, &converter}, {} }; Settings settings("test 0x2a 43"); BMessage message; CPPUNIT_ASSERT_EQUAL(B_OK, settings.ToMessage(kTemplate, message)); CPPUNIT_ASSERT_EQUAL(42, message.GetInt32("test", 0, 0)); CPPUNIT_ASSERT_EQUAL(43, message.GetInt32("test", 1, 0)); CPPUNIT_ASSERT_EQUAL(1, message.CountNames(B_ANY_TYPE)); }
bool TTracker::QuitRequested() { // don't allow user quitting if (CurrentMessage() && CurrentMessage()->FindBool("shortcut")) return false; gStatusWindow->AttemptToQuit(); // try quitting the copy/move/empty trash threads BVolume bootVolume; DEBUG_ONLY(status_t err =) BVolumeRoster().GetBootVolume(&bootVolume); ASSERT(err == B_OK); BMessage message; AutoLock<WindowList> lock(&fWindowList); // save open windows in a message inside an attribute of the desktop int32 count = fWindowList.CountItems(); for (int32 i = 0; i < count; i++) { BContainerWindow *window = dynamic_cast<BContainerWindow *> (fWindowList.ItemAt(i)); if (window && window->TargetModel() && !window->PoseView()->IsDesktopWindow()) { if (window->TargetModel()->IsRoot()) message.AddBool("open_disks_window", true); else { BEntry entry; BPath path; const entry_ref *ref = window->TargetModel()->EntryRef(); if (entry.SetTo(ref) == B_OK && entry.GetPath(&path) == B_OK) { int8 flags = window->IsMinimized() ? kOpenWindowMinimized : kOpenWindowNoFlags; uint32 deviceFlags = GetVolumeFlags(window->TargetModel()); // save state for every window which is // a) already open on another workspace // b) on a volume not capable of writing attributes if (window != FindContainerWindow(ref) || (deviceFlags & (B_FS_HAS_ATTR | B_FS_IS_READONLY)) != B_FS_HAS_ATTR) { BMessage stateMessage; window->SaveState(stateMessage); window->SetSaveStateEnabled(false); // This is to prevent its state to be saved to the node when closed. message.AddMessage("window state", &stateMessage); flags |= kOpenWindowHasState; } const char *target; bool pathAlreadyExists = false; for (int32 index = 0;message.FindString("paths", index, &target) == B_OK;index++) { if (!strcmp(target,path.Path())) { pathAlreadyExists = true; break; } } if (!pathAlreadyExists) message.AddString("paths", path.Path()); message.AddInt8(path.Path(), flags); } } } } lock.Unlock(); // write windows to open on disk BDirectory deskDir; if (!BootedInSafeMode() && FSGetDeskDir(&deskDir, bootVolume.Device()) == B_OK) { // if message is empty, delete the corresponding attribute if (message.CountNames(B_ANY_TYPE)) { size_t size = (size_t)message.FlattenedSize(); char *buffer = new char[size]; message.Flatten(buffer, (ssize_t)size); deskDir.WriteAttr(kAttrOpenWindows, B_MESSAGE_TYPE, 0, buffer, size); delete [] buffer; } else deskDir.RemoveAttr(kAttrOpenWindows); } for (int32 count = 0; count == 50; count++) { // wait 5 seconds for the copiing/moving to quit if (gStatusWindow->AttemptToQuit()) break; snooze(100000); } return _inherited::QuitRequested(); }
void DriverSettingsMessageAdapterTest::TestPrimitivesToMessage() { const settings_template kTemplate[] = { {B_BOOL_TYPE, "bool1", NULL}, {B_BOOL_TYPE, "bool2", NULL}, {B_BOOL_TYPE, "bool3", NULL}, {B_BOOL_TYPE, "bool4", NULL}, {B_BOOL_TYPE, "bool5", NULL}, {B_BOOL_TYPE, "bool6", NULL}, {B_BOOL_TYPE, "bool7", NULL}, {B_BOOL_TYPE, "bool8", NULL}, {B_BOOL_TYPE, "bool9", NULL}, {B_BOOL_TYPE, "empty_bool", NULL}, {B_INT32_TYPE, "int32", NULL}, {B_INT32_TYPE, "negative_int32", NULL}, {B_INT32_TYPE, "empty_int32", NULL}, {B_STRING_TYPE, "string", NULL}, {B_STRING_TYPE, "long_string", NULL}, {B_STRING_TYPE, "empty_string", NULL}, {} }; Settings settings("bool1 true\n" "bool2 1\n" "bool3 on\n" "bool4 yes\n" "bool5 enabled\n" "bool6 false\n" "bool7 off\n" "bool8 gurkensalat\n" "bool9 0\n" "empty_bool\n" "int32 42\n" "negative_int32 -42\n" "empty_int32\n" "string Hey\n" "long_string \"This is but a test\"\n" "empty_string\n"); BMessage message; CPPUNIT_ASSERT_EQUAL(B_OK, settings.ToMessage(kTemplate, message)); CPPUNIT_ASSERT_EQUAL(10, message.CountNames(B_BOOL_TYPE)); CPPUNIT_ASSERT_EQUAL(2, message.CountNames(B_INT32_TYPE)); CPPUNIT_ASSERT_EQUAL(2, message.CountNames(B_STRING_TYPE)); CPPUNIT_ASSERT_EQUAL(14, message.CountNames(B_ANY_TYPE)); // bool values CPPUNIT_ASSERT_EQUAL_MESSAGE("bool1", true, message.GetBool("bool1")); CPPUNIT_ASSERT_EQUAL_MESSAGE("bool2", true, message.GetBool("bool2")); CPPUNIT_ASSERT_EQUAL_MESSAGE("bool3", true, message.GetBool("bool3")); CPPUNIT_ASSERT_EQUAL_MESSAGE("bool4", true, message.GetBool("bool4")); CPPUNIT_ASSERT_EQUAL_MESSAGE("bool5", true, message.GetBool("bool5")); CPPUNIT_ASSERT_EQUAL_MESSAGE("bool6", false, message.GetBool("bool6", true)); CPPUNIT_ASSERT_EQUAL_MESSAGE("bool7", false, message.GetBool("bool7", true)); CPPUNIT_ASSERT_EQUAL_MESSAGE("bool8", false, message.GetBool("bool8", true)); CPPUNIT_ASSERT_EQUAL_MESSAGE("bool9", false, message.GetBool("bool9", true)); CPPUNIT_ASSERT_EQUAL_MESSAGE("empty_bool", true, message.GetBool("empty_bool")); // int32 values CPPUNIT_ASSERT_EQUAL(42, message.GetInt32("int32", 0)); CPPUNIT_ASSERT_EQUAL(-42, message.GetInt32("negative_int32", 0)); CPPUNIT_ASSERT_EQUAL(false, message.HasInt32("empty_int32")); // string values CPPUNIT_ASSERT_EQUAL(BString("Hey"), BString(message.GetString("string"))); CPPUNIT_ASSERT_EQUAL(BString("This is but a test"), BString(message.GetString("long_string"))); CPPUNIT_ASSERT_EQUAL(false, message.HasString("empty_string")); }
bool TRecentsMenu::AddRecents(int32 count) { if (fItemIndex == 0) { fRecentList.MakeEmpty(); BRoster roster; switch (fWhich) { case kRecentDocuments: roster.GetRecentDocuments(&fRecentList, count); break; case kRecentApplications: roster.GetRecentApps(&fRecentList, count); break; case kRecentAppDocuments: roster.GetRecentDocuments(&fRecentList, count, NULL, fSignature); break; case kRecentFolders: roster.GetRecentFolders(&fRecentList, count); break; default: return false; } } for (;;) { entry_ref ref; if (fRecentList.FindRef("refs", fItemIndex++, &ref) != B_OK) break; if (ref.name && strlen(ref.name) > 0) { Model model(&ref, true); if (fWhich != kRecentApplications) { BMessage* message = new BMessage(B_REFS_RECEIVED); if (fWhich == kRecentAppDocuments) { // add application as handler message->AddRef("handler", fAppRef); } ModelMenuItem* item = BNavMenu::NewModelItem(&model, message, Target(), false, NULL, TypesList()); if (item) AddItem(item); } else { // The application items expand to a list of recent documents // for that application - so they must be handled extra BFile file(&ref, B_READ_ONLY); char signature[B_MIME_TYPE_LENGTH]; BAppFileInfo appInfo(&file); if (appInfo.InitCheck() != B_OK || appInfo.GetSignature(signature) != B_OK) continue; ModelMenuItem* item = NULL; BMessage doc; be_roster->GetRecentDocuments(&doc, 1, NULL, signature); // ToDo: check if the documents do exist at all to // avoid the creation of the submenu. if (doc.CountNames(B_REF_TYPE) > 0) { // create recents menu that will contain the recent docs of // this app TRecentsMenu* docs = new TRecentsMenu(model.Name(), fBarView, kRecentAppDocuments, signature, &ref); docs->SetTypesList(TypesList()); docs->SetTarget(Target()); item = new ModelMenuItem(&model, docs); } else item = new ModelMenuItem(&model, model.Name(), NULL); if (item) { // add refs-message so that the recent app can be launched BMessage* msg = new BMessage(B_REFS_RECEIVED); msg->AddRef("refs", &ref); item->SetMessage(msg); item->SetTarget(Target()); AddItem(item); } } // return true so that we know to reenter this list return true; } } // return false if we are done with this list return false; }