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 43 const BRect SERVER_RECT( 44 SERVER_H, 45 SERVER_V, 46 SERVER_H + SERVER_WIDTH, 47 SERVER_V + SERVER_HEIGHT); 48 49 const BRect QUEUE_RECT( 50 QUEUE_H, 51 QUEUE_V, 52 QUEUE_H + QUEUE_WIDTH, 53 QUEUE_V + QUEUE_HEIGHT); 54 55 const BRect OK_RECT( 56 OK_H, 57 OK_V, 58 OK_H + BUTTON_WIDTH, 59 OK_V + BUTTON_HEIGHT); 60 61 const BRect CANCEL_RECT( 62 CANCEL_H, 63 CANCEL_V, 64 CANCEL_H + BUTTON_WIDTH, 65 CANCEL_V + BUTTON_HEIGHT); 66 67 enum MSGS { 68 M_CANCEL = 1, 69 M_OK 70 }; 71 72 73 class SetupView : public BView { 74 public: 75 SetupView(BRect, BDirectory* ); 76 virtual void AttachedToWindow(); 77 78 bool CheckSetup(); 79 private: 80 81 BTextControl* fServerAddress; 82 BTextControl* fQueuePort; 83 BDirectory* fPrinterDirectory; 84 }; 85 86 87 SetupView::SetupView(BRect frame, BDirectory* directory) 88 : BView(frame, "", B_FOLLOW_ALL, B_WILL_DRAW), 89 fPrinterDirectory(directory) 90 { 91 SetViewUIColor(B_PANEL_BACKGROUND_COLOR); 92 } 93 94 95 void 96 SetupView::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 125 bool 126 SetupView::CheckSetup() 127 { 128 if (*fServerAddress->Text() && *fQueuePort->Text()) { 129 BNetEndpoint* ep = new(std::nothrow) BNetEndpoint(SOCK_STREAM); 130 if (ep != NULL && 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() 139 << ":" << (int) port << "!"; 140 BAlert* alert = new BAlert("", text.String(), "OK"); 141 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 142 alert->Go(); 143 delete ep; 144 return false; 145 }; 146 147 char str[256]; 148 sprintf(str, "%s:%d", fServerAddress->Text(), port); 149 fPrinterDirectory->WriteAttr("transport_address", B_STRING_TYPE, 150 0, str, strlen(str) + 1); 151 delete ep; 152 return true; 153 }; 154 delete ep; 155 }; 156 157 BAlert* alert = new BAlert("", "Please input parameters.", "OK"); 158 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 159 alert->Go(); 160 return false; 161 } 162 163 164 // #pragma mark - 165 166 167 SetupWindow::SetupWindow(BDirectory* printerDirectory) 168 : BWindow(BRect(100, 100, 100 + DLG_WIDTH, 100 + DLG_HEIGHT), 169 "HP JetDirect Setup", B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL, 170 B_NOT_RESIZABLE | B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE 171 | B_CLOSE_ON_ESCAPE) 172 { 173 fResult = 0; 174 175 Lock(); 176 SetupView* view = new SetupView(Bounds(), printerDirectory); 177 AddChild(view); 178 Unlock(); 179 180 fExitSem = create_sem(0, "SetupWindowSem"); 181 } 182 183 184 bool 185 SetupWindow::QuitRequested() 186 { 187 fResult = B_ERROR; 188 release_sem(fExitSem); 189 return true; 190 } 191 192 193 void 194 SetupWindow::MessageReceived(BMessage* msg) 195 { 196 bool success; 197 198 switch (msg->what) { 199 case M_OK: 200 Lock(); 201 success = ((SetupView*)ChildAt(0))->CheckSetup(); 202 Unlock(); 203 if (success) { 204 fResult = B_NO_ERROR; 205 release_sem(fExitSem); 206 } 207 break; 208 209 case M_CANCEL: 210 fResult = B_ERROR; 211 release_sem(fExitSem); 212 break; 213 214 default: 215 BWindow::MessageReceived(msg); 216 break; 217 } 218 } 219 220 221 int 222 SetupWindow::Go() 223 { 224 Show(); 225 acquire_sem(fExitSem); 226 delete_sem(fExitSem); 227 int value = fResult; 228 Lock(); 229 Quit(); 230 return value; 231 } 232 233 234