People: open files in READ_WRITE mode only when necessary. (#5791)
This commit is contained in:
parent
b1975a590f
commit
a09c983cc6
@ -55,10 +55,10 @@ PersonView::PersonView(const char* name, const char* categoryAttribute,
|
||||
SetName(name);
|
||||
SetFlags(Flags() | B_WILL_DRAW);
|
||||
|
||||
if (ref)
|
||||
fFile = new BFile(ref, O_RDWR);
|
||||
else
|
||||
fFile = NULL;
|
||||
fRef = ref;
|
||||
BFile* file = NULL;
|
||||
if (fRef != NULL)
|
||||
file = new BFile(fRef, B_READ_ONLY);
|
||||
|
||||
float spacing = be_control_look->DefaultItemSpacing();
|
||||
BGridLayout* layout = GridLayout();
|
||||
@ -71,14 +71,14 @@ PersonView::PersonView(const char* name, const char* categoryAttribute,
|
||||
layout->ItemAt(0, 0)->SetExplicitAlignment(
|
||||
BAlignment(B_ALIGN_CENTER, B_ALIGN_TOP));
|
||||
|
||||
if (fFile)
|
||||
fFile->GetModificationTime(&fLastModificationTime);
|
||||
if (file != NULL)
|
||||
file->GetModificationTime(&fLastModificationTime);
|
||||
delete file;
|
||||
}
|
||||
|
||||
|
||||
PersonView::~PersonView()
|
||||
{
|
||||
delete fFile;
|
||||
}
|
||||
|
||||
|
||||
@ -269,8 +269,7 @@ PersonView::BuildGroupMenu()
|
||||
void
|
||||
PersonView::CreateFile(const entry_ref* ref)
|
||||
{
|
||||
delete fFile;
|
||||
fFile = new BFile(ref, B_READ_WRITE);
|
||||
fRef = ref;
|
||||
Save();
|
||||
}
|
||||
|
||||
@ -293,13 +292,19 @@ PersonView::IsSaved() const
|
||||
void
|
||||
PersonView::Save()
|
||||
{
|
||||
BFile* file = new(std::nothrow) BFile(fRef, B_READ_WRITE);
|
||||
if (file == NULL || file->InitCheck() != B_NO_ERROR) {
|
||||
delete file;
|
||||
return;
|
||||
}
|
||||
|
||||
fSaving = true;
|
||||
|
||||
int32 count = fControls.CountItems();
|
||||
for (int32 i = 0; i < count; i++) {
|
||||
AttributeTextControl* control = fControls.ItemAt(i);
|
||||
const char* value = control->Text();
|
||||
fFile->WriteAttr(control->Attribute().String(), B_STRING_TYPE, 0,
|
||||
file->WriteAttr(control->Attribute().String(), B_STRING_TYPE, 0,
|
||||
value, strlen(value) + 1);
|
||||
control->Update();
|
||||
}
|
||||
@ -307,8 +312,8 @@ PersonView::Save()
|
||||
// Write the picture, if any, in the person file content
|
||||
if (fPictureView) {
|
||||
// Trim any previous content
|
||||
fFile->Seek(0, SEEK_SET);
|
||||
fFile->SetSize(0);
|
||||
file->Seek(0, SEEK_SET);
|
||||
file->SetSize(0);
|
||||
|
||||
BBitmap* picture = fPictureView->Bitmap();
|
||||
if (picture) {
|
||||
@ -318,7 +323,7 @@ PersonView::Save()
|
||||
stream.DetachBitmap(&picture);
|
||||
|
||||
BTranslatorRoster* roster = BTranslatorRoster::Default();
|
||||
roster->Translate(&stream, NULL, NULL, fFile,
|
||||
roster->Translate(&stream, NULL, NULL, file,
|
||||
fPictureView->SuggestedType(), B_TRANSLATOR_BITMAP,
|
||||
fPictureView->SuggestedMIMEType());
|
||||
|
||||
@ -327,9 +332,10 @@ PersonView::Save()
|
||||
fPictureView->Update();
|
||||
}
|
||||
|
||||
fFile->GetModificationTime(&fLastModificationTime);
|
||||
file->GetModificationTime(&fLastModificationTime);
|
||||
|
||||
fSaving = false;
|
||||
delete file;
|
||||
}
|
||||
|
||||
|
||||
@ -350,14 +356,20 @@ PersonView::SetAttribute(const char* attribute, bool update)
|
||||
{
|
||||
char* value = NULL;
|
||||
attr_info info;
|
||||
if (fFile != NULL && fFile->GetAttrInfo(attribute, &info) == B_OK) {
|
||||
BFile* file = NULL;
|
||||
|
||||
if (fRef != NULL)
|
||||
file = new(std::nothrow) BFile(fRef, B_READ_ONLY);
|
||||
|
||||
if (file != NULL && file->GetAttrInfo(attribute, &info) == B_OK) {
|
||||
value = (char*)calloc(info.size, 1);
|
||||
fFile->ReadAttr(attribute, B_STRING_TYPE, 0, value, info.size);
|
||||
file->ReadAttr(attribute, B_STRING_TYPE, 0, value, info.size);
|
||||
}
|
||||
|
||||
SetAttribute(attribute, value, update);
|
||||
|
||||
free(value);
|
||||
delete file;
|
||||
}
|
||||
|
||||
|
||||
|
@ -62,7 +62,7 @@ public:
|
||||
bool IsTextSelected() const;
|
||||
|
||||
private:
|
||||
BFile* fFile;
|
||||
const entry_ref* fRef;
|
||||
time_t fLastModificationTime;
|
||||
BPopUpMenu* fGroups;
|
||||
typedef BObjectList<AttributeTextControl> AttributeList;
|
||||
|
Loading…
Reference in New Issue
Block a user