1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef netscapeprofilemigratorbase___h___
7 #define netscapeprofilemigratorbase___h___
8 
9 #include "nsIFile.h"
10 #include "nsIStringBundle.h"
11 #include "nsString.h"
12 #include "nsTArray.h"
13 #include "nsIObserverService.h"
14 #include "nsITimer.h"
15 #include "nsIMailProfileMigrator.h"
16 
17 class nsIPrefBranch;
18 class nsIMutableArray;
19 
20 struct fileTransactionEntry {
21   nsCOMPtr<nsIFile> srcFile;   // the src path including leaf name
22   nsCOMPtr<nsIFile> destFile;  // the destination path
23   nsString
24       newName;  // only valid if the file should be renamed after getting copied
25 };
26 
27 #define F(a) nsNetscapeProfileMigratorBase::a
28 
29 #define MAKEPREFTRANSFORM(pref, newpref, getmethod, setmethod)         \
30   {                                                                    \
31     pref, newpref, F(Get##getmethod), F(Set##setmethod), false, { -1 } \
32   }
33 
34 #define MAKESAMETYPEPREFTRANSFORM(pref, method)            \
35   {                                                        \
36     pref, 0, F(Get##method), F(Set##method), false, { -1 } \
37   }
38 
39 class nsNetscapeProfileMigratorBase : public nsIMailProfileMigrator,
40                                       public nsITimerCallback
41 
42 {
43  public:
44   NS_DECL_ISUPPORTS
45   NS_DECL_NSITIMERCALLBACK
46 
47   nsNetscapeProfileMigratorBase();
48 
49   NS_IMETHOD GetSourceHasMultipleProfiles(bool* aResult) override;
50   NS_IMETHOD GetSourceExists(bool* aResult) override;
51 
52   struct PrefTransform;
53   typedef nsresult (*prefConverter)(PrefTransform*, nsIPrefBranch*);
54 
55   struct PrefTransform {
56     const char* sourcePrefName;
57     const char* targetPrefName;
58     prefConverter prefGetterFunc;
59     prefConverter prefSetterFunc;
60     bool prefHasValue;
61     union {
62       int32_t intValue;
63       bool boolValue;
64       char* stringValue;
65     };
66   };
67 
68   struct PrefBranchStruct {
69     char* prefName;
70     int32_t type;
71     union {
72       char* stringValue;
73       int32_t intValue;
74       bool boolValue;
75     };
76   };
77 
78   typedef nsTArray<PrefBranchStruct*> PBStructArray;
79 
80   static nsresult GetString(PrefTransform* aTransform, nsIPrefBranch* aBranch);
81   static nsresult SetString(PrefTransform* aTransform, nsIPrefBranch* aBranch);
82   static nsresult GetBool(PrefTransform* aTransform, nsIPrefBranch* aBranch);
83   static nsresult SetBool(PrefTransform* aTransform, nsIPrefBranch* aBranch);
84   static nsresult GetInt(PrefTransform* aTransform, nsIPrefBranch* aBranch);
85   static nsresult SetInt(PrefTransform* aTransform, nsIPrefBranch* aBranch);
86 
87   nsresult RecursiveCopy(nsIFile* srcDir, nsIFile* destDir);  // helper routine
88 
89  protected:
~nsNetscapeProfileMigratorBase()90   virtual ~nsNetscapeProfileMigratorBase() {}
91   void CopyNextFolder();
92   void EndCopyFolders();
93 
94   nsresult GetProfileDataFromProfilesIni(
95       nsIFile* aDataDir, nsTArray<nsString>& aProfileNames,
96       nsTArray<RefPtr<nsIFile>>& aProfileLocations);
97 
98   nsresult CopyFile(const nsAString& aSourceFileName,
99                     const nsAString& aTargetFileName);
100 
101   nsresult GetSignonFileName(bool aReplace, nsACString& aFileName);
102   nsresult LocateSignonsFile(nsACString& aResult);
103 
104   nsCOMPtr<nsIFile> mSourceProfile;
105   nsCOMPtr<nsIFile> mTargetProfile;
106 
107   // List of src/destination files we still have to copy into the new profile
108   // directory.
109   nsTArray<fileTransactionEntry> mFileCopyTransactions;
110   uint32_t mFileCopyTransactionIndex;
111 
112   int64_t mMaxProgress;
113   int64_t mCurrentProgress;
114 
115   nsCOMPtr<nsIObserverService> mObserverService;
116   nsCOMPtr<nsITimer> mFileIOTimer;
117 };
118 
119 #endif
120