The right place for an importer in the document architecture
I have an importer to convert different file types into my native file format. Due to the nature of the importer process, I need to run it as a separate converter, so the process flow is:
1. The user selects the file to open (not in my native format)
2. The program reads it and writes out a file in my native format
3. I read the document from the native format version
4. Set the URL to nil because it is effectively a new document
5. Display the document
So I need to know where in the process I need to intercept the original file open request and do the conversion, and forward the request to open the converted file.
Would it be best to subclass NSDocumentController and override -makeDocumentWithContentsOfURL:ofType:error: and do the conversion, and call super with the new URL to the converted document, get the result back and set the URL to nil, and return it?
Would there be any side effects from that (because openDocument... used a different URL)?
...or will I need to go back to -openDocumentWithContentsOfURL:display:error: and work out the file type from the extension and work from there?
I'm assuming that anything within NSDocument or NSPersistentDocument is too late in the piece...
On Apr 8, 2010, at 4:46 AM, Gideon King wrote:
> So I need to know where in the process I need to intercept the original file open request and do the conversion, and forward the request to open the converted file.
It depends on your interface. If you're offering the ability to simply "open" this type of document using the normal mechanisms like the Open menu item, or dragging the file onto the application icon, then you would add the document type to Info.plist. In the normal file reading methods, you would read from the file and convert it to your format, and set the url to nil etc.
Here's an example:
If you want a separate import process like using File -> Import, which does an open dialog, pick a file and it creates an untitled document, you'd just stick all of that in a normal top-level controller that does the reading, creates a new document of your native type, and sets the data on that document.