summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/autofill_address_view_controller_mac.mm
blob: aa77644ff66814983e5b91c4330e9f46ac1908b0 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/autofill/autofill_address_view_controller_mac.h"
#include "base/mac_util.h"
#include "base/sys_string_conversions.h"
#import "chrome/browser/autofill/autofill_address_model_mac.h"
#import "chrome/browser/autofill/autofill_dialog_controller_mac.h"
#include "chrome/browser/autofill/autofill_profile.h"

@implementation AutoFillAddressViewController

@synthesize addressModel = addressModel_;

- (id)initWithProfile:(const AutoFillProfile&)profile
    disclosure:(NSCellStateValue)disclosureState
    controller:(AutoFillDialogController*) parentController {
  self = [super initWithNibName:@"AutoFillAddressFormView"
      bundle:mac_util::MainAppBundle()
      disclosure:disclosureState];
  if (self) {
    // Pull in the view for initialization.
    [self view];

    // Create the model.
    [self setAddressModel:[[[AutoFillAddressModel alloc]
        initWithProfile:profile] autorelease]];

    // We keep track of our parent controller for model-update purposes.
    parentController_ = parentController;

    // Register |self| as observer so we can notify parent controller.  See
    // |observeValueForKeyPath:| for details.
    [addressModel_ addObserver:self forKeyPath:@"label"
        options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
        context:NULL];
  }
  return self;
}

- (void)dealloc {
  [addressModel_ removeObserver:self forKeyPath:@"label"];
  [addressModel_ release];
  [super dealloc];
}

// Override KVO method to notify parent controller when the address "label"
// changes.  Credit card UI updates accordingly.
- (void)observeValueForKeyPath:(NSString *)keyPath
    ofObject:(id)object
    change:(NSDictionary*)change
    context:(void *)context {
  if ([keyPath isEqual:@"label"]) {
    [parentController_ notifyAddressChange:self];
  }

  // Propagate KVO up to super.
  [super observeValueForKeyPath:keyPath
      ofObject:object change:change context:context];
}

- (IBAction)deleteAddress:(id)sender {
  [parentController_ deleteAddress:self];
}

- (void)copyModelToProfile:(AutoFillProfile*)profile {
  [addressModel_ copyModelToProfile:profile];
}

@end