PageRenderTime 45ms CodeModel.GetById 10ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 1ms

/.attic/sshfs-gui/AppController.m

http://macfuse.googlecode.com/
Objective C | 190 lines | 135 code | 31 blank | 24 comment | 14 complexity | 121f35172e8538c8621f0744bb0425dd MD5 | raw file
  1// Copyright (C) 2007 Google Inc.
  2// 
  3// Licensed under the Apache License, Version 2.0 (the "License");
  4// you may not use this file except in compliance with the License.
  5// You may obtain a copy of the License at
  6// 
  7//      http://www.apache.org/licenses/LICENSE-2.0
  8// 
  9// Unless required by applicable law or agreed to in writing, software
 10// distributed under the License is distributed on an "AS IS" BASIS,
 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12// See the License for the specific language governing permissions and
 13// limitations under the License.
 14
 15#import "AppController.h"
 16#import "DiskImageUtilities.h"
 17
 18#import <Cocoa/Cocoa.h>
 19#import "GoogleShared_SystemVersion.h"
 20
 21#include <stdlib.h>
 22#include <sys/param.h>
 23#include <sys/mount.h>
 24
 25#define kRecentCount    10 // NSDocumentController's _recentsLimit ivar
 26#define kRecentServers  @"recentservers"
 27#define kServerName     @"server"
 28#define kUsernameName   @"username"
 29#define kPathName       @"pathname"
 30
 31@implementation AppController
 32
 33- (void)applicationWillFinishLaunching:(NSNotification *)notification {
 34  [DiskImageUtilities handleApplicationLaunchFromReadOnlyDiskImage];
 35}
 36
 37- (void)applicationDidFinishLaunching:(NSNotification *)notification {
 38  [self showConnect:self];
 39}
 40
 41- (IBAction)showConnect:(id)sender {
 42  int connectResult = [NSApp runModalForWindow:connectPanel_];
 43  
 44  if (connectResult == NSRunAbortedResponse) {
 45    [connectPanel_ orderOut:self];
 46    return;
 47  }
 48  
 49  NSString *server   = [serverField_   stringValue];
 50  NSString *username = [usernameField_ stringValue];
 51  NSString *path     = [pathField_     stringValue];
 52  [connectPanel_ orderOut:self];
 53  [self sshConnect:server username:username path:path];
 54}
 55
 56- (IBAction)connectCancel:(id)sender {
 57  [NSApp abortModal];
 58}
 59
 60- (IBAction)connectOK:(id)sender {
 61  [NSApp stopModal];
 62}
 63
 64- (void)sshConnect:(NSString *)server
 65          username:(NSString *)username
 66              path:(NSString *)path {
 67  
 68  // create a folder in /Volumes
 69  NSFileManager *fileManager = [NSFileManager defaultManager];
 70  NSString *mountpoint = [NSString stringWithFormat:@"/Volumes/%@", server];
 71  int tries = 0;
 72  while ([fileManager fileExistsAtPath:mountpoint])
 73    mountpoint = [NSString stringWithFormat:@"/Volumes/%@ %d", server, ++tries];
 74  [fileManager createDirectoryAtPath:mountpoint attributes:nil];
 75  
 76  // setup for task
 77  NSString *askPassPath = [[NSBundle mainBundle] pathForResource:@"askpass"
 78                                                          ofType:@""];
 79  NSString *sshfsPath = [[NSBundle mainBundle] pathForResource:@"sshfs-static"
 80                                                        ofType:@""];
 81  if ([GoogleShared_SystemVersion isLeopard]) {
 82      sshfsPath = [sshfsPath stringByAppendingString:@"-10.5"];
 83  }
 84  NSDictionary *sshfsEnv = [NSDictionary dictionaryWithObjectsAndKeys:
 85    @"NONE", @"DISPLAY", // I know "NONE" is bogus; I just need something
 86    askPassPath, @"SSH_ASKPASS",
 87    nil];
 88    
 89  NSArray *sshfsArgs = [NSArray arrayWithObjects:
 90    [NSString stringWithFormat:@"%@@%@:%@", username, server, path],
 91    [NSString stringWithFormat:@"-ovolname=%@", server],
 92    @"-oping_diskarb",
 93    @"-oreconnect",
 94    @"-oNumberOfPasswordPrompts=1",
 95    mountpoint,
 96    nil
 97    ];
 98  
 99  // fire it off
100  NSTask *sshfsTask = [[[NSTask alloc] init] autorelease];
101  [sshfsTask setLaunchPath:sshfsPath];
102  [sshfsTask setArguments:sshfsArgs];
103  [sshfsTask setEnvironment:sshfsEnv];
104  
105  [sshfsTask launch];
106  [sshfsTask waitUntilExit];
107  if ([sshfsTask terminationStatus]) {
108    [fileManager removeFileAtPath:mountpoint handler:nil];
109  }
110  else {
111    [self addToRecents:server username:username path:path];
112  }
113}
114
115- (IBAction)clearRecents:(id)sender {
116  NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
117  [userDefaults setObject:[NSArray array] forKey:kRecentServers];
118}
119
120- (void)addToRecents:(NSString *)server
121            username:(NSString *)username
122                path:(NSString *)path {
123  // make the item
124  NSDictionary *newRecentItem = [NSDictionary dictionaryWithObjectsAndKeys:
125    server, kServerName, username, kUsernameName, path, kPathName, nil];
126  
127  // get our list
128  NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
129  NSArray *oldRecents = [userDefaults arrayForKey:kRecentServers];
130  if (!oldRecents)
131    oldRecents = [NSArray array];
132  
133  // we don't want dups, so remove it if it's already there
134  NSMutableArray *newRecents = [NSMutableArray arrayWithArray:oldRecents];
135  [newRecents removeObject:newRecentItem];
136  
137  // insert and save
138  [newRecents insertObject:newRecentItem atIndex:0];
139  if ([newRecents count] > kRecentCount)
140    [newRecents removeObjectsInRange:
141      NSMakeRange(kRecentCount, [newRecents count] - kRecentCount)];
142  
143  [userDefaults setObject:newRecents forKey:kRecentServers];
144}
145
146- (void)menuNeedsUpdate:(NSMenu *)menu {
147  // pull out all the old menu items
148  NSMenuItem *item = [menu itemAtIndex:0];
149  while ([item representedObject] != nil || [item isSeparatorItem]) {
150    [menu removeItemAtIndex:0];
151    item = [menu itemAtIndex:0];
152  }
153  
154  // get the new items
155  NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
156  NSArray *recents = [userDefaults arrayForKey:kRecentServers];
157  if (!recents)
158    recents = [NSArray array];
159  
160  // put them in
161  for (int i=0; i < [recents count]; ++i) {
162    NSDictionary *recent = [recents objectAtIndex:i];
163    NSString *recentName = [NSString stringWithFormat:@"%@ %C %@ %C %@",
164      [recent objectForKey:kServerName], 0x00B7,
165      [recent objectForKey:kUsernameName], 0x00B7,
166      [recent objectForKey:kPathName]];
167    NSMenuItem *recentMenuItem =
168      [[[NSMenuItem alloc] initWithTitle:recentName
169                                  action:@selector(connectToRecent:)
170                           keyEquivalent:@""] autorelease];
171    [recentMenuItem setTarget:self];
172    [recentMenuItem setRepresentedObject:recent];
173    
174    [menu insertItem:recentMenuItem atIndex:i];
175  }
176  
177  // put in a separator if we put any items in
178  if ([recents count]) {
179    [menu insertItem:[NSMenuItem separatorItem] atIndex:[recents count]];
180  }
181}
182
183- (void)connectToRecent:(id)sender {
184  NSDictionary *connect = [sender representedObject];
185  [self sshConnect:[connect objectForKey:kServerName]
186          username:[connect objectForKey:kUsernameName]
187              path:[connect objectForKey:kPathName]];
188}
189
190@end