~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/widget/src/photon/nsFilePicker.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 
2
 *
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 * 
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 * 
 
13
 * The Original Code is the Mozilla browser.
 
14
 * 
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications Corporation. Portions created by Netscape are
 
17
 * Copyright (C) 2001 Netscape Communications Corporation. All
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s):
 
21
 *   Adrian Mardare <amardare@qnx.com>
 
22
 */
 
23
 
 
24
 
 
25
#include "nsCOMPtr.h"
 
26
#include "nsReadableUtils.h"
 
27
#include "nsNetUtil.h"
 
28
#include "nsWindow.h"
 
29
#include "nsIServiceManager.h"
 
30
#include "nsIPlatformCharset.h"
 
31
#include "nsFilePicker.h"
 
32
#include "nsILocalFile.h"
 
33
#include "nsIURL.h"
 
34
#include "nsIFileURL.h"
 
35
#include "nsIStringBundle.h"
 
36
#include "nsEnumeratorUtils.h"
 
37
#include "nsCRT.h"
 
38
 
 
39
 
 
40
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
 
41
 
 
42
NS_IMPL_ISUPPORTS1(nsFilePicker, nsIFilePicker)
 
43
 
 
44
char nsFilePicker::mLastUsedDirectory[PATH_MAX+1] = { 0 };
 
45
 
 
46
#define MAX_EXTENSION_LENGTH PATH_MAX
 
47
 
 
48
//-------------------------------------------------------------------------
 
49
//
 
50
// nsFilePicker constructor
 
51
//
 
52
//-------------------------------------------------------------------------
 
53
nsFilePicker::nsFilePicker()
 
54
        : mParentWidget( nsnull )
 
55
  , mUnicodeEncoder(nsnull)
 
56
  , mUnicodeDecoder(nsnull)
 
57
{
 
58
  mDisplayDirectory = do_CreateInstance("@mozilla.org/file/local;1");
 
59
        char *path = getenv( "HOME" );
 
60
        if( path ) {
 
61
                mDisplayDirectory->InitWithNativePath( nsDependentCString(path) );
 
62
                }
 
63
}
 
64
 
 
65
//-------------------------------------------------------------------------
 
66
//
 
67
// nsFilePicker destructor
 
68
//
 
69
//-------------------------------------------------------------------------
 
70
nsFilePicker::~nsFilePicker()
 
71
{
 
72
  NS_IF_RELEASE(mUnicodeEncoder);
 
73
  NS_IF_RELEASE(mUnicodeDecoder);
 
74
}
 
75
 
 
76
//-------------------------------------------------------------------------
 
77
//
 
78
// Show - Display the file dialog
 
79
//
 
80
//-------------------------------------------------------------------------
 
81
NS_IMETHODIMP nsFilePicker::Show(PRInt16 *aReturnVal)
 
