Skip to content
Permalink
Browse files

Silence reoccurring error dialogues due to non-writable portable_sett…

…ings.txt, by introducing a portable-read-only mode as soon as an update to that file fails
  • Loading branch information...
ansgarbecker committed Jul 10, 2019
1 parent a249753 commit fae11b70aae5b416f22a2df2647089da72ffa464
Showing with 44 additions and 13 deletions.
  1. +4 −1 out/locale/en/LC_MESSAGES/default.po
  2. +27 −6 source/apphelpers.pas
  3. +13 −6 source/main.pas
@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: HeidiSQL\n"
"POT-Creation-Date: 2012-11-05 21:40\n"
"PO-Revision-Date: 2019-07-08 20:21+0200\n"
"PO-Revision-Date: 2019-07-10 11:01+0200\n"
"Last-Translator: Ansgar Becker <anse@heidisql.com>\n"
"Language-Team: English (http://www.transifex.com/projects/p/heidisql/language/en/)\n"
"MIME-Version: 1.0\n"
@@ -4370,6 +4370,9 @@ msgstr "Settings successfully restored from %s"
msgid "Portable settings file written, with %d changes."
msgstr "Portable settings file written, with %d changes."

msgid "Switching to read-only mode. Settings won't be saved. Use the command line parameter %s to use a custom file path."
msgstr "Switching to read-only mode. Settings won't be saved. Use the command line parameter %s to use a custom file path."

#: main.pas:2142
msgid "Splitting SQL queries ..."
msgstr "Splitting SQL queries ..."
@@ -200,6 +200,7 @@ TAppSettings = class(TObject)
FSessionPath: String;
FRegistry: TRegistry;
FPortableMode: Boolean;
FPortableModeReadOnly: Boolean;
FRestoreTabsInitValue: Boolean;
FSettingsFile: String;
FSettings: Array[TAppSettingIndex] of TAppSetting;
@@ -242,10 +243,11 @@ TAppSettings = class(TObject)
procedure ResetPath;
property SessionPath: String read FSessionPath write SetSessionPath;
property PortableMode: Boolean read FPortableMode;
property PortableModeReadOnly: Boolean read FPortableModeReadOnly write FPortableModeReadOnly;
property Writes: Integer read FWrites;
procedure ImportSettings(Filename: String);
procedure ExportSettings(Filename: String); overload;
procedure ExportSettings; overload;
function ExportSettings(Filename: String): Boolean; overload;
function ExportSettings: Boolean; overload;
// Common directories
function DirnameUserAppData: String;
function DirnameUserDocuments: String;
@@ -3402,6 +3404,7 @@ constructor TAppSettings.Create;

// Switch to portable mode if lock file exists. File content is ignored.
FPortableMode := FileExists(PortableLockFile);
FPortableModeReadOnly := False;

if FPortableMode then begin
// Create file if only the lock file exists
@@ -3709,7 +3712,11 @@ destructor TAppSettings.Destroy;
begin
// Export settings into textfile in portable mode.
if FPortableMode then try
ExportSettings(FSettingsFile);
try
ExportSettings;
except
// do nothing, even ShowMessage or ErrorDialog would trigger timer events followed by crashes;
end;
FRegistry.CloseKey;
FRegistry.DeleteKey(FBasePath);

@@ -4142,7 +4149,7 @@ procedure TAppSettings.ImportSettings(Filename: String);
end;


procedure TAppSettings.ExportSettings(Filename: String);
function TAppSettings.ExportSettings(Filename: String): Boolean;
var
Content, Value: String;
DataType: TRegDataType;
@@ -4188,12 +4195,26 @@ procedure TAppSettings.ExportSettings(Filename: String);
Content := '';
ReadKeyToContent(FBasePath);
SaveUnicodeFile(FileName, Content);
Result := True;
end;


procedure TAppSettings.ExportSettings;
function TAppSettings.ExportSettings: Boolean;
begin
ExportSettings(FSettingsFile);
Result := False;
if not FPortableModeReadOnly then begin
try
ExportSettings(FSettingsFile);
Result := True;
except
on E:Exception do begin
FPortableModeReadOnly := True;
Raise Exception.Create(E.Message + CRLF + CRLF
+ f_('Switching to read-only mode. Settings won''t be saved. Use the command line parameter %s to use a custom file path.', ['--psettings'])
);
end;
end;
end;
end;


@@ -12616,13 +12616,20 @@ procedure TMainForm.ApplicationEvents1Deactivate(Sender: TObject);

procedure TMainForm.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
begin
if AppSettings.PortableMode and (FLastPortableSettingsSave < GetTickCount-60000) then begin
if AppSettings.Writes > FLastAppSettingsWrites then begin
AppSettings.ExportSettings;
LogSQL(f_('Portable settings file written, with %d changes.', [AppSettings.Writes-FLastAppSettingsWrites]), lcDebug);
if AppSettings.PortableMode
and (not AppSettings.PortableModeReadOnly)
and (FLastPortableSettingsSave < GetTickCount-60000) then begin
try
if AppSettings.Writes > FLastAppSettingsWrites then begin
if AppSettings.ExportSettings then
LogSQL(f_('Portable settings file written, with %d changes.', [AppSettings.Writes-FLastAppSettingsWrites]), lcDebug);
end;
FLastAppSettingsWrites := AppSettings.Writes;
FLastPortableSettingsSave := GetTickCount;
except
on E:Exception do
ErrorDialog(E.Message);
end;
FLastAppSettingsWrites := AppSettings.Writes;
FLastPortableSettingsSave := GetTickCount;
end;

// Sort list tables in idle time, so ListTables.TreeOptions.AutoSort does not crash the list

0 comments on commit fae11b7

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