Skip to content

Commit 4856754

Browse files
ADD: Edit comment in data/info grid column.
FIX: The data/info column can be moved but it is not updated corectly when torrent is reloaded.
1 parent 40fcf21 commit 4856754

File tree

6 files changed

+454
-152
lines changed

6 files changed

+454
-152
lines changed

controlergridtorrentdata.pas

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
unit controlergridtorrentdata;
2+
3+
{
4+
The view string grid shows all the information of the torrent.
5+
The grid column position order can be rearange by the user.
6+
The updating and reading of the column position must be 'dynamic'.
7+
Must keep track of the position of the column even when the user rearange it.
8+
9+
There are 10 column that must be 'track'
10+
}
11+
{$mode objfpc}{$H+}
12+
13+
interface
14+
15+
uses
16+
Classes, SysUtils, Grids;
17+
18+
type
19+
20+
{ TControlerGridTorrentData }
21+
22+
TControlerGridTorrentData = class
23+
private
24+
//The view grid that must be controled.
25+
FStringGridTorrentData: TStringGrid;
26+
27+
//The collumn must be in this design order.
28+
FTorrentFile, //0
29+
FInfoFileName, //1
30+
FInfoHash, //2
31+
FCreatedOn, //3
32+
FCreatedBy, //4
33+
FComment, //5
34+
FPrivateTorrent, //6
35+
FPieceLength, //7
36+
FTotaSize, //8
37+
FIndexOrder //9
38+
: TGridColumn;
39+
40+
FRowIsMovedNeedUpdate: boolean;
41+
procedure StringGridTorrentDataColRowMoved(Sender: TObject;
42+
IsColumn: boolean; sIndex, tIndex: integer);
43+
procedure AddColumn(var GridColumn: TGridColumn; index: integer);
44+
procedure UpdateColumnTag;
45+
procedure ClearAllImageIndex;
46+
procedure WriteCell(GridColumn: TGridColumn; const Str: UTF8String);
47+
public
48+
//All the string that can be written to grid.
49+
TorrentFile, //0
50+
InfoFileName, //1
51+
InfoHash, //2
52+
CreatedOn, //3
53+
CreatedBy, //4
54+
Comment, //5
55+
PrivateTorrent, //6
56+
PieceLength, //7
57+
TotaSize, //8
58+
IndexOrder //9
59+
: UTF8String;
60+
61+
procedure AppendRow;
62+
procedure ReorderGrid;
63+
function ReadComment(Rowindex: integer): UTF8String;
64+
constructor Create(StringGridTorrentData: TStringGrid);
65+
destructor Destroy; override;
66+
end;
67+
68+
implementation
69+
70+
{ TControlerGridTorrentData }
71+
const
72+
COLUMN_COUNT = 10;
73+
74+
procedure TControlerGridTorrentData.StringGridTorrentDataColRowMoved(Sender: TObject;
75+
IsColumn: boolean; sIndex, tIndex: integer);
76+
begin
77+
//This is called before the column is moved 'rearrange' by the user.
78+
FRowIsMovedNeedUpdate := True;
79+
end;
80+
81+
procedure TControlerGridTorrentData.AddColumn(var GridColumn: TGridColumn;
82+
index: integer);
83+
begin
84+
GridColumn := FStringGridTorrentData.Columns[index];
85+
end;
86+
87+
procedure TControlerGridTorrentData.UpdateColumnTag;
88+
var
89+
i: integer;
90+
begin
91+
//fill the 'tag' value as the position of the coulumn.
92+
//this methode must be only called when the user change the column order.
93+
for i := 0 to FStringGridTorrentData.ColCount - 1 do
94+
begin
95+
FStringGridTorrentData.Columns[i].Tag :=
96+
FStringGridTorrentData.Columns.IndexOf(FStringGridTorrentData.Columns[i]);
97+
end;
98+
99+
//The tag is now in sync column index. It is process.
100+
FRowIsMovedNeedUpdate := False;
101+
end;
102+
103+
procedure TControlerGridTorrentData.ClearAllImageIndex;
104+
var
105+
i: integer;
106+
begin
107+
//The sort icon must be removed from the title bar
108+
for i := 0 to FStringGridTorrentData.ColCount - 1 do
109+
begin
110+
FStringGridTorrentData.Columns[i].Title.ImageIndex := -1;
111+
end;
112+
end;
113+
114+
procedure TControlerGridTorrentData.WriteCell(GridColumn: TGridColumn;
115+
const Str: UTF8String);
116+
begin
117+
FStringGridTorrentData.Cells[GridColumn.Tag,
118+
FStringGridTorrentData.RowCount - 1] := Str;
119+
end;
120+
121+
procedure TControlerGridTorrentData.AppendRow;
122+
begin
123+
//Add a new empty row, copy all the stings to this empty row.
124+
125+
//Update Column.tag if row have change.
126+
if FRowIsMovedNeedUpdate then
127+
UpdateColumnTag;
128+
129+
//Create a empty row to at the bottom.
130+
FStringGridTorrentData.InsertColRow(False, FStringGridTorrentData.RowCount);
131+
132+
//write all the string to the cell.
133+
WriteCell(FTorrentFile, TorrentFile);
134+
WriteCell(FInfoFileName, InfoFileName);
135+
WriteCell(FInfoHash, InfoHash);
136+
WriteCell(FCreatedOn, CreatedOn);
137+
WriteCell(FCreatedBy, CreatedBy);
138+
WriteCell(FComment, Comment);
139+
WriteCell(FPrivateTorrent, PrivateTorrent);
140+
WriteCell(FPieceLength, PieceLength);
141+
WriteCell(FTotaSize, TotaSize);
142+
WriteCell(FIndexOrder, IndexOrder);
143+
144+
end;
145+
146+
procedure TControlerGridTorrentData.ReorderGrid;
147+
begin
148+
//Undo all posible sort column used by the user. Sort it back to 'begin state'
149+
//FIndexOrder is a non visible row use for this purpose. TGridColumnTitle
150+
FStringGridTorrentData.SortOrder := soAscending;
151+
FStringGridTorrentData.SortColRow(True,
152+
FStringGridTorrentData.Columns.IndexOf(FIndexOrder));
153+
154+
//The order are no longer the same. Hide the ascending/decending icon.
155+
ClearAllImageIndex;
156+
end;
157+
158+
function TControlerGridTorrentData.ReadComment(Rowindex: integer): UTF8String;
159+
begin
160+
//Update Column.tag if row have change.
161+
if FRowIsMovedNeedUpdate then
162+
UpdateColumnTag;
163+
164+
//Read the 'comment' grid cell.
165+
Result := FStringGridTorrentData.Cells[FComment.Tag, Rowindex];
166+
end;
167+
168+
constructor TControlerGridTorrentData.Create(StringGridTorrentData: TStringGrid);
169+
begin
170+
inherited Create;
171+
FStringGridTorrentData := StringGridTorrentData;
172+
173+
//When user move the row, call StringGridTorrentDataColRowMoved.
174+
FStringGridTorrentData.OnColRowMoved := @StringGridTorrentDataColRowMoved;
175+
176+
//The view and the controler part must have the same column count.
177+
Assert(FStringGridTorrentData.ColCount <> COLUMN_COUNT, 'Wrong column count');
178+
179+
//Track the column
180+
AddColumn(FTorrentFile, 0);
181+
AddColumn(FInfoFileName, 1);
182+
AddColumn(FInfoHash, 2);
183+
AddColumn(FCreatedOn, 3);
184+
AddColumn(FCreatedBy, 4);
185+
AddColumn(FComment, 5);
186+
AddColumn(FPrivateTorrent, 6);
187+
AddColumn(FPieceLength, 7);
188+
AddColumn(FTotaSize, 8);
189+
AddColumn(FIndexOrder, 9);
190+
191+
//Fillin the tag value
192+
UpdateColumnTag;
193+
end;
194+
195+
destructor TControlerGridTorrentData.Destroy;
196+
begin
197+
inherited Destroy;
198+
end;
199+
200+
end.

