Tags

    Aperture MetaWeblog Plugin: Saving User Information

    Comments

    Saving the information

    We need to save the information in a plist file. We will save the information whenever we start exporting information. So, in MetaWeblogPlugIn.m, find the exportManagerShouldBeginExport method and add the following code to it:

    - (void)exportManagerShouldBeginExport
    {
      // create the blog instance
      _blog = [[MetaWeblog alloc] init];
      [_blog setUrl: [blogAccessPointTextField stringValue]];
      [_blog setBlogId: [blogIdTextField stringValue]];
      [_blog setUsername: [blogUserNameTextField stringValue]];
      [_blog setPassword: [blogPasswordTextField stringValue]];
      
      // save settings
      NSAutoreleasePool *pool =  [[NSAutoreleasePool alloc] init];
      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
      NSString* pluginIdentifier = [[[NSBundle bundleForClass: [self class]] infoDictionary] objectForKey: @"CFBundleIdentifier"];
      NSMutableDictionary* settings = [NSMutableDictionary dictionaryWithDictionary: [defaults persistentDomainForName:pluginIdentifier]];
      [settings setValue: [_blog url] forKey: @"accessPoint"];
      [settings setValue: [_blog blogId] forKey: @"blogId"]; 
      [settings setValue: [_blog username] forKey: @"username"];
      [settings setValue: [_blog password] forKey: @"password"]; 
      [defaults setPersistentDomain:settings forName:pluginIdentifier];  
      [defaults synchronize];
      [pool release];
     
      // Begin Export 
      [_exportManager shouldBeginExport];
    }
    

    When the UI is activated for the first time, we need to read the values and populate the corresponding UI controls. So lets overwrite the willBeActivated method

    - (void)willBeActivated
    { 
      NSAutoreleasePool *pool =  [[NSAutoreleasePool alloc] init];
      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
      NSString* pluginIdentifier = [[[NSBundle bundleForClass: [self class]] infoDictionary] objectForKey: @"CFBundleIdentifier"];
      NSMutableDictionary* settings = [NSMutableDictionary dictionaryWithDictionary: [defaults persistentDomainForName:pluginIdentifier]];
      [blogAccessPointTextField setStringValue: [settings valueForKey: @"accessPoint"]];
      [blogIdTextField setStringValue: [settings valueForKey: @"blogId"]];
      [blogUserNameTextField setStringValue: [settings valueForKey: @"username"]];
      [blogPasswordTextField setStringValue: [settings valueForKey: @"password"]];
      [pool release];
    }
    

    In the first installment of the plug-in, we configured the Bundle Identifier to be com.bloudraak.export.MetaWeblogPlugIn. You'll find a file with that name (com.bloudraak.export.MetaWeblogPlugIn.plist) in ~/Library/Preferences/. If you open it, you can see the values. In a future installment, we'll look at storing the password in the user's keychain. Until then, the password is stored in clear text in the plist file. Yes, its bad. If it bothers you enough, then feel free to change it.

    Conclusion

    In this installment persisted the user preferences related to the blogging server. What is outstanding is storing the password in the keychain rather than the plist file.