| 1 | (* ***** BEGIN LICENSE BLOCK *****
|
|---|
| 2 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|---|
| 3 | *
|
|---|
| 4 | * The contents of this file are subject to the Mozilla Public License Version
|
|---|
| 5 | * 1.1 (the "License"); you may not use this file except in compliance with
|
|---|
| 6 | * the License. You may obtain a copy of the License at
|
|---|
| 7 | * http://www.mozilla.org/MPL/
|
|---|
| 8 | *
|
|---|
| 9 | * Software distributed under the License is distributed on an "AS IS" basis,
|
|---|
| 10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|---|
| 11 | * for the specific language governing rights and limitations under the
|
|---|
| 12 | * License.
|
|---|
| 13 | *
|
|---|
| 14 | * The Original Code is GeckoComponents for Delphi.
|
|---|
| 15 | *
|
|---|
| 16 | * The Initial Developer of the Original Code is Takanori Ito.
|
|---|
| 17 | * Portions created by the Initial Developer are Copyright (C) 2003
|
|---|
| 18 | * the Initial Developer. All Rights Reserved.
|
|---|
| 19 | *
|
|---|
| 20 | * Contributor(s):
|
|---|
| 21 | *
|
|---|
| 22 | * Alternatively, the contents of this file may be used under the terms of
|
|---|
| 23 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
|---|
| 24 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|---|
| 25 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
|---|
| 26 | * of those above. If you wish to allow use of your version of this file only
|
|---|
| 27 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
|---|
| 28 | * use your version of this file under the terms of the MPL, indicate your
|
|---|
| 29 | * decision by deleting the provisions above and replace them with the notice
|
|---|
| 30 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
|---|
| 31 | * the provisions above, a recipient may use your version of this file under
|
|---|
| 32 | * the terms of any one of the MPL, the GPL or the LGPL.
|
|---|
| 33 | *
|
|---|
| 34 | * ***** END LICENSE BLOCK ***** *)
|
|---|
| 35 | unit GeckoSimpleProfile;
|
|---|
| 36 |
|
|---|
| 37 | interface
|
|---|
| 38 |
|
|---|
| 39 | uses
|
|---|
| 40 | Windows, Messages, SysUtils, Classes;
|
|---|
| 41 |
|
|---|
| 42 | type
|
|---|
| 43 | TGeckoSimpleProfile = class(TComponent)
|
|---|
| 44 | private
|
|---|
| 45 | { Private �錾 }
|
|---|
| 46 | procedure SetProfileBaseDirectory(Value: AnsiString);
|
|---|
| 47 | function GetProfileBaseDirectory: AnsiString;
|
|---|
| 48 | protected
|
|---|
| 49 | { Protected �錾 }
|
|---|
| 50 | public
|
|---|
| 51 | { Public �錾 }
|
|---|
| 52 | constructor Create(AOwner: TComponent); override;
|
|---|
| 53 | destructor Destroy; override;
|
|---|
| 54 | published
|
|---|
| 55 | { Published �錾 }
|
|---|
| 56 | property ProfileBaseDirectory: AnsiString
|
|---|
| 57 | read GetProfileBaseDirectory
|
|---|
| 58 | write SetProfileBaseDirectory;
|
|---|
| 59 | end;
|
|---|
| 60 |
|
|---|
| 61 | procedure Register;
|
|---|
| 62 |
|
|---|
| 63 | implementation
|
|---|
| 64 |
|
|---|
| 65 | uses
|
|---|
| 66 | nsXPCOM, nsXPCOMGlue, nsError, nsGeckoStrings, nsProfile, nsTypes, nsInit;
|
|---|
| 67 |
|
|---|
| 68 | procedure Register;
|
|---|
| 69 | begin
|
|---|
| 70 | RegisterComponents('Gecko', [TGeckoSimpleProfile]);
|
|---|
| 71 | end;
|
|---|
| 72 |
|
|---|
| 73 | var
|
|---|
| 74 | sProfileDirServiceProvider: nsProfileDirServiceProvider;
|
|---|
| 75 | sProfileBaseDirectory: AnsiString;
|
|---|
| 76 | sRefCnt: Integer = 0;
|
|---|
| 77 |
|
|---|
| 78 | procedure ChangeProfileBaseDirectory; forward;
|
|---|
| 79 | procedure SavePreferences; forward;
|
|---|
| 80 | procedure LoadPreferences; forward;
|
|---|
| 81 |
|
|---|
| 82 | constructor TGeckoSimpleProfile.Create(AOwner: TComponent);
|
|---|
| 83 | var
|
|---|
| 84 | rv: nsresult;
|
|---|
| 85 | provider: nsProfileDirServiceProvider;
|
|---|
| 86 | begin
|
|---|
| 87 | inherited Create(AOwner);
|
|---|
| 88 |
|
|---|
| 89 | if not (csDesigning in ComponentState) then
|
|---|
| 90 | begin
|
|---|
| 91 | rv := GRE_Startup;
|
|---|
| 92 | if NS_FAILED(rv) then
|
|---|
| 93 | raise Exception.Create('GRE_Startup');
|
|---|
| 94 |
|
|---|
| 95 | if not Assigned(sProfileDirServiceProvider) then
|
|---|
| 96 | begin
|
|---|
| 97 | provider := NS_NewProfileDirServiceProvider(True);
|
|---|
| 98 |
|
|---|
| 99 | sProfileDirServiceProvider := provider;
|
|---|
| 100 |
|
|---|
| 101 | Inc(sRefCnt);
|
|---|
| 102 |
|
|---|
| 103 | if Length(sProfileBaseDirectory)>0 then ChangeProfileBaseDirectory;
|
|---|
| 104 | end;
|
|---|
| 105 | end;
|
|---|
| 106 | end;
|
|---|
| 107 |
|
|---|
| 108 | destructor TGeckoSimpleProfile.Destroy;
|
|---|
| 109 | begin
|
|---|
| 110 | if not (csDesigning in ComponentState) then
|
|---|
| 111 | begin
|
|---|
| 112 | SavePreferences;
|
|---|
| 113 |
|
|---|
| 114 | Dec(sRefCnt);
|
|---|
| 115 | if sRefCnt=0 then
|
|---|
| 116 | sProfileDirServiceProvider := nil;
|
|---|
| 117 | end;
|
|---|
| 118 |
|
|---|
| 119 | inherited;
|
|---|
| 120 | end;
|
|---|
| 121 |
|
|---|
| 122 | procedure TGeckoSimpleProfile.SetProfileBaseDirectory(Value: AnsiString);
|
|---|
| 123 | begin
|
|---|
| 124 | if Value = sProfileBaseDirectory then Exit;
|
|---|
| 125 |
|
|---|
| 126 | sProfileBaseDirectory := Value;
|
|---|
| 127 |
|
|---|
| 128 | if not (csDesigning in ComponentState) then
|
|---|
| 129 | begin
|
|---|
| 130 | ChangeProfileBaseDirectory;
|
|---|
| 131 | LoadPreferences;
|
|---|
| 132 | end;
|
|---|
| 133 | end;
|
|---|
| 134 |
|
|---|
| 135 | function TGeckoSimpleProfile.GetProfileBaseDirectory: AnsiString;
|
|---|
| 136 | begin
|
|---|
| 137 | Result := sProfileBaseDirectory;
|
|---|
| 138 | end;
|
|---|
| 139 |
|
|---|
| 140 | procedure ChangeProfileBaseDirectory;
|
|---|
| 141 | var
|
|---|
| 142 | rv: nsresult;
|
|---|
| 143 | localFile: nsILocalFile;
|
|---|
| 144 | localFileStr: IInterfacedCString;
|
|---|
| 145 | begin
|
|---|
| 146 | if Length(sProfileBaseDirectory)=0 then
|
|---|
| 147 | begin
|
|---|
| 148 | sProfileDirServiceProvider.SetProfileDir(nil);
|
|---|
| 149 | Exit;
|
|---|
| 150 | end;
|
|---|
| 151 |
|
|---|
| 152 | try
|
|---|
| 153 | localFileStr := NewCString(sProfileBaseDirectory);
|
|---|
| 154 | except
|
|---|
| 155 | Exit;
|
|---|
| 156 | end;
|
|---|
| 157 | rv := NS_NewNativeLocalFile(localFileStr.ACString, True, localFile);
|
|---|
| 158 | if NS_FAILED(rv) then Exit;
|
|---|
| 159 |
|
|---|
| 160 | sProfileDirServiceProvider.SetProfileDir(localFile);
|
|---|
| 161 | sProfileDirServiceProvider.Register;
|
|---|
| 162 | end;
|
|---|
| 163 |
|
|---|
| 164 | procedure SavePreferences;
|
|---|
| 165 | var
|
|---|
| 166 | prefSrv: nsIPrefService;
|
|---|
| 167 | prefFile: nsIFile;
|
|---|
| 168 | begin
|
|---|
| 169 | NS_GetService(NS_PREFSERVICE_CID, nsIPrefService, prefSrv);
|
|---|
| 170 | prefFile := NS_GetSpecialDirectory(NS_APP_PREFS_50_FILE);
|
|---|
| 171 | prefSrv.SavePrefFile(prefFile);
|
|---|
| 172 | end;
|
|---|
| 173 |
|
|---|
| 174 | procedure LoadPreferences;
|
|---|
| 175 | var
|
|---|
| 176 | prefSrv: nsIPrefService;
|
|---|
| 177 | prefFile: nsIFile;
|
|---|
| 178 | begin
|
|---|
| 179 | NS_GetService(NS_PREFSERVICE_CID, nsIPrefService, prefSrv);
|
|---|
| 180 | prefFile := NS_GetSpecialDirectory(NS_APP_PREFS_50_FILE);
|
|---|
| 181 | if not prefFile.Exists then
|
|---|
| 182 | prefFile.Create(NS_IFILE_NORMAL_FILE_TYPE,0666);
|
|---|
| 183 | prefSrv.ResetPrefs;
|
|---|
| 184 | prefSrv.ReadUserPrefs(prefFile);
|
|---|
| 185 | end;
|
|---|
| 186 |
|
|---|
| 187 | end.
|
|---|