status_t ActivityView::AddDataSource(const DataSource* source, const BMessage* state) { if (source == NULL) return B_BAD_VALUE; BAutolock _(fSourcesLock); // Search for the correct insert spot to maintain the order of the sources int32 insert = DataSource::IndexOf(source); for (int32 i = 0; i < fSources.CountItems() && i < insert; i++) { DataSource* before = fSources.ItemAt(i); if (DataSource::IndexOf(before) > insert) { insert = i; break; } } if (insert > fSources.CountItems()) insert = fSources.CountItems(); // Generate DataHistory and ViewHistory objects for the source // (one might need one history per CPU) uint32 count = 1; if (source->PerCPU()) { SystemInfo info; count = info.CPUCount(); } for (uint32 i = 0; i < count; i++) { DataHistory* values = new(std::nothrow) DataHistory(10 * 60000000LL, RefreshInterval()); ListAddDeleter<DataHistory> valuesDeleter(fValues, values, insert); ViewHistory* viewValues = new(std::nothrow) ViewHistory; ListAddDeleter<ViewHistory> viewValuesDeleter(fViewValues, viewValues, insert); if (valuesDeleter.Failed() || viewValuesDeleter.Failed()) return B_NO_MEMORY; values->SetScale(_ScaleFor(source->ScaleType())); DataSource* copy; if (source->PerCPU()) copy = source->CopyForCPU(i); else copy = source->Copy(); ListAddDeleter<DataSource> sourceDeleter(fSources, copy, insert); if (sourceDeleter.Failed()) return B_NO_MEMORY; BString colorName = source->Name(); colorName << " color"; if (state != NULL) { const rgb_color* color = NULL; ssize_t colorLength; if (state->FindData(colorName.String(), B_RGB_COLOR_TYPE, i, (const void**)&color, &colorLength) == B_OK && colorLength == sizeof(rgb_color)) copy->SetColor(*color); } valuesDeleter.Detach(); viewValuesDeleter.Detach(); sourceDeleter.Detach(); insert++; } #ifdef __HAIKU__ InvalidateLayout(); #endif return B_OK; }