aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/FilePicker.cpp
blob: 42c17be069f4a70c8de9dd5c4aa7c740466c9e1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 *  Copyright (C) 2001 Philip Langdale
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* Things to be aware of:
 *
 * This filepicker, like the mozilla one, does not make an attempt
 * to verify the validity of the initial directory you pass it.
 * It does check that the user doesn't give it a garbage path
 * during use, but it is the caller's responsibility to give a
 * sensible initial path.
 *
 * At the current moment, we instantiate the filepicker directly
 * in our contenthandler where there is path verification code
 * and else where through our C wrapper, which also does verification.
 * If, at a future date, you need to instantiate filepicker without
 * using the C wrapper, please verify the initial path. See
 * ContentHandler for a way to do this.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "ephy-string.h"
#include "ephy-gui.h"

#include <glib/gconvert.h>
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkmenu.h>
#include <gtk/gtkmenuitem.h>
#include <gtk/gtkcheckbutton.h>
#include <gtk/gtktogglebutton.h>
#include <gtk/gtkfilesel.h>
#include <gtk/gtkhbbox.h>
#include <gtk/gtkoptionmenu.h>
#include <gtk/gtkmessagedialog.h>
#include <libgnome/gnome-i18n.h>

#include "nsIFilePicker.h"

#include "nsCRT.h"
#include "nsCOMPtr.h"
#include "nsISupportsArray.h"
#include "nsIServiceManager.h"

#include "nsString.h"
#include "nsXPIDLString.h"
#include "nsIPrefService.h"
#include "nsIURI.h"
#include "nsIFileURL.h"
#include "nsIChannel.h"
#include "nsIFileChannel.h"
#include "nsNetCID.h"
#include "nsILocalFile.h"
#include "nsIPromptService.h"
#include "nsReadableUtils.h"

#include <libgnome/gnome-util.h>

#include "FilePicker.h"
#include "MozillaPrivate.h"

/* Implementation file */
NS_IMPL_ISUPPORTS1(GFilePicker, nsIFilePicker)

GFilePicker::GFilePicker()
{
    NS_INIT_ISUPPORTS();

    /* member initializers and constructor code */
    mFile = do_CreateInstance (NS_LOCAL_FILE_CONTRACTID);
    mDisplayDirectory = do_CreateInstance (NS_LOCAL_FILE_CONTRACTID);
    mDisplayDirectory->InitWithNativePath(nsDependentCString(g_get_home_dir()));
}

GFilePicker::~GFilePicker()
{
    /* destructor code */
}

////////////////////////////////////////////////////////////////////////////////
// begin nsIFilePicker impl
////////////////////////////////////////////////////////////////////////////////

/* void init (in nsIDOMWindowInternal parent, in wstring title, in short mode); */
NS_IMETHODIMP GFilePicker::Init(nsIDOMWindowInternal *aParent, 
                const PRUnichar *aTitle, PRInt16 aMode)
{
    mParent = do_QueryInterface(aParent);
    mParentWidget = MozillaFindGtkParent(mParent);
    mTitle = NS_ConvertUCS2toUTF8(aTitle);
    mMode = aMode;
    
    return NS_OK;
}

/* void appendFilters (in long filterMask); */
NS_IMETHODIMP GFilePicker::AppendFilters(PRInt32 aFilterMask)
{
    //This function cannot be implemented due to the crippled
    //nature of GtkFileSelection, but NS_ERROR_NOT_IMPLEMENTED
    //is interpreted as a terminal error by some callers.
    return NS_OK;
}

/* void appendFilter (in wstring title, in wstring filter); */
NS_IMETHODIMP GFilePicker::AppendFilter(const PRUnichar *aTitle,
                    const PRUnichar *aFilter)
{
    //GtkFileSelection is crippled, so we can't provide a short-list
    //of filters to choose from. We provide minimal functionality
    //by using the most recent AppendFilter call as the active filter.
    mFilter = NS_ConvertUCS2toUTF8(aFilter);
    return NS_OK;
}

/* attribute long filterIndex; */
NS_IMETHODIMP GFilePicker::GetFilterIndex(PRInt32 *aFilterIndex)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP GFilePicker::SetFilterIndex(PRInt32 aFilterIndex)
{
    return NS_OK;
}

