After much work, we can now link to libssh2.a
[printdrop.git] / Source / AppController.m
1 /*
2 * PrintDrop
3 * Copyright (c) 2008, 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 "AppController.h"
18 #import <libssh2.h>
19 #import <libssh2_sftp.h>
20 #import <sys/socket.h>
21 #import <arpa/inet.h>
22
23 @interface AppController (Private)
24
25 //- (BOOL)uploadFile;
26 - (void)setStatus:(NSString *)msg isError:(BOOL)error;
27
28 @end
29
30
31 @implementation AppController
32
33 /**
34 * Set up the printer list
35 */
36 - (id)init
37 {
38 if (self = [super init])
39 {
40 printers = [NSMutableArray array];
41
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]];
45
46 values = [NSArray arrayWithObjects:@"Single-Sided, Stapled $0.10", @"pubps", nil];
47 [printers addObject:[NSDictionary dictionaryWithObjects:values forKeys:keys]];
48
49 values = [NSArray arrayWithObjects:@"Double-Sided, Not Stapled $0.05", @"publpns", nil];
50 [printers addObject:[NSDictionary dictionaryWithObjects:values forKeys:keys]];
51
52 values = [NSArray arrayWithObjects:@"Single-Sided, Not Stapled $0.10", @"pubpsns", nil];
53 [printers addObject:[NSDictionary dictionaryWithObjects:values forKeys:keys]];
54 }
55 return self;
56 }
57
58 /**
59 * Sets the status text
60 */
61 - (void)setStatus:(NSString *)msg isError:(BOOL)error
62 {
63 [status setStringValue:msg];
64 if (error)
65 {
66 [status setTextColor:[NSColor redColor]];
67 [progress stopAnimation:self];
68 }
69 else
70 {
71 [status setTextColor:[NSColor blackColor]];
72 }
73 }
74
75 /**
76 * Sends an item to the printer
77 */
78 - (IBAction)print:(id)sender
79 {
80 [progress startAnimation:self];
81 [progress setHidden:NO];
82 [status setHidden:NO];
83
84 NSString *printer = [[printersController selection] valueForKey:@"unixName"];
85
86 NSLog(@"printer = %@", printer);
87 LIBSSH2_SESSION *ssh = libssh2_session_init();
88 if (ssh == NULL)
89 {
90 return [self setStatus:@"Failed to initialize SSH context" isError:YES];
91 }
92
93 struct sockaddr_in sin;
94 int sock = socket(AF_INET, SOCK_STREAM, 0);
95 sin.sin_port = htons(22);
96 in_addr_t t = inet_addr("acs.bu.edu");
97 sin.sin_addr.s_addr = inet_addr("acs.bu.edu");
98 sin.sin_family = AF_INET;
99 if (connect(sock, (struct sockaddr *)(&sin), sizeof(struct sockaddr_in)) != 0)
100 {
101 return [self setStatus:@"Could not connect to acs.bu.edu" isError:YES];
102 }
103
104 close(sock);
105 }
106
107 @end