Back to index

lightning-sunbird  0.9+nobinonly
nsMsgPrompts.cpp
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) 1998
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 of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or 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 #include "nsMsgPrompts.h"
00038 
00039 #include "nsMsgCopy.h"
00040 #include "nsIMsgStringService.h"
00041 #include "nsIPrompt.h"
00042 #include "nsIWindowWatcher.h"
00043 #include "nsMsgComposeStringBundle.h"
00044 #include "nsXPIDLString.h"
00045 #include "nsMsgCompCID.h"
00046 
00047 nsresult
00048 nsMsgBuildErrorMessageByID(PRInt32 msgID, nsString& retval, nsString* param0, nsString* param1)
00049 {
00050   nsresult rv;
00051 
00052   nsCOMPtr<nsIMsgStringService> composebundle (do_GetService(NS_MSG_COMPOSESTRINGSERVICE_CONTRACTID, &rv));
00053   nsXPIDLString msg;
00054 
00055   if (composebundle)
00056   {
00057     composebundle->GetStringByID(msgID, getter_Copies(msg));
00058     retval = msg;
00059 
00060     nsString target;
00061     if (param0)
00062     {
00063       target.AssignLiteral("%P0%");
00064       retval.ReplaceSubstring(target, *param0);
00065     }
00066     if (param1)
00067     {
00068       target.AssignLiteral("%P1%");
00069       retval.ReplaceSubstring(target, *param1);
00070     }
00071   }
00072   return rv;
00073 }
00074 
00075 nsresult
00076 nsMsgDisplayMessageByID(nsIPrompt * aPrompt, PRInt32 msgID, const PRUnichar * windowTitle)
00077 {
00078   nsresult rv;
00079 
00080   nsCOMPtr<nsIMsgStringService> composebundle (do_GetService(NS_MSG_COMPOSESTRINGSERVICE_CONTRACTID, &rv));
00081   nsXPIDLString msg;
00082 
00083   if (composebundle)
00084   {
00085     composebundle->GetStringByID(msgID, getter_Copies(msg));
00086     rv = nsMsgDisplayMessageByString(aPrompt, msg, windowTitle);
00087   }
00088   return rv;
00089 }
00090 
00091 nsresult
00092 nsMsgDisplayMessageByString(nsIPrompt * aPrompt, const PRUnichar * msg, const PRUnichar * windowTitle)
00093 {
00094   nsresult rv;
00095   nsCOMPtr<nsIPrompt> prompt = aPrompt;
00096 
00097   if ((!msg) || (!*msg))
00098     return NS_ERROR_INVALID_ARG;
00099   
00100   if (!prompt)
00101   {
00102     nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
00103     if (wwatch)
00104       wwatch->GetNewPrompter(0, getter_AddRefs(prompt));
00105   }
00106   
00107   if (prompt)
00108     rv = prompt->Alert(windowTitle, msg);
00109   return NS_OK;
00110 }
00111 
00112 nsresult
00113 nsMsgAskBooleanQuestionByID(nsIPrompt * aPrompt, PRInt32 msgID, PRBool *answer, const PRUnichar * windowTitle)
00114 {
00115   nsCOMPtr<nsIMsgStringService> composebundle (do_GetService(NS_MSG_COMPOSESTRINGSERVICE_CONTRACTID));
00116   nsXPIDLString msg;
00117 
00118   if (composebundle)
00119   {
00120     composebundle->GetStringByID(msgID, getter_Copies(msg));
00121     nsMsgAskBooleanQuestionByString(aPrompt, msg, answer, windowTitle);
00122   }
00123 
00124   return NS_OK;
00125 }     
00126 
00127 nsresult
00128 nsMsgAskBooleanQuestionByString(nsIPrompt * aPrompt, const PRUnichar * msg, PRBool *answer, const PRUnichar * windowTitle)
00129 {
00130   nsresult rv;
00131   PRInt32 result;
00132   nsCOMPtr<nsIPrompt> dialog = aPrompt;
00133 
00134   if ((!msg) || (!*msg))
00135     return NS_ERROR_INVALID_ARG;
00136   
00137   if (!dialog)
00138   {
00139     nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
00140     if (wwatch)
00141       wwatch->GetNewPrompter(0, getter_AddRefs(dialog));
00142   }
00143 
00144   if (dialog) 
00145   {
00146     rv = dialog->Confirm(windowTitle, msg, &result);
00147     if (result == 1) 
00148       *answer = PR_TRUE;
00149     else 
00150       *answer = PR_FALSE;
00151   }
00152 
00153   return NS_OK;
00154 }     
00155 
00156 // returns 0 (send in UTF-8) in case of error
00157 PRInt32
00158 nsMsgAskAboutUncoveredCharacters(nsIPrompt * aPrompt)
00159 {
00160   PRInt32 result;
00161   nsCOMPtr<nsIPrompt> dialog = aPrompt;
00162 
00163   if (!dialog)
00164   {
00165     nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
00166     if (wwatch)
00167       wwatch->GetNewPrompter(0, getter_AddRefs(dialog));
00168   }
00169 
00170   NS_ENSURE_TRUE(dialog, 0);
00171 
00172   nsCOMPtr<nsIStringBundleService> bundleService =
00173     do_GetService(NS_STRINGBUNDLE_CONTRACTID);
00174   NS_ENSURE_TRUE(bundleService, 0);
00175 
00176   nsCOMPtr<nsIStringBundle> composeBundle;
00177   bundleService->CreateBundle("chrome://messenger/locale/messengercompose/"
00178                               "composeMsgs.properties",
00179                                getter_AddRefs(composeBundle));
00180   NS_ENSURE_TRUE(composeBundle, 0);
00181 
00182   nsXPIDLString title;
00183   nsXPIDLString msg;
00184   nsXPIDLString sendInUTF8;
00185   nsXPIDLString sendAnyway;
00186 
00187   composeBundle->
00188     GetStringFromName(NS_LITERAL_STRING("initErrorDlogTitle").get(),
00189                       getter_Copies(title));
00190   composeBundle->
00191     GetStringFromID(NS_ERROR_GET_CODE(NS_ERROR_MSG_MULTILINGUAL_SEND),
00192                     getter_Copies(msg));
00193   composeBundle->
00194     GetStringFromName(NS_LITERAL_STRING("sendInUTF8").get(),
00195                       getter_Copies(sendInUTF8));
00196   composeBundle->
00197     GetStringFromName(NS_LITERAL_STRING("sendAnyway").get(),
00198                       getter_Copies(sendAnyway));
00199 
00200   nsresult rv = dialog->
00201     ConfirmEx(title, msg, 
00202               nsIPrompt::BUTTON_TITLE_IS_STRING * nsIPrompt::BUTTON_POS_0 +
00203               nsIPrompt::BUTTON_TITLE_CANCEL * nsIPrompt::BUTTON_POS_1 +
00204               nsIPrompt::BUTTON_TITLE_IS_STRING * nsIPrompt::BUTTON_POS_2 +
00205               nsIPrompt::BUTTON_POS_0_DEFAULT,
00206               sendInUTF8, nsnull, sendAnyway, nsnull, 0, &result);
00207 
00208   NS_ENSURE_SUCCESS(rv, 0);
00209   return result;
00210 }