Skip to content
Permalink
Browse files

Issue #140: Use local tabs.ini and Backups directory in portable mode

  • Loading branch information...
ansgarbecker committed Apr 11, 2019
1 parent a6d372c commit 32845094d9604d94b4a0486644441403660126e8
Showing with 27 additions and 11 deletions.
  1. +16 −2 source/apphelpers.pas
  2. +11 −9 source/main.pas
@@ -168,7 +168,7 @@ TQueryThread = class(TThread)
asCopyTableData, asCopyTableRecentFilter, asServerVersion, asServerVersionFull, asLastConnect,
asConnectCount, asRefusedCount, asSessionCreated, asDoUsageStatistics,
asLastUsageStatisticCall, asWheelZoom, asDisplayBars, asMySQLBinaries, asCustomSnippetsDirectory,
asPromptSaveFileOnTabClose, asRestoreTabs, asBackupDirectory, asWarnUnsafeUpdates, asQueryWarningsMessage,
asPromptSaveFileOnTabClose, asRestoreTabs, asWarnUnsafeUpdates, asQueryWarningsMessage,
asCompletionProposal, asCompletionProposalWidth, asCompletionProposalNbLinesInWindow, asAutoUppercase,
asTabsToSpaces, asFilterPanel, asAllowMultipleInstances, asFindDialogSearchHistory, asGUIFontName, asGUIFontSize,
asTheme, asIconPack,
@@ -245,6 +245,7 @@ TAppSettings = class(TObject)
procedure ImportSettings(Filename: String);
procedure ExportSettings(Filename: String); overload;
procedure ExportSettings; overload;
function DirnameBackups: String;
end;


@@ -3633,7 +3634,6 @@ constructor TAppSettings.Create;
InitSetting(asCustomSnippetsDirectory, 'CustomSnippetsDirectory', 0, False, DefaultSnippetsDirectory);
InitSetting(asPromptSaveFileOnTabClose, 'PromptSaveFileOnTabClose', 0, True);
InitSetting(asRestoreTabs, 'RestoreTabs', 0, True);
InitSetting(asBackupDirectory, 'BackupDirectory', 0, False, DirnameUserAppData + 'Backups\');
InitSetting(asWarnUnsafeUpdates, 'WarnUnsafeUpdates', 0, True);
InitSetting(asQueryWarningsMessage, 'QueryWarningsMessage', 0, True);
InitSetting(asCompletionProposal, 'CompletionProposal', 0, True);
@@ -4204,6 +4204,20 @@ procedure TAppSettings.ExportSettings;
end;


function TAppSettings.DirnameBackups: String;
begin
// Create backup folder if it does not exist and return it
if PortableMode then begin
Result := ExtractFilePath(Application.ExeName) + 'Backups\'
end else begin
Result := DirnameUserAppData + 'Backups\';
end;
if not DirectoryExists(Result) then begin
ForceDirectories(Result);
end;
end;



end.

@@ -1068,7 +1068,6 @@ TMainForm = class(TForm)
FLastPortableSettingsSave: Cardinal;
FLastAppSettingsWrites: Integer;
FFormatSettings: TFormatSettings;
FTabsIniFilename: String;

// Host subtabs backend structures
FHostListResults: TDBQueryList;
@@ -1695,7 +1694,6 @@ procedure TMainForm.FormCreate(Sender: TObject);

// Ensure directories exist
ForceDirectories(DirnameUserAppData);
ForceDirectories(AppSettings.ReadString(asBackupDirectory));

// Move files from old default snippets directory, see issue #159
if not AppSettings.PortableMode then begin
@@ -2112,7 +2110,6 @@ procedure TMainForm.AfterFormCreate;
end;

// Restore backup'ed query tabs
FTabsIniFilename := DirnameUserAppData + 'tabs.ini';
if AppSettings.ReadBool(asRestoreTabs) then begin
RestoreTabs;
TimerStoreTabs.Enabled := True;
@@ -2134,21 +2131,26 @@ function TMainForm.InitTabsIniFile: TIniFile;
var
WaitingSince: UInt64;
Attempts: Integer;
TabsIniFilename: String;
begin
// Try to open tabs.ini for writing or reading
// Taking multiple application instances into account
if AppSettings.PortableMode then
TabsIniFilename := ExtractFilePath(Application.ExeName) + 'tabs.ini'
else
TabsIniFilename := DirnameUserAppData + 'tabs.ini';
WaitingSince := GetTickCount64;
Attempts := 0;
while not FileIsWritable(FTabsIniFilename) do begin
while not FileIsWritable(TabsIniFilename) do begin
if GetTickCount64 - WaitingSince > 3000 then
Raise Exception.Create(f_('Could not open file %s', [FTabsIniFilename]));
Raise Exception.Create(f_('Could not open file %s', [TabsIniFilename]));
Sleep(200);
Inc(Attempts);
end;
if Attempts > 0 then begin
LogSQL(Format('Had to wait %d ms before opening %s', [GetTickCount64 - WaitingSince, FTabsIniFilename]), lcDebug);
LogSQL(Format('Had to wait %d ms before opening %s', [GetTickCount64 - WaitingSince, TabsIniFilename]), lcDebug);
end;
Result := TIniFile.Create(FTabsIniFilename);
Result := TIniFile.Create(TabsIniFilename);
end;


@@ -2198,9 +2200,9 @@ procedure TMainForm.RestoreTabs;
begin
// Restore query tab setup from tabs.ini

LogSQL('Restoring tab setup from '+FTabsIniFilename, lcDebug);
try
TabsIni := InitTabsIniFile;
LogSQL('Restoring tab setup from '+TabsIni.FileName, lcDebug);

Sections := TStringList.Create;
TabsIni.ReadSections(Sections);
@@ -12931,7 +12933,7 @@ function TQueryTab.MemoBackupFilename: String;
if (MemoFilename <> '') and (not Memo.Modified) then begin
Result := '';
end else begin
Result := IncludeTrailingBackslash(AppSettings.ReadString(asBackupDirectory))
Result := IncludeTrailingBackslash(AppSettings.DirnameBackups)
+ goodfilename(Format(BACKUP_FILEPATTERN, [Uid]))
;
end;

0 comments on commit 3284509

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