-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrayMenu.m
156 lines (115 loc) · 4.74 KB
/
TrayMenu.m
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// TrayMenu.m
// SSHLauncher
//
// Created by Martin Jansen on 15.05.10.
// Copyright 2010 Martin Jansen. All rights reserved.
//
#import "TrayMenu.h"
#import "SSHProfileMenuItem.h"
#import "Profile.h"
#import "ProfileReader.h"
#import "Section.h"
#import "UKKQueue.h"
#import "NSFileManager+MJAdditions.h"
#pragma mark private method definitions
@interface TrayMenu (PRIVATE)
- (void) setupFileObservers;
- (void) configurationDidChange:(id) sender;
@end
#pragma mark -
#pragma mark Class implementation
@implementation TrayMenu
- (void) applicationDidFinishLaunching:(NSNotification*) notification
{
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
[statusItem setMenu:[self createMenu]];
[statusItem setHighlightMode:YES];
[statusItem setImage:[NSImage imageNamed:@"application_osx_terminal.png"]];
[self setupFileObservers];
}
- (void) actionQuit:(id) sender
{
[NSApp terminate:sender];
}
- (void) actionSSH:(SSHProfileMenuItem*) sender
{
Profile* profile = [sender profile];
NSString *ssh = [NSString stringWithFormat:@"ssh %@ -p %d %@@%@ %@", [profile.command length] ? @"-t" : @"", profile.port, profile.username, profile.hostname, profile.command];
NSString* command = [NSString stringWithFormat:@"activate application \"Terminal\"\n"
"tell application \"System Events\"\n"
" keystroke \"t\" using {command down}\n"
"end tell\n"
"tell application \"Terminal\" to do script \"%@\" in the last tab of window 1\n", ssh];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:command];
[script compileAndReturnError:nil];
[script executeAndReturnError:nil];
});
}
- (NSMenu*) createMenuFromSection:(Section*) aSection withZone:(NSZone*) aMenuZone
{
NSMenu *menu = [NSMenu allocWithZone:aMenuZone];
NSImage* connectionImage = [NSImage imageNamed:@"application_osx_terminal.png"];
for (int z = 0; z < [aSection.subsections count]; z++) {
Section* section = [aSection.subsections objectAtIndex:z];
NSMenu* submenu = [self createMenuFromSection:section withZone:aMenuZone];
NSMenuItem* submenuItem = [[NSMenuItem alloc] init];
submenuItem.title = section.title;
[submenuItem setSubmenu:submenu];
[menu addItem:submenuItem];
[submenuItem release];
}
if ([aSection.subsections count] > 0 && [aSection.profiles count] > 0) {
[menu addItem:[NSMenuItem separatorItem]];
}
for (int i = 0; i < [aSection.profiles count]; i++) {
Profile* connection = [aSection.profiles objectAtIndex:i];
SSHProfileMenuItem* menuitem = [[SSHProfileMenuItem allocWithZone:aMenuZone] init];
[menuitem setProfile:connection];
[menuitem setTitle:connection.label];
[menuitem setTarget:self];
[menuitem setAction:@selector(actionSSH:)];
[menuitem setImage:connectionImage];
[menu addItem:menuitem];
[menuitem release];
}
return [menu autorelease];
}
- (NSMenu*) createMenu
{
NSZone *menuZone = [NSMenu menuZone];
ProfileReader* reader = [[ProfileReader alloc] init];
Section* root = [reader getProfiles];
[reader release];
NSMenu* menu = [self createMenuFromSection:root withZone:menuZone];
[menu addItem: [NSMenuItem separatorItem]];
NSMenuItem* quitMenuItem = [menu addItemWithTitle:@"Quit"
action:@selector(actionQuit:)
keyEquivalent:@"q"];
[quitMenuItem setToolTip:@"Quit application"];
[quitMenuItem setTarget:self];
return menu;
}
- (void) setupFileObservers
{
UKKQueue* queue = [UKKQueue sharedFileWatcher];
NSString *path = [[NSFileManager defaultManager] applicationSupportDirectory];
/* Depending on which application one uses to edit the property list, UKKQueue
* either detects a write on the directory or the file itself.
*/
[queue addPathToQueue:path];
[queue addPathToQueue:[NSString stringWithFormat:@"%@/Profiles.plist", path]];
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
NSNotificationCenter* notificationCenter = [workspace notificationCenter];
[notificationCenter addObserver:self
selector:@selector(configurationDidChange:)
name:UKFileWatcherWriteNotification
object:nil];
}
- (void) configurationDidChange: (id) sender
{
NSMenu* newMenu = [self createMenu];
[statusItem setMenu:newMenu];
}
@end