Back to index

lightning-sunbird  0.9+nobinonly
nsFilePicker.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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 mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  *  Zack Rusin <zack@kde.org>.
00020  * Portions created by the Initial Developer are Copyright (C) 2004
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Lars Knoll <knoll@kde.org>
00025  *   Zack Rusin <zack@kde.org>
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 #include "nsFilePicker.h"
00041 
00042 #include "nsILocalFile.h"
00043 #include "nsIFileURL.h"
00044 #include "nsIURI.h"
00045 #include "nsISupportsArray.h"
00046 #include "nsMemory.h"
00047 #include "nsEnumeratorUtils.h"
00048 #include "nsNetUtil.h"
00049 #include "nsReadableUtils.h"
00050 
00051 #include <qfiledialog.h>
00052 #include <qfile.h>
00053 #include <qstringlist.h>
00054 
00055 /* Implementation file */
00056 NS_IMPL_ISUPPORTS1(nsFilePicker, nsIFilePicker)
00057 
00058 nsFilePicker::nsFilePicker()
00059     : mDialog(0),
00060       mMode(nsIFilePicker::modeOpen)
00061 {
00062 }
00063 
00064 nsFilePicker::~nsFilePicker()
00065 {
00066     delete mDialog;
00067 }
00068 
00069 NS_IMETHODIMP
00070 nsFilePicker::Init(nsIDOMWindow *parent, const nsAString & title, PRInt16 mode)
00071 {
00072     return nsBaseFilePicker::Init(parent, title, mode);
00073 }
00074 
00075 /* void appendFilters (in long filterMask); */
00076 NS_IMETHODIMP
00077 nsFilePicker::AppendFilters(PRInt32 filterMask)
00078 {
00079     return nsBaseFilePicker::AppendFilters(filterMask);
00080 }
00081 
00082 /* void appendFilter (in AString title, in AString filter); */
00083 NS_IMETHODIMP
00084 nsFilePicker::AppendFilter(const nsAString & aTitle, const nsAString & aFilter)
00085 {
00086     if (aFilter.Equals(NS_LITERAL_STRING("..apps"))) {
00087         // No platform specific thing we can do here, really....
00088         return NS_OK;
00089     }
00090 
00091     nsCAutoString filter, name;
00092     CopyUTF16toUTF8(aFilter, filter);
00093     CopyUTF16toUTF8(aTitle, name);
00094 
00095     mFilters.AppendCString(filter);
00096     mFilterNames.AppendCString(name);
00097 
00098     return NS_OK;
00099 }
00100 
00101 /* attribute AString defaultString; */
00102 NS_IMETHODIMP
00103 nsFilePicker::GetDefaultString(nsAString & aDefaultString)
00104 {
00105     mDefault = aDefaultString;
00106 
00107     return NS_OK;
00108 }
00109 NS_IMETHODIMP
00110 nsFilePicker::SetDefaultString(const nsAString & aDefaultString)
00111 {
00112     return NS_ERROR_FAILURE;
00113 }
00114 
00115 /* attribute AString defaultExtension; */
00116 NS_IMETHODIMP
00117 nsFilePicker::GetDefaultExtension(nsAString & aDefaultExtension)
00118 {
00119     aDefaultExtension = mDefaultExtension;
00120 
00121     return NS_OK;
00122 }
00123 NS_IMETHODIMP
00124 nsFilePicker::SetDefaultExtension(const nsAString & aDefaultExtension)
00125 {
00126     mDefaultExtension = aDefaultExtension;
00127 
00128     return NS_OK;
00129 }
00130 
00131 /* attribute long filterIndex; */
00132 NS_IMETHODIMP
00133 nsFilePicker::GetFilterIndex(PRInt32 *aFilterIndex)
00134 {
00135     *aFilterIndex = mSelectedType;
00136 
00137     return NS_OK;
00138 }
00139 NS_IMETHODIMP
00140 nsFilePicker::SetFilterIndex(PRInt32 aFilterIndex)
00141 {
00142     mSelectedType = aFilterIndex;
00143 
00144     return NS_OK;
00145 }
00146 
00147 /* readonly attribute nsILocalFile file; */
00148 NS_IMETHODIMP
00149 nsFilePicker::GetFile(nsILocalFile * *aFile)
00150 {
00151     NS_ENSURE_ARG_POINTER(aFile);
00152 
00153     *aFile = nsnull;
00154     if (mFile.IsEmpty()) {
00155         return NS_OK;
00156     }
00157 
00158     nsCOMPtr<nsILocalFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
00159     NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
00160 
00161     file->InitWithNativePath(mFile);
00162 
00163     NS_ADDREF(*aFile = file);
00164 
00165     return NS_OK;
00166 }
00167 
00168 /* readonly attribute nsIFileURL fileURL; */
00169 NS_IMETHODIMP
00170 nsFilePicker::GetFileURL(nsIFileURL * *aFileURL)
00171 {
00172     nsCOMPtr<nsILocalFile> file;
00173     GetFile(getter_AddRefs(file));
00174 
00175     nsCOMPtr<nsIURI> uri;
00176     NS_NewFileURI(getter_AddRefs(uri), file);
00177     NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
00178 
00179     return CallQueryInterface(uri, aFileURL);
00180 }
00181 
00182 /* readonly attribute nsISimpleEnumerator files; */
00183 NS_IMETHODIMP
00184 nsFilePicker::GetFiles(nsISimpleEnumerator * *aFiles)
00185 {
00186     NS_ENSURE_ARG_POINTER(aFiles);
00187 
00188     if (mMode == nsIFilePicker::modeOpenMultiple) {
00189         return NS_NewArrayEnumerator(aFiles, mFiles);
00190     }
00191 
00192     return NS_ERROR_FAILURE;
00193 }
00194 
00195 /* short show (); */
00196 NS_IMETHODIMP
00197 nsFilePicker::Show(PRInt16 *aReturn)
00198 {
00199     nsCAutoString directory;
00200     if (mDisplayDirectory) {
00201         mDisplayDirectory->GetNativePath(directory);
00202     }
00203 
00204     switch (mMode) {
00205     case nsIFilePicker::modeOpen:
00206         break;
00207     case nsIFilePicker::modeOpenMultiple:
00208         mDialog->setMode(QFileDialog::ExistingFiles);
00209         break;
00210     case nsIFilePicker::modeSave:
00211         mDialog->setMode(QFileDialog::AnyFile);
00212         break;
00213     case nsIFilePicker::modeGetFolder:
00214         mDialog->setMode(QFileDialog::DirectoryOnly);
00215         break;
00216     default:
00217         break;
00218     }
00219 
00220     mDialog->setSelection(QString::fromUcs2(mDefault.get()));
00221 
00222     mDialog->setDir(directory.get());
00223 
00224     QStringList filters;
00225     PRInt32 count = mFilters.Count();
00226     for (PRInt32 i = 0; i < count; ++i) {
00227         filters.append( mFilters[i]->get() );
00228     }
00229     mDialog->setFilters(filters);
00230 
00231     switch (mDialog->exec()) {
00232     case QDialog::Accepted: {
00233         QCString path = QFile::encodeName(mDialog->selectedFile());
00234         qDebug("path is '%s'", path.data());
00235         mFile.Assign(path);
00236         *aReturn = nsIFilePicker::returnOK;
00237         if (mMode == modeSave) {
00238             nsCOMPtr<nsILocalFile> file;
00239             GetFile(getter_AddRefs(file));
00240             if (file) {
00241                 PRBool exists = PR_FALSE;
00242                 file->Exists(&exists);
00243                 if (exists) {
00244                     *aReturn = nsIFilePicker::returnReplace;
00245                 }
00246             }
00247         }
00248     }
00249         break;
00250     case QDialog::Rejected: {
00251         *aReturn = nsIFilePicker::returnCancel;
00252     }
00253         break;
00254     default:
00255         *aReturn = nsIFilePicker::returnCancel;
00256         break;
00257     }
00258 
00259 
00260     return NS_OK;
00261 }
00262 
00263 void nsFilePicker::InitNative(nsIWidget *parent, const nsAString &title, PRInt16 mode)
00264 {
00265     QWidget *parentWidget = (parent)? (QWidget*)parent->GetNativeData(NS_NATIVE_WIDGET):0;
00266 
00267     nsAutoString str(title);
00268     mDialog = new QFileDialog(parentWidget);
00269     mDialog->setCaption(QString::fromUcs2(str.get()));
00270     mMode = mode;
00271 }