[haiku-commits] haiku: hrev45779 - src/apps/people

  • From: stpere@xxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sun, 23 Jun 2013 01:50:59 +0200 (CEST)

hrev45779 adds 1 changeset to branch 'master'
old head: b1975a590faee9dc91f0a6bc626bc5d84475eb24
new head: a09c983cc63b6d50c46a0f2a872fb1adbffcc363
overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=a09c983+%5Eb1975a5

----------------------------------------------------------------------------

a09c983: People: open files in READ_WRITE mode only when necessary. (#5791)

                                [ Philippe Saint-Pierre <stpere@xxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev45779
Commit:      a09c983cc63b6d50c46a0f2a872fb1adbffcc363
URL:         http://cgit.haiku-os.org/haiku/commit/?id=a09c983
Author:      Philippe Saint-Pierre <stpere@xxxxxxxxx>
Date:        Sat Jun 22 23:46:40 2013 UTC

Ticket:      https://dev.haiku-os.org/ticket/5791

----------------------------------------------------------------------------

2 files changed, 29 insertions(+), 17 deletions(-)
src/apps/people/PersonView.cpp | 44 ++++++++++++++++++++++++--------------
src/apps/people/PersonView.h   |  2 +-

----------------------------------------------------------------------------

diff --git a/src/apps/people/PersonView.cpp b/src/apps/people/PersonView.cpp
index 656bef1..2cf3484 100644
--- a/src/apps/people/PersonView.cpp
+++ b/src/apps/people/PersonView.cpp
@@ -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;
 }
 
 
diff --git a/src/apps/people/PersonView.h b/src/apps/people/PersonView.h
index 3695b22..f6ecc27 100644
--- a/src/apps/people/PersonView.h
+++ b/src/apps/people/PersonView.h
@@ -62,7 +62,7 @@ public:
                        bool                            IsTextSelected() const;
 
 private:
-                       BFile*                          fFile;
+                       const entry_ref*                fRef;
                        time_t                          fLastModificationTime;
                        BPopUpMenu*                     fGroups;
                        typedef BObjectList<AttributeTextControl> AttributeList;


Other related posts: