Menu

[r2799]: / trunk / Src / FmUserDataPathDlg.pas  Maximize  Restore  History

Download this file

199 lines (168 with data), 5.3 kB

  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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a dialogue box that can be used to move the user database to a
* different directory.
}
unit FmUserDataPathDlg;
interface
uses
// Delphi
SysUtils, Classes, ActnList, StdCtrls, Controls, ExtCtrls,
// Project
FmGenericViewDlg, UBaseObjects;
type
TUserDataPathDlg = class(TGenericViewDlg, INoPublicConstruct)
actBrowse: TAction;
actDefaultPath: TAction;
actMove: TAction;
alDlg: TActionList;
btnBrowse: TButton;
btnMove: TButton;
btnDefaultPath: TButton;
edPath: TEdit;
lblInstructions: TLabel;
lblPath: TLabel;
procedure actBrowseExecute(Sender: TObject);
procedure actDefaultPathExecute(Sender: TObject);
procedure actDefaultPathUpdate(Sender: TObject);
procedure actMoveExecute(Sender: TObject);
procedure actMoveUpdate(Sender: TObject);
strict private
function NewDirFromEditCtrl: string;
procedure DoMove;
procedure HandleException(const E: Exception);
strict protected
procedure ArrangeForm; override;
procedure InitForm; override;
public
class procedure Execute(AOwner: TComponent);
end;
implementation
uses
// Delphi
IOUtils, RTLConsts,
// Project
UAppInfo, UBrowseForFolderDlg, UCtrlArranger, UExceptions, UMessageBox,
UStrUtils;
{$R *.dfm}
{ TUserDataPathDlg }
procedure TUserDataPathDlg.actBrowseExecute(Sender: TObject);
var
Dlg: TBrowseForFolderDlg; // browse for folder standard dialog box
resourcestring
sDlgTitle = 'Choose Database Directory';
sDlgHeading = 'Choose an empty directory or create a new one';
begin
Dlg := TBrowseForFolderDlg.Create(nil);
try
Dlg.Title := sDlgTitle;
Dlg.Headline := sDlgHeading;
Dlg.MakeFolderBtnVisible := True;
Dlg.HelpKeyword := 'ChooseUserDBDirDlg';
if Dlg.Execute then
edPath.Text := Dlg.FolderName;
finally
Dlg.Free;
end;
end;
procedure TUserDataPathDlg.actDefaultPathExecute(Sender: TObject);
begin
edPath.Text := TAppInfo.DefaultUserDataDir;
end;
procedure TUserDataPathDlg.actDefaultPathUpdate(Sender: TObject);
begin
actDefaultPath.Enabled := (NewDirFromEditCtrl <> '')
and not StrSameText(NewDirFromEditCtrl, TAppInfo.DefaultUserDataDir);
end;
procedure TUserDataPathDlg.actMoveExecute(Sender: TObject);
resourcestring
sNonEmptyDir = 'The specified directory is not empty';
sConfirmMsg = 'Are you sure you want to move the user database';
begin
if TDirectory.Exists(NewDirFromEditCtrl)
and not TDirectory.IsEmpty(NewDirFromEditCtrl) then
raise EDataEntry.Create(sNonEmptyDir, edPath);
if not TMessageBox.Confirm(Self, sConfirmMsg) then
Exit;
try
DoMove;
TAppInfo.ChangeUserDataDir(NewDirFromEditCtrl);
except
on E: Exception do
HandleException(E);
end;
end;
procedure TUserDataPathDlg.actMoveUpdate(Sender: TObject);
begin
actMove.Enabled := (NewDirFromEditCtrl <> '')
and not StrSameText(NewDirFromEditCtrl, TAppInfo.UserDataDir);
end;
procedure TUserDataPathDlg.ArrangeForm;
begin
TCtrlArranger.SetLabelHeight(lblInstructions);
TCtrlArranger.AlignLefts([lblInstructions, lblPath, edPath], 0);
TCtrlArranger.MoveToRightOf(edPath, btnBrowse, 4);
lblInstructions.Width := TCtrlArranger.RightOf(btnBrowse);
TCtrlArranger.AlignHCentresTo([edPath, btnBrowse], [btnDefaultPath, btnMove]);
pnlBody.ClientWidth := TCtrlArranger.TotalControlWidth(pnlBody);
lblInstructions.Top := 0;
TCtrlArranger.MoveBelow(lblInstructions, lblPath, 12);
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf(lblPath, 4), [edPath, btnBrowse]
);
TCtrlArranger.MoveBelow([edPath, btnBrowse], btnDefaultPath, 8);
TCtrlArranger.MoveBelow(btnDefaultPath, btnMove, 18);
pnlBody.ClientHeight := TCtrlArranger.TotalControlHeight(pnlBody) + 18;
inherited;
end;
procedure TUserDataPathDlg.DoMove;
var
SourceDir, DestDir: string;
begin
SourceDir := TAppInfo.UserDataDir;
DestDir := NewDirFromEditCtrl;
if TDirectory.Exists(DestDir) and not TDirectory.IsEmpty(DestDir) then
raise EInOutError.Create(SDirectoryNotEmpty);
if not TDirectory.Exists(DestDir) then
TDirectory.CreateDirectory(DestDir);
TDirectory.Copy(SourceDir, DestDir);
TDirectory.Delete(SourceDir, True);
end;
class procedure TUserDataPathDlg.Execute(AOwner: TComponent);
begin
{$IFDEF PORTABLE}
raise EBug.Create(ClassName + '.Execute: Call forbidden in portable version');
{$ENDIF}
with InternalCreate(AOwner) do
try
ShowModal
finally
Free;
end;
end;
procedure TUserDataPathDlg.HandleException(const E: Exception);
begin
if (E is EInOutError) or (E is ENotSupportedException)
or (E is EDirectoryNotFoundException) or (E is EPathTooLongException)
or (E is EArgumentException) then
raise EDataEntry.Create(E.Message);
raise E;
end;
procedure TUserDataPathDlg.InitForm;
begin
inherited;
edPath.Text := TAppInfo.UserDataDir;
end;
function TUserDataPathDlg.NewDirFromEditCtrl: string;
begin
Result := StrTrim(edPath.Text);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.