/* attribute wstring defaultString; */
NS_IMETHODIMP GFilePicker::GetDefaultString(PRUnichar * *aDefaultString)
{
    gsize bytesWritten;
    gchar *utf8DefaultString = g_filename_to_utf8(mDefaultString.get(), -1,
                              NULL,
                              &bytesWritten, NULL);

    *aDefaultString = ToNewUnicode(NS_ConvertUTF8toUCS2(utf8DefaultString));
    g_free(utf8DefaultString);

    return NS_OK;
}
NS_IMETHODIMP GFilePicker::SetDefaultString(const PRUnichar *aDefaultString)
{
    if (aDefaultString)
    {
        gsize bytesWritten;
        gchar *localeDefaultString =
            g_filename_from_utf8(NS_ConvertUCS2toUTF8(aDefaultString).get(),
                         -1, NULL,
                         &bytesWritten, NULL);
        mDefaultString = localeDefaultString;                        
        g_free(localeDefaultString);
    }
    else
        mDefaultString = "";
    return NS_OK;
}

/* attribute wstring defaultExtension; */
// Again, due to the crippled file selector, we can't really
// do anything here.
NS_IMETHODIMP GFilePicker::GetDefaultExtension(PRUnichar * *aDefaultExtension)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP GFilePicker::SetDefaultExtension(const PRUnichar *aDefaultExtension)
{
    return NS_OK;
}

/* attribute nsILocalFile displayDirectory; */
NS_IMETHODIMP GFilePicker::GetDisplayDirectory(nsILocalFile * *aDisplayDirectory)
{
    NS_IF_ADDREF(*aDisplayDirectory = mDisplayDirectory);
    return NS_OK;
}
NS_IMETHODIMP GFilePicker::SetDisplayDirectory(nsILocalFile * aDisplayDirectory)
{
    mDisplayDirectory = aDisplayDirectory;
    return NS_OK;
}

/* readonly attribute nsILocalFile file; */
NS_IMETHODIMP GFilePicker::GetFile(nsILocalFile * *aFile)
{
    NS_IF_ADDREF(*aFile = mFile);
    return NS_OK;
}

/* readonly attribute nsIFileURL fileURL; */
NS_IMETHODIMP GFilePicker::GetFileURL(nsIFileURL * *aFileURL)
{
    nsCOMPtr<nsIFileURL> fileURL = 
        do_CreateInstance(NS_STANDARDURL_CONTRACTID);
    fileURL->SetFile(mFile);
    NS_IF_ADDREF(*aFileURL = fileURL);
    return NS_OK;
}

/* readonly attribute nsISimpleEnumerator files; */
NS_IMETHODIMP GFilePicker::GetFiles(nsISimpleEnumerator * *aFiles)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

/* short show (); */
NS_IMETHODIMP GFilePicker::Show(PRInt16 *_retval)
{
    mFileSelector = gtk_file_selection_new(mTitle.get());

    nsCAutoString cFileName;
    if(mMode == nsIFilePicker::modeGetFolder)
        cFileName.Assign("");
    else
        cFileName = mDefaultString;

    nsCAutoString cDirName;
    mDisplayDirectory->GetNativePath(cDirName);

    nsCAutoString cFullPath;
    cFullPath.Assign(cDirName + NS_LITERAL_CSTRING("/") + cFileName);
    gtk_file_selection_set_filename(GTK_FILE_SELECTION(mFileSelector),
                    cFullPath.get());

    if (!mFilter.IsEmpty())
    {
        gtk_file_selection_complete(GTK_FILE_SELECTION(mFileSelector),
                        mFilter.get());
    }

    if (mParentWidget)
        gtk_window_set_transient_for(GTK_WINDOW(mFileSelector),
                         GTK_WINDOW(mParentWidget));

    if (mMode == nsIFilePicker::modeGetFolder)
    {
        gtk_widget_set_sensitive(GTK_FILE_SELECTION(mFileSelector)
                     ->file_list, FALSE);
    }

    gtk_window_set_modal(GTK_WINDOW(mFileSelector), TRUE);

    gint retVal = gtk_dialog_run(GTK_DIALOG(mFileSelector));
    
    HandleFilePickerResult();

    if (retVal != GTK_RESPONSE_OK)
    {
        *_retval = returnCancel;
    }
    else
    {   
        ValidateFilePickerResult(_retval);
    }

    gtk_widget_destroy(mFileSelector);

    return NS_OK;
}

////////////////////////////////////////////////////////////////////////////////
// begin local public methods impl
////////////////////////////////////////////////////////////////////////////////

NS_METHOD GFilePicker::InitWithGtkWidget (GtkWidget *aParentWidget, 
                      const char *aTitle, PRInt16 aMode)
{
    mParentWidget = aParentWidget;

    mTitle = nsDependentCString(aTitle);

    mMode = aMode;

    mFile = do_CreateInstance (NS_LOCAL_FILE_CONTRACTID);

    return NS_OK;
}

