Switch to using VariableNode in the interface. Reduce use of NSXMLElementAdditions...
[macgdbp.git] / Source / VariableNode.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2010, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "VariableNode.h"
18
19 #import "AppDelegate.h"
20 #include "NSXMLElementAdditions.h"
21
22 // Private Properties //////////////////////////////////////////////////////////
23
24 @interface VariableNode ()
25
26 @property (copy) NSString* name;
27 @property (copy) NSString* fullName;
28 @property (copy) NSString* className;
29 @property (copy) NSString* type;
30 @property (copy) NSString* value;
31 @property (retain) NSMutableArray* children;
32
33 @end
34
35 ////////////////////////////////////////////////////////////////////////////////
36
37 @implementation VariableNode
38
39 @synthesize name = name_;
40 @synthesize fullName = fullName_;
41 @synthesize className = className_;
42 @synthesize type = type_;
43 @synthesize value = value_;
44 @synthesize children = children_;
45 @synthesize childCount = childCount_;
46
47 - (id)initWithXMLNode:(NSXMLElement*)node
48 {
49 if (self = [super init]) {
50 self.name = [[node attributeForName:@"name"] stringValue];
51 self.fullName = [[node attributeForName:@"fullname"] stringValue];
52 self.className = [[node attributeForName:@"classname"] stringValue];
53 self.type = [[node attributeForName:@"type"] stringValue];
54 self.value = [node base64DecodedValue];
55 self.children = [NSMutableArray array];
56 if ([node children]) {
57 [self setChildrenFromXMLChildren:[node children]];
58 }
59 childCount_ = [[[node attributeForName:@"numchildren"] stringValue] integerValue];
60 }
61 return self;
62 }
63
64 - (void)dealloc
65 {
66 self.name = nil;
67 self.fullName = nil;
68 self.className = nil;
69 self.type = nil;
70 self.value = nil;
71 self.children = nil;
72 [super dealloc];
73 }
74
75 - (void)setChildrenFromXMLChildren:(NSArray*)children
76 {
77 for (NSXMLNode* child in children) {
78 // Other child nodes may be the string value.
79 if ([child isKindOfClass:[NSXMLElement class]]) {
80 VariableNode* node = [[VariableNode alloc] initWithXMLNode:(NSXMLElement*)child];
81 [children_ addObject:[node autorelease]];
82 }
83 }
84 }
85
86 - (NSArray*)dynamicChildren
87 {
88 NSArray* children = self.children;
89 if (![self isLeaf] && [children count] < 1) {
90 // If this node has children but they haven't been loaded from the backend,
91 // request them asynchronously.
92 [[AppDelegate instance].debugger fetchProperty:self.fullName forNode:self];
93 }
94 return children;
95 }
96
97 - (BOOL)isLeaf
98 {
99 return (self.childCount == 0);
100 }
101
102 - (NSString*)displayType
103 {
104 if (self.className != nil) {
105 return [NSString stringWithFormat:@"%@ (%@)", self.className, self.type];
106 }
107 return self.type;
108 }
109
110 @end