Skip to content
Permalink
Browse files

Issue #140: Move code for all remaining common directory paths settin…

…gs to TAppSettings (UserAppData, UserDocuments, Snippets)
  • Loading branch information...
ansgarbecker committed Apr 11, 2019
1 parent 3284509 commit e40210b1b5cf450aea039e9f8550e77b12587f44
Showing with 46 additions and 68 deletions.
  1. +0 −12 out/locale/en/LC_MESSAGES/default.po
  2. +34 −23 source/apphelpers.pas
  3. +12 −33 source/main.pas
@@ -4777,18 +4777,6 @@ msgstr "Delete snippet file?"
msgid "Failed deleting %s"
msgstr "Failed deleting %s"

#: main.pas:6135
msgid "Snippets folder does not exist"
msgstr "Snippets folder does not exist"

#: main.pas:6135
msgid "The folder \"%s\" is normally created when you install %s."
msgstr "The folder \"%s\" is normally created when you install %s."

#: main.pas:6135
msgid "Shall it be created now?"
msgstr "Shall it be created now?"

#: main.pas:6153
msgid "Clear query history?"
msgstr "Clear query history?"
@@ -245,6 +245,10 @@ TAppSettings = class(TObject)
procedure ImportSettings(Filename: String);
procedure ExportSettings(Filename: String); overload;
procedure ExportSettings; overload;
// Common directories
function DirnameUserAppData: String;
function DirnameUserDocuments: String;
function DirnameSnippets: String;
function DirnameBackups: String;
end;

@@ -272,10 +276,6 @@ TAppSettings = class(TObject)
function fixNewlines(txt: String): String;
function ExtractLiteral(var SQL: String; Prefix: String): String;
function GetShellFolder(CSIDL: integer): string;
// Common directories
function DirnameUserAppData: String;
function DirnameUserDocuments: String;
function DirnameSnippets: String;
function goodfilename( str: String ): String;
function ExtractBaseFileName(FileName: String): String;
function FormatNumber( str: String; Thousands: Boolean=True): String; Overload;
@@ -831,25 +831,6 @@ function GetShellFolder(CSIDL: integer): string;
end;


function DirnameUserAppData: String;
begin
// User folder for HeidiSQL's data (<user name>\Application Data)
Result := GetShellFolder(CSIDL_APPDATA) + '\' + APPNAME + '\';
end;

function DirnameUserDocuments: String;
begin
// "HeidiSQL" folder under user's documents folder, e.g. c:\Users\Mike\Documents\HeidiSQL\
Result := GetShellFolder(CSIDL_MYDOCUMENTS) + '\' + APPNAME + '\';
end;


function DirnameSnippets: String;
begin
// Folder for snippets
Result := IncludeTrailingBackslash(AppSettings.ReadString(asCustomSnippetsDirectory));
end;


