1 | //␊ |
2 | // TableViewsController.mm␊ |
3 | // ChameleonPrefPane␊ |
4 | //␊ |
5 | // Created by Rekursor on 1/29/10.␊ |
6 | //␊ |
7 | ␊ |
8 | #import "ChameleonPrefPane.h"␊ |
9 | #import "ChameleonPropertyList.h"␊ |
10 | #import "PreferencesControllerBase.H"␊ |
11 | #import "TableViewsController.h"␊ |
12 | #import "BootSetupController.h"␊ |
13 | #import "ShellProcess.h"␊ |
14 | ␊ |
15 | #import <SecurityFoundation/SFAuthorization.h>␊ |
16 | #import <SecurityInterface/SFAuthorizationView.h>␊ |
17 | #include <string>␊ |
18 | #include <vector>␊ |
19 | ␊ |
20 | //--------------------------------------------------------------------------␊ |
21 | // MenuEntry definitions␊ |
22 | //--------------------------------------------------------------------------␊ |
23 | typedef enum { ␊ |
24 | ␉PaneBootFrom, PaneBootSetup, PaneBootFlags, PanePeripherals, ␊ |
25 | ␉PaneAdvanced, PaneEfiInject, PaneSmbios, PaneAbout␊ |
26 | } MenuEntryPane;␊ |
27 | ␊ |
28 | struct MenuEntry␊ |
29 | {␉␊ |
30 | ␉NSString* title;␊ |
31 | ␉MenuEntryPane pane;␊ |
32 | ␉NSImage**␉img;␊ |
33 | };␊ |
34 | ␊ |
35 | ␊ |
36 | static NSImage*␉␉␉␉␉mImgPaneBootFrom; ␊ |
37 | static NSImage*␉␉␉␉␉mImgPaneBootSetup;␊ |
38 | static NSImage*␉␉␉␉␉mImgPaneBootFlags;␊ |
39 | static NSImage*␉␉␉␉␉mImgPanePeripherals;␊ |
40 | static NSImage*␉␉␉␉␉mImgPaneAdvanced;␊ |
41 | static NSImage*␉␉␉␉␉mImgPaneEfiInject;␊ |
42 | static NSImage*␉␉␉␉␉mImgPaneSmbios;␊ |
43 | static NSImage*␉␉␉␉␉mImgPaneAbout;␊ |
44 | ␊ |
45 | ␊ |
46 | static const MenuEntry sMenuList[] =␊ |
47 | {␊ |
48 | ␉{@"Boot_From", PaneBootFrom, &mImgPaneBootFrom}, ␊ |
49 | ␉{@"Boot_Setup",␉PaneBootSetup, &mImgPaneBootSetup},␊ |
50 | ␉{@"Boot_Flags", PaneBootFlags, &mImgPaneBootFlags}, ␊ |
51 | ␉{@"Peripherals", PanePeripherals, &mImgPanePeripherals}, ␊ |
52 | ␉{@"Efi_Inject", PaneEfiInject, &mImgPaneEfiInject}, ␊ |
53 | ␉{@"Bios", PaneSmbios, &mImgPaneSmbios},␊ |
54 | ␉{@"Advanced", PaneAdvanced, &mImgPaneAdvanced}, ␊ |
55 | ␉{@"About", PaneAbout, &mImgPaneAbout}␊ |
56 | };␊ |
57 | const int iMenuListSize = sizeof(sMenuList)/ sizeof(MenuEntry);␊ |
58 | ␊ |
59 | static TableViewsController* _instance; // Singleton DP def.␊ |
60 | ␊ |
61 | ␊ |
62 | //--------------------------------------------------------------------------␊ |
63 | @implementation CenteredTextFieldCell␊ |
64 | //--------------------------------------------------------------------------␊ |
65 | - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView␊ |
66 | {␊ |
67 | NSSize contentSize = [self cellSize];␊ |
68 | cellFrame.origin.y += (cellFrame.size.height - contentSize.height) / 2.0;␊ |
69 | cellFrame.size.height = contentSize.height;␊ |
70 | ␉␊ |
71 | ␉NSAttributedString *drawString = [self attributedStringValue];␊ |
72 | ␉␊ |
73 | ␉NSRange range;␊ |
74 | ␉NSDictionary *attributes = [drawString attributesAtIndex:0 effectiveRange:&range];␊ |
75 | ␉␊ |
76 | ␉float maxWidth = cellFrame.size.width;␊ |
77 | ␉float stringWidth = [drawString size].width;␊ |
78 | ␉␊ |
79 | ␉if (maxWidth < stringWidth)␊ |
80 | ␉{␊ |
81 | ␉␉int i;␊ |
82 | ␉␉␊ |
83 | ␉␉for (i = 0;i <= [drawString length];i++)␊ |
84 | ␉␉{␊ |
85 | ␉␉␉if ([[drawString attributedSubstringFromRange:NSMakeRange(0,i)]size].width >= maxWidth)␊ |
86 | ␉␉␉{␉␊ |
87 | ␉␉␉␉drawString = [[NSMutableAttributedString alloc] initWithString:␊ |
88 | ␉␉␉␉␉␉␉ [[[drawString attributedSubstringFromRange:NSMakeRange(0,i-3)]string] stringByAppendingString:@"..."]attributes:attributes];␊ |
89 | ␉␉␉␉[drawString autorelease];␊ |
90 | ␉␉␉}␊ |
91 | ␉␉}␊ |
92 | ␉}␊ |
93 | ␉␊ |
94 | ␉[drawString drawInRect:cellFrame];␊ |
95 | ␉␊ |
96 | }␊ |
97 | ␊ |
98 | @end␊ |
99 | ␊ |
100 | //--------------------------------------------------------------------------␊ |
101 | @implementation TableViewsController␊ |
102 | //--------------------------------------------------------------------------␊ |
103 | - (id) init␊ |
104 | {␊ |
105 | ␉[super init];␊ |
106 | ␉_instance = self;␊ |
107 | ␉␊ |
108 | ␉currentRowSel = -1;␊ |
109 | ␉// set the image to display the current file being played␊ |
110 | ␉mMacOSXImage = [self getImageResource: @"MacOSX" ofType: @"png"];␊ |
111 | ␉mWindowsImage = [self getImageResource: @"Windows" ofType: @"png"];␊ |
112 | ␉mLinuxImage = [self getImageResource: @"Linux" ofType: @"png"];␊ |
113 | ␉mCDROMImage = [self getImageResource: @"CDROM" ofType: @"png"];␊ |
114 | ␉mUnknownImage = [self getImageResource: @"Chameleon" ofType: @"tiff"];␊ |
115 | ␉␊ |
116 | ␉mImgPaneBootFrom = [self getImageResource:@"disk" ofType:@"png"]; ␊ |
117 | ␉mImgPaneBootSetup = [self getImageResource:@"wrench" ofType:@"png"];␊ |
118 | ␉mImgPaneBootFlags = [self getImageResource:@"flag" ofType:@"png"]; ␊ |
119 | ␉mImgPanePeripherals = [self getImageResource:@"plug" ofType:@"png"]; ␊ |
120 | ␉mImgPaneAdvanced = [self getImageResource:@"advanced" ofType:@"png"];␊ |
121 | ␉mImgPaneEfiInject = [self getImageResource:@"syringe" ofType:@"png"]; ␊ |
122 | ␉mImgPaneSmbios = [self getImageResource:@"chip" ofType:@"png"];␊ |
123 | ␉mImgPaneAbout = [self getImageResource:@"chamsmall" ofType:@"png"];␊ |
124 | ␊ |
125 | ␉return self;␊ |
126 | }␊ |
127 | ␊ |
128 | //--------------------------------------------------------------------------␊ |
129 | // Singleton impl␊ |
130 | + (TableViewsController*) instance␊ |
131 | {␊ |
132 | ␉return _instance;␊ |
133 | }␊ |
134 | //--------------------------------------------------------------------------␊ |
135 | + (bool) isUnlocked␊ |
136 | { ␊ |
137 | ␉return [[ChameleonPrefPane instance] isUnlocked];␊ |
138 | }␊ |
139 | ␊ |
140 | //--------------------------------------------------------------------------␊ |
141 | + (AuthorizationRef) authorization␊ |
142 | { ␊ |
143 | ␉return [TableViewsController isUnlocked] ?␊ |
144 | ␉[ [ [ChameleonPrefPane instance]->authView authorization] authorizationRef] : NULL;␊ |
145 | }␊ |
146 | //--------------------------------------------------------------------------␊ |
147 | - (NSTableView*) partitionsTable␊ |
148 | {␊ |
149 | ␉return mPartitionsTable;␊ |
150 | }␊ |
151 | //--------------------------------------------------------------------------␊ |
152 | - (id) getImageResource: (NSString *) str ofType: (NSString*) sType␊ |
153 | {␊ |
154 | ␉NSImage * img=nil;␊ |
155 | ␉if(!str) return nil;␊ |
156 | ␉NSBundle * b = [NSBundle bundleForClass:[self class]];␊ |
157 | ␉NSString* sRes = [b pathForResource: str ofType:sType ];␊ |
158 | ␉img = [[NSImage alloc] initWithContentsOfFile: sRes];␊ |
159 | ␉return img;␊ |
160 | }␊ |
161 | ␊ |
162 | //--------------------------------------------------------------------------␊ |
163 | - (void)tableViewSelectionDidChange:(NSNotification *)notification {␊ |
164 | ␉␊ |
165 | ␉NSTableView* tv= [notification object];␊ |
166 | ␉if (tv == mPartitionsTable)␊ |
167 | ␉{␊ |
168 | ␉␉if ([tv selectedRow] != currentRowSel)␊ |
169 | ␉␉{␊ |
170 | ␉␉␉currentRowSel = [tv selectedRow];␊ |
171 | ␉␉␉if (currentRowSel>=0)␊ |
172 | ␉␉␉{␊ |
173 | ␉␉␉␉const std::vector<PartitionInfo>& partInfo = PartitionExtractor::instance().partList();␊ |
174 | ␉␉␉␉char hd[7+1]="hd(n,m)";␊ |
175 | ␉␉␉␉hd[3]= ('0'+partInfo[currentRowSel].disk());␊ |
176 | ␉␉␉␉hd[5]= ('0'+partInfo[currentRowSel].partition());␉␊ |
177 | ␉␉␉␉AuthorizationRef auth= [TableViewsController authorization];␊ |
178 | ␉␉␉␉if(BootProp::instance().setStringForKey(kDefaultPartition, hd))␊ |
179 | ␉␉␉␉␉BootProp::instance().save(auth);␊ |
180 | ␉␉␉␉[ PreferencesControllerBase loadOptionsFromBootFile];␊ |
181 | ␉␉␉}␊ |
182 | ␉␉␉else␊ |
183 | ␉␉␉{ // no line selected␊ |
184 | ␉␉␉␉BootProp::instance().removeKeyAndValue(kDefaultPartition);␊ |
185 | ␉␉␉␉[ PreferencesControllerBase loadOptionsFromBootFile];␊ |
186 | ␉␉␉␉␊ |
187 | ␉␉␉}␊ |
188 | ␉␉}␊ |
189 | ␉}␊ |
190 | ␉else if (tv == mTabViewPanesSelect)␊ |
191 | ␉{␊ |
192 | ␉␉int selected = [tv selectedRow];␊ |
193 | ␉␉if(selected>=0)␊ |
194 | ␉␉{␊ |
195 | ␉␉␉switch (sMenuList[selected].pane) ␊ |
196 | ␉␉␉{␊ |
197 | ␉␉␉␉case PaneBootFrom:␊ |
198 | ␉␉␉␉␉[mTabViewPanes selectTabViewItem: mBootFrom];␊ |
199 | ␉␉␉␉␉break;␊ |
200 | ␉␉␉␉case PaneBootSetup:␊ |
201 | ␉␉␉␉␉[mTabViewPanes selectTabViewItem: mBootSetup];␊ |
202 | ␉␉␉␉␉break;␊ |
203 | ␉␉␉␉case PaneBootFlags:␊ |
204 | ␉␉␉␉␉[mTabViewPanes selectTabViewItem: mBootFlags];␊ |
205 | ␉␉␉␉␉break;␊ |
206 | ␉␉␉␉case PanePeripherals:␊ |
207 | ␉␉␉␉␉[mTabViewPanes selectTabViewItem: mPeripherals];␊ |
208 | ␉␉␉␉␉break;␊ |
209 | ␉␉␉␉case PaneEfiInject:␊ |
210 | ␉␉␉␉␉[mTabViewPanes selectTabViewItem: mEfiInject];␊ |
211 | ␉␉␉␉␉break;␊ |
212 | ␉␉␉␉case PaneAdvanced:␊ |
213 | ␉␉␉␉␉[mTabViewPanes selectTabViewItem: mAdvanced];␊ |
214 | ␉␉␉␉␉break;␊ |
215 | ␉␉␉␉case PaneSmbios:␊ |
216 | ␉␉␉␉␉[mTabViewPanes selectTabViewItem: mSmbios];␊ |
217 | ␉␉␉␉␉break;␊ |
218 | ␉␉␉␉case PaneAbout:␊ |
219 | ␉␉␉␉␉[mTabViewPanes selectTabViewItem: mAbout];␊ |
220 | ␉␉␉␉␉break;␊ |
221 | ␉␉␉␉default:␊ |
222 | ␉␉␉␉␉break;␊ |
223 | ␉␉␉}␊ |
224 | ␉␉␉[mTabViewPanes setNeedsDisplay: true ];␊ |
225 | ␉␉}␊ |
226 | ␉}␊ |
227 | }␊ |
228 | ␊ |
229 | //--------------------------------------------------------------------------␊ |
230 | - (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView␊ |
231 | {␊ |
232 | ␉int size=0;␊ |
233 | ␉if (tableView == mPartitionsTable)␊ |
234 | ␉␉size = PartitionExtractor::instance().partList().size();␊ |
235 | ␉else␊ |
236 | ␉␉size = iMenuListSize;␊ |
237 | ␉return size;␊ |
238 | }␊ |
239 | ␊ |
240 | //--------------------------------------------------------------------------␊ |
241 | - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row␊ |
242 | {␊ |
243 | ␉id ret=nil;␊ |
244 | ␉␊ |
245 | ␉const std::vector<PartitionInfo>& partInfo = PartitionExtractor::instance().partList();␊ |
246 | ␉␊ |
247 | ␉if (tableColumn == mPartitionImgColumn) ␊ |
248 | ␉{␊ |
249 | ␉␉switch(partInfo[row].imageIndexFromFs())␊ |
250 | ␉␉{␊ |
251 | ␉␉␉case 0:␊ |
252 | ␉␉␉␉ret = mMacOSXImage;␊ |
253 | ␉␉␉␉break;␊ |
254 | ␉␉␉case 1:␊ |
255 | ␉␉␉␉ret = mWindowsImage;␊ |
256 | ␉␉␉␉break;␊ |
257 | ␉␉␉case 2:␊ |
258 | ␉␉␉␉ret = mLinuxImage;␊ |
259 | ␉␉␉␉break;␊ |
260 | ␉␉␉default:␊ |
261 | ␉␉␉␉ret = mUnknownImage;␊ |
262 | ␉␉␉␉break;␊ |
263 | ␉␉␉␉␊ |
264 | ␉␉}␊ |
265 | ␉}␊ |
266 | ␉else if (tableColumn == mFileSystemColumn) ␊ |
267 | ␉{␊ |
268 | ␉␉ret = [NSString stringWithFormat: @"%s", partInfo[row].cfsType() ];␊ |
269 | ␉}␊ |
270 | ␉else if (tableColumn == mPartitionNameColumn) ␊ |
271 | ␉{␊ |
272 | ␉␉ret = [NSString stringWithFormat: @"%s", partInfo[row].clabel()␉];␊ |
273 | ␉}␊ |
274 | ␉else if (tableColumn == mPartitionIDColumn) ␊ |
275 | ␉{␊ |
276 | ␉␉ret= [NSString stringWithFormat: @"hd(%d,%d)", ␊ |
277 | ␉␉␉ partInfo[row].disk(),␊ |
278 | ␉␉␉ partInfo[row].partition()␊ |
279 | ␉␉␉ ];␊ |
280 | ␉} ␊ |
281 | ␉// menu tv handling␊ |
282 | ␉else if (tableColumn == mMenuName) ␊ |
283 | ␉{␊ |
284 | ␉␉NSBundle * b = [NSBundle bundleForClass:[self class]];␊ |
285 | ␉␉ret = NSLocalizedStringFromTableInBundle(sMenuList[row].title, nil, b, @"comment");␊ |
286 | ␉}␉␊ |
287 | ␉else if (tableColumn == mMenuIcon) ␊ |
288 | ␉{␊ |
289 | ␉␉ret = *sMenuList[row].img;␊ |
290 | ␉}␉␊ |
291 | ␉return ret;␊ |
292 | }␊ |
293 | //--------------------------------------------------------------------------␊ |
294 | ␊ |
295 | @end␊ |
296 | |