decodetorrent.pas

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ TDecodeTorrent = class
4848

4949
//Torrent file must have 'info' item.
5050
FBEncoded_Info:TBEncoded;
51+
FBEncoded_Comment: TBEncoded;
5152

5253
FInfoHash: utf8string;
5354
FCreatedBy: utf8string;
@@ -68,6 +69,7 @@ TDecodeTorrent = class
6869
function GetName: utf8string;
6970
function GetPieceLenght: int64;
7071
function GetPrivateTorrent: boolean;
72+
procedure SetComment(const AValue: utf8string);
7173
public
7274
//All the trackers inside this torrent file
7375
TrackerList: TStringList;
@@ -88,7 +90,7 @@ TDecodeTorrent = class
8890
property CreatedDate: TDateTime read FCreatedDate;
8991

9092
//Comment
91-
property Comment: utf8string read FComment;
93+
property Comment: utf8string read FComment write SetComment;
9294

9395
//info.name
9496
property Name: utf8string read FName;
@@ -413,6 +415,56 @@ function TDecodeTorrent.GetPrivateTorrent: boolean;
413415
end;
414416
end;
415417

418+
procedure TDecodeTorrent.SetComment(const AValue: utf8string);
419+
var
420+
// Encoded: TBEncoded;
421+
Data: TBEncodedData;
422+
begin
423+
if FComment=AValue then Exit;
424+
FComment:=AValue;
425+
try
426+
//if empty comment then remove the element.
427+
if FComment = '' then
428+
begin
429+
FBEncoded.ListData.RemoveElement('comment');
430+
exit;
431+
end;
432+
433+
//if there is no comment element, then make new one
434+
if not assigned(FBEncoded_Comment) then
435+
begin
436+
FBEncoded_Comment := TBEncoded.Create;
437+
FBEncoded_Comment.Format := befString;
438+
FBEncoded_Comment.StringData := FComment;
439+
Data := TBEncodedData.Create(FBEncoded_Comment);
440+
Data.Header := 'comment';
441+
FBEncoded.ListData.Add(Data);
442+
end
443+
else
444+
begin
445+
FBEncoded_Comment.StringData := FComment;
446+
end;
447+
448+
except
449+
end;
450+
451+
end;
452+
453+
454+
{
455+
try
456+
Encoded := TBEncoded.Create;
457+
Encoded.Format := befString;
458+
Encoded.StringData := TrackerURL;
459+
Data := TBEncodedData.Create(Encoded);
460+
Data.Header := 'announce';
461+
FBEncoded.ListData.Add(Data);
462+
FBEncoded.ListData.Sort(@sort_);//text must be in alfabetical order.
463+
except
464+
end;
465+
466+
}
467+
416468
procedure TDecodeTorrent.RemovePrivateTorrentFlag;
417469
begin
418470
try
@@ -588,14 +640,12 @@ function TDecodeTorrent.GetCreatedDate: TDateTime;
588640
end;
589641