82
{
 
83
        PRInt32 flags = 0;
 
84
        char *btn1;
 
85
 
 
86
        NS_ENSURE_ARG_POINTER(aReturnVal);
 
87
 
 
88
  if (mMode == modeGetFolder) {
 
89
                flags |= Pt_FSR_SELECT_DIRS|Pt_FSR_NO_SELECT_FILES;
 
90
                btn1 = "&Select";
 
91
  }
 
92
  else if (mMode == modeOpen) {
 
93
                btn1 = "&Open";
 
94
  }
 
95
  else if (mMode == modeSave) {
 
96
                flags |= Pt_FSR_NO_FCHECK;
 
97
                btn1 = "&Save";
 
98
  }
 
99
        else if( mMode == modeOpenMultiple ) {
 
100
                flags |= Pt_FSR_MULTIPLE;
 
101
                btn1 = "&Select";
 
102
                }
 
103
  else {
 
104
    printf("nsFilePicker::Show() wrong mode");
 
105
    return PR_FALSE;
 
106
  }
 
107
 
 
108
  char *title = ConvertToFileSystemCharset(mTitle);
 
109
  if (nsnull == title)
 
110
    title = ToNewCString(mTitle);
 
111
 
 
112
  nsCAutoString initialDir;
 
113
  mDisplayDirectory->GetNativePath(initialDir);
 
114
  // If no display directory, re-use the last one.
 
115
  if(initialDir.IsEmpty()) {
 
116
    // Allocate copy of last used dir.
 
117
    initialDir = mLastUsedDirectory;
 
118
  }
 
119
 
 
120
        if( !mDefault.IsEmpty() ) {
 
121
                initialDir.AppendWithConversion( NS_LITERAL_STRING( "/" ) );
 
122
                initialDir.AppendWithConversion( mDefault );
 
123
                }
 
124
 
 
125
        char extensionBuffer[MAX_EXTENSION_LENGTH+1] = "*";
 
126
        if( !mFilterList.IsEmpty() ) {
 
127
                char *text = ConvertToFileSystemCharset( mFilterList );
 
128
                if( text ) {
 
129
                        extensionBuffer[0] = 0;
 
130
 
 
131
                        /* eliminate the ';' and the duplicates */
 
132
                        char buffer[MAX_EXTENSION_LENGTH+1], buf[MAX_EXTENSION_LENGTH+1], *q, *delims = "; ", *dummy;
 
133
                        strcpy( buffer, text );
 
134
                        q = strtok_r( buffer, delims, &dummy );
 
135
                        while( q ) {
 
136
                                sprintf( buf, "%s ", q );
 
137
                                if( !strstr( extensionBuffer, buf ) )
 
138
                                        strcat( extensionBuffer, buf );
 
139
                                q = strtok_r( NULL, delims, &dummy );
 
140
                                }
 
141
 
 
142
                        nsMemory::Free( text );
 
143
                        }
 
144
                }
 
145
        else if (!mDefaultExtension.IsEmpty()) {
 
146
                // Someone was cool and told us what to do
 
147
                char *convertedExt = ConvertToFileSystemCharset(mDefaultExtension);
 
148
                if (!convertedExt) {
 
149
                        mDefaultExtension.ToCString(extensionBuffer, MAX_EXTENSION_LENGTH);
 
150
                        }
 
151
                else {
 
152
                        PL_strncpyz(extensionBuffer, convertedExt, MAX_EXTENSION_LENGTH+1);
 
153
                        nsMemory::Free( convertedExt );
 
154
                        }
 
155
                }
 
156
 
 
157
        PtFileSelectionInfo_t info;
 
158
        memset( &info, 0, sizeof( info ) );
 
159
 
 
160
        if( PtFileSelection( mParentWidget, NULL, title, initialDir.get(),
 
161
                extensionBuffer, btn1, "&Cancel", "nsd", &info, flags ) ) {
 
162
                        if (title) nsMemory::Free( title );
 
163
                        return NS_ERROR_FAILURE;
 
164
                        }
 
165
 
 
166
        *aReturnVal = returnOK;
 
167
 
 
168
        if( info.ret == Pt_FSDIALOG_BTN2 ) {
 
169
                *aReturnVal = returnCancel;
 
170
                }
 
171
        else if( mMode != modeOpenMultiple ) {
 
172
                mFile.SetLength(0);
 
173
                mFile.Append( info.path );
 
174
 
 
175
                if( mMode == modeSave ) {
 
176
                        nsCOMPtr<nsILocalFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
 
177
                        NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
 
178
 
 
179
                        file->InitWithNativePath( mFile );
 
180
                        
 
181
                        PRBool exists = PR_FALSE;
 
182
                        file->Exists(&exists);
 
183
                        if (exists)
 
184
                                *aReturnVal = returnReplace;
 
185
                        }
 
186
                }
 
187
        else { /* here mMode is modeOpenMultiple */
 
188
                PtFileSelectorInfo_t *minfo = info.minfo;
 
189
                if( minfo ) {
 
190
                        nsresult rv = NS_NewISupportsArray(getter_AddRefs(mFiles));
 
191
                        NS_ENSURE_SUCCESS(rv,rv);
 
192
 
 
193
                        for( int i=0; i<minfo->nitems; i++ ) {
 
194
                                nsCOMPtr<nsILocalFile> file = do_CreateInstance("@mozilla.org/file/local;1", &rv);
 
195
                                NS_ENSURE_SUCCESS(rv,rv);
 
196
        
 
197
                                nsCString s ( minfo->multipath[i] );
 
198
                                rv = file->InitWithNativePath( s );
 
199
                                NS_ENSURE_SUCCESS(rv,rv);
 
200
        
 
201
                                rv = mFiles->AppendElement(file);
 
202
                                NS_ENSURE_SUCCESS(rv,rv);
 
203
                                }
 
204
 
 
205
                        PtFSFreeInfo( &info ); /* clean the info structure if the multiple mode is set */
 
206
                        }
 
207
                }
 
208
 
 
209
        PL_strncpyz( mLastUsedDirectory, info.path, PATH_MAX+1 );
 
210
        mDisplayDirectory->InitWithNativePath( nsDependentCString(mLastUsedDirectory) );
 
211
 
 
212
        if( title ) nsMemory::Free( title );
 
213
                
 
214
  return NS_OK;
 
215
 
 
216
// TODO: implement filters
 
217
}
 
