root/lang/pascal/GeckoComponents/GeckoSimpleProfile.pas

Revision 4542, 5.1 kB (checked in by plus7, 12 months ago)

check existence of pref.js

  • Property svn:executable set to *
Line 
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 ***** *)
35unit GeckoSimpleProfile;
36
37interface
38
39uses
40  Windows, Messages, SysUtils, Classes;
41
42type
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
61procedure Register;
62
63implementation
64
65uses
66  nsXPCOM, nsXPCOMGlue, nsError, nsGeckoStrings, nsProfile, nsTypes, nsInit;
67
68procedure Register;
69begin
70  RegisterComponents('Gecko', [TGeckoSimpleProfile]);
71end;
72
73var
74  sProfileDirServiceProvider: nsProfileDirServiceProvider;
75  sProfileBaseDirectory: AnsiString;
76  sRefCnt: Integer = 0;
77
78procedure ChangeProfileBaseDirectory; forward;
79procedure SavePreferences; forward;
80procedure LoadPreferences; forward;
81
82constructor TGeckoSimpleProfile.Create(AOwner: TComponent);
83var
84  rv: nsresult;
85  provider: nsProfileDirServiceProvider;
86begin
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;
106end;
107
108destructor TGeckoSimpleProfile.Destroy;
109begin
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;
120end;
121
122procedure TGeckoSimpleProfile.SetProfileBaseDirectory(Value: AnsiString);
123begin
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;
133end;
134
135function TGeckoSimpleProfile.GetProfileBaseDirectory: AnsiString;
136begin
137  Result := sProfileBaseDirectory;
138end;
139
140procedure ChangeProfileBaseDirectory;
141var
142  rv: nsresult;
143  localFile: nsILocalFile;
144  localFileStr: IInterfacedCString;
145begin
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;
162end;
163
164procedure SavePreferences;
165var
166  prefSrv: nsIPrefService;
167  prefFile: nsIFile;
168begin
169  NS_GetService(NS_PREFSERVICE_CID, nsIPrefService, prefSrv);
170  prefFile := NS_GetSpecialDirectory(NS_APP_PREFS_50_FILE);
171  prefSrv.SavePrefFile(prefFile);
172end;
173
174procedure LoadPreferences;
175var
176  prefSrv: nsIPrefService;
177  prefFile: nsIFile;
178begin
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);
185end;
186
187end.
Note: See TracBrowser for help on using the browser.