|
| 1 | +unit controler_trackerlist_online; |
| 2 | + |
| 3 | +{ |
| 4 | + Show the present status of the trackerURL via TStringGrid |
| 5 | +} |
| 6 | +{$mode objfpc}{$H+} |
| 7 | + |
| 8 | +interface |
| 9 | + |
| 10 | +uses |
| 11 | + Classes, SysUtils, Grids, trackerlist_online, newtrackon; |
| 12 | + |
| 13 | +type |
| 14 | + |
| 15 | + { TControlerTrackerListOnline } |
| 16 | + |
| 17 | + TControlerTrackerListOnline = class |
| 18 | + private |
| 19 | + FStringGridTorrentURL: TStringGrid; |
| 20 | + FNewTrackon: TNewTrackon; |
| 21 | + FTrackerListOnline: TTrackerListOnline; |
| 22 | + FTrackerList: TStringList; |
| 23 | + |
| 24 | + //The collumn must be in this design order. |
| 25 | + FSelect, //< 0 |
| 26 | + FTorrentURL, //< 1 |
| 27 | + FTorrentURL_Status //< 2 |
| 28 | + : TGridColumn; |
| 29 | + |
| 30 | + function GetChecked(index: integer): boolean; |
| 31 | + procedure SetChecked(index: integer; AValue: boolean); |
| 32 | + procedure ShowTrackerStatus(Visible: boolean); |
| 33 | + procedure AppendRow(Checked: boolean; Status: TTrackerListOnlineStatus; |
| 34 | + const TrackerURL: UTF8String); |
| 35 | + public |
| 36 | + |
| 37 | + property Checked[index: integer]: boolean read GetChecked write SetChecked; |
| 38 | + function TrackerURL(index: integer): string; |
| 39 | + function TrackerStatus(index: integer): TTrackerListOnlineStatus; |
| 40 | + function Count: integer; |
| 41 | + function StableTrackers: TStringList; |
| 42 | + |
| 43 | + //must be called if the tracker list is updated |
| 44 | + procedure UpdateView; |
| 45 | + |
| 46 | + function DownloadTrackers: boolean; |
| 47 | + |
| 48 | + constructor Create(StringGridTorrentURL: TStringGrid; TrackerList: TStringList); |
| 49 | + destructor Destroy; override; |
| 50 | + end; |
| 51 | + |
| 52 | + |
| 53 | +implementation |
| 54 | + |
| 55 | +uses Graphics; |
| 56 | + |
| 57 | +{ TControlerTrackerListOnline } |
| 58 | + |
| 59 | + |
| 60 | +function TControlerTrackerListOnline.DownloadTrackers: boolean; |
| 61 | +begin |
| 62 | + Result := FNewTrackon.DownloadTrackers; |
| 63 | + UpdateView; |
| 64 | + ShowTrackerStatus(Result); |
| 65 | +end; |
| 66 | + |
| 67 | +constructor TControlerTrackerListOnline.Create(StringGridTorrentURL: TStringGrid; |
| 68 | + TrackerList: TStringList); |
| 69 | +begin |
| 70 | + |
| 71 | + FTrackerList := TrackerList; |
| 72 | + |
| 73 | + FStringGridTorrentURL := StringGridTorrentURL; |
| 74 | + FStringGridTorrentURL.RowCount := 1; |
| 75 | + FStringGridTorrentURL.FixedRows := 1; |
| 76 | + FStringGridTorrentURL.AlternateColor := clCream; |
| 77 | + |
| 78 | + FSelect := FStringGridTorrentURL.Columns.Add; |
| 79 | + FSelect.Title.Caption := 'Keep'; |
| 80 | + FSelect.ButtonStyle := cbsCheckboxColumn; |
| 81 | + FSelect.ReadOnly := False; |
| 82 | + |
| 83 | + |
| 84 | + FTorrentURL_Status := FStringGridTorrentURL.Columns.Add; |
| 85 | + FTorrentURL_Status.Title.Caption := 'Online status'; |
| 86 | + ShowTrackerStatus(False); |
| 87 | + FTorrentURL_Status.ReadOnly := True; |
| 88 | + |
| 89 | + FTorrentURL := FStringGridTorrentURL.Columns.Add; |
| 90 | + FTorrentURL.Title.Caption := 'Tracker URL'; |
| 91 | + FTorrentURL.ReadOnly := True; |
| 92 | + |
| 93 | + //make sure all text are fit inside the columns |
| 94 | + FStringGridTorrentURL.AutoSizeColumns; |
| 95 | + |
| 96 | + FNewTrackon := TNewTrackon.Create; |
| 97 | + FTrackerListOnline := TTrackerListOnline.Create; |
| 98 | + |
| 99 | + //copy tracker list from TNewTrackon to TTrackerListOnline |
| 100 | + FTrackerListOnline.TrackerList_Live := FNewTrackon.TrackerList_Live; |
| 101 | + FTrackerListOnline.TrackerList_Dead := FNewTrackon.TrackerList_Dead; |
| 102 | + FTrackerListOnline.TrackerList_Stable := FNewTrackon.TrackerList_Stable; |
| 103 | +end; |
| 104 | + |
| 105 | +destructor TControlerTrackerListOnline.Destroy; |
| 106 | +begin |
| 107 | + FTrackerListOnline.Free; |
| 108 | + FNewTrackon.Free; |
| 109 | + inherited Destroy; |
| 110 | +end; |
| 111 | + |
| 112 | +procedure TControlerTrackerListOnline.ShowTrackerStatus(Visible: boolean); |
| 113 | +begin |
| 114 | + FTorrentURL_Status.Visible := Visible; |
| 115 | +end; |
| 116 | + |
| 117 | +function TControlerTrackerListOnline.GetChecked(index: integer): boolean; |
| 118 | +begin |
| 119 | + //read the select checkbox. If '1' then it is True |
| 120 | + Result := FStringGridTorrentURL.Cells[0, index + |
| 121 | + FStringGridTorrentURL.FixedRows] = '1'; |
| 122 | +end; |
| 123 | + |
| 124 | +procedure TControlerTrackerListOnline.SetChecked(index: integer; AValue: boolean); |
| 125 | +begin |
| 126 | + FStringGridTorrentURL.Cells[0, index + FStringGridTorrentURL.FixedRows] := |
| 127 | + BoolToStr(AValue, '1', '0'); |
| 128 | +end; |
| 129 | + |
| 130 | + |
| 131 | +procedure TControlerTrackerListOnline.AppendRow(Checked: boolean; |
| 132 | + Status: TTrackerListOnlineStatus; const TrackerURL: UTF8String); |
| 133 | +var |
| 134 | + CheckedStr, StatusStr: string; |
| 135 | +begin |
| 136 | + CheckedStr := BoolToStr(Checked, '1', '0'); |
| 137 | + StatusStr := FTrackerListOnline.TrackerListOnlineStatusToString(Status); |
| 138 | + |
| 139 | + //append to the end of the view list |
| 140 | + FStringGridTorrentURL.InsertRowWithValues(FStringGridTorrentURL.RowCount, |
| 141 | + [CheckedStr, StatusStr, TrackerURL]); |
| 142 | +end; |
| 143 | + |
| 144 | +function TControlerTrackerListOnline.TrackerURL(index: integer): string; |
| 145 | +begin |
| 146 | + Result := FStringGridTorrentURL.Cells[2, index + FStringGridTorrentURL.FixedRows]; |
| 147 | +end; |
| 148 | + |
| 149 | +function TControlerTrackerListOnline.TrackerStatus(index: integer): |
| 150 | +TTrackerListOnlineStatus; |
| 151 | +begin |
| 152 | + Result := FTrackerListOnline.TrackerStatus(TrackerURL(index)); |
| 153 | +end; |
| 154 | + |
| 155 | +function TControlerTrackerListOnline.Count: integer; |
| 156 | +begin |
| 157 | + Result := FStringGridTorrentURL.RowCount - FStringGridTorrentURL.FixedRows; |
| 158 | +end; |
| 159 | + |
| 160 | +function TControlerTrackerListOnline.StableTrackers: TStringList; |
| 161 | +begin |
| 162 | + Result := FNewTrackon.TrackerList_Stable; |
| 163 | +end; |
| 164 | + |
| 165 | +procedure TControlerTrackerListOnline.UpdateView; |
| 166 | +var |
| 167 | + tracker: string; |
| 168 | + DeafultChecked: boolean; |
| 169 | +begin |
| 170 | + //Clear all the previeus data in the view |
| 171 | + FStringGridTorrentURL.RowCount := FStringGridTorrentURL.FixedRows; |
| 172 | + |
| 173 | + //the default status is is put all the checkox to true. => keep all trackers |
| 174 | + DeafultChecked := True; |
| 175 | + |
| 176 | + FStringGridTorrentURL.BeginUpdate; |
| 177 | + |
| 178 | + //Show the TrackerList list in string grid view |
| 179 | + for tracker in FTrackerList do |
| 180 | + begin |
| 181 | + AppendRow(DeafultChecked, FTrackerListOnline.TrackerStatus(tracker), tracker); |
| 182 | + end; |
| 183 | + |
| 184 | + //make sure all text are fit inside the columns |
| 185 | + FStringGridTorrentURL.AutoSizeColumns; |
| 186 | + |
| 187 | + FStringGridTorrentURL.EndUpdate; |
| 188 | +end; |
| 189 | + |
| 190 | +end. |
0 commit comments