Back to index

lightning-sunbird  0.9+nobinonly
nsAlertController.mm
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037  
00038 #import "nsAlertController.h"
00039 #import "CHBrowserService.h"
00040 
00041 
00042 enum { kOKButton = 0, kCancelButton = 1, kOtherButton = 2 };
00043 
00044 @implementation nsAlertController
00045 
00046 - (IBAction)hitButton1:(id)sender
00047 {
00048   [NSApp stopModalWithCode:kOKButton];
00049 }
00050 
00051 - (IBAction)hitButton2:(id)sender
00052 {
00053   [NSApp stopModalWithCode:kCancelButton];
00054 }
00055 
00056 - (IBAction)hitButton3:(id)sender
00057 {
00058   [NSApp stopModalWithCode:kOtherButton];
00059 }
00060 
00061 
00062 - (void)awakeFromNib
00063 {
00064   CHBrowserService::SetAlertController(self);
00065 }
00066 
00067 - (void)alert:(NSWindow*)parent title:(NSString*)title text:(NSString*)text
00068 {
00069   [alertPanelText setStringValue:text];
00070   [alertPanel setTitle:title];
00071 
00072   [NSApp runModalForWindow:alertPanel relativeToWindow:parent];
00073   
00074   [alertPanel close];
00075 }
00076 
00077 - (void)alertCheck:(NSWindow*)parent title:(NSString*)title text:(NSString*)text checkMsg:(NSString*)checkMsg checkValue:(BOOL*)checkValue
00078 {
00079   [alertCheckPanelText setStringValue:text];
00080   [alertCheckPanel setTitle:title];
00081   int state = (*checkValue ? NSOnState : NSOffState);
00082   [alertCheckPanelCheck setState:state];
00083   [alertCheckPanelCheck setTitle:checkMsg];
00084 
00085   [NSApp runModalForWindow:alertCheckPanel relativeToWindow:parent];
00086 
00087   *checkValue = ([alertCheckPanelCheck state] == NSOnState);
00088   [alertCheckPanel close];
00089 }
00090 
00091 - (BOOL)confirm:(NSWindow*)parent title:(NSString*)title text:(NSString*)text
00092 {
00093   [confirmPanelText setStringValue:text];
00094   [confirmPanel setTitle:title];
00095 
00096   int result = [NSApp runModalForWindow:confirmPanel relativeToWindow:parent];
00097   
00098   [confirmPanel close];
00099 
00100   return (result == kOKButton);
00101 }
00102 
00103 - (BOOL)confirmCheck:(NSWindow*)parent title:(NSString*)title text:(NSString*)text checkMsg:(NSString*)checkMsg checkValue:(BOOL*)checkValue
00104 {
00105   [confirmCheckPanelText setStringValue:text];
00106   [confirmCheckPanel setTitle:title];
00107   int state = (*checkValue ? NSOnState : NSOffState);
00108   [confirmCheckPanelCheck setState:state];
00109   [confirmCheckPanelCheck setTitle:checkMsg];
00110 
00111   int result = [NSApp runModalForWindow:confirmCheckPanel relativeToWindow:parent];
00112 
00113   *checkValue = ([confirmCheckPanelCheck state] == NSOnState);
00114   [confirmCheckPanel close];
00115 
00116   return (result == kOKButton);
00117 }
00118 
00119 - (int)confirmEx:(NSWindow*)parent title:(NSString*)title text:(NSString*)text
00120    button1:(NSString*)btn1 button2:(NSString*)btn2 button3:(NSString*)btn3
00121 {
00122   [confirmPanelText setStringValue:text];
00123   [confirmPanel setTitle:title];
00124 
00125   [confirmPanelButton1 setTitle:btn1];
00126   [confirmPanelButton2 setTitle:btn2];
00127   [confirmPanelButton3 setTitle:btn3];  
00128 
00129   int result = [NSApp runModalForWindow:confirmPanel relativeToWindow:parent];
00130   
00131   [confirmPanel close];
00132 
00133   return result;
00134 }
00135 
00136 - (int)confirmCheckEx:(NSWindow*)parent title:(NSString*)title text:(NSString*)text 
00137   button1:(NSString*)btn1 button2:(NSString*)btn2 button3:(NSString*)btn3
00138   checkMsg:(NSString*)checkMsg checkValue:(BOOL*)checkValue
00139 {
00140   [confirmCheckPanelText setStringValue:text];
00141   [confirmCheckPanel setTitle:title];
00142   int state = (*checkValue ? NSOnState : NSOffState);
00143   [confirmCheckPanelCheck setState:state];
00144   [confirmCheckPanelCheck setTitle:checkMsg];
00145 
00146   [confirmCheckPanelButton1 setTitle:btn1];
00147   [confirmCheckPanelButton2 setTitle:btn2];
00148   [confirmCheckPanelButton3 setTitle:btn3];  
00149   
00150   int result = [NSApp runModalForWindow:confirmCheckPanel relativeToWindow:parent];
00151 
00152   *checkValue = ([confirmCheckPanelCheck state] == NSOnState);
00153   [confirmCheckPanel close];
00154 
00155   return result;
00156 }
00157 
00158 - (BOOL)confirmStorePassword:(NSWindow*)parent
00159 {
00160   int result = [NSApp runModalForWindow:confirmStorePasswordPanel relativeToWindow:parent];
00161   [confirmStorePasswordPanel close];
00162   return (result == kOKButton);
00163 }
00164 
00165 - (BOOL)prompt:(NSWindow*)parent title:(NSString*)title text:(NSString*)text promptText:(NSMutableString*)promptText checkMsg:(NSString*)checkMsg checkValue:(BOOL*)checkValue doCheck:(BOOL)doCheck
00166 {
00167   [promptPanelText setStringValue:text];
00168   [promptPanel setTitle:title];
00169   if (doCheck) {
00170     int state = (*checkValue ? NSOnState : NSOffState);
00171     [promptPanelCheck setState:state];
00172     [promptPanelCheck setTransparent:NO];
00173   }
00174   else {
00175     [promptPanelCheck setTransparent:YES];
00176   }  
00177   [promptPanelCheck setTitle:checkMsg];
00178   [promptPanelInput setStringValue:promptText];
00179 
00180   int result = [NSApp runModalForWindow:promptPanel relativeToWindow:parent];
00181 
00182   *checkValue = ([promptPanelCheck state] == NSOnState);
00183 
00184   NSString* value = [promptPanelInput stringValue];
00185   PRUint32 length = [promptText length];
00186   if (length) {
00187     NSRange all;
00188     all.location = 0;
00189     all.length = [promptText length];
00190     [promptText deleteCharactersInRange:all];
00191   }
00192   [promptText appendString:value];
00193 
00194   [promptPanel close];
00195 
00196   return (result == kOKButton);    
00197 }
00198 
00199 - (BOOL)promptUserNameAndPassword:(NSWindow*)parent title:(NSString*)title text:(NSString*)text userNameText:(NSMutableString*)userNameText passwordText:(NSMutableString*)passwordText checkMsg:(NSString*)checkMsg checkValue:(BOOL*)checkValue doCheck:(BOOL)doCheck
00200 {
00201   [usernamePanelText setStringValue:text];
00202   [usernamePanel setTitle:title];
00203   if (doCheck) {
00204     int state = (*checkValue ? NSOnState : NSOffState);
00205     [usernamePanelCheck setState:state];
00206     [usernamePanelCheck setTransparent:NO];
00207   }
00208   else {
00209     [usernamePanelCheck setTransparent:YES];
00210   }  
00211   [usernamePanelCheck setTitle:checkMsg];
00212   [usernamePanelPassword setStringValue:passwordText];
00213   [usernamePanelUserName setStringValue:userNameText];
00214 
00215   int result = [NSApp runModalForWindow:usernamePanel relativeToWindow:parent];
00216 
00217   *checkValue = ([usernamePanelCheck state] == NSOnState);
00218 
00219   NSString* value = [usernamePanelUserName stringValue];
00220   PRUint32 length = [userNameText length];
00221   if (length) {
00222     NSRange all;
00223     all.location = 0;
00224     all.length = [userNameText length];
00225     [userNameText deleteCharactersInRange:all];
00226   }
00227   [userNameText appendString:value];
00228 
00229   value = [usernamePanelPassword stringValue];
00230   length = [passwordText length];
00231   if (length) {
00232     NSRange all;
00233     all.location = 0;
00234     all.length = [passwordText length];
00235     [passwordText deleteCharactersInRange:all];
00236   }
00237   [passwordText appendString:value];
00238 
00239   [usernamePanel close];
00240 
00241   return (result == kOKButton);    
00242 }
00243 
00244 - (BOOL)promptPassword:(NSWindow*)parent title:(NSString*)title text:(NSString*)text passwordText:(NSMutableString*)passwordText checkMsg:(NSString*)checkMsg checkValue:(BOOL*)checkValue doCheck:(BOOL)doCheck
00245 {
00246   [passwordPanelText setStringValue:text];
00247   [passwordPanel setTitle:title];
00248   if (doCheck) {
00249     int state = (*checkValue ? NSOnState : NSOffState);
00250     [passwordPanelCheck setState:state];
00251     [passwordPanelCheck setTransparent:NO];
00252   }
00253   else {
00254     [passwordPanelCheck setTransparent:YES];
00255   }  
00256   [passwordPanelCheck setTitle:checkMsg];
00257   [passwordPanelInput setStringValue:passwordText];
00258 
00259   int result = [NSApp runModalForWindow:passwordPanel relativeToWindow:parent];
00260 
00261   *checkValue = ([passwordPanelCheck state] == NSOnState);
00262 
00263   NSString* value = [passwordPanelInput stringValue];
00264   PRUint32 length = [passwordText length];
00265   if (length) {
00266     NSRange all;
00267     all.location = 0;
00268     all.length = [passwordText length];
00269     [passwordText deleteCharactersInRange:all];
00270   }
00271   [passwordText appendString:value];
00272 
00273   [passwordPanel close];
00274 
00275   return (result == kOKButton);    
00276 }
00277 
00278 
00279 #if PROBABLY_DONT_WANT_THIS
00280 
00281 - (BOOL)postToInsecureFromSecure:(NSWindow*)parent
00282 {
00283   int result = [NSApp runModalForWindow:postToInsecureFromSecurePanel relativeToWindow:parent];
00284   [postToInsecureFromSecurePanel close];
00285 
00286   return (result == kOKButton);
00287 }
00288 
00289 
00290 - (BOOL)badCert:(NSWindow*)parent
00291 {
00292   int result = [NSApp runModalForWindow:securityMismatchPanel relativeToWindow:parent];
00293   [securityMismatchPanel close];
00294 
00295   return (result == kOKButton);
00296 }
00297 
00298 - (BOOL)expiredCert:(NSWindow*)parent
00299 {
00300   int result = [NSApp runModalForWindow:expiredCertPanel relativeToWindow:parent];
00301   [expiredCertPanel close];
00302 
00303   return (result == kOKButton);
00304 }
00305 
00306 
00307 - (int)unknownCert:(NSWindow*)parent
00308 {
00309   // this dialog is a little backward, with "Stop" returning 0, and the default returning
00310   // 1. That's just how nsIBadCertListener defines its constants. *shrug*
00311   int result = [NSApp runModalForWindow:securityUnknownPanel relativeToWindow:parent];
00312   [securityUnknownPanel close];
00313 
00314   return result;
00315 }
00316 
00317 #endif
00318 
00319 @end