218
 
 
219
 
 
220
 
 
221
NS_IMETHODIMP nsFilePicker::GetFile(nsILocalFile **aFile)
 
222
{
 
223
  NS_ENSURE_ARG_POINTER(aFile);
 
224
 
 
225
  if (mFile.IsEmpty())
 
226
      return NS_OK;
 
227
 
 
228
  nsCOMPtr<nsILocalFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
 
229
    
 
230
  NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
 
231
 
 
232
  file->InitWithNativePath(mFile);
 
233
 
 
234
  NS_ADDREF(*aFile = file);
 
235
 
 
236
  return NS_OK;
 
237
}
 
238
 
 
239
NS_IMETHODIMP nsFilePicker::GetFiles(nsISimpleEnumerator **aFiles)
 
240
{
 
241
        NS_ENSURE_ARG_POINTER(aFiles);
 
242
        return NS_NewArrayEnumerator(aFiles, mFiles);
 
243
}
 
244
 
 
245
//-------------------------------------------------------------------------
 
246
NS_IMETHODIMP nsFilePicker::GetFileURL(nsIFileURL **aFileURL)
 
247
{
 
248
  nsCOMPtr<nsILocalFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
 
249
  NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
 
250
  file->InitWithNativePath(mFile);
 
251
 
 
252
  nsCOMPtr<nsIURI> uri;
 
253
  NS_NewFileURI(getter_AddRefs(uri), file);
 
254
  nsCOMPtr<nsIFileURL> fileURL(do_QueryInterface(uri));
 
255
  NS_ENSURE_TRUE(fileURL, NS_ERROR_FAILURE);
 
256
  
 
257
  NS_ADDREF(*aFileURL = fileURL);
 
258
 
 
259
  return NS_OK;
 
260
}
 
261
 
 
262
//-------------------------------------------------------------------------
 
263
//
 
264
// Get the file + path
 
265
//
 
266
//-------------------------------------------------------------------------
 
267
NS_IMETHODIMP nsFilePicker::SetDefaultString(const nsAString& aString)
 
268
{
 
269
  mDefault = aString;
 
270
  return NS_OK;
 
271
}
 
272
 
 
273
NS_IMETHODIMP nsFilePicker::GetDefaultString(nsAString& aString)
 
274
{
 
275
  return NS_ERROR_FAILURE;
 
276
}
 
277
 
 
278
//-------------------------------------------------------------------------
 
279
//
 
280
// The default extension to use for files
 
281
//
 
