1 2 3#include "SetupWindow.h" 4 5#include <stdio.h> 6#include <stdlib.h> 7 8#include <Alert.h> 9#include <Button.h> 10#include <Directory.h> 11#include <NetEndpoint.h> 12#include <Rect.h> 13#include <String.h> 14#include <TextControl.h> 15#include <View.h> 16 17#define DLG_WIDTH 370 18#define DLG_HEIGHT 100 19 20#define BUTTON_WIDTH 70 21#define BUTTON_HEIGHT 20 22 23#define SERVER_H 10 24#define SERVER_V 10 25#define SERVER_WIDTH (DLG_WIDTH - SERVER_H - SERVER_H) 26#define SERVER_HEIGHT 20 27#define SERVER_TEXT "Printer address" 28 29#define QUEUE_H 10 30#define QUEUE_V SERVER_V + SERVER_HEIGHT + 2 31#define QUEUE_WIDTH (DLG_WIDTH - QUEUE_H - QUEUE_H) 32#define QUEUE_HEIGHT 20 33#define QUEUE_TEXT "Port" 34 35#define OK_H (DLG_WIDTH - BUTTON_WIDTH - 11) 36#define OK_V (DLG_HEIGHT - BUTTON_HEIGHT - 11) 37#define OK_TEXT "OK" 38 39#define CANCEL_H (OK_H - BUTTON_WIDTH - 12) 40#define CANCEL_V OK_V 41#define CANCEL_TEXT "Cancel" 42 43const BRect SERVER_RECT( 44 SERVER_H, 45 SERVER_V, 46 SERVER_H + SERVER_WIDTH, 47 SERVER_V + SERVER_HEIGHT); 48 49const BRect QUEUE_RECT( 50 QUEUE_H, 51 QUEUE_V, 52 QUEUE_H + QUEUE_WIDTH, 53 QUEUE_V + QUEUE_HEIGHT); 54 55const BRect OK_RECT( 56 OK_H, 57 OK_V, 58 OK_H + BUTTON_WIDTH, 59 OK_V + BUTTON_HEIGHT); 60 61const BRect CANCEL_RECT( 62 CANCEL_H, 63 CANCEL_V, 64 CANCEL_H + BUTTON_WIDTH, 65 CANCEL_V + BUTTON_HEIGHT); 66 67enum MSGS { 68 M_CANCEL = 1, 69 M_OK 70}; 71 72 73class SetupView : public BView { 74public: 75 SetupView(BRect, BDirectory* ); 76 virtual void AttachedToWindow(); 77 78 bool CheckSetup(); 79private: 80 81 BTextControl* fServerAddress; 82 BTextControl* fQueuePort; 83 BDirectory* fPrinterDirectory; 84}; 85 86 87SetupView::SetupView(BRect frame, BDirectory* directory) 88 : BView(frame, "", B_FOLLOW_ALL, B_WILL_DRAW), 89 fPrinterDirectory(directory) 90{ 91 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 92} 93 94 95void 96SetupView::AttachedToWindow() 97{ 98 float width = MAX(StringWidth(SERVER_TEXT), StringWidth(QUEUE_TEXT)) + 10; 99 100 /* server name box */ 101 102 fServerAddress = new BTextControl(SERVER_RECT, "", SERVER_TEXT, "<printer's hostname or address>", NULL); 103 AddChild(fServerAddress); 104 fServerAddress->SetDivider(width); 105 106 /* queue name box */ 107 108 fQueuePort = new BTextControl(QUEUE_RECT, "", QUEUE_TEXT, "9100", NULL); // 9100 is default HP JetDirect port number 109 AddChild(fQueuePort); 110 fQueuePort->SetDivider(width); 111 112 /* cancel */ 113 114 BButton* button = new BButton(CANCEL_RECT, "", CANCEL_TEXT, new BMessage(M_CANCEL)); 115 AddChild(button); 116 117 /* ok */ 118 119 button = new BButton(OK_RECT, "", OK_TEXT, new BMessage(M_OK)); 120 AddChild(button); 121 button->MakeDefault(true); 122} 123 124 125bool 126SetupView::CheckSetup() 127{ 128 if (*fServerAddress->Text() && *fQueuePort->Text()) { 129 BNetEndpoint* ep = new BNetEndpoint(SOCK_STREAM); 130 if (ep->InitCheck() == B_NO_ERROR) { 131 uint16 port = atoi(fQueuePort->Text()); 132 133 if (! port) 134 port = 9100; 135 136 if (ep->Connect(fServerAddress->Text(), port) != B_OK) { 137 BString text; 138 text << "Failed to connect to " << fServerAddress->Text() << ":" << (int) port << "!"; 139 BAlert* alert = new BAlert("", text.String(), "OK"); 140 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 141 alert->Go(); 142 return false; 143 }; 144 145 char str[256]; 146 sprintf(str, "%s:%d", fServerAddress->Text(), port); 147 fPrinterDirectory->WriteAttr("transport_address", B_STRING_TYPE, 148 0, str, strlen(str) + 1); 149 return true; 150 }; 151 }; 152 153 BAlert* alert = new BAlert("", "Please input parameters.", "OK"); 154 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 155 alert->Go(); 156 return false; 157} 158 159 160// #pragma mark - 161 162 163SetupWindow::SetupWindow(BDirectory* printerDirectory) 164 : BWindow(BRect(100, 100, 100 + DLG_WIDTH, 100 + DLG_HEIGHT), 165 "HP JetDirect Setup", B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL, 166 B_NOT_RESIZABLE | B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE 167 | B_CLOSE_ON_ESCAPE) 168{ 169 fResult = 0; 170 171 Lock(); 172 SetupView* view = new SetupView(Bounds(), printerDirectory); 173 AddChild(view); 174 Unlock(); 175 176 fExitSem = create_sem(0, "SetupWindowSem"); 177} 178 179 180bool 181SetupWindow::QuitRequested() 182{ 183 fResult = B_ERROR; 184 release_sem(fExitSem); 185 return true; 186} 187 188 189void 190SetupWindow::MessageReceived(BMessage* msg) 191{ 192 bool success; 193 194 switch (msg->what) { 195 case M_OK: 196 Lock(); 197 success = ((SetupView*)ChildAt(0))->CheckSetup(); 198 Unlock(); 199 if (success) { 200 fResult = B_NO_ERROR; 201 release_sem(fExitSem); 202 } 203 break; 204 205 case M_CANCEL: 206 fResult = B_ERROR; 207 release_sem(fExitSem); 208 break; 209 210 default: 211 BWindow::MessageReceived(msg); 212 break; 213 } 214} 215 216 217int 218SetupWindow::Go() 219{ 220 Show(); 221 acquire_sem(fExitSem); 222 delete_sem(fExitSem); 223 int value = fResult; 224 Lock(); 225 Quit(); 226 return value; 227} 228 229 230