{***
Remove special characters from a filename
@@ -4204,6 +4185,36 @@ procedure TAppSettings.ExportSettings;
end;


function TAppSettings.DirnameUserAppData: String;
begin
// User folder for HeidiSQL's data (<user name>\Application Data)
Result := GetShellFolder(CSIDL_APPDATA) + '\' + APPNAME + '\';
if not DirectoryExists(Result) then begin
ForceDirectories(Result);
end;
end;


function TAppSettings.DirnameUserDocuments: String;
begin
// "HeidiSQL" folder under user's documents folder, e.g. c:\Users\Mike\Documents\HeidiSQL\
Result := GetShellFolder(CSIDL_MYDOCUMENTS) + '\' + APPNAME + '\';
if not DirectoryExists(Result) then begin
ForceDirectories(Result);
end;
end;


function TAppSettings.DirnameSnippets: String;
begin
// Folder for snippets
Result := IncludeTrailingBackslash(ReadString(asCustomSnippetsDirectory));
if not DirectoryExists(Result) then begin
ForceDirectories(Result);
end;
end;


function TAppSettings.DirnameBackups: String;
begin
// Create backup folder if it does not exist and return it
@@ -1692,16 +1692,11 @@ procedure TMainForm.FormCreate(Sender: TObject);
end;


// Ensure directories exist
ForceDirectories(DirnameUserAppData);

// Move files from old default snippets directory, see issue #159
if not AppSettings.PortableMode then begin
// This was the default folder up to r5244 / 8b2966c52efb685b00189037a0507157ed03a368
OldSnippetsDir := GetShellFolder(CSIDL_COMMON_APPDATA) + '\' + APPNAME + '\Snippets\';
CurrentSnippetsDir := DirnameSnippets;
if not DirectoryExists(CurrentSnippetsDir) then
ForceDirectories(CurrentSnippetsDir);
CurrentSnippetsDir := AppSettings.DirnameSnippets;
//showmessage('1:'+crlf+OldSnippetsDir+crlf+CurrentSnippetsDir);
if (not OldSnippetsDir.IsEmpty) and (not CurrentSnippetsDir.IsEmpty)
and DirectoryExists(OldSnippetsDir) and DirectoryExists(CurrentSnippetsDir)
@@ -2138,7 +2133,7 @@ function TMainForm.InitTabsIniFile: TIniFile;
if AppSettings.PortableMode then
TabsIniFilename := ExtractFilePath(Application.ExeName) + 'tabs.ini'
else
TabsIniFilename := DirnameUserAppData + 'tabs.ini';
TabsIniFilename := AppSettings.DirnameUserAppData + 'tabs.ini';
WaitingSince := GetTickCount64;
Attempts := 0;
while not FileIsWritable(TabsIniFilename) do begin
@@ -3667,7 +3662,7 @@ function TMainForm.RunQueryFiles(Filenames: TStrings; Encoding: TEncoding; Force
for i:=0 to Filenames.Count-1 do begin
RunQueryFile(Filenames[i], Encoding);
// Add filename to history menu
if Pos(DirnameSnippets, Filenames[i]) = 0 then
if Pos(AppSettings.DirnameSnippets, Filenames[i]) = 0 then
MainForm.AddOrRemoveFromQueryLoadHistory(Filenames[i], True, True);
end;
end;
@@ -4420,9 +4415,7 @@ procedure TMainForm.actSaveSQLAsExecute(Sender: TObject);
SaveDialog := TSaveDialog.Create(Self);
SaveDialog.Options := SaveDialog.Options + [ofOverwritePrompt];
if (Sender = actSaveSQLSnippet) or (Sender = actSaveSQLSelectionSnippet) then begin
if not DirectoryExists(DirnameSnippets) then
ForceDirectories(DirnameSnippets);
SaveDialog.InitialDir := DirnameSnippets;
SaveDialog.InitialDir := AppSettings.DirnameSnippets;
SaveDialog.Options := SaveDialog.Options + [ofNoChangeDir];
SaveDialog.Title := _('Save snippet');
end;
@@ -4575,7 +4568,7 @@ procedure TMainform.popupQueryLoadClick(Sender: TObject);
Filename := (Sender as TMenuItem).Caption;
Filename := StripHotkey(Filename);
if Pos('\', Filename) = 0 then // assuming we load a snippet
Filename := DirnameSnippets + Filename + '.sql'
Filename := AppSettings.DirnameSnippets + Filename + '.sql'
else begin // assuming we load a file from the recent-list
p := Pos(' ', Filename) + 1;
filename := Copy(Filename, p, Length(Filename));
@@ -6287,7 +6280,7 @@ procedure TMainForm.SynMemoQueryDragDrop(Sender, Source: TObject; X,
1:
case Tree.FocusedNode.Parent.Index of
HELPERNODE_SNIPPETS:
Text := ReadTextFile(DirnameSnippets + Tree.Text[Tree.FocusedNode, 0] + '.sql', nil);
Text := ReadTextFile(AppSettings.DirnameSnippets + Tree.Text[Tree.FocusedNode, 0] + '.sql', nil);
HELPERNODE_HISTORY:
Text := '';
else begin
@@ -7174,7 +7167,7 @@ procedure TMainForm.menuDeleteSnippetClick(Sender: TObject);
if not Assigned(ActiveQueryHelpers.FocusedNode) then
Exit;

snippetfile := DirnameSnippets + ActiveQueryHelpers.Text[ActiveQueryHelpers.FocusedNode, 0] + '.sql';
snippetfile := AppSettings.DirnameSnippets + ActiveQueryHelpers.Text[ActiveQueryHelpers.FocusedNode, 0] + '.sql';
if MessageDialog(_('Delete snippet file?'), snippetfile, mtConfirmation, [mbOk, mbCancel]) = mrOk then
begin
Screen.Cursor := crHourGlass;
@@ -7206,7 +7199,7 @@ procedure TMainForm.menuDoubleClickInsertsNodeTextClick(Sender: TObject);
}
procedure TMainForm.menuInsertSnippetAtCursorClick(Sender: TObject);
begin
ActiveQueryTab.LoadContents(DirnameSnippets + ActiveQueryHelpers.Text[ActiveQueryHelpers.FocusedNode, 0] + '.sql', False, nil);
ActiveQueryTab.LoadContents(AppSettings.DirnameSnippets + ActiveQueryHelpers.Text[ActiveQueryHelpers.FocusedNode, 0] + '.sql', False, nil);
end;


@@ -7215,7 +7208,7 @@ procedure TMainForm.menuInsertSnippetAtCursorClick(Sender: TObject);
}
procedure TMainForm.menuLoadSnippetClick(Sender: TObject);
begin
ActiveQueryTab.LoadContents(DirnameSnippets + ActiveQueryHelpers.Text[ActiveQueryHelpers.FocusedNode, 0] + '.sql', True, nil);
ActiveQueryTab.LoadContents(AppSettings.DirnameSnippets + ActiveQueryHelpers.Text[ActiveQueryHelpers.FocusedNode, 0] + '.sql', True, nil);
end;


@@ -7224,21 +7217,7 @@ procedure TMainForm.menuLoadSnippetClick(Sender: TObject);
}
procedure TMainForm.menuExploreClick(Sender: TObject);
begin
// Normally the snippets folder is created at installation. But it sure
// can be the case that it has been deleted or that the application was
// not installed properly. Ask if we should create the folder now.
if DirectoryExists(DirnameSnippets) then
ShellExec('', DirnameSnippets)
else
if MessageDialog(_('Snippets folder does not exist'),
f_('The folder "%s" is normally created when you install %s.', [DirnameSnippets, APPNAME]) + CRLF + CRLF + _('Shall it be created now?'),
mtWarning, [mbYes, mbNo]) = mrYes then
try
Screen.Cursor := crHourglass;
ForceDirectories(DirnameSnippets);
finally
Screen.Cursor := crDefault;
end;
ShellExec('', AppSettings.DirnameSnippets);
end;


@@ -12321,7 +12300,7 @@ procedure TMainForm.SetSnippetFilenames;
FSnippetFilenames := TStringList.Create;
FSnippetFilenames.Clear;
try
Files := TDirectory.GetFiles(DirnameSnippets, '*.sql');
Files := TDirectory.GetFiles(AppSettings.DirnameSnippets, '*.sql');
for i:=0 to Length(Files)-1 do begin
Snip := ExtractFilename(Files[i]);
Snip := Copy(Snip, 1, Length(Snip)-4);
@@ -12861,7 +12840,7 @@ function TQueryTab.LoadContents(Filename: String; ReplaceContent: Boolean; Encod
MainForm.LogSQL(f_('Loading file "%s" (%s) into query tab #%d ...', [Filename, FormatByteNumber(Filesize), Number]), lcInfo);
try
Content := ReadTextfile(Filename, Encoding);
if Pos(DirnameSnippets, Filename) = 0 then
if Pos(AppSettings.DirnameSnippets, Filename) = 0 then
MainForm.AddOrRemoveFromQueryLoadHistory(Filename, True, True);
Memo.UndoList.AddGroupBreak;
Memo.BeginUpdate;

0 comments on commit e40210b

Please sign in to comment.
You can’t perform that action at this time.