Determining when a Lion Versioned document is locked/unlocked

  • SUMMARY
    I'd like to know when a document is in the "locked" state. Cocoa provides -[NSDocument checkAutosavingSafetyAndReturnError:]; but this only seems to tell me when the heuristics think the document should be locked, not when the user has deliberately locked the document. Is there any way to determine the actual locked state, or receive a notification when it changes?

    PROBLEM
    I have a document-bases app where documents conceptually contain widgets and other data. The widget properties are managed from an application (singleton) library. Normally, the user would expect the widgets to always be in sync with this library. However, when the document is locked or emailed to someone else (who may have a quite different widget library), the document should retain the widget properties as they existed in the library when the document was locked.

    This means the document must contain a copy of any widgets it uses. It also means that either the app must automatically "sync" the document's widgets with the library at appropriate times, or the user must do this her/himself. Since, the user almost always wants the two to be in sync, I would much rather the application do this automatically. Thus, the app needs to know when this is appropriate, and so, needs to know the locked state.

    But if you can think of a better application design, I'd be happy to hear it.

    Dave
  • To answer the first part of my question…
    This (deliberately set) locked state is the same as the Finder's locked state. You can get it using -[NSURL getResourceValue:forKey:error:] with the key, NSURLIsUserImmutableKey.

    So you need to check both the NSURLIsUserImmutableKey file-attribute and the checkAutosavingSafetyAndReturnError document heuristic in order to know whether "Locked" is displayed in the document's title bar.

    As for notifications -
    It seems autosaveWithImplicitCancellability is called when the user locks the file from title bar menu, but not when the user unlocks the file. Makes sense; however, *unlocking a locked file* is the transition I am interested in. I suppose I can poll for it, but ick!

    Is there a better way to be notified when a file's attributes change?

    Thanks,
    Dave

    On 2011-11-16, at 1:31 PM, Dave Fernandes wrote:

    > SUMMARY
    > I'd like to know when a document is in the "locked" state. Cocoa provides -[NSDocument checkAutosavingSafetyAndReturnError:]; but this only seems to tell me when the heuristics think the document should be locked, not when the user has deliberately locked the document. Is there any way to determine the actual locked state, or receive a notification when it changes?
    >
    > PROBLEM
    > I have a document-bases app where documents conceptually contain widgets and other data. The widget properties are managed from an application (singleton) library. Normally, the user would expect the widgets to always be in sync with this library. However, when the document is locked or emailed to someone else (who may have a quite different widget library), the document should retain the widget properties as they existed in the library when the document was locked.
    >
    > This means the document must contain a copy of any widgets it uses. It also means that either the app must automatically "sync" the document's widgets with the library at appropriate times, or the user must do this her/himself. Since, the user almost always wants the two to be in sync, I would much rather the application do this automatically. Thus, the app needs to know when this is appropriate, and so, needs to know the locked state.
    >
    > But if you can think of a better application design, I'd be happy to hear it.
    >
    > Dave
  • On 2011 Nov 16, at 18:55, Dave Fernandes wrote:

    > Is there a better way to be notified when a file's attributes change?

    Maybe.  I'm not sure about this particular attribute, but possibly it would be detected by kqueue's NOTE_ATTRIB.  See man kqueue(2).  Cocoa wrappers for kqueue are available.
previous month november 2011 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
Go to today