customizing save behavior in doc based apps
customizing save behavior in doc based apps
- Subject: customizing save behavior in doc based apps
- From: Matthew Russell <email@hidden>
- Date: Mon, 26 Dec 2005 00:47:40 -0500
I'm developing an NSDocument (more precisely, based on
NSPersistentDocument w/ CoreData) based app and I'd like for it to
have the following characteristics:
1. If a document is untitled and not dirty, there will be no save
dialog displayed when the user closes the window or closes the app
with cmd-q
2. If a document is untitled and dirty, the red dot darkens as normal
the moment the doc becomes dirty, and the user is prompted for a save
when the window closes or the app closes with cmd-q.
3. If the user saves an untitled dirty document for the first time
via a typical method such as cmd-s, the document immediately begins
to appear "continuously saved" (see next bullet)
4. if a document is already saved, it continuously appears to be
saved (the red dot never darkens and thus it never appears to be
dirty) and it is automatically saved when the window closes or the
app closes with cmd-q. Hence "continuously saved" means that the user
never has to do it.
So a simple practical example of the behavior I'm looking for is the
way notes in Stickies behave (less the comments about the red dot
since they don't have a red dot).
I've done a lot of reading and experimenting on this but just can't
get it right. The closest I can seem to come is by doing the
following things in my NSPersistentDocument subclass:
0. // _isDocEdited is an instance variable
1. In windowControllerDidLoadNib: I put the following snippet
//some stuff
if ([self fileURL]) //file is saved to disk
_isDocEdited = NO; //don't show save dialog. handle it
automatically (see isDocumentEdited)
else
_isDocEdited = YES; //make user manually save once before
setting this to NO (see isDocumentEdited)
2. In writeToURL:ofType:ForSaveOperation:originalContentsURL:error: I
put the following snippet
//some stuff
//a call resembling -- success = [super writeToURL.........]
if (success)
_isDocEdited = NO; //don't bother with save dialog anymore
//I've also tried manually reseting the change count here (calling
[self updateChangeCount:NSChangeCleared])
3. I've overridden isDocumentEdited with this:
//for new documents, make sure they're manually saved at least once
(not untitled) before setting this value to NO.
-(BOOL)isDocumentEdited {
if (!_isDocEdited) //set in windowControllerDidLoadNib or in
writeToURL....
return NO;
else
return [super isDocumentEdited]; //if not overriding, do
"normal" behavior
4. I've overridden close with this in order to handle individual
window closings:
- (void)close {
if ([self fileURL]) //in case user decided not to save an
untitled document, avoid exception spewage
[self writeToURL:[self fileURL]
ofType:[self fileType]
forSaveOperation:NSSaveOperation
originalContentsURL:[self fileURL]
error:nil];
[super close];
}
In my AppController (NSApp's delegate), I've done this in order to
'gracefully' terminate the app (via app termination w/ cmd-q, etc.):
- (NSApplicationTerminateReply)applicationShouldTerminate:
(NSApplication *)app {
NSArray *docs = [[NSDocumentController sharedDocumentController]
documents];
unsigned count = [docs count];
NSDocument *d;
while (count--) {
d = [docs objectAtIndex:count];
if ([d fileURL]) //file already exists on disk and is not
untitled
[d writeToURL:[d fileURL]
ofType:[d fileType]
forSaveOperation:NSSaveOperation
originalContentsURL:[d fileURL]
error:nil];
}
return NSTerminateNow;
}
So what's wrong? Well here's the problems I'm having as I see them:
0. If I have an untitled document that's not dirty (just opened) and
try to quit it with cmd-q or closing the window, I get the save
dialog. I can save it to disk just fine, but I don't want to see the
dialog at all with an undirty, untitled document. Not sure how to
avoid it though using the path I've trodden down.
1. If I have an untitled document that is dirty and try to quit it by
closing the window, I can save it to disk just fine. If I try to quit
the app with cmd-q, however, I get the initial 3 button don't save,
cancel, save... dialog -- but when I click on "save..." the dialog
hangs and the actual save panel is never displayed....the only thing
that unhangs it and gets the panel to display is to go to the app's
menu and click on the quit option with the mouse. (Weird.)
2. If I try to save an untitled dirty document using a typical save
method such as cmd-s, it saves just fine but the document appears to
still be marked as dirty because the red dot is still dark...but
after another keystroke in the document, the dot lightens and things
appear as normal. Not a huge deal, but obviously I'm doing something
wrong here.
So if I have a document that's already saved to disk everything
appears to work alright, but otherwise, there's the anomalies pointed
out in 0, 1 & 2 that I just can't figure out how to fix up. I've
looked at the TextEdit example but am not totally sure which parts of
it apply here since it is not based on the NSDocument architecture.
My line of reasoning is simple (but obviously flawed) --
-- If the document is saved, make it appear to never be dirty by
overriding isDocumentEdited (returning NO) and programatically save
in close and applicationShouldTerminate but if it's not saved, have
the user manually save it one time before resorting to this behavior.
Could some learned individual out there point out what I'm missing? I
have a feeling there's a few things, and unfortunately, the obvious
variations I've been trying haven't made any difference. This leads
me to believe there's some fundamental thing I'm missing here
Your thoughts?
Matthew
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden