forked from transmission-remote-gui/transgui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
connoptions.pas
510 lines (462 loc) · 14.3 KB
/
connoptions.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
{*************************************************************************************
This file is part of Transmission Remote GUI.
Copyright (c) 2008-2010 by Yury Sidorov.
Transmission Remote GUI is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Transmission Remote GUI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Transmission Remote GUI; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*************************************************************************************}
unit ConnOptions;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, Spin, ComCtrls, Buttons, ButtonPanel, ExtCtrls;
const
DefSpeeds = '0,10,25,50,100,250,500,750,1000,2500,5000,7000';
resourcestring
sNoHost = 'No host name specified.';
sNoProxy = 'No proxy server specified.';
SDelConnection = 'Are you sure to delete connection ''%s''?';
type
{ TConnOptionsForm }
TConnOptionsForm = class(TForm)
btNew: TButton;
btDel: TButton;
btRename: TButton;
Buttons: TButtonPanel;
cbUseProxy: TCheckBox;
edUpSpeeds: TEdit;
edHost: TEdit;
cbSSL: TCheckBox;
cbConnection: TComboBox;
edDownSpeeds: TEdit;
edProxy: TEdit;
edProxyPassword: TEdit;
edProxyPort: TSpinEdit;
edProxyUserName: TEdit;
edUserName: TEdit;
edPassword: TEdit;
edPaths: TMemo;
gbSpeed: TGroupBox;
Label1: TLabel;
txDownSpeeds: TLabel;
panTop: TPanel;
tabProxy: TTabSheet;
tabMisc: TTabSheet;
txUpSpeeds: TLabel;
txPaths: TLabel;
tabPaths: TTabSheet;
Page: TPageControl;
tabConnection: TTabSheet;
txProxy: TLabel;
txProxyPassword: TLabel;
txProxyPort: TLabel;
txProxyUserName: TLabel;
txUserName: TLabel;
txPort: TLabel;
edPort: TSpinEdit;
txHost: TLabel;
txPassword: TLabel;
procedure btDelClick(Sender: TObject);
procedure btNewClick(Sender: TObject);
procedure btOKClick(Sender: TObject);
procedure btRenameClick(Sender: TObject);
procedure cbConnectionSelect(Sender: TObject);
procedure cbUseProxyClick(Sender: TObject);
procedure edHostChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure tabPathsShow(Sender: TObject);
private
FCurConn: string;
FCurHost: string;
edConnection: TEdit;
function Validate: boolean;
procedure BeginEdit;
procedure EndEdit;
procedure SaveConnectionsList;
public
ActiveConnection: string;
ActiveSettingChanged: boolean;
procedure LoadConnSettings(const ConnName: string);
procedure SaveConnSettings(const ConnName: string);
function IsConnSettingsChanged(const ConnName: string): boolean;
end;
implementation
uses Main, synacode, utils;
{ TConnOptionsForm }
procedure TConnOptionsForm.btOKClick(Sender: TObject);
begin
if not Validate then
exit;
EndEdit;
SaveConnSettings(FCurConn);
SaveConnectionsList;
ModalResult:=mrOk;
end;
procedure TConnOptionsForm.btRenameClick(Sender: TObject);
begin
if edConnection.Visible then begin
if Trim(edConnection.Text) = '' then exit;
EndEdit;
exit;
end;
if cbConnection.Text = '' then exit;
BeginEdit;
ActiveControl:=edConnection;
edConnection.SelectAll;
end;
procedure TConnOptionsForm.cbConnectionSelect(Sender: TObject);
var
i: integer;
s: string;
begin
i:=cbConnection.ItemIndex;
if i >= 0 then
s:=cbConnection.Items[i]
else
s:='';
if (FCurConn <> s) and (FCurConn <> '') then begin
if not Validate then begin
cbConnection.ItemIndex:=cbConnection.Items.IndexOf(FCurConn);
exit;
end;
SaveConnSettings(FCurConn);
end;
if s <> '' then
LoadConnSettings(s);
end;
procedure TConnOptionsForm.btNewClick(Sender: TObject);
begin
EndEdit;
if (FCurConn <> '') and not Validate then
exit;
SaveConnSettings(FCurConn);
LoadConnSettings('');
BeginEdit;
edConnection.Text:='';
Page.ActivePage:=tabConnection;
ActiveControl:=edHost;
end;
procedure TConnOptionsForm.btDelClick(Sender: TObject);
var
i: integer;
begin
if edConnection.Visible or (cbConnection.Text = '') then
exit;
if MessageDlg('', Format(SDelConnection, [cbConnection.Text]), mtConfirmation, mbYesNo, 0, mbNo) <> mrYes then exit;
if FCurConn <> '' then begin
MainForm.Ini.EraseSection('Connection.' + FCurConn);
MainForm.Ini.EraseSection('Connection');
MainForm.Ini.EraseSection('AddTorrent.' + FCurConn);
i:=cbConnection.ItemIndex;
if i >= 0 then begin
cbConnection.Items.Delete(i);
if i >= cbConnection.Items.Count then begin
i:=cbConnection.Items.Count - 1;
if i < 0 then
i:=0;
end;
end
else
i:=0;
if i < cbConnection.Items.Count then
cbConnection.ItemIndex:=i
else
cbConnection.ItemIndex:=-1;
end
else
cbConnection.ItemIndex:=-1;
if cbConnection.ItemIndex >= 0 then begin
if FCurConn = ActiveConnection then
ActiveConnection:='';
LoadConnSettings(cbConnection.Items[cbConnection.ItemIndex]);
if ActiveConnection = '' then
ActiveConnection:=FCurConn;
end
else begin
FCurConn:='';
btNewClick(nil);
end;
SaveConnectionsList;
end;
procedure TConnOptionsForm.cbUseProxyClick(Sender: TObject);
var
c: TColor;
begin
edProxy.Enabled:=cbUseProxy.Checked;
edProxyPort.Enabled:=cbUseProxy.Checked;
edProxyUserName.Enabled:=cbUseProxy.Checked;
edProxyPassword.Enabled:=cbUseProxy.Checked;
txProxy.Enabled:=cbUseProxy.Checked;
txProxyPort.Enabled:=cbUseProxy.Checked;
txProxyUserName.Enabled:=cbUseProxy.Checked;
txProxyPassword.Enabled:=cbUseProxy.Checked;
if cbUseProxy.Checked then
c:=clWindow
else
c:=edProxy.Parent.Color;
edProxy.Color:=c;
edProxyPort.Color:=c;
edProxyUserName.Color:=c;
edProxyPassword.Color:=c;
end;
procedure TConnOptionsForm.edHostChange(Sender: TObject);
begin
if edConnection.Visible and (edConnection.Text = FCurHost) then
edConnection.Text:=edHost.Text;
FCurHost:=edHost.Text;
end;
procedure TConnOptionsForm.FormCreate(Sender: TObject);
var
i, cnt: integer;
s: string;
begin
Font.Size:=MainForm.Font.Size;
Page.ActivePageIndex:=0;
ActiveControl:=edHost;
AutoSizeForm(Self);
Buttons.OKButton.ModalResult:=mrNone;
Buttons.OKButton.OnClick:=@btOKClick;
edConnection:=TEdit.Create(cbConnection.Parent);
edConnection.Visible:=False;
edConnection.BoundsRect:=cbConnection.BoundsRect;
edConnection.Parent:=cbConnection.Parent;
cnt:=MainForm.Ini.ReadInteger('Hosts', 'Count', 0);
for i:=1 to cnt do begin
s:=MainForm.Ini.ReadString('Hosts', Format('Host%d', [i]), '');
if s <> '' then
cbConnection.Items.Add(s);
end;
end;
procedure TConnOptionsForm.FormShow(Sender: TObject);
begin
if edConnection.Visible then
exit;
if cbConnection.Items.Count = 0 then begin
btNewClick(nil);
exit;
end;
cbConnection.ItemIndex:=cbConnection.Items.IndexOf(ActiveConnection);
if cbConnection.ItemIndex < 0 then
cbConnection.ItemIndex:=0;
LoadConnSettings(cbConnection.Text);
end;
procedure TConnOptionsForm.tabPathsShow(Sender: TObject);
var
R: TRect;
begin
R:=edPaths.BoundsRect;
R.Top:=txPaths.BoundsRect.Bottom + 8;
edPaths.BoundsRect:=R;
end;
function TConnOptionsForm.Validate: boolean;
begin
Result:=False;
edHost.Text:=Trim(edHost.Text);
if Trim(edHost.Text) = '' then begin
Page.ActivePage:=tabConnection;
edHost.SetFocus;
MessageDlg(sNoHost, mtError, [mbOK], 0);
exit;
end;
edProxy.Text:=Trim(edProxy.Text);
if cbUseProxy.Checked and (edProxy.Text = '') then begin
Page.ActivePage:=tabProxy;
edProxy.SetFocus;
MessageDlg(sNoProxy, mtError, [mbOK], 0);
exit;
end;
Result:=True;
end;
procedure TConnOptionsForm.EndEdit;
procedure RenameSection(const OldName, NewName: string);
var
i: integer;
sl: TStringList;
begin
sl:=TStringList.Create;
with MainForm.Ini do
try
ReadSectionValues(OldName, sl);
for i:=0 to sl.Count - 1 do
WriteString(NewName, sl.Names[i], sl.ValueFromIndex[i]);
EraseSection(OldName);
finally
sl.Free;
end;
end;
var
NewName, s: string;
i, p: integer;
begin
if not edConnection.Visible then exit;
NewName:=Trim(edConnection.Text);
if NewName = '' then
NewName:=Trim(edHost.Text);
if NewName <> FCurConn then begin
if FCurConn <> '' then begin
p:=cbConnection.Items.IndexOf(FCurConn);
if p >= 0 then
cbConnection.Items.Delete(p);
end
else
p:=-1;
i:=1;
s:=NewName;
while cbConnection.Items.IndexOf(NewName) >= 0 do begin
Inc(i);
NewName:=Format('%s (%d)', [s, i]);
end;
if FCurConn <> '' then begin
RenameSection('Connection.' + FCurConn, 'Connection.' + NewName);
RenameSection('AddTorrent.' + FCurConn, 'AddTorrent.' + NewName);
end;
if p >= 0 then
cbConnection.Items.Insert(p, NewName)
else
cbConnection.Items.Add(NewName);
if (FCurConn = ActiveConnection) or (FCurConn = '') then
ActiveConnection:=NewName;
FCurConn:=NewName;
SaveConnectionsList;
end;
cbConnection.ItemIndex:=cbConnection.Items.IndexOf(NewName);
cbConnection.Visible:=True;
edConnection.Visible:=False;
end;
procedure TConnOptionsForm.SaveConnectionsList;
var
i: integer;
begin
with MainForm.Ini do begin
WriteString('Hosts', 'CurHost', ActiveConnection);
WriteInteger('Hosts', 'Count', cbConnection.Items.Count);
for i:=0 to cbConnection.Items.Count - 1 do
WriteString('Hosts', Format('Host%d', [i + 1]), cbConnection.Items[i]);
end;
end;
procedure TConnOptionsForm.BeginEdit;
var
i: integer;
begin
i:=cbConnection.ItemIndex;
if i >= 0 then
edConnection.Text:=cbConnection.Items[i]
else
edConnection.Text:='';
edConnection.Visible:=True;
cbConnection.Visible:=False;
end;
procedure TConnOptionsForm.LoadConnSettings(const ConnName: string);
var
Sec: string;
begin
with MainForm.Ini do begin
Sec:='Connection.' + ConnName;
if (ConnName <> '') and not SectionExists(Sec) then
Sec:='Connection';
edHost.Text:=ReadString(Sec, 'Host', '');
FCurHost:=edHost.Text;
edPort.Value:=ReadInteger(Sec, 'Port', 9091);
cbSSL.Checked:=ReadBool(Sec, 'UseSSL', False);
edUserName.Text:=ReadString(Sec, 'UserName', '');
if ReadString(Sec, 'Password', '') <> '' then
edPassword.Text:='******'
else
edPassword.Text:='';
cbUseProxy.Checked:=ReadBool(Sec, 'UseProxy', False);
edProxy.Text:=ReadString(Sec, 'ProxyHost', '');
edProxyPort.Value:=ReadInteger(Sec, 'ProxyPort', 8080);
edProxyUserName.Text:=ReadString(Sec, 'ProxyUser', '');
if ReadString(Sec, 'ProxyPass', '') <> '' then
edProxyPassword.Text:='******'
else
edProxyPassword.Text:='';
edPaths.Text:=StringReplace(ReadString(Sec, 'PathMap', ''), '|', LineEnding, [rfReplaceAll]);
edDownSpeeds.Text:=ReadString(Sec, 'DownSpeeds', DefSpeeds);
edUpSpeeds.Text:=ReadString(Sec, 'UpSpeeds', DefSpeeds);
cbUseProxyClick(nil);
end;
FCurConn:=ConnName;
FCurHost:=edHost.Text;
end;
procedure TConnOptionsForm.SaveConnSettings(const ConnName: string);
var
Sec: string;
i: integer;
s: string;
begin
if ConnName = '' then
exit;
if ConnName = ActiveConnection then
if IsConnSettingsChanged(ConnName) then
ActiveSettingChanged:=True;
with MainForm.Ini do begin
Sec:='Connection.' + ConnName;
WriteString(Sec, 'Host', Trim(edHost.Text));
WriteBool(Sec, 'UseSSL', cbSSL.Checked);
WriteInteger(Sec, 'Port', edPort.Value);
WriteString(Sec, 'UserName', edUserName.Text);
if edPassword.Text <> '******' then begin
if edPassword.Text = '' then
s:=''
else
s:=EncodeBase64(edPassword.Text);
WriteString(Sec, 'Password', s);
end;
WriteBool(Sec, 'UseProxy', cbUseProxy.Checked);
WriteString(Sec, 'ProxyHost', Trim(edProxy.Text));
WriteInteger(Sec, 'ProxyPort', edProxyPort.Value);
WriteString(Sec, 'ProxyUser', edProxyUserName.Text);
if edProxyPassword.Text <> '******' then begin
if edProxyPassword.Text = '' then
s:=''
else
s:=EncodeBase64(edProxyPassword.Text);
WriteString(Sec, 'ProxyPass', s);
end;
WriteString(Sec, 'PathMap', StringReplace(edPaths.Text, LineEnding, '|', [rfReplaceAll]));
WriteString(Sec, 'DownSpeeds', Trim(edDownSpeeds.Text));
WriteString(Sec, 'UpSpeeds', Trim(edUpSpeeds.Text));
i:=cbConnection.Items.IndexOf(ConnName);
if i < 0 then
cbConnection.Items.Insert(0, ConnName);
end;
end;
function TConnOptionsForm.IsConnSettingsChanged(const ConnName: string): boolean;
var
Sec: string;
begin
with MainForm.Ini do begin
Sec:='Connection.' + ConnName;
if not SectionExists(Sec) then
Sec:='Connection';
Result:=(edPort.Value <> ReadInteger(Sec, 'Port', 9091)) or
(edHost.Text <> ReadString(Sec, 'Host', '')) or
(cbSSL.Checked <> ReadBool(Sec, 'UseSSL', False)) or
(edUserName.Text <> ReadString(Sec, 'UserName', '')) or
((ReadString(Sec, 'Password', '') = '') and (edPassword.Text <> '')) or
((ReadString(Sec, 'Password', '') <> '') and (edPassword.Text <> '******')) or
(cbUseProxy.Checked <> ReadBool(Sec, 'UseProxy', False)) or
(edProxy.Text <> ReadString(Sec, 'ProxyHost', '')) or
(edProxyPort.Value <> ReadInteger(Sec, 'ProxyPort', 8080)) or
(edProxyUserName.Text <> ReadString(Sec, 'ProxyUser', '')) or
((ReadString(Sec, 'ProxyPass', '') = '') and (edProxyPassword.Text <> '')) or
((ReadString(Sec, 'ProxyPass', '') <> '') and (edProxyPassword.Text <> '******')) or
(edPaths.Text <> StringReplace(ReadString(Sec, 'PathMap', ''), '|', LineEnding, [rfReplaceAll])) or
(edDownSpeeds.Text <> ReadString(Sec, 'DownSpeeds', '')) or
(edUpSpeeds.Text <> ReadString(Sec, 'UpSpeeds', ''))
;
end;
end;
initialization
{$I connoptions.lrs}
end.