|
| 1 | +unit controler_treeview_torrent_data; |
| 2 | + |
| 3 | +{ |
| 4 | + Show the torrent files content in a tree view format. |
| 5 | +} |
| 6 | +{$mode objfpc}{$H+} |
| 7 | + |
| 8 | +interface |
| 9 | + |
| 10 | +uses |
| 11 | + Classes, SysUtils, Controls, ComCtrls, Menus, StdCtrls, DecodeTorrent; |
| 12 | + |
| 13 | +type |
| 14 | + |
| 15 | + { Tcontroler_treeview_torrent_data } |
| 16 | + Tcontroler_treeview_torrent_data_items = ( |
| 17 | + ctv_ShowAllFiles = 0, |
| 18 | + ctv_ShowAllTrackers = 1, |
| 19 | + ctv_ShowAllInfo = 2, |
| 20 | + ctv_ShowEverything, |
| 21 | + ctv_HideAll |
| 22 | + ); |
| 23 | + |
| 24 | + Tcontroler_treeview_torrent_data = class |
| 25 | + private |
| 26 | + FTotalFileInsideTorrent: integer; |
| 27 | + FTotalFileSizeInsideTorrent: int64; |
| 28 | + |
| 29 | + FGroupBoxTorrentContents: TGroupBox; |
| 30 | + FPopupMenuTorrentFilesContent: TPopupMenu; |
| 31 | + FOwner: TWinControl; |
| 32 | + FTreeViewFileContents: TTreeView; |
| 33 | + FTreeNodeRoot: TTreeNode; |
| 34 | + FMenuItemTorrentFilesTreeHideAll: TMenuItem; |
| 35 | + FMenuItemTorrentFilesTreeShowTrackers: TMenuItem; |
| 36 | + FMenuItemTorrentFilesTreeShowInfo: TMenuItem; |
| 37 | + FMenuItemTorrentFilesTreeShowAll: TMenuItem; |
| 38 | + FMenuItemTorrentFilesTreeShowFiles: TMenuItem; |
| 39 | + procedure FillThePopupMenu; |
| 40 | + procedure AddMenuItem(var MenuItem: TMenuItem; Onclick: TNotifyEvent; |
| 41 | + const Caption: string; tag: integer); |
| 42 | + procedure MenuItemTorrentFilesTreeShowOrHideItemClick(Sender: TObject); |
| 43 | + procedure MenuItemTorrentFilesTreeHideAllClick(Sender: TObject); |
| 44 | + procedure MenuItemTorrentFilesTreeShowAllClick(Sender: TObject); |
| 45 | + procedure Clear; |
| 46 | + procedure MenuItemTorrentFilesTreeSyncWithPopupMenu; |
| 47 | + public |
| 48 | + //called before AddOneTorrentFileDecoded |
| 49 | + procedure BeginUpdate; |
| 50 | + |
| 51 | + //called after AddOneTorrentFileDecoded |
| 52 | + procedure EndUpdate; |
| 53 | + |
| 54 | + //Add every torrent one by one to the list |
| 55 | + procedure AddOneTorrentFileDecoded(DecodeTorrent: TDecodeTorrent); |
| 56 | + |
| 57 | + constructor Create(Owner: TWinControl); |
| 58 | + |
| 59 | + destructor Destroy; override; |
| 60 | + end; |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +implementation |
| 65 | + |
| 66 | +uses torrent_miscellaneous; |
| 67 | + |
| 68 | +const |
| 69 | + TORRENT_FILES_CONTENTS_FORM_CAPTION = |
| 70 | + 'Show all the files inside the torrents. (Use right mouse for popup menu.)'; |
| 71 | + |
| 72 | +{ Tcontroler_treeview_torrent_data } |
| 73 | + |
| 74 | +procedure Tcontroler_treeview_torrent_data.FillThePopupMenu; |
| 75 | +begin |
| 76 | + FPopupMenuTorrentFilesContent := TPopupMenu.Create(FTreeViewFileContents); |
| 77 | + FTreeViewFileContents.PopupMenu := FPopupMenuTorrentFilesContent; |
| 78 | + |
| 79 | + //There are 5 menu items the user can select/click on |
| 80 | + //Create all these 5 menu items. |
| 81 | + |
| 82 | + //'Show Everything' |
| 83 | + AddMenuItem(FMenuItemTorrentFilesTreeShowAll, @MenuItemTorrentFilesTreeShowAllClick, |
| 84 | + 'Show Everything', Ord(ctv_ShowEverything));//-1 |
| 85 | + |
| 86 | + //'Hide All' |
| 87 | + AddMenuItem(FMenuItemTorrentFilesTreeHideAll, @MenuItemTorrentFilesTreeHideAllClick, |
| 88 | + 'Hide All', Ord(ctv_HideAll));//0 |
| 89 | + |
| 90 | + //'Show All Files' |
| 91 | + AddMenuItem(FMenuItemTorrentFilesTreeShowFiles, |
| 92 | + @MenuItemTorrentFilesTreeShowOrHideItemClick, 'Show All Files', |
| 93 | + Ord(ctv_ShowAllFiles)); |
| 94 | + FMenuItemTorrentFilesTreeShowFiles.AutoCheck := True; |
| 95 | + FMenuItemTorrentFilesTreeShowFiles.Checked := True; |
| 96 | + |
| 97 | + //'Show All Trackers' |
| 98 | + AddMenuItem(FMenuItemTorrentFilesTreeShowTrackers, |
| 99 | + @MenuItemTorrentFilesTreeShowOrHideItemClick, 'Show All Trackers', |
| 100 | + Ord(ctv_ShowAllTrackers)); |
| 101 | + FMenuItemTorrentFilesTreeShowTrackers.AutoCheck := True; |
| 102 | + FMenuItemTorrentFilesTreeShowTrackers.Checked := True; |
| 103 | + |
| 104 | + //'Show All Info' |
| 105 | + AddMenuItem(FMenuItemTorrentFilesTreeShowInfo, |
| 106 | + @MenuItemTorrentFilesTreeShowOrHideItemClick, 'Show All Info', Ord(ctv_ShowAllInfo)); |
| 107 | + FMenuItemTorrentFilesTreeShowInfo.AutoCheck := True; |
| 108 | + FMenuItemTorrentFilesTreeShowInfo.Checked := True; |
| 109 | + |
| 110 | +end; |
| 111 | + |
| 112 | +procedure Tcontroler_treeview_torrent_data.AddMenuItem(var MenuItem: TMenuItem; |
| 113 | + Onclick: TNotifyEvent; const Caption: string; tag: integer); |
| 114 | +begin |
| 115 | + MenuItem := TMenuItem.Create(FPopupMenuTorrentFilesContent); |
| 116 | + MenuItem.OnClick := Onclick; |
| 117 | + MenuItem.Caption := Caption; |
| 118 | + MenuItem.Tag := tag; |
| 119 | + FPopupMenuTorrentFilesContent.Items.Add(MenuItem); |
| 120 | +end; |
| 121 | + |
| 122 | +constructor Tcontroler_treeview_torrent_data.Create(Owner: TWinControl); |
| 123 | +begin |
| 124 | + inherited Create; |
| 125 | + FOwner := Owner; |
| 126 | + |
| 127 | + //create TGroupBox |
| 128 | + FGroupBoxTorrentContents := TGroupBox.Create(FOwner); |
| 129 | + FGroupBoxTorrentContents.Parent := FOwner; |
| 130 | + FGroupBoxTorrentContents.Align := alClient; |
| 131 | + FGroupBoxTorrentContents.Caption := TORRENT_FILES_CONTENTS_FORM_CAPTION; |
| 132 | + |
| 133 | + //create TTreeView |
| 134 | + FTreeViewFileContents := TTreeView.Create(FGroupBoxTorrentContents); |
| 135 | + FTreeViewFileContents.Parent := FGroupBoxTorrentContents; |
| 136 | + FTreeViewFileContents.Align := alClient; |
| 137 | + FTreeViewFileContents.ReadOnly := True; |
| 138 | + FTreeViewFileContents.ShowRoot := False; |
| 139 | + FTreeViewFileContents.Options := |
| 140 | + FTreeViewFileContents.Options + [tvoAutoExpand, tvoReadOnly] - [tvoShowRoot]; |
| 141 | + |
| 142 | + //create popupmenu |
| 143 | + FillThePopupMenu; |
| 144 | + |
| 145 | +end; |
| 146 | + |
| 147 | +destructor Tcontroler_treeview_torrent_data.Destroy; |
| 148 | +begin |
| 149 | + inherited Destroy; |
| 150 | +end; |
| 151 | + |
| 152 | + |
| 153 | +procedure Tcontroler_treeview_torrent_data.MenuItemTorrentFilesTreeHideAllClick( |
| 154 | + Sender: TObject); |
| 155 | +var |
| 156 | + i, CountTorrents: integer; |
| 157 | +begin |
| 158 | + //hide all -> Show only torrent file names |
| 159 | + |
| 160 | + //user what to hide all the items. |
| 161 | + //All the popup menu item must first be unchecked. |
| 162 | + FMenuItemTorrentFilesTreeShowInfo.Checked := False; |
| 163 | + FMenuItemTorrentFilesTreeShowFiles.Checked := False; |
| 164 | + FMenuItemTorrentFilesTreeShowTrackers.Checked := False; |
| 165 | + //Update the TorrentFilesTree |
| 166 | + // MenuItemTorrentFilesTreeSyncWithPopupMenu; |
| 167 | + |
| 168 | + if not assigned(FTreeNodeRoot) then |
| 169 | + exit; |
| 170 | + |
| 171 | + //how many torrent files are there. |
| 172 | + CountTorrents := FTreeNodeRoot.Count; |
| 173 | + if CountTorrents = 0 then |
| 174 | + exit; |
| 175 | + |
| 176 | + //Show the torrent files names only. |
| 177 | + for i := 0 to CountTorrents - 1 do |
| 178 | + begin |
| 179 | + FTreeNodeRoot.Items[i].Collapse(True); |
| 180 | + end; |
| 181 | + |
| 182 | +end; |
| 183 | + |
| 184 | +procedure Tcontroler_treeview_torrent_data.MenuItemTorrentFilesTreeShowAllClick( |
| 185 | + Sender: TObject); |
| 186 | +begin |
| 187 | + //show everything |
| 188 | + if assigned(FTreeNodeRoot) then |
| 189 | + FTreeNodeRoot.Expand(True); |
| 190 | + |
| 191 | + //user what to see all the items. |
| 192 | + //All the popup menu item must first be checked. |
| 193 | + FMenuItemTorrentFilesTreeShowInfo.Checked := True; |
| 194 | + FMenuItemTorrentFilesTreeShowFiles.Checked := True; |
| 195 | + FMenuItemTorrentFilesTreeShowTrackers.Checked := True; |
| 196 | + //Update the TorrentFilesTree |
| 197 | + // MenuItemTorrentFilesTreeSyncWithPopupMenu; |
| 198 | +end; |
| 199 | + |
| 200 | +procedure Tcontroler_treeview_torrent_data.Clear; |
| 201 | +begin |
| 202 | + FTreeViewFileContents.Items.Clear; |
| 203 | + FTreeNodeRoot := FTreeViewFileContents.Items.Add(nil, 'Torrent Files'); |
| 204 | + FTotalFileInsideTorrent := 0; |
| 205 | + FTotalFileSizeInsideTorrent := 0; |
| 206 | +end; |
| 207 | + |
| 208 | +procedure Tcontroler_treeview_torrent_data.MenuItemTorrentFilesTreeSyncWithPopupMenu; |
| 209 | +begin |
| 210 | + MenuItemTorrentFilesTreeShowOrHideItemClick(FMenuItemTorrentFilesTreeShowTrackers); |
| 211 | + MenuItemTorrentFilesTreeShowOrHideItemClick(FMenuItemTorrentFilesTreeShowInfo); |
| 212 | + MenuItemTorrentFilesTreeShowOrHideItemClick(FMenuItemTorrentFilesTreeShowFiles); |
| 213 | +end; |
| 214 | + |
| 215 | +procedure Tcontroler_treeview_torrent_data.BeginUpdate; |
| 216 | +begin |
| 217 | + //Called before loading torrent file. |
| 218 | + |
| 219 | + //do not update the view |
| 220 | + FTreeViewFileContents.BeginUpdate; |
| 221 | + |
| 222 | + //Clear everything before adding new torrent files |
| 223 | + Clear; |
| 224 | +end; |
| 225 | + |
| 226 | +procedure Tcontroler_treeview_torrent_data.EndUpdate; |
| 227 | +begin |
| 228 | + //The view can be updated again |
| 229 | + FTreeViewFileContents.EndUpdate; |
| 230 | + |
| 231 | + //Update the text about howmany files are there etc. |
| 232 | + //Show the size of all the files inside the torrent |
| 233 | + //http://en.wikipedia.org/wiki/Gigabyte |
| 234 | + FGroupBoxTorrentContents.Caption := |
| 235 | + TORRENT_FILES_CONTENTS_FORM_CAPTION + ' (Files count: ' + |
| 236 | + IntToStr(FTotalFileInsideTorrent) + ') Files sizes: ' + |
| 237 | + ByteSizeToBiggerSizeFormatStr(FTotalFileSizeInsideTorrent) + ''; |
| 238 | + |
| 239 | + //Sync the popup menu with show/hide items. |
| 240 | + MenuItemTorrentFilesTreeSyncWithPopupMenu; |
| 241 | + |
| 242 | +end; |
| 243 | + |
| 244 | + |
| 245 | +procedure Tcontroler_treeview_torrent_data.MenuItemTorrentFilesTreeShowOrHideItemClick( |
| 246 | + Sender: TObject); |
| 247 | +var |
| 248 | + i, CountTorrents, itemsNr: integer; |
| 249 | + ShowNode: boolean; |
| 250 | +begin |
| 251 | + //Show or hide all the items below the torrent files. |
| 252 | + |
| 253 | + //Get the top node. |
| 254 | + if not assigned(FTreeNodeRoot) then |
| 255 | + exit; |
| 256 | + |
| 257 | + //how many torrent files are there. |
| 258 | + CountTorrents := FTreeNodeRoot.Count; |
| 259 | + if CountTorrents = 0 then |
| 260 | + exit; |
| 261 | + |
| 262 | + //The tag number define if it is for files, trackers or info items |
| 263 | + itemsNr := TMenuItem(Sender).tag; |
| 264 | + |
| 265 | + //Must show or hide the items |
| 266 | + ShowNode := TMenuItem(Sender).Checked; |
| 267 | + |
| 268 | + //process all the torrent files one by one. |
| 269 | + for i := 0 to CountTorrents - 1 do |
| 270 | + begin |
| 271 | + if ShowNode then |
| 272 | + begin |
| 273 | + FTreeNodeRoot.Items[i].Expand(False); //Show the torrent name + child |
| 274 | + FTreeNodeRoot.Items[i].Items[itemsNr].Expand(False); //expand child |
| 275 | + end |
| 276 | + else |
| 277 | + begin |
| 278 | + FTreeNodeRoot.Items[i].Items[itemsNr].Collapse(False); |
| 279 | + end; |
| 280 | + end; |
| 281 | +end; |
| 282 | + |
| 283 | +procedure Tcontroler_treeview_torrent_data.AddOneTorrentFileDecoded( |
| 284 | + DecodeTorrent: TDecodeTorrent); |
| 285 | +var |
| 286 | + CountFiles: integer; |
| 287 | + TorrentFileNameStr, TrackerStr: UTF8String; |
| 288 | + DateTimeStr: string; |
| 289 | + TreeNodeTorrent, TreeNodeFiles, TreeNodeTrackers, TreeNodeInfo: TTreeNode; |
| 290 | + |
| 291 | +begin |
| 292 | + //--------------------- Fill the treeview with torrent files |
| 293 | + |
| 294 | + TorrentFileNameStr := ExtractFileName(DecodeTorrent.FilenameTorrent); |
| 295 | + |
| 296 | + //Add the torrent file name + size of all the files combined. |
| 297 | + TorrentFileNameStr := TorrentFileNameStr + ' SIZE: ' + |
| 298 | + ByteSizeToBiggerSizeFormatStr(DecodeTorrent.TotalFileSize) + |
| 299 | + ' Files: ' + IntToStr(DecodeTorrent.InfoFilesCount) + '' + |
| 300 | + ' Tracker: ' + IntToStr(DecodeTorrent.TrackerList.Count) + ''; |
| 301 | + |
| 302 | + |
| 303 | + TreeNodeTorrent := FTreeViewFileContents.Items.AddChild(FTreeNodeRoot, |
| 304 | + //FTrackerList.TorrentFileNameList[RowIndex]); //With directory path |
| 305 | + TorrentFileNameStr); //Without directory path |
| 306 | + |
| 307 | + //must be in this order (Files, Trackers, Info) |
| 308 | + TreeNodeFiles := FTreeViewFileContents.Items.AddChild(TreeNodeTorrent, 'Files'); |
| 309 | + TreeNodeTrackers := FTreeViewFileContents.Items.AddChild(TreeNodeTorrent, |
| 310 | + 'Trackers'); |
| 311 | + TreeNodeInfo := FTreeViewFileContents.Items.AddChild(TreeNodeTorrent, 'Info'); |
| 312 | + |
| 313 | + //Show all the files inside the torrent |
| 314 | + if DecodeTorrent.InfoFilesCount > 0 then |
| 315 | + begin |
| 316 | + for CountFiles := 0 to DecodeTorrent.InfoFilesCount - 1 do |
| 317 | + begin |
| 318 | + FTreeViewFileContents.Items.AddChild(TreeNodeFiles, |
| 319 | + DecodeTorrent.InfoFilesNameIndex(CountFiles) + ' SIZE: ' + |
| 320 | + ByteSizeToBiggerSizeFormatStr(DecodeTorrent.InfoFilesLengthIndex(CountFiles))); |
| 321 | + end; |
| 322 | + end; |
| 323 | + |
| 324 | + //Show a how many files are there |
| 325 | + TreeNodeFiles.Text := TreeNodeFiles.Text + ' (' + IntToStr(TreeNodeFiles.Count) + ')'; |
| 326 | + |
| 327 | + //Show all the trackers inside the torrent |
| 328 | + for TrackerStr in DecodeTorrent.TrackerList do |
| 329 | + begin |
| 330 | + FTreeViewFileContents.Items.AddChild(TreeNodeTrackers, TrackerStr); |
| 331 | + end; |
| 332 | + |
| 333 | + //Show a how many trackers are there |
| 334 | + TreeNodeTrackers.Text := TreeNodeTrackers.Text + ' (' + |
| 335 | + IntToStr(TreeNodeTrackers.Count) + ')'; |
| 336 | + |
| 337 | + |
| 338 | + //Show all the info of torrent |
| 339 | + FTreeViewFileContents.Items.AddChild(TreeNodeInfo, 'Name: ' + DecodeTorrent.Name); |
| 340 | + |
| 341 | + FTreeViewFileContents.Items.AddChild(TreeNodeInfo, 'Comment: ' + |
| 342 | + DecodeTorrent.Comment); |
| 343 | + FTreeViewFileContents.Items.AddChild(TreeNodeInfo, 'Info Hash: ' + |
| 344 | + DecodeTorrent.InfoHash); |
| 345 | + FTreeViewFileContents.Items.AddChild(TreeNodeInfo, 'Created On: ' + DateTimeStr); |
| 346 | + FTreeViewFileContents.Items.AddChild(TreeNodeInfo, 'Created By: ' + |
| 347 | + DecodeTorrent.CreatedBy); |
| 348 | + FTreeViewFileContents.Items.AddChild(TreeNodeInfo, 'Piece Lenght: ' + |
| 349 | + IntToStr(DecodeTorrent.PieceLenght div 1024) + ' KiB'); |
| 350 | + if DecodeTorrent.PrivateTorrent then |
| 351 | + begin |
| 352 | + FTreeViewFileContents.Items.AddChild(TreeNodeInfo, 'Private: yes'); |
| 353 | + end |
| 354 | + else |
| 355 | + begin |
| 356 | + FTreeViewFileContents.Items.AddChild(TreeNodeInfo, 'Private: no'); |
| 357 | + end; |
| 358 | + |
| 359 | + //All the files count inside the torrent must be added to FTotalFileInsideTorrent |
| 360 | + Inc(FTotalFileInsideTorrent, DecodeTorrent.InfoFilesCount); |
| 361 | + |
| 362 | + //The file size of all files inside the torrent must be added to FTotalFileSizeInsideTorrent |
| 363 | + Inc(FTotalFileSizeInsideTorrent, DecodeTorrent.TotalFileSize); |
| 364 | + |
| 365 | +end; |
| 366 | + |
| 367 | + |
| 368 | + |
| 369 | +end. |
0 commit comments