282
//-------------------------------------------------------------------------
 
283
NS_IMETHODIMP nsFilePicker::GetDefaultExtension(nsAString& aExtension)
 
284
{
 
285
  aExtension = mDefaultExtension;
 
286
  return NS_OK;
 
287
}
 
288
 
 
289
NS_IMETHODIMP nsFilePicker::SetDefaultExtension(const nsAString& aExtension)
 
290
{
 
291
        mDefaultExtension = aExtension;
 
292
  return NS_OK;
 
293
}
 
294
 
 
295
//-------------------------------------------------------------------------
 
296
//
 
297
// Set the display directory
 
298
//
 
299
//-------------------------------------------------------------------------
 
300
NS_IMETHODIMP nsFilePicker::SetDisplayDirectory(nsILocalFile *aDirectory)
 
301
{
 
302
  mDisplayDirectory = aDirectory;
 
303
  return NS_OK;
 
304
}
 
305
 
 
306
//-------------------------------------------------------------------------
 
307
//
 
308
// Get the display directory
 
309
//
 
310
//-------------------------------------------------------------------------
 
311
NS_IMETHODIMP nsFilePicker::GetDisplayDirectory(nsILocalFile **aDirectory)
 
312
{
 
313
  *aDirectory = mDisplayDirectory;
 
314
  NS_IF_ADDREF(*aDirectory);
 
315
  return NS_OK;
 
316
}
 
317
 
 
318
//-------------------------------------------------------------------------
 
319
void nsFilePicker::InitNative(nsIWidget *aParent,
 
320
                              const nsAString& aTitle,
 
321
                              PRInt16 aMode)
 
322
{
 
323
        mParentWidget = (PtWidget_t *)aParent->GetNativeData(NS_NATIVE_WIDGET);
 
324
  mTitle.SetLength(0);
 
325
  mTitle.Append(aTitle);
 
326
  mMode = aMode;
 
327
}
 
328
 
 
329
 
 
330
NS_IMETHODIMP
 
331
nsFilePicker::AppendFilter(const nsAString& aTitle, const nsAString& aFilter)
 
332
{
 
333
  mFilterList.Append(aFilter);
 
334
        mFilterList.Append(PRUnichar(' '));
 
335
 
 
336
  return NS_OK;
 
337
}
 
338
 
 
339
 
 
340
//-------------------------------------------------------------------------
 
341
void nsFilePicker::GetFileSystemCharset(nsCString & fileSystemCharset)
 
342
{
 
343
  static nsCAutoString aCharset;
 
344
  nsresult rv;
 
345
 
 
346
  if (aCharset.Length() < 1) {
 
347
    nsCOMPtr <nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
 
348
    if (NS_SUCCEEDED(rv))
 
349
      rv = platformCharset->GetCharset(kPlatformCharsetSel_FileName, aCharset);
 
350
 
 
351
    NS_ASSERTION(NS_SUCCEEDED(rv), "error getting platform charset");
 
352
    if (NS_FAILED(rv))
 
353
      aCharset.Assign(NS_LITERAL_CSTRING("windows-1252"));
 
354
  }
 
355
  fileSystemCharset = aCharset;
 
356
}
 
357
 
 
358
 
 
359
//-------------------------------------------------------------------------
 
360
char * nsFilePicker::ConvertToFileSystemCharset(const nsAString& inString)
 
