1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  *
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef nsFilePicker_h__
8 #define nsFilePicker_h__
9 
10 #include <windows.h>
11 
12 #include "nsIFile.h"
13 #include "nsISimpleEnumerator.h"
14 #include "nsCOMArray.h"
15 #include "nsBaseFilePicker.h"
16 #include "nsString.h"
17 #include "nsdefs.h"
18 #include <commdlg.h>
19 #include <shobjidl.h>
20 #undef LogSeverity  // SetupAPI.h #defines this as DWORD
21 
22 class nsILoadContext;
23 
24 class nsBaseWinFilePicker : public nsBaseFilePicker {
25  public:
26   NS_IMETHOD GetDefaultString(nsAString& aDefaultString) override;
27   NS_IMETHOD SetDefaultString(const nsAString& aDefaultString) override;
28   NS_IMETHOD GetDefaultExtension(nsAString& aDefaultExtension) override;
29   NS_IMETHOD SetDefaultExtension(const nsAString& aDefaultExtension) override;
30 
31  protected:
32   nsString mDefaultFilePath;
33   nsString mDefaultFilename;
34   nsString mDefaultExtension;
35 };
36 
37 /**
38  * Native Windows FileSelector wrapper
39  */
40 
41 class nsFilePicker : public nsBaseWinFilePicker {
42   virtual ~nsFilePicker() = default;
43 
44  public:
45   nsFilePicker();
46 
47   NS_IMETHOD Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
48                   int16_t aMode) override;
49 
50   NS_DECL_ISUPPORTS
51 
52   // nsIFilePicker (less what's in nsBaseFilePicker and nsBaseWinFilePicker)
53   NS_IMETHOD GetFilterIndex(int32_t* aFilterIndex) override;
54   NS_IMETHOD SetFilterIndex(int32_t aFilterIndex) override;
55   NS_IMETHOD GetFile(nsIFile** aFile) override;
56   NS_IMETHOD GetFileURL(nsIURI** aFileURL) override;
57   NS_IMETHOD GetFiles(nsISimpleEnumerator** aFiles) override;
58   NS_IMETHOD AppendFilter(const nsAString& aTitle,
59                           const nsAString& aFilter) override;
60 
61  protected:
62   /* method from nsBaseFilePicker */
63   virtual void InitNative(nsIWidget* aParent, const nsAString& aTitle) override;
64   nsresult Show(int16_t* aReturnVal) override;
65   nsresult ShowW(int16_t* aReturnVal);
66   void GetFilterListArray(nsString& aFilterList);
67   bool ShowFolderPicker(const nsString& aInitialDir);
68   bool ShowFilePicker(const nsString& aInitialDir);
69   void RememberLastUsedDirectory();
70   bool IsPrivacyModeEnabled();
71   bool IsDefaultPathLink();
72   bool IsDefaultPathHtml();
73 
74   nsCOMPtr<nsILoadContext> mLoadContext;
75   nsCOMPtr<nsIWidget> mParentWidget;
76   nsString mTitle;
77   nsCString mFile;
78   nsString mFilterList;
79   int16_t mSelectedType;
80   nsCOMArray<nsIFile> mFiles;
81   nsString mUnicodeFile;
82 
83   struct FreeDeleter {
operatorFreeDeleter84     void operator()(void* aPtr) { ::free(aPtr); }
85   };
86   static mozilla::UniquePtr<char16_t[], FreeDeleter> sLastUsedUnicodeDirectory;
87 
88   class ComDlgFilterSpec {
89    public:
ComDlgFilterSpec()90     ComDlgFilterSpec() {}
~ComDlgFilterSpec()91     ~ComDlgFilterSpec() {}
92 
Length()93     const uint32_t Length() { return mSpecList.Length(); }
94 
IsEmpty()95     const bool IsEmpty() { return (mSpecList.Length() == 0); }
96 
get()97     const COMDLG_FILTERSPEC* get() { return mSpecList.Elements(); }
98 
99     void Append(const nsAString& aTitle, const nsAString& aFilter);
100 
101    private:
102     AutoTArray<COMDLG_FILTERSPEC, 1> mSpecList;
103     AutoTArray<nsString, 2> mStrings;
104   };
105 
106   ComDlgFilterSpec mComFilterList;
107 };
108 
109 #endif  // nsFilePicker_h__
110