Back to index

lightning-sunbird  0.9+nobinonly
nsBaseFilePicker.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Stuart Parmenter <pavlov@netscape.com>
00025  *   Mike Pinkerton   <pinkerton@netscape.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsCOMPtr.h"
00042 #include "nsIDOMWindow.h"
00043 #include "nsIScriptGlobalObject.h"
00044 #include "nsIDocShell.h"
00045 #include "nsIDocShellTreeItem.h"
00046 #include "nsIInterfaceRequestorUtils.h"
00047 #include "nsIBaseWindow.h"
00048 #include "nsIContentViewer.h"
00049 #include "nsIDocumentViewer.h"
00050 #include "nsIViewManager.h"
00051 #include "nsIView.h"
00052 #include "nsIWidget.h"
00053 
00054 #include "nsIStringBundle.h"
00055 #include "nsXPIDLString.h"
00056 #include "nsIServiceManager.h"
00057 #include "nsISupportsArray.h"
00058 #include "nsILocalFile.h"
00059 #include "nsEnumeratorUtils.h"
00060 
00061 #include "nsBaseFilePicker.h"
00062 
00063 
00064 static NS_DEFINE_CID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);
00065 #define FILEPICKER_PROPERTIES "chrome://global/locale/filepicker.properties"
00066 
00067 
00068 nsBaseFilePicker::nsBaseFilePicker()
00069 {
00070 
00071 }
00072 
00073 nsBaseFilePicker::~nsBaseFilePicker()
00074 {
00075 
00076 }
00077 
00078 nsIWidget *nsBaseFilePicker::DOMWindowToWidget(nsIDOMWindow *dw)
00079 {
00080   nsCOMPtr<nsIWidget> widget;
00081 
00082   nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(dw);
00083   if (sgo) {
00084     nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(sgo->GetDocShell()));
00085 
00086     while (!widget && baseWin) {
00087       baseWin->GetParentWidget(getter_AddRefs(widget));
00088       if (!widget) {
00089         nsCOMPtr<nsIDocShellTreeItem> docShellAsItem(do_QueryInterface(baseWin));
00090         if (!docShellAsItem)
00091           return nsnull;
00092 
00093         nsCOMPtr<nsIDocShellTreeItem> parent;
00094         docShellAsItem->GetSameTypeParent(getter_AddRefs(parent));
00095 
00096         sgo = do_GetInterface(parent);
00097         if (!sgo)
00098           return nsnull;
00099 
00100         baseWin = do_QueryInterface(sgo->GetDocShell());
00101       }
00102     }
00103   }
00104 
00105   // This will return a pointer that we're about to release, but
00106   // that's ok since the docshell (nsIBaseWindow) holds the widget
00107   // alive.
00108   return widget.get();
00109 }
00110 
00111 //-------------------------------------------------------------------------
00112 NS_IMETHODIMP nsBaseFilePicker::Init(nsIDOMWindow *aParent,
00113                                      const nsAString& aTitle,
00114                                      PRInt16 aMode)
00115 {
00116   NS_PRECONDITION(aParent, "Null parent passed to filepicker, no file "
00117                   "picker for you!");
00118   nsIWidget *widget = DOMWindowToWidget(aParent);
00119   NS_ENSURE_TRUE(widget, NS_ERROR_FAILURE);
00120 
00121   InitNative(widget, aTitle, aMode);
00122 
00123   return NS_OK;
00124 }
00125 
00126 
00127 NS_IMETHODIMP
00128 nsBaseFilePicker::AppendFilters(PRInt32 aFilterMask)
00129 {
00130   nsresult rv;
00131   nsCOMPtr<nsIStringBundleService> stringService = do_GetService(kStringBundleServiceCID);
00132   nsCOMPtr<nsIStringBundle> stringBundle;
00133 
00134   rv = stringService->CreateBundle(FILEPICKER_PROPERTIES, getter_AddRefs(stringBundle));
00135   if (NS_FAILED(rv))
00136     return NS_ERROR_FAILURE;
00137 
00138   nsXPIDLString title;
00139   nsXPIDLString filter;
00140 
00141   if (aFilterMask & filterAll) {
00142     stringBundle->GetStringFromName(NS_LITERAL_STRING("allTitle").get(), getter_Copies(title));
00143     stringBundle->GetStringFromName(NS_LITERAL_STRING("allFilter").get(), getter_Copies(filter));
00144     AppendFilter(title,filter);
00145   }
00146   if (aFilterMask & filterHTML) {
00147     stringBundle->GetStringFromName(NS_LITERAL_STRING("htmlTitle").get(), getter_Copies(title));
00148     stringBundle->GetStringFromName(NS_LITERAL_STRING("htmlFilter").get(), getter_Copies(filter));
00149     AppendFilter(title,filter);
00150   }
00151   if (aFilterMask & filterText) {
00152     stringBundle->GetStringFromName(NS_LITERAL_STRING("textTitle").get(), getter_Copies(title));
00153     stringBundle->GetStringFromName(NS_LITERAL_STRING("textFilter").get(), getter_Copies(filter));
00154     AppendFilter(title,filter);
00155   }
00156   if (aFilterMask & filterImages) {
00157     stringBundle->GetStringFromName(NS_LITERAL_STRING("imageTitle").get(), getter_Copies(title));
00158     stringBundle->GetStringFromName(NS_LITERAL_STRING("imageFilter").get(), getter_Copies(filter));
00159     AppendFilter(title,filter);
00160   }
00161   if (aFilterMask & filterXML) {
00162     stringBundle->GetStringFromName(NS_LITERAL_STRING("xmlTitle").get(), getter_Copies(title));
00163     stringBundle->GetStringFromName(NS_LITERAL_STRING("xmlFilter").get(), getter_Copies(filter));
00164     AppendFilter(title,filter);
00165   }
00166   if (aFilterMask & filterXUL) {
00167     stringBundle->GetStringFromName(NS_LITERAL_STRING("xulTitle").get(), getter_Copies(title));
00168     stringBundle->GetStringFromName(NS_LITERAL_STRING("xulFilter").get(), getter_Copies(filter));
00169     AppendFilter(title, filter);
00170   }
00171   if (aFilterMask & filterApps) {
00172     stringBundle->GetStringFromName(NS_LITERAL_STRING("appsTitle").get(), getter_Copies(title));
00173     // Pass the magic string "..apps" to the platform filepicker, which it
00174     // should recognize and do the correct platform behavior for.
00175     AppendFilter(title, NS_LITERAL_STRING("..apps"));
00176   }
00177   return NS_OK;
00178 }
00179 
00180 //-------------------------------------------------------------------------
00181 //
00182 // Set the filter index
00183 //
00184 //-------------------------------------------------------------------------
00185 NS_IMETHODIMP nsBaseFilePicker::GetFilterIndex(PRInt32 *aFilterIndex)
00186 {
00187   *aFilterIndex = 0;
00188   return NS_OK;
00189 }
00190 
00191 NS_IMETHODIMP nsBaseFilePicker::SetFilterIndex(PRInt32 aFilterIndex)
00192 {
00193   return NS_OK;
00194 }
00195 
00196 NS_IMETHODIMP nsBaseFilePicker::GetFiles(nsISimpleEnumerator **aFiles)
00197 {
00198   NS_ENSURE_ARG_POINTER(aFiles);
00199   nsCOMPtr <nsISupportsArray> files;
00200   nsresult rv = NS_NewISupportsArray(getter_AddRefs(files));
00201   NS_ENSURE_SUCCESS(rv,rv);
00202 
00203   // if we get into the base class, the platform
00204   // doesn't implement GetFiles() yet.
00205   // so we fake it.
00206   nsCOMPtr <nsILocalFile> file;
00207   rv = GetFile(getter_AddRefs(file));
00208   NS_ENSURE_SUCCESS(rv,rv);
00209 
00210   rv = files->AppendElement(file);
00211   NS_ENSURE_SUCCESS(rv,rv);
00212 
00213   return NS_NewArrayEnumerator(aFiles, files);
00214 }
00215 
00216 #ifdef BASEFILEPICKER_HAS_DISPLAYDIRECTORY
00217 //-------------------------------------------------------------------------
00218 //
00219 // Set the display directory
00220 //
00221 //-------------------------------------------------------------------------
00222 NS_IMETHODIMP nsBaseFilePicker::SetDisplayDirectory(nsILocalFile *aDirectory)
00223 {
00224   if (!aDirectory) {
00225     mDisplayDirectory = nsnull;
00226     return NS_OK;
00227   }
00228   nsCOMPtr<nsIFile> directory;
00229   nsresult rv = aDirectory->Clone(getter_AddRefs(directory));
00230   if (NS_FAILED(rv))
00231     return rv;
00232   mDisplayDirectory = do_QueryInterface(directory, &rv);
00233   return rv;
00234 }
00235 
00236 //-------------------------------------------------------------------------
00237 //
00238 // Get the display directory
00239 //
00240 //-------------------------------------------------------------------------
00241 NS_IMETHODIMP nsBaseFilePicker::GetDisplayDirectory(nsILocalFile **aDirectory)
00242 {
00243   *aDirectory = nsnull;
00244   if (!mDisplayDirectory)
00245     return NS_OK;
00246   nsCOMPtr<nsIFile> directory;
00247   nsresult rv = mDisplayDirectory->Clone(getter_AddRefs(directory));
00248   if (NS_FAILED(rv))
00249     return rv;
00250   return CallQueryInterface(directory, aDirectory);
00251 }
00252 #endif