GUI: main.pas, main.lfm and the controllers
For the user view of the same window, see the GUI guide.
- Overview
- The index invariant
- Loading torrent files
- Updating the torrent files:
UpdateTorrent TControllerTrackerListOnline: the Keep gridTControllerGridTorrentData: the Data/Info gridTControllerTreeviewTorrentData: the tree- Menu handlers that talk to the network
- Resource file
main.lfm
Overview
flowchart TB
subgraph Form["TFormTrackerModify (main.pas + main.lfm)"]
direction TB
MENU["MainMenu: File, Trackers, Update torrent,<br/>Online Check, ngosang/trackerslist, Help"]
subgraph TABS["PageControl: 5 tabs"]
T1["TabSheetTrackersList<br/>MemoNewTrackers + StringGridTrackerOnline"]
T2["TabSheetPublicPrivateTorrent<br/>CheckListBoxPublicPrivateTorrent"]
T3["TabSheetTorrentData<br/>StringGridTorrentData"]
T4["TabSheetTorrentsContents<br/>(filled by the tree controller)"]
T5["TabSheetPrivateTrackers<br/>CheckBoxSkipAnnounceCheck, CheckBoxRemoveAllSourceTag,<br/>LabeledEditInfoSource"]
end
end
T1 -. "grid" .-> C1["TControllerTrackerListOnline"]
T3 -. "grid" .-> C2["TControllerGridTorrentData"]
T4 -. "parent" .-> C3["TControllerTreeviewTorrentData"]
C1 --> NT["TNewTrackon + TTrackerListOnline"]
Form --> DEC["FDecodePresentTorrent: TDecodeTorrent"]
Form --> TL["FTrackerList: TTrackerList"]
Form --> NG["FngosangTrackerList: TngosangTrackerList"]
main.pas is a thin orchestrator. The rule: no decisions in the event handlers that can be a pure function. main.pas cannot be unit tested; main_common.pas and torrent_miscellaneous.pas can.
Fields of TFormTrackerModify
| Field | Type | Role |
|---|---|---|
FTrackerList | TTrackerList | The shared record, see Tracker pipeline. |
FDecodePresentTorrent | TDecodeTorrent | One decoder object that is reused for every torrent file (load, view, update). |
FControllerTrackerListOnline | TControllerTrackerListOnline | The Keep / Online status / Tracker URL grid. |
FControllerGridTorrentData | TControllerGridTorrentData | The Data/Info grid. |
FControllerTreeviewTorrentData | TControllerTreeviewTorrentData | The Files/Trackers/Info tree. |
FngosangTrackerList | TngosangTrackerList | Downloads the ngosang lists. |
FDownloadStatus | boolean | The newTrackon lists are downloaded. |
FDragAndDropStartUp | boolean | Started with one parameter. While true the announce check is relaxed so that a drag and drop never fails on it. Reset at the end of FormCreate. |
FConsoleMode | boolean | Two or more parameters. |
FFilePresentBanByUserList | boolean | remove_trackers.txt exists (an empty file is meaningful). |
FFolderForTrackerListLoadAndSave | string | Result of DetermineTrackerListFolder. |
The index invariant
Four views describe the same torrent files and are matched by index:
| Place | Item for torrent number i (0 based) |
|---|---|
FTrackerList.TorrentFileNameList | [i] |
CheckListBoxPublicPrivateTorrent | item i (the public/private checkbox) |
StringGridTorrentData | row i + 1 (row 0 is the header). The user may sort it. |
FControllerTreeviewTorrentData | child i of the root node |
ReadTorrentFileSettingList builds the TTorrentFileSettingArray by index. Before it runs, FControllerGridTorrentData.ReorderGrid sorts the grid back to load order using the hidden IndexOrder column, otherwise the edited comments would be written to the wrong torrent. If a torrent fails to load, every view is cleared (ClearTorrentFilesView) so the indices never drift. After an update, a file that cannot be read back is deleted from TorrentFileNameList for the same reason.
Loading torrent files
sequenceDiagram
autonumber
actor User
participant Form as TFormTrackerModify
participant Dec as FDecodePresentTorrent
participant Ctl as Controllers
User->>Form: File > Open ... / drag and drop
Form->>Form: ClearAllTorrentFilesNameAndTrackerInside
Form->>Form: ViewUpdateBegin (BeginUpdate on views, clear them)
loop every torrent file (AddTorrentFileList)
Form->>Dec: DecodeTorrent(file)
alt cannot decode
Form->>Form: clear lists + views, ShowUserErrorMessage, stop
else decoded
Form->>Ctl: ViewUpdateOneTorrentFileDecoded<br/>(tree node, public/private item, grid row)
Form->>Form: UpdateTorrentTrackerList<br/>(AddTorrentFileTrackers into FTrackerList)
Form->>Form: TorrentFileNameList.Add(file)
end
end
Form->>Form: ViewUpdateEnd
Note over Form: ViewUpdateEnd: EndUpdate, group box caption,<br/>ShowTrackerInsideFileList (fills the Keep grid),<br/>UpdateViewRemoveTracker (remove_trackers.txt), form caption
UpdateViewRemoveTracker applies remove_trackers.txt to the view: it unchecks the banned trackers in the Keep grid and removes them from the Add new trackers memo. It is skipped for -U5 and -U6. The memo is changed like in console mode: RemoveTrackersFromList runs on a copy of MemoNewTrackers.Lines, and the control is written once, only when a line is removed. Every read of Lines goes to the control, so searching it for each banned tracker was very slow for a long list.
Entry points that end in AddTorrentFileList:
| Entry | Handler |
|---|---|
| File > Open one torrent file | MenuOpenTorrentFileClick |
| File > Open multiple torrent files inside one folder | MenuFileTorrentFolderClick (uses torrent_miscellaneous.LoadTorrentViaDir) |
| Drag and drop (also from the program icon: one parameter) | FormDropFiles, DragAndDropStartupMode |
FormDropFiles sorts the dropped items: a folder (checked first because a folder name may contain a dot), a .torrent file, or a .txt file (its trackers are appended to the memo after SanitizeTrackerList). The first torrent item clears the previously loaded torrents. If the dropped .txt makes the memo invalid, the previous memo text is restored.
DragAndDropStartupMode (one parameter at the start) only loads the files and reports an error. Like every other entry point it leaves the Keep grid and remove_trackers.txt to ViewUpdateEnd, which rebuilds the grid with the default check state and applies the remove list. Doing it before ViewUpdateEnd as well was wasted work: the rebuild undid it.
Updating the torrent files: UpdateTorrent
All eight Update torrent menu items only set FTrackerList.TrackerListOrderForUpdatedTorrent and call UpdateTorrent.
flowchart TD
M["menu item click"] --> O["FTrackerList.TrackerListOrderForUpdatedTorrent := order"]
O --> U["UpdateTorrent"]
U --> R["FControllerGridTorrentData.ReorderGrid"]
R --> S["FTrackerList.SourceTag := LabeledEditInfoSource.Text"]
S --> W{"warning dialog<br/>'Torrent files will be change!'<br/>OK ?"}
W -- cancel --> END1(["stop"])
W -- OK --> N{"torrent files loaded ?"}
N -- no --> E1["error 'No torrent file selected'"] --> END1
N -- yes --> I["ReadDeselectedTrackersFromGrid<br/>unchecked rows -> TrackerManuallyDeselectedByUserList"]
I --> V{"CopyUserInputNewTrackersToList<br/>(memo valid ?)"}
V -- no --> END1
V -- yes --> C["CombineFiveTrackerListToOne(tloSort, ...)<br/>CountTrackers := final count"]
C --> Z{"CountTrackers = 0 ?"}
Z -- yes --> W2{"warning 'no trackers selected'<br/>OK ?"}
W2 -- cancel --> END1
W2 -- OK --> X
Z -- no --> X["UpdateTorrentFileList(FTrackerList, FDecodePresentTorrent,<br/>ReadTorrentFileSettingList)"]
X --> EX["TrySaveTrackerFinalListToFile<br/>(export_trackers.txt)"]
EX --> RL["ReloadAllTorrentAndRefreshView<br/>(decode every file again, rebuild all views)"]
RL --> MSG["message box:<br/>'All torrent file(s) have now N trackers.'<br/>+ warnings (read-only, write failed, read back failed, export failed)"]
Important details:
- The hour glass cursor is reset in a
finallyblock, so everyexitis safe. - The message depends on the order mode: for
-U5/-U6(“remove nothing”) the tracker count is meaningless (every torrent differs), so the message is All torrent file(s) are updated. - The grid Keep column only fills
TrackerManuallyDeselectedByUserList(the unchecked rows), the same way as console mode.TrackerFromInsideTorrentFilesListis not changed before the merge: the grid is built from it, so an update that stops early (invalid memo, Cancel) loses no row. OnlyReloadAllTorrentAndRefreshViewfills it again, from the updated files. An unchecked tracker is still in U (the “others”), but the merge removes it again, unless it is also a new tracker.
TControllerTrackerListOnline: the Keep grid
- Three columns:
Keep(checkbox column, cell text'1'/'0'),Online status(hidden until a download succeeds),Tracker URL. - It is built on
FTrackerList.TrackerFromInsideTorrentFilesList(passed in the constructor) and a callbackTDefaultChecked(TFormTrackerModify.TrackerWithURLAndAnnounce) that decides the initial check state: a URL with a wrong scheme, or without/announcewhile the announce check is on, starts unchecked. UpdateViewclears the grid and appends one row per tracker with the status fromTTrackerListOnline.TrackerStatus. Every row gets the default check state again, so it is only called when the tracker list changes (torrent files loaded or reloaded).- It owns a
TNewTrackonand aTTrackerListOnline; the latter shares the threeTStringLists of the former (Live,Dead,Stable).DownloadTrackers_All_Live_Stabledownloads, callsTTrackerListOnline.RebuildIndex(the lists were refilled in place), callsUpdateStatusColumnand shows the status column.UpdateStatusColumnonly writes the Online status cells: the rows and the Keep choices of the user stay. - Alternating row colour is disabled for a dark theme (
IsDarkTheme): the text would be invisible on Linux and macOS. SubmitTrackersforwards toTNewTrackon.SubmitTrackers.
TControllerGridTorrentData: the Data/Info grid
- 13 columns, defined in
main.lfm(design time), in this fixed order:TorrentFile,InfoFileName,TorrentVersion,Padding,InfoHash,CreatedOn,CreatedBy,Comment,PrivateTorrent,InfoSource,PieceLength,TotalSize,IndexOrder(hidden). - The form fills the public string properties for one torrent, then calls
AppendRow. - The user can move columns.
StringGridTorrentDataColRowMovedonly sets a flag;UpdateColumnTagthen writes the real position of every column into itsTag, andWriteCell/ReadCommentuse thatTag. That is why the code usesColumn.Tagand not a fixed column number. ReorderGridsorts the hiddenIndexOrdercolumn ascending to restore the load order.- Only the Comment column is editable (the grid has
goEditing, other columns areReadOnly).
TControllerTreeviewTorrentData: the tree
- Created on
TabSheetTorrentsContents; it creates its ownTGroupBox+TTreeView+ popup menu at run time. - Structure: root node Torrent Files → one node per torrent → three children in this order:
Files V1/V2,Trackers,Info. The menuTagvalues (ctv_ShowAllFiles = 0,ctv_ShowAllTrackers = 1,ctv_ShowAllInfo = 2) are the child index, so the order of creation must not change. BeginUpdateclears the tree,AddOneTorrentFileDecodedadds one torrent,EndUpdatewrites the totals in the group box caption and synchronises the popup menu.
Menu handlers that talk to the network
| Handler | Calls | On failure |
|---|---|---|
MenuItemOnlineCheckDownloadNewTrackonClick | FControllerTrackerListOnline.DownloadTrackers_All_Live_Stable | error message |
MenuItemOnlineCheckAppendStableTrackersClick | download if not FDownloadStatus, then AppendTrackersToMemoNewTrackers(StableTrackers) | |
MenuTrackersDeleteTrackersWithStatusClick | download if needed; exit when it failed (all statuses would be Unknown); then unchecks the rows of the status given by Sender.Tag (0 = Unstable, 1 = Dead, 2 = Unknown) | error message |
MenuItemOnlineCheckSubmitNewTrackonClick | GetTrackersForOnlineSubmit (never private trackers), then SubmitTrackers | error message |
MenuItemNgosangAppend*Click | AppendNgosangTrackersToMemoNewTrackers(list): checks LastDownloadFailed first | error message |
AppendTrackersToMemoNewTrackers appends to the memo and validates it with CopyUserInputNewTrackersToList; when invalid, the memo text from before is restored.
Resource file main.lfm
main.lfm holds the layout, the captions and the OnClick bindings. After you edit it in the Lazarus IDE, check that the captions that the user manual documents did not change: see Maintaining this documentation.
PROGRAM_VERSION (main.pas, implementation section) is the version number shown in the window caption. Change it for a release, together with docs/history.md.