Menu

[r2017]: / trunk / Src / USelectionIOMgr.pas  Maximize  Restore  History

Download this file

195 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
{
* 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) 2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Provides a static object that manages loading and saving of stored
* database selections.
}
unit USelectionIOMgr;
interface
uses
// Project
UBaseObjects, USearch;
type
TSelectionIOMgr = class sealed(TNoConstructObject)
strict private
class procedure CanOpenDialogClose(Sender: TObject;
var CanClose: Boolean);
class procedure CanSaveDialogClose(Sender: TObject;
var CanClose: Boolean);
class function GetLoadFileName(out FileName: string): Boolean;
class function GetSaveFileName(out FileName: string): Boolean;
public
class procedure SaveCurrentSelection;
class function CanSaveCurrentSelection: Boolean;
class function LoadSelectionSearch(out Search: ISearch): Boolean;
end;
implementation
uses
// Delphi
SysUtils, Dialogs,
// Project
DB.USnippet, UConsts, UMessageBox, UOpenDialogEx, UOpenDialogHelper,
UQuery, USaveDialogEx, USelectionIOHandler, USnippetIDs;
{ TSelectionIOMgr }
class procedure TSelectionIOMgr.CanOpenDialogClose(Sender: TObject;
var CanClose: Boolean);
var
Dlg: TOpenDialogEx; // dialog box instance triggering this event
FileSpec: string; // full path to entered or selected file name
resourcestring
// Error messages
sFileDoesNotExist = '"%s" does not exist.';
begin
Dlg := Sender as TOpenDialogEx;
FileSpec := FileOpenEditedFileName(Dlg);
CanClose := FileExists(FileSpec);
if not CanClose then
TMessageBox.Error(Dlg, Format(sFileDoesNotExist, [FileSpec]));
end;
class function TSelectionIOMgr.CanSaveCurrentSelection: Boolean;
begin
Result := Query.IsSearchActive and not Query.Selection.IsEmpty;
end;
class procedure TSelectionIOMgr.CanSaveDialogClose(Sender: TObject;
var CanClose: Boolean);
var
Dlg: TSaveDialogEx; // dialog box instance triggering this event
FileSpec: string; // full path to entered or selected file name
function QueryOverwrite: Boolean;
resourcestring
// Text of query displayed in dialog box
sQueryMsg = '%s already exists.' + EOL + 'Do you want to replace it?';
begin
Result := TMessageBox.Confirm(Dlg, Format(sQueryMsg, [FileSpec]));
end;
begin
Dlg := Sender as TSaveDialogEx;
FileSpec := FileOpenFileNameWithExt(Dlg);
CanClose := not FileExists(FileSpec) or QueryOverwrite;
end;
class function TSelectionIOMgr.GetLoadFileName(out FileName: string): Boolean;
resourcestring
sDlgTitle = 'Load Selection'; // dialogue box title
sFilter = 'CodeSnip selection files (*.cssel)|*.cssel|'
+ 'All files (*.*)|*.*'; // file filter
var
OpenDlg: TOpenDialogEx; // load selection dialogue box
begin
OpenDlg := TOpenDialogEx.Create(nil);
try
OpenDlg.OnCanClose := CanOpenDialogClose;
OpenDlg.Filter := sFilter;
OpenDlg.FilterIndex := 1;
OpenDlg.InitialDir := '';
// we don't include ofFileMustExist in Options below since we handle
// non-existant files ourselves
OpenDlg.Options := [ofHideReadOnly, ofEnableSizing];
OpenDlg.OptionsEx := [];
OpenDlg.Title := sDlgTitle;
OpenDlg.HelpKeyword := 'LoadSelectionDlg';
Result := OpenDlg.Execute;
if Result then
FileName := OpenDlg.FileName;
finally
OpenDlg.Free;
end;
end;
class function TSelectionIOMgr.GetSaveFileName(out FileName: string): Boolean;
resourcestring
sDlgTitle = 'Save Selection'; // dialogue box caption
sFilter = 'CodeSnip selection files (*.cssel)|*.cssel|'
+ 'All files (*.*)|*.*'; // file filter
var
SaveDlg: TSaveDialogEx; // save selection dialogue box
begin
SaveDlg := TSaveDialogEx.Create(nil);
try
SaveDlg.Title := sDlgTitle;
SaveDlg.Options := [ofShowHelp, ofNoTestFileCreate, ofEnableSizing];
SaveDlg.Filter := sFilter;
SaveDlg.FilterIndex := 1;
SaveDlg.HelpKeyword := 'SaveSelectionDlg';
SaveDlg.OnCanClose := CanSaveDialogClose;
Result := SaveDlg.Execute;
if Result then
FileName := FileOpenFileNameWithExt(SaveDlg);
finally
SaveDlg.Free;
end;
end;
class function TSelectionIOMgr.LoadSelectionSearch(out Search: ISearch):
Boolean;
var
FileName: string;
Reader: TSelectionFileReader;
SnippetIDs: ISnippetIDList;
Criteria: IStoredSelectionSearchCriteria;
begin
if not GetLoadFileName(FileName) then
Exit(False);
Reader := TSelectionFileReader.Create;
try
SnippetIDs := Reader.ReadFile(FileName);
finally
Reader.Free;
end;
Criteria := TSearchCriteriaFactory.CreateStoredSelectionSearchCriteria(
SnippetIDs
);
Search := TSearchFactory.CreateStoredSelectionSearch(Criteria);
Result := True;
end;
class procedure TSelectionIOMgr.SaveCurrentSelection;
var
FileName: string;
Writer: TSelectionFileWriter;
SnippetIDs: ISnippetIDList;
Snippet: TSnippet;
begin
if not GetSaveFileName(FileName) then
Exit;
Writer := TSelectionFileWriter.Create;
try
SnippetIDs := TSnippetIDList.Create;
for Snippet in Query.Selection do
SnippetIDs.Add(Snippet.ID);
Writer.WriteFile(FileName, SnippetIDs);
finally
Writer.Free;
end;
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.