1 11 package org.eclipse.swt.browser; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.internal.*; 15 import org.eclipse.swt.internal.mozilla.*; 16 import org.eclipse.swt.widgets.*; 17 18 class PromptService { 19 XPCOMObject supports; 20 XPCOMObject promptService; 21 int refCount = 0; 22 23 public PromptService () { 24 createCOMInterfaces (); 25 } 26 27 int AddRef () { 28 refCount++; 29 return refCount; 30 } 31 32 void createCOMInterfaces () { 33 34 supports = new XPCOMObject (new int[] {2, 0, 0}) { 35 public int method0 (int [] args) {return QueryInterface (args[0], args[1]);} 36 public int method1 (int [] args) {return AddRef ();} 37 public int method2 (int [] args) {return Release ();} 38 }; 39 40 promptService = new XPCOMObject (new int[] {2, 0, 0, 3, 5, 4, 6, 10, 7, 8, 7, 7}) { 41 public int method0 (int [] args) {return QueryInterface (args[0], args[1]);} 42 public int method1 (int [] args) {return AddRef ();} 43 public int method2 (int [] args) {return Release ();} 44 public int method3 (int [] args) {return Alert (args[0], args[1], args[2]);} 45 public int method4 (int [] args) {return AlertCheck (args[0], args[1], args[2], args[3], args[4]);} 46 public int method5 (int [] args) {return Confirm (args[0], args[1], args[2], args[3]);} 47 public int method6 (int [] args) {return ConfirmCheck (args[0], args[1], args[2], args[3], args[4], args[5]);} 48 public int method7 (int [] args) {return ConfirmEx (args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);} 49 public int method8 (int [] args) {return Prompt (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);} 50 public int method9 (int [] args) {return PromptUsernameAndPassword (args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);} 51 public int method10 (int [] args) {return PromptPassword (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);} 52 public int method11 (int [] args) {return Select (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);} 53 }; 54 } 55 56 void disposeCOMInterfaces () { 57 if (supports != null) { 58 supports.dispose (); 59 supports = null; 60 } 61 if (promptService != null) { 62 promptService.dispose (); 63 promptService = null; 64 } 65 } 66 67 int getAddress () { 68 return promptService.getAddress (); 69 } 70 71 int QueryInterface (int riid, int ppvObject) { 72 if (riid == 0 || ppvObject == 0) return XPCOM.NS_ERROR_NO_INTERFACE; 73 nsID guid = new nsID (); 74 XPCOM.memmove (guid, riid, nsID.sizeof); 75 76 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) { 77 XPCOM.memmove (ppvObject, new int [] {supports.getAddress ()}, C.PTR_SIZEOF); 78 AddRef (); 79 return XPCOM.NS_OK; 80 } 81 if (guid.Equals (nsIPromptService.NS_IPROMPTSERVICE_IID)) { 82 XPCOM.memmove (ppvObject, new int [] {promptService.getAddress ()}, C.PTR_SIZEOF); 83 AddRef (); 84 return XPCOM.NS_OK; 85 } 86 87 XPCOM.memmove (ppvObject, new int [] {0}, C.PTR_SIZEOF); 88 return XPCOM.NS_ERROR_NO_INTERFACE; 89 } 90 91 int Release () { 92 refCount--; 93 if (refCount == 0) disposeCOMInterfaces (); 94 return refCount; 95 } 96 97 Browser getBrowser (int aDOMWindow) { 98 if (aDOMWindow == 0) return null; 99 100 int [] result = new int [1]; 101 int rc = XPCOM.NS_GetServiceManager (result); 102 if (rc != XPCOM.NS_OK) Mozilla.error (rc); 103 if (result[0] == 0) Mozilla.error (XPCOM.NS_NOINTERFACE); 104 105 nsIServiceManager serviceManager = new nsIServiceManager (result[0]); 106 result[0] = 0; 107 byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_WINDOWWATCHER_CONTRACTID, true); 108 rc = serviceManager.GetServiceByContractID (aContractID, nsIWindowWatcher.NS_IWINDOWWATCHER_IID, result); 109 if (rc != XPCOM.NS_OK) Mozilla.error(rc); 110 if (result[0] == 0) Mozilla.error (XPCOM.NS_NOINTERFACE); 111 serviceManager.Release (); 112 113 nsIWindowWatcher windowWatcher = new nsIWindowWatcher (result[0]); 114 result[0] = 0; 115 116 nsIDOMWindow window = new nsIDOMWindow (aDOMWindow); 117 rc = window.GetTop (result); 118 if (rc != XPCOM.NS_OK) Mozilla.error (rc); 119 if (result[0] == 0) Mozilla.error (XPCOM.NS_NOINTERFACE); 120 aDOMWindow = result[0]; 121 result[0] = 0; 122 rc = windowWatcher.GetChromeForWindow (aDOMWindow, result); 123 if (rc != XPCOM.NS_OK) Mozilla.error (rc); 124 if (result[0] == 0) Mozilla.error (XPCOM.NS_NOINTERFACE); 125 windowWatcher.Release (); 126 127 nsIWebBrowserChrome webBrowserChrome = new nsIWebBrowserChrome (result[0]); 128 result[0] = 0; 129 rc = webBrowserChrome.QueryInterface (nsIEmbeddingSiteWindow.NS_IEMBEDDINGSITEWINDOW_IID, result); 130 if (rc != XPCOM.NS_OK) Mozilla.error (rc); 131 if (result[0] == 0) Mozilla.error (XPCOM.NS_NOINTERFACE); 132 webBrowserChrome.Release (); 133 134 nsIEmbeddingSiteWindow embeddingSiteWindow = new nsIEmbeddingSiteWindow (result[0]); 135 result[0] = 0; 136 rc = embeddingSiteWindow.GetSiteWindow (result); 137 if (rc != XPCOM.NS_OK) Mozilla.error (rc); 138 if (result[0] == 0) Mozilla.error (XPCOM.NS_NOINTERFACE); 139 embeddingSiteWindow.Release (); 140 141 return Mozilla.findBrowser (result[0]); 142 } 143 144 String getLabel (int buttonFlag, int index, int buttonTitle) { 145 String label = null; 146 int flag = (buttonFlag & (0xff * index)) / index; 147 switch (flag) { 148 case nsIPromptService.BUTTON_TITLE_CANCEL : label = SWT.getMessage ("SWT_Cancel"); break; case nsIPromptService.BUTTON_TITLE_NO : label = SWT.getMessage ("SWT_No"); break; case nsIPromptService.BUTTON_TITLE_OK : label = SWT.getMessage ("SWT_OK"); break; case nsIPromptService.BUTTON_TITLE_SAVE : label = SWT.getMessage ("SWT_Save"); break; case nsIPromptService.BUTTON_TITLE_YES : label = SWT.getMessage ("SWT_Yes"); break; case nsIPromptService.BUTTON_TITLE_IS_STRING : { 154 int length = XPCOM.strlen_PRUnichar (buttonTitle); 155 char[] dest = new char[length]; 156 XPCOM.memmove (dest, buttonTitle, length * 2); 157 label = new String (dest); 158 } 159 } 160 return label; 161 } 162 163 164 165 public int Alert (int aParent, int aDialogTitle, int aText) { 166 Browser browser = getBrowser (aParent); 167 168 int length = XPCOM.strlen_PRUnichar (aDialogTitle); 169 char[] dest = new char[length]; 170 XPCOM.memmove (dest, aDialogTitle, length * 2); 171 String titleLabel = new String (dest); 172 173 length = XPCOM.strlen_PRUnichar (aText); 174 dest = new char[length]; 175 XPCOM.memmove (dest, aText, length * 2); 176 String textLabel = new String (dest); 177 178 Shell shell = browser == null ? new Shell () : browser.getShell (); 179 MessageBox messageBox = new MessageBox (shell, SWT.OK | SWT.ICON_WARNING); 180 messageBox.setText (titleLabel); 181 messageBox.setMessage (textLabel); 182 messageBox.open (); 183 return XPCOM.NS_OK; 184 } 185 186 public int AlertCheck (int aParent, int aDialogTitle, int aText, int aCheckMsg, int aCheckState) { 187 Browser browser = getBrowser (aParent); 188 189 int length = XPCOM.strlen_PRUnichar (aDialogTitle); 190 char[] dest = new char[length]; 191 XPCOM.memmove (dest, aDialogTitle, length * 2); 192 String titleLabel = new String (dest); 193 194 length = XPCOM.strlen_PRUnichar (aText); 195 dest = new char[length]; 196 XPCOM.memmove (dest, aText, length * 2); 197 String textLabel = new String (dest); 198 199 length = XPCOM.strlen_PRUnichar (aCheckMsg); 200 dest = new char[length]; 201 XPCOM.memmove (dest, aCheckMsg, length * 2); 202 String checkLabel = new String (dest); 203 204 Shell shell = browser == null ? new Shell () : browser.getShell (); 205 PromptDialog dialog = new PromptDialog (shell); 206 int[] check = new int[1]; 207 if (aCheckState != 0) XPCOM.memmove (check, aCheckState, 4); 208 dialog.alertCheck (titleLabel, textLabel, checkLabel, check); 209 if (aCheckState != 0) XPCOM.memmove (aCheckState, check, 4); 210 return XPCOM.NS_OK; 211 } 212 213 public int Confirm (int aParent, int aDialogTitle, int aText, int _retval) { 214 Browser browser = getBrowser (aParent); 215 216 int length = XPCOM.strlen_PRUnichar (aDialogTitle); 217 char[] dest = new char[length]; 218 XPCOM.memmove (dest, aDialogTitle, length * 2); 219 String titleLabel = new String (dest); 220 221 length = XPCOM.strlen_PRUnichar (aText); 222 dest = new char[length]; 223 XPCOM.memmove (dest, aText, length * 2); 224 String textLabel = new String (dest); 225 226 Shell shell = browser == null ? new Shell () : browser.getShell (); 227 MessageBox messageBox = new MessageBox (shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION); 228 messageBox.setText (titleLabel); 229 messageBox.setMessage (textLabel); 230 int id = messageBox.open (); 231 int[] result = {id == SWT.OK ? 1 : 0}; 232 XPCOM.memmove (_retval, result, 4); 233 return XPCOM.NS_OK; 234 } 235 236 public int ConfirmCheck (int aParent, int aDialogTitle, int aText, int aCheckMsg, int aCheckState, int _retval) { 237 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 238 } 239 240 public int ConfirmEx (int aParent, int aDialogTitle, int aText, int aButtonFlags, int aButton0Title, int aButton1Title, int aButton2Title, int aCheckMsg, int aCheckState, int _retval) { 241 Browser browser = getBrowser (aParent); 242 243 int length = XPCOM.strlen_PRUnichar (aDialogTitle); 244 char[] dest = new char[length]; 245 XPCOM.memmove (dest, aDialogTitle, length * 2); 246 String titleLabel = new String (dest); 247 248 length = XPCOM.strlen_PRUnichar (aText); 249 dest = new char[length]; 250 XPCOM.memmove (dest, aText, length * 2); 251 String textLabel = new String (dest); 252 253 String checkLabel = null; 254 if (aCheckMsg != 0) { 255 length = XPCOM.strlen_PRUnichar (aCheckMsg); 256 dest = new char[length]; 257 XPCOM.memmove (dest, aCheckMsg, length * 2); 258 checkLabel = new String (dest); 259 } 260 261 String button0Label = getLabel ((int)aButtonFlags, nsIPromptService.BUTTON_POS_0, aButton0Title); 262 String button1Label = getLabel ((int)aButtonFlags, nsIPromptService.BUTTON_POS_1, aButton1Title); 263 String button2Label = getLabel ((int)aButtonFlags, nsIPromptService.BUTTON_POS_2, aButton2Title); 264 265 int defaultIndex = 0; 266 if ((aButtonFlags & nsIPromptService.BUTTON_POS_1_DEFAULT) != 0) { 267 defaultIndex = 1; 268 } else if ((aButtonFlags & nsIPromptService.BUTTON_POS_2_DEFAULT) != 0) { 269 defaultIndex = 2; 270 } 271 272 Shell shell = browser == null ? new Shell () : browser.getShell (); 273 PromptDialog dialog = new PromptDialog (shell); 274 int[] check = new int[1], result = new int[1]; 275 if (aCheckState != 0) XPCOM.memmove (check, aCheckState, 4); 276 dialog.confirmEx (titleLabel, textLabel, checkLabel, button0Label, button1Label, button2Label, defaultIndex, check, result); 277 if (aCheckState != 0) XPCOM.memmove (aCheckState, check, 4); 278 XPCOM.memmove (_retval, result, 4); 279 return XPCOM.NS_OK; 280 } 281 282 public int Prompt (int aParent, int aDialogTitle, int aText, int aValue, int aCheckMsg, int aCheckState, int _retval) { 283 Browser browser = getBrowser (aParent); 284 String titleLabel = null, textLabel, checkLabel = null; 285 String [] valueLabel = new String [1]; 286 char[] dest; 287 int length; 288 if (aDialogTitle != 0) { 289 length = XPCOM.strlen_PRUnichar (aDialogTitle); 290 dest = new char[length]; 291 XPCOM.memmove (dest, aDialogTitle, length * 2); 292 titleLabel = new String (dest); 293 } 294 295 length = XPCOM.strlen_PRUnichar (aText); 296 dest = new char[length]; 297 XPCOM.memmove (dest, aText, length * 2); 298 textLabel = new String (dest); 299 300 int [] valueAddr = new int [1]; 301 XPCOM.memmove (valueAddr, aValue, C.PTR_SIZEOF); 302 if (valueAddr[0] != 0) { 303 length = XPCOM.strlen_PRUnichar (valueAddr[0]); 304 dest = new char[length]; 305 XPCOM.memmove (dest, valueAddr[0], length * 2); 306 valueLabel[0] = new String (dest); 307 } 308 309 if (aCheckMsg != 0) { 310 length = XPCOM.strlen_PRUnichar (aCheckMsg); 311 if (length > 0) { 312 dest = new char[length]; 313 XPCOM.memmove (dest, aCheckMsg, length * 2); 314 checkLabel = new String (dest); 315 } 316 } 317 318 Shell shell = browser == null ? new Shell () : browser.getShell (); 319 PromptDialog dialog = new PromptDialog (shell); 320 int[] check = new int[1], result = new int[1]; 321 if (aCheckState != 0) XPCOM.memmove (check, aCheckState, 4); 322 dialog.prompt (titleLabel, textLabel, checkLabel, valueLabel, check, result); 323 324 XPCOM.memmove (_retval, result, 4); 325 if (result[0] == 1) { 326 330 int cnt, size; 331 int ptr; 332 char[] buffer; 333 int [] result2 = new int [1]; 334 if (valueLabel[0] != null) { 335 cnt = valueLabel[0].length (); 336 buffer = new char[cnt + 1]; 337 valueLabel[0].getChars (0, cnt, buffer, 0); 338 size = buffer.length * 2; 339 ptr = C.malloc (size); 340 XPCOM.memmove (ptr, buffer, size); 341 XPCOM.memmove (aValue, new int [] {ptr}, C.PTR_SIZEOF); 342 343 if (valueAddr[0] != 0) { 344 int rc = XPCOM.NS_GetServiceManager (result2); 345 if (rc != XPCOM.NS_OK) SWT.error (rc); 346 if (result2[0] == 0) SWT.error (XPCOM.NS_NOINTERFACE); 347 348 nsIServiceManager serviceManager = new nsIServiceManager (result2[0]); 349 result2[0] = 0; 350 byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_MEMORY_CONTRACTID, true); 351 rc = serviceManager.GetServiceByContractID (aContractID, nsIMemory.NS_IMEMORY_IID, result2); 352 if (rc != XPCOM.NS_OK) SWT.error (rc); 353 if (result2[0] == 0) SWT.error (XPCOM.NS_NOINTERFACE); 354 serviceManager.Release (); 355 356 nsIMemory memory = new nsIMemory (result2[0]); 357 result2[0] = 0; 358 memory.Free (valueAddr[0]); 359 memory.Release (); 360 } 361 } 362 } 363 if (aCheckState != 0) XPCOM.memmove (aCheckState, check, 4); 364 return XPCOM.NS_OK; 365 } 366 367 public int PromptUsernameAndPassword (int aParent, int aDialogTitle, int aText, int aUsername, int aPassword, int aCheckMsg, int aCheckState, int _retval) { 368 Browser browser = getBrowser (aParent); 369 String titleLabel, textLabel, checkLabel = null; 370 String [] userLabel = new String [1], passLabel = new String [1]; 371 char[] dest; 372 int length; 373 if (aDialogTitle != 0) { 374 length = XPCOM.strlen_PRUnichar (aDialogTitle); 375 dest = new char[length]; 376 XPCOM.memmove (dest, aDialogTitle, length * 2); 377 titleLabel = new String (dest); 378 } else { 379 titleLabel = ""; } 381 382 length = XPCOM.strlen_PRUnichar (aText); 383 dest = new char[length]; 384 XPCOM.memmove (dest, aText, length * 2); 385 textLabel = new String (dest); 386 387 int [] userAddr = new int [1]; 388 XPCOM.memmove (userAddr, aUsername, C.PTR_SIZEOF); 389 if (userAddr[0] != 0) { 390 length = XPCOM.strlen_PRUnichar (userAddr[0]); 391 dest = new char[length]; 392 XPCOM.memmove (dest, userAddr[0], length * 2); 393 userLabel[0] = new String (dest); 394 } 395 396 int [] passAddr = new int [1]; 397 XPCOM.memmove (passAddr, aPassword, C.PTR_SIZEOF); 398 if (passAddr[0] != 0) { 399 length = XPCOM.strlen_PRUnichar (passAddr[0]); 400 dest = new char[length]; 401 XPCOM.memmove (dest, passAddr[0], length * 2); 402 passLabel[0] = new String (dest); 403 } 404 405 if (aCheckMsg != 0) { 406 length = XPCOM.strlen_PRUnichar (aCheckMsg); 407 if (length > 0) { 408 dest = new char[length]; 409 XPCOM.memmove (dest, aCheckMsg, length * 2); 410 checkLabel = new String (dest); 411 } 412 } 413 414 Shell shell = browser == null ? new Shell () : browser.getShell (); 415 PromptDialog dialog = new PromptDialog (shell); 416 int[] check = new int[1], result = new int[1]; 417 if (aCheckState != 0) XPCOM.memmove (check, aCheckState, 4); 418 dialog.promptUsernameAndPassword (titleLabel, textLabel, checkLabel, userLabel, passLabel, check, result); 419 420 XPCOM.memmove (_retval, result, 4); 421 if (result[0] == 1) { 422 426 int cnt, size; 427 int ptr; 428 char[] buffer; 429 int [] result2 = new int [1]; 430 if (userLabel[0] != null) { 431 cnt = userLabel[0].length (); 432 buffer = new char[cnt + 1]; 433 userLabel[0].getChars (0, cnt, buffer, 0); 434 size = buffer.length * 2; 435 ptr = C.malloc (size); 436 XPCOM.memmove (ptr, buffer, size); 437 XPCOM.memmove (aUsername, new int [] {ptr}, C.PTR_SIZEOF); 438 439 if (userAddr[0] != 0) { 440 int rc = XPCOM.NS_GetServiceManager (result2); 441 if (rc != XPCOM.NS_OK) SWT.error (rc); 442 if (result2[0] == 0) SWT.error (XPCOM.NS_NOINTERFACE); 443 444 nsIServiceManager serviceManager = new nsIServiceManager (result2[0]); 445 result2[0] = 0; 446 byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_MEMORY_CONTRACTID, true); 447 rc = serviceManager.GetServiceByContractID (aContractID, nsIMemory.NS_IMEMORY_IID, result2); 448 if (rc != XPCOM.NS_OK) SWT.error (rc); 449 if (result[0] == 0) SWT.error (XPCOM.NS_NOINTERFACE); 450 serviceManager.Release (); 451 452 nsIMemory memory = new nsIMemory (result2[0]); 453 result2[0] = 0; 454 memory.Free (userAddr[0]); 455 memory.Release (); 456 } 457 } 458 if (passLabel[0] != null) { 459 cnt = passLabel[0].length (); 460 buffer = new char[cnt + 1]; 461 passLabel[0].getChars (0, cnt, buffer, 0); 462 size = buffer.length * 2; 463 ptr = C.malloc (size); 464 XPCOM.memmove (ptr, buffer, size); 465 XPCOM.memmove (aPassword, new int [] {ptr}, C.PTR_SIZEOF); 466 467 if (passAddr[0] != 0) { 468 int rc = XPCOM.NS_GetServiceManager (result2); 469 if (rc != XPCOM.NS_OK) SWT.error (rc); 470 if (result2[0] == 0) SWT.error (XPCOM.NS_NOINTERFACE); 471 472 nsIServiceManager serviceManager = new nsIServiceManager (result2[0]); 473 result2[0] = 0; 474 byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_MEMORY_CONTRACTID, true); 475 rc = serviceManager.GetServiceByContractID (aContractID, nsIMemory.NS_IMEMORY_IID, result2); 476 if (rc != XPCOM.NS_OK) SWT.error (rc); 477 if (result2[0] == 0) SWT.error (XPCOM.NS_NOINTERFACE); 478 serviceManager.Release (); 479 480 nsIMemory memory = new nsIMemory (result2[0]); 481 result2[0] = 0; 482 memory.Free (passAddr[0]); 483 memory.Release (); 484 } 485 } 486 } 487 if (aCheckState != 0) XPCOM.memmove (aCheckState, check, 4); 488 return XPCOM.NS_OK; 489 } 490 491 public int PromptPassword (int aParent, int aDialogTitle, int aText, int aPassword, int aCheckMsg, int aCheckState, int _retval) { 492 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 493 } 494 495 public int Select (int aParent, int aDialogTitle, int aText, int aCount, int aSelectList, int aOutSelection, int _retval) { 496 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 497 } 498 } 499 | Popular Tags |