590642
function TDecodeTorrent.GetComment: utf8string;
591-
var
592-
TempBEncoded: TBEncoded;
593643
begin
594644
Result := '';
595645
try
596-
TempBEncoded := FBEncoded.ListData.FindElement('comment');
597-
if assigned(TempBEncoded) then
598-
Result := UTF8Trim( TempBEncoded.StringData);
646+
FBEncoded_Comment := FBEncoded.ListData.FindElement('comment');
647+
if assigned(FBEncoded_Comment) then
648+
Result := UTF8Trim( FBEncoded_Comment.StringData);
599649
except
600650
end;
601651
end;

main.lfm

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,52 +139,66 @@ object FormTrackerModify: TFormTrackerModify
139139
Top = 0
140140
Width = 1171
141141
Align = alClient
142-
ColCount = 9
142+
ColCount = 10
143143
ColumnClickSorts = True
144144
Columns = <
145145
item
146+
ReadOnly = True
146147
Title.Caption = 'Torrent File'
147148
Width = 250
148149
end
149150
item
151+
ReadOnly = True
150152
Title.Caption = 'Info Filename'
151153
Width = 250
152154
end
153155
item
154156
MaxSize = 250
157+
ReadOnly = True
155158
Title.Caption = 'Info Hash'
156159
Width = 280
157160
end
158161
item
162+
ReadOnly = True
159163
Title.Caption = 'Created On'
160164
Width = 145
161165
end
162166
item
167+
ReadOnly = True
163168
Title.Caption = 'Created By'
164169
Width = 145
165170
end
166171
item
167-
Title.Caption = 'Comment'
168-
Width = 145
172+
Title.Caption = 'Comment (can be edited)'
173+
Width = 160
169174
end
170175
item
176+
ReadOnly = True
171177
Title.Caption = 'Private'
172178
Width = 50
173179
end
174180
item
175181
Alignment = taRightJustify
182+
ReadOnly = True
176183
Title.Alignment = taRightJustify
177184
Title.Caption = 'Piece Length (KiB)'
178185
Width = 100
179186
end
180187
item
181188
Alignment = taRightJustify
189+
ReadOnly = True
182190
Title.Alignment = taRightJustify
183191
Title.Caption = 'Total size (KiB)'
184192
Width = 100
193+
end
194+
item
195+
ReadOnly = True
196+
Title.Alignment = taRightJustify
197+
Title.Caption = 'IndexOrder (internal used)'
198+
Visible = False
185199
end>
186200
FixedCols = 0
187-
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goDrawFocusSelected, goColSizing, goColMoving, goSmoothScroll]
201+
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goDrawFocusSelected, goColSizing, goColMoving, goEditing, goSmoothScroll]
188202
RowCount = 1
189203
TabOrder = 0
190204
TitleFont.Height = -11
@@ -194,10 +208,11 @@ object FormTrackerModify: TFormTrackerModify
194208
280
195209
145
196210
145
197-
145
211+
160
198212
50
199213
100
200214
100
215+
0
201216
)
202217
end
203218
end

0 commit comments

Comments
 (0)