3 * Copyright (c) 2008, Blue Static <http://www.bluestatic.org>
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.
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.
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
17 #import "AppController.h"
19 #import <sys/socket.h>
23 @interface AppController (Private
)
26 - (void)setStatus
:(NSString
*)msg isError
:(BOOL)error
;
31 @implementation AppController
34 * Set up the printer list
38 if (self = [super init
])
40 printers
= [NSMutableArray array
];
42 NSArray
*values
= [NSArray arrayWithObjects
:@
"Double-Sided, Stapled $0.05", @
"publp", nil];
43 NSArray
*keys
= [NSArray arrayWithObjects
:@
"displayName", @
"unixName", nil];
44 [printers addObject
:[NSDictionary dictionaryWithObjects
:values forKeys
:keys
]];
46 values
= [NSArray arrayWithObjects
:@
"Single-Sided, Stapled $0.10", @
"pubps", nil];
47 [printers addObject
:[NSDictionary dictionaryWithObjects
:values forKeys
:keys
]];
49 values
= [NSArray arrayWithObjects
:@
"Double-Sided, Not Stapled $0.05", @
"publpns", nil];
50 [printers addObject
:[NSDictionary dictionaryWithObjects
:values forKeys
:keys
]];
52 values
= [NSArray arrayWithObjects
:@
"Single-Sided, Not Stapled $0.10", @
"pubpsns", nil];
53 [printers addObject
:[NSDictionary dictionaryWithObjects
:values forKeys
:keys
]];
59 * Sets the status text
61 - (void)setStatus
:(NSString
*)msg isError
:(BOOL)error
63 [status setStringValue
:msg
];
66 [status setTextColor
:[NSColor redColor
]];
67 [progress stopAnimation
:self];
71 [status setTextColor
:[NSColor blackColor
]];
76 * Sends an item to the printer
78 - (IBAction
)print
:(id)sender
80 [progress startAnimation
:self];
81 [progress setHidden
:NO
];
82 [status setHidden
:NO
];
84 NSString
*printer
= [[printersController selection
] valueForKey
:@
"unixName"];
88 [self setStatus
:@
"Connecting to acs.bu.edu" isError
:NO
];
89 struct sockaddr_in sin
;
90 int sock
= socket(AF_INET
, SOCK_STREAM
, 0);
91 sin.sin_port
= htons(22);
92 sin.sin_family
= AF_INET
;
94 struct hostent
*host
= gethostbyname("acs.bu.edu");
95 memcpy(&sin.sin_addr
, host
->h_addr_list
[0], host
->h_length
);
97 if (connect(sock
, (struct sockaddr
*)(&sin
), sizeof(struct sockaddr_in
)) != 0)
99 return [self setStatus
:@
"Could not connect to acs.bu.edu" isError
:YES
];
102 LIBSSH2_SESSION
*ssh
= libssh2_session_init();
105 return [self setStatus
:@
"Failed to initialize SSH context" isError
:YES
];
108 if (libssh2_session_startup(ssh
, sock
))
110 return [self setStatus
:@
"Could not tunnel over SSH" isError
:YES
];
113 if (libssh2_userauth_password(ssh
, [[username stringValue
] UTF8String
], [[password stringValue
] UTF8String
]))
115 [self setStatus
:@
"Bad username/password" isError
:YES
];
119 stat([[dragRegion filePath
] UTF8String
], &fileInfo
);
121 LIBSSH2_CHANNEL
*channel
= libssh2_scp_send(ssh
, "~/tempupload.pdf", 0755, (unsigned long)fileInfo.st_size
);
124 [self setStatus
:@
"Unable to open upload SCP session" isError
:YES
];
128 [self setStatus
:@
"Uploading file..." isError
:NO
];
130 localFile
= fopen([[dragRegion filePath
] UTF8String
], "r");
133 int numread
, numwrote
;
136 numread
= fread(buf
, 1, sizeof(buf
), localFile
);
145 numwrote
= libssh2_channel_write(channel
, pbuf
, numread
);
148 } while (numwrote
> 0);
152 [self setStatus
:@
"File uploaded!" isError
:NO
];
154 libssh2_channel_send_eof(channel
);
155 libssh2_channel_wait_eof(channel
);
156 libssh2_channel_wait_closed(channel
);
157 libssh2_channel_free(channel
);
161 libssh2_session_disconnect(ssh
, "Normal disconnect.");
162 libssh2_session_free(ssh
);
166 [progress stopAnimation
:self];