361
{
 
362
  char *outString = nsnull;
 
363
  nsresult rv = NS_OK;
 
364
 
 
365
  // get file system charset and create a unicode encoder
 
366
  if (nsnull == mUnicodeEncoder) {
 
367
    nsCAutoString fileSystemCharset;
 
368
    GetFileSystemCharset(fileSystemCharset);
 
369
 
 
370
    nsCOMPtr<nsICharsetConverterManager> ccm = 
 
371
             do_GetService(kCharsetConverterManagerCID, &rv); 
 
372
    if (NS_SUCCEEDED(rv)) {
 
373
      rv = ccm->GetUnicodeEncoderRaw(fileSystemCharset.get(), &mUnicodeEncoder);
 
374
    }
 
375
  }
 
376
 
 
377
  // converts from unicode to the file system charset
 
378
  if (NS_SUCCEEDED(rv)) {
 
379
    PRInt32 inLength = inString.Length();
 
380
 
 
381
    const nsAFlatString& flatInString = PromiseFlatString(inString);
 
382
 
 
383
    PRInt32 outLength;
 
384
    rv = mUnicodeEncoder->GetMaxLength(flatInString.get(), inLength,
 
385
                                       &outLength);
 
386
    if (NS_SUCCEEDED(rv)) {
 
387
      outString = NS_STATIC_CAST( char*, nsMemory::Alloc( outLength+1 ) );
 
388
      if (nsnull == outString) {
 
389
        return nsnull;
 
390
      }
 
391
      rv = mUnicodeEncoder->Convert(flatInString.get(), &inLength, outString,
 
392
                                    &outLength);
 
393
      if (NS_SUCCEEDED(rv)) {
 
394
        outString[outLength] = '\0';
 
395
      }
 
396
    }
 
397
  }
 
398
  
 
399
  return NS_SUCCEEDED(rv) ? outString : nsnull;
 
400
}
 
401
 
 
402
//-------------------------------------------------------------------------
 
403
PRUnichar * nsFilePicker::ConvertFromFileSystemCharset(const char *inString)
 
404
{
 
405
  PRUnichar *outString = nsnull;
 
406
  nsresult rv = NS_OK;
 
407
 
 
408
  // get file system charset and create a unicode encoder
 
409
  if (nsnull == mUnicodeDecoder) {
 
410
    nsCAutoString fileSystemCharset;
 
411
    GetFileSystemCharset(fileSystemCharset);
 
412
 
 
413
    nsCOMPtr<nsICharsetConverterManager> ccm = 
 
414
             do_GetService(kCharsetConverterManagerCID, &rv); 
 
415
    if (NS_SUCCEEDED(rv)) {
 
416
      rv = ccm->GetUnicodeDecoderRaw(fileSystemCharset.get(), &mUnicodeDecoder);
 
417
    }
 
418
  }
 
419
 
 
420
  // converts from the file system charset to unicode
 
421
  if (NS_SUCCEEDED(rv)) {
 
422
    PRInt32 inLength = strlen(inString);
 
423
    PRInt32 outLength;
 
424
    rv = mUnicodeDecoder->GetMaxLength(inString, inLength, &outLength);
 
425
    if (NS_SUCCEEDED(rv)) {
 
426
      outString = NS_STATIC_CAST( PRUnichar*, nsMemory::Alloc( (outLength+1) * sizeof( PRUnichar ) ) );
 
427
      if (nsnull == outString) {
 
428
        return nsnull;
 
429
      }
 
430
      rv = mUnicodeDecoder->Convert(inString, &inLength, outString, &outLength);
 
431
      if (NS_SUCCEEDED(rv)) {
 
432
        outString[outLength] = 0;
 
433
      }
 
434
    }
 
435
  }
 
436
 
 
437
  NS_ASSERTION(NS_SUCCEEDED(rv), "error charset conversion");
 
438
  return NS_SUCCEEDED(rv) ? outString : nsnull;
 
439
}
 
440
 
 
441
//-------------------------------------------------------------------------
 
442
//
 
443
// Set the filter index
 
444
//
 
445
//-------------------------------------------------------------------------
 
446
NS_IMETHODIMP nsFilePicker::GetFilterIndex(PRInt32 *aFilterIndex)
 
447
{
 
448
  return NS_OK;
 
449
}
 
450
 
 
451
NS_IMETHODIMP nsFilePicker::SetFilterIndex(PRInt32 aFilterIndex)
 
452
{
 
453
  return NS_OK;
 
454
}