-
Notifications
You must be signed in to change notification settings - Fork 5
/
accountlist.pas
156 lines (124 loc) · 4.36 KB
/
accountlist.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
unit accountlist;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, libraryParser, bbutilsbeta;
type
{ TAccountList }
TAccountEvent = procedure (acc: TCustomAccountAccess) of object;
TAccountEnumerator = specialize TCommonEnumerator<TCustomAccountAccess>;
TAccountList = class(TStringList)
private
fileName: string;
libs: TLibraryManager;
function get(i: integer): TCustomAccountAccess; reintroduce;
public
OnAccountAdd: TAccountEvent;
constructor create(listFile: string; libraries: TLibraryManager);
procedure load;
procedure save;
property Accounts[i: integer]: TCustomAccountAccess read get; default;
destructor Destroy; override;
function add(const libID: string; prettyName, aname, pass: string; extendType: TExtendType; extendDays:integer; history: boolean; atype: integer):TCustomAccountAccess;
procedure add(account: TCustomAccountAccess);
function GetEnumerator: TAccountEnumerator;
end;
implementation
uses bbutils, bbdebugtools, applicationconfig;
{ TAccountList }
procedure stringsSaveSafeCallback(stream: TStream; data: pointer);
begin
stream.Size := 0;
tstrings(data).SaveToStream(stream);
end;
procedure stringsSaveSafe(sl: TStrings; fn: string);
begin
fileSaveSafe(fn, @stringsSaveSafeCallback, sl );
end;
function TAccountList.get(i: integer): TCustomAccountAccess;
begin
result := TCustomAccountAccess(Objects[i]);
end;
constructor TAccountList.create(listFile: string; libraries: TLibraryManager);
begin
inherited create;
fileName := listFile;
libs := libraries;
end;
procedure TAccountList.load;
var
i: Integer;
deprecatedAccounts: Boolean;
begin
if not FileExists(fileName) then begin
if logging then log('Creating file: '+filename+ ' (written lines: '+inttostr(count)+')');
stringsSaveSafe(self, fileName);
end;
LoadFromFile(fileName);
deprecatedAccounts := false;
for i:=0 to count-1 do begin
Objects[i]:=libs.getAccount(Strings[i]);
if (Accounts[i].getPlusEncodedID() <> Strings[i])
and (Accounts[i].getLibrary().deprecatedId <> '')
and strBeginsWith(Strings[i], Accounts[i].getLibrary().deprecatedId) then begin
if logging then log('Renamed account: '+Strings[i]+' => '+Accounts[i].getPlusEncodedID());
deprecatedAccounts := true;
Strings[i] := Accounts[i].getPlusEncodedID();
end;
end;
if deprecatedAccounts then save;
end;
procedure TAccountList.save;
begin
if logging then log('Save account list');
stringsSaveSafe(self, fileName);
end;
destructor TAccountList.Destroy;
var
i: Integer;
begin
for i:=0 to count-1 do
try
Objects[i].free;
except
;
end;
inherited Destroy;
end;
function TAccountList.add(const libID: string; prettyName, aname, pass: string; extendType: TExtendType; extendDays: integer;
history: boolean; atype: integer): TCustomAccountAccess;
begin
{if accountList.Selected <> nil then
if (accountList.Selected.Caption=edtAccountPrettyName.Text) or
(accountList.Selected.SubItems[0]=edtAccountUser.Text) then begin
ShowMessage('Das Konto existiert bereits auf diesem Computer und kann deshalb nicht erstellt werden. '#13#10+
'Falls Sie eine Eigenschaft von einem Konto ändern wollen, klicken Sie bitte auf den Button "Konto ändern"'#13#10+
'Falls Sie das Konto neu erstellen wollen, löschen Sie bitte das zuerst das alte, und erstellen es dann neu');
exit;
end; }
result:=libraryManager.getAccount(libID,aname);
result.prettyName:=prettyName;
result.password:=pass;
result.keepHistory:=history;//ckbAccountHistory.Checked;
result.extendType:=extendType;// TExtendType( cmbAccountExtend.ItemIndex));
result.extendDays:=extendDays;// StrToInt(edtAccountExtendDays.Text));
result.accountType := atype;
add(result);
{ if MessageDlg('Daten laden?',
'Das Konto '+lib.getPrettyName()+' wurde erstellt.'#13#10'Sollen jetzt die Mediendaten heruntergeladen werden?',
mtConfirmation ,[mbYes,mbNo],0)=mrYes then
mainForm.updateLibrary(lib,false,false);}
end;
procedure TAccountList.add(account: TCustomAccountAccess);
begin
AddObject(account.getPlusEncodedID(),account);
account.saveConfig();
account.saveBooks();
save;
if assigned(OnAccountAdd) then OnAccountAdd(account);
end;
function TAccountList.GetEnumerator: TAccountEnumerator;
begin
result := TAccountEnumerator.create(Count, @get);
end;
end.