NS_METHOD GFilePicker::SanityCheck (PRBool *retIsSane)
{
    *retIsSane = PR_TRUE;
    
    nsresult rv;
    PRBool dirExists, fileExists = PR_TRUE;

    if (mDisplayDirectory)
    {
        rv = mDisplayDirectory->Exists (&dirExists);
        g_return_val_if_fail (NS_SUCCEEDED(rv), rv);
    }
    else
    {
        dirExists = PR_FALSE;
    }

    if (mMode != nsIFilePicker::modeGetFolder)
    {
        rv = mFile->Exists (&fileExists);
        g_return_val_if_fail (NS_SUCCEEDED(rv), rv);
    }

    if (mMode == nsIFilePicker::modeSave && !fileExists)
    {
        return NS_OK;
    }
    
    if (!dirExists || !fileExists)
    {
        GtkWidget *errorDialog = gtk_message_dialog_new (
                NULL,
                GTK_DIALOG_MODAL,
                GTK_MESSAGE_ERROR,
                GTK_BUTTONS_OK,
                _("The specified path does not exist."));

        if (mParentWidget)
            gtk_window_set_transient_for(GTK_WINDOW(errorDialog),
                             GTK_WINDOW(mFileSelector));

        gtk_window_set_modal (GTK_WINDOW(errorDialog), TRUE);
        gtk_dialog_run (GTK_DIALOG(errorDialog));
        gtk_widget_destroy (errorDialog);
        *retIsSane = PR_FALSE;
        return NS_OK;
    }

    PRBool correctType;
    char *errorText;
    if (mMode == nsIFilePicker::modeGetFolder)
    {
        rv = mDisplayDirectory->IsDirectory (&correctType);
        g_return_val_if_fail (NS_SUCCEEDED(rv), rv);
        errorText = g_strdup (_("A file was selected when a "
                    "folder was expected."));
    }
    else
    {
        rv = mFile->IsFile (&correctType);
        g_return_val_if_fail (NS_SUCCEEDED(rv), rv);
        errorText = g_strdup (_("A folder was selected when a "
                        "file was expected."));
    }
    
    if(!correctType)
    {
        GtkWidget *errorDialog = gtk_message_dialog_new (
                NULL,
                GTK_DIALOG_MODAL,
                GTK_MESSAGE_ERROR,
                GTK_BUTTONS_OK,
                errorText);

        if (mParentWidget)
            gtk_window_set_transient_for(GTK_WINDOW(errorDialog),
                             GTK_WINDOW(mFileSelector));

        gtk_window_set_modal (GTK_WINDOW(errorDialog), TRUE);
        gtk_dialog_run (GTK_DIALOG(errorDialog));
        gtk_widget_destroy (errorDialog);
        *retIsSane = PR_FALSE;
    }
    g_free (errorText);

    return NS_OK;
}

////////////////////////////////////////////////////////////////////////////////
// begin local private methods impl
////////////////////////////////////////////////////////////////////////////////

NS_METHOD GFilePicker::HandleFilePickerResult()
{
    const char *fileName = gtk_file_selection_get_filename(GTK_FILE_SELECTION(mFileSelector));

    if (!fileName || strlen(fileName) == 0) return NS_ERROR_FAILURE;

    const nsACString &cFileName = nsDependentCString(fileName);
    mFile->InitWithNativePath(cFileName);

    if (mMode == nsIFilePicker::modeGetFolder)
    {
        mDisplayDirectory->InitWithNativePath(cFileName);
        mDefaultString = "";
    }
    else
    {
        nsCOMPtr<nsIFile> directory;
        mFile->GetParent(getter_AddRefs(directory));
        mDisplayDirectory = do_QueryInterface(directory);
        mFile->GetNativeLeafName(mDefaultString);
    }

    return NS_OK;
}

NS_METHOD GFilePicker::ValidateFilePickerResult(PRInt16 *retval)
{
    nsresult rv;
    const char *fileName = gtk_file_selection_get_filename(GTK_FILE_SELECTION(mFileSelector));

    *retval = returnCancel;

    PRBool passesSanityCheck;
    rv = SanityCheck(&passesSanityCheck);
    if (NS_SUCCEEDED(rv) && !passesSanityCheck) return NS_ERROR_FAILURE;

    if (mMode == nsIFilePicker::modeSave)
    {
        if (!ephy_gui_confirm_overwrite_file (mFileSelector,
                                  fileName))
        {
            return NS_OK;
        }
    }

    *retval = returnOK;

    return NS_OK;
}