Skip to content
Permalink
Browse files

Issue #140: Store application process id in each tabs.ini section, an…

…d don't restore tabs from other running processes. If the process no longer runs, restore such tab sections again, so nothing gets lost with multiple running application instances.
  • Loading branch information...
ansgarbecker committed Apr 13, 2019
1 parent e9cc528 commit a2ef1aeccdcb705f893f33166e98f96cf71e2394
Showing with 39 additions and 3 deletions.
  1. +21 −0 source/apphelpers.pas
  2. +18 −3 source/main.pas
@@ -359,6 +359,7 @@ TAppSettings = class(TObject)
function DpiScaleFactor(Form: TForm): Double;
function GetThemeColor(Color: TColor): TColor;
function ThemeIsDark(ThemeName: String): Boolean;
function ProcessExists(pid: Cardinal): Boolean;

var
AppSettings: TAppSettings;
@@ -3010,6 +3011,26 @@ function ThemeIsDark(ThemeName: String): Boolean;
end;


function ProcessExists(pid: Cardinal): Boolean;
var
Proc: TProcessEntry32;
SnapShot: THandle;
ContinueLoop: Boolean;
begin
// Check if a given process id exists
SnapShot := CreateToolhelp32Snapshot(TH32CS_SnapProcess, 0);
Proc.dwSize := Sizeof(Proc);
Result := False;
ContinueLoop := Process32First(SnapShot, Proc);
while ContinueLoop do begin
Result := Proc.th32ProcessID = pid;
if Result then
Break;
ContinueLoop := Process32Next(SnapShot, Proc);
end;
CloseHandle(Snapshot);
end;



{ Threading stuff }
@@ -2173,11 +2173,13 @@ procedure TMainForm.StoreTabs;
Section := Tab.Uid;

if Tab.Memo.GetTextLen > 0 then begin
// Avoid writing the tabs.ini file through WriteString if nothing was effectively changed
// Avoid writing the tabs.ini file if nothing was effectively changed
if TabsIni.ReadString(Section, 'BackupFilename', '') <> Tab.MemoBackupFilename then
TabsIni.WriteString(Section, 'BackupFilename', Tab.MemoBackupFilename);
if TabsIni.ReadString(Section, 'Filename', '') <> Tab.MemoFilename then
TabsIni.WriteString(Section, 'Filename', Tab.MemoFilename);
if TabsIni.ReadInteger(Section, 'pid', 0) <> Integer(GetCurrentProcessId) then
TabsIni.WriteInteger(Section, 'pid', Integer(GetCurrentProcessId));
end;
end;

@@ -2197,6 +2199,7 @@ procedure TMainForm.RestoreTabs;
Sections: TStringList;
Section, Filename, BackupFilename: String;
TabsIni: TIniFile;
pid: Cardinal;
begin
// Restore query tab setup from tabs.ini

@@ -2206,9 +2209,19 @@ procedure TMainForm.RestoreTabs;

Sections := TStringList.Create;
TabsIni.ReadSections(Sections);

for Section in Sections do begin

Filename := TabsIni.ReadString(Section, 'Filename', '');
BackupFilename := TabsIni.ReadString(Section, 'BackupFilename', '');
pid := Cardinal(TabsIni.ReadInteger(Section, 'pid', 0));

// Don't restore this tab if it belongs to a different running process
if (pid > 0) and (pid <> GetCurrentProcessId) and ProcessExists(pid) then
Continue;

// Either we have a backup file, or a user stored file.
// Both of them may not exist.
if not BackupFilename.IsEmpty then begin
if FileExists(BackupFilename) then begin
Tab := ActiveOrEmptyQueryTab(False);
@@ -2217,7 +2230,7 @@ procedure TMainForm.RestoreTabs;
Tab.MemoFilename := Filename;
Tab.Memo.Modified := True;
end else begin
// Remove ini item if file is gone
// Remove tab section if backup file is gone or inaccessible for some reason
TabsIni.EraseSection(Section);
end;
end else if not Filename.IsEmpty then begin
@@ -2227,11 +2240,13 @@ procedure TMainForm.RestoreTabs;
Tab.LoadContents(Filename, True, nil);
Tab.MemoFilename := Filename;
end else begin
// Remove ini item if file is gone
// Remove tab section if user stored file was deleted by user
TabsIni.EraseSection(Section);
end;
end;

end;

Sections.Free;
// Close file
TabsIni.Free;

0 comments on commit a2ef1ae

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