Changeset 12 for trunk/UCore.pas


Ignore:
Timestamp:
Mar 15, 2013, 11:42:38 PM (11 years ago)
Author:
chronos
Message:
  • Přidáno: Podpora pro více účtů.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UCore.pas

    r11 r12  
    88  Classes, SysUtils, FileUtil, ExtCtrls, Controls, ActnList, Menus, UFioAPI,
    99  URegistry, UApplicationInfo, UCoolTranslator, Registry, DateUtils, Forms,
    10   Dialogs;
     10  Dialogs, SpecializedList, dom, XMLRead, XMLWrite;
    1111
    1212type
     13
     14  { TAccount }
     15
     16  TAccount = class
     17    Token: string;
     18    Name: string;
     19    Number: string;
     20    Balance: Double;
     21    Time: TDateTime;
     22    procedure Assign(Source: TAccount);
     23    procedure LoadFromRegistry(Context: TRegistryContext);
     24    procedure SaveToRegistry(Context: TRegistryContext);
     25  end;
     26
     27  { TAccountList }
     28
     29  TAccountList = class(TListObject)
     30    procedure LoadFromRegistry(Context: TRegistryContext);
     31    procedure SaveToRegistry(Context: TRegistryContext);
     32    procedure LoadToStrings(Strings: TStrings);
     33    procedure Assign(Source: TAccountList);
     34  end;
    1335
    1436  { TCore }
     
    1638  TCore = class(TDataModule)
    1739    AAbout: TAction;
     40    AAccounts: TAction;
    1841    ActionList1: TActionList;
    1942    ADownloadInterval: TAction;
     
    3659    PopupMenuTray: TPopupMenu;
    3760    TrayIcon1: TTrayIcon;
     61    procedure AAccountsExecute(Sender: TObject);
    3862    procedure ADownloadIntervalExecute(Sender: TObject);
    3963    procedure ADownloadMonthlyExecute(Sender: TObject);
     
    4973  public
    5074    RegistryContext: TRegistryContext;
    51     Token: string;
     75    Accounts: TAccountList;
     76    CurrentAccount: TAccount;
    5277    DataFormat: TFioDataFormat;
    5378    TargetDirectory: string;
     
    5681    ReportTimeFrom: TDateTime;
    5782    ReportTimeTo: TDateTime;
     83    procedure LoadAccount(Account: TAccount);
     84    procedure LoadAccounts;
    5885    procedure SaveToRegistry(Context: TRegistryContext);
    5986    procedure LoadFromRegistry(Context: TRegistryContext);
     
    6895
    6996uses
    70   UFormAbout, UFormMain, UFormSettings;
     97  UFormAbout, UFormMain, UFormSettings, UFormAccounts;
    7198
    7299resourcestring
     
    75102  SSavedToFile = 'Dump saved to file %s';
    76103
     104{ TAccount }
     105
     106procedure TAccount.Assign(Source: TAccount);
     107begin
     108  Token := Source.Token;
     109  Name := Source.Name;
     110  Balance := Source.Balance;
     111  Time := Source.Time;
     112  Number := Source.Number;
     113end;
     114
     115procedure TAccount.LoadFromRegistry(Context: TRegistryContext);
     116begin
     117  with TRegistryEx.Create do
     118  try
     119    RootKey := Context.RootKey;
     120    OpenKey(Context.Key, True);
     121    Name := ReadStringWithDefault('Name', '');
     122    Time := ReadDateTimeWithDefault('Time', 0);
     123    Token := ReadStringWithDefault('Token', '');
     124    Balance := ReadFloatWithDefault('Balance', 0);
     125    Number := ReadStringWithDefault('Number', '');
     126  finally
     127    Free;
     128  end;
     129end;
     130
     131procedure TAccount.SaveToRegistry(Context: TRegistryContext);
     132begin
     133  with TRegistryEx.Create do
     134  try
     135    RootKey := Context.RootKey;
     136    OpenKey(Context.Key, True);
     137    WriteString('Number', Number);
     138    WriteString('Name', Name);
     139    WriteDateTime('Time', Time);
     140    WriteString('Token', Token);
     141    WriteFloat('Balance', Balance);
     142  finally
     143    Free;
     144  end;
     145end;
     146
     147{ TAccountList }
     148
     149procedure TAccountList.LoadFromRegistry(Context: TRegistryContext);
     150var
     151  I: Integer;
     152begin
     153  with TRegistryEx.Create do
     154  try
     155    RootKey := Context.RootKey;
     156    OpenKey(Context.Key, True);
     157    Count := ReadIntegerWithDefault('Count', 0);
     158  finally
     159    Free;
     160  end;
     161  for I := 0 to Count - 1 do begin
     162    if not Assigned(Items[I]) then Items[I] := TAccount.Create;
     163    TAccount(Items[I]).LoadFromRegistry(RegContext(Context.RootKey, Context.Key + '\' + IntToStr(I)));
     164  end;
     165end;
     166
     167procedure TAccountList.SaveToRegistry(Context: TRegistryContext);
     168var
     169  I: Integer;
     170begin
     171  with TRegistryEx.Create do
     172  try
     173    RootKey := Context.RootKey;
     174    OpenKey(Context.Key, True);
     175    WriteInteger('Count', Count);
     176  finally
     177    Free;
     178  end;
     179  for I := 0 to Count - 1 do
     180    TAccount(Items[I]).SaveToRegistry(RegContext(Context.RootKey, Context.Key + '\' + IntToStr(I)));
     181end;
     182
     183procedure TAccountList.LoadToStrings(Strings: TStrings);
     184var
     185  I: Integer;
     186begin
     187  while Strings.Count < Count do
     188    Strings.Add('');
     189  while Strings.Count > Count do
     190    Strings.Delete(Strings.Count - 1);
     191  for I := 0 to Count - 1 do
     192    Strings.Strings[I] := TAccount(Items[I]).Name;
     193end;
     194
     195procedure TAccountList.Assign(Source: TAccountList);
     196var
     197  I: Integer;
     198begin
     199  //inherited Assign(Source);
     200  Count := Source.Count;
     201  for I := 0 to Count - 1 do begin
     202    if not Assigned(Items[I]) then Items[I] := TAccount.Create;
     203    TAccount(Items[I]).Assign(TAccount(Source.Items[I]));
     204  end;
     205end;
     206
    77207{ TCore }
    78208
     
    84214procedure TCore.DataModuleCreate(Sender: TObject);
    85215begin
     216  Accounts := TAccountList.Create;
    86217  RegistryContext := RegContext(HKEY(ApplicationInfo1.RegistryRoot),
    87218    ApplicationInfo1.RegistryKey);
    88219  LoadFromRegistry(RegistryContext);
     220  //LoadAccounts;
    89221end;
    90222
     
    92224begin
    93225  SaveToRegistry(RegistryContext);
     226  FreeAndNil(Accounts);
     227end;
     228
     229procedure TCore.LoadAccount(Account: TAccount);
     230var
     231  FioAPI: TFioAPI;
     232  List: TStringList;
     233  FileName: string;
     234  XMLDocument: TXMLDocument;
     235  Mem: TMemoryStream;
     236  RootNode: TDOMNode;
     237begin
     238  FioAPI := TFioAPI.Create;
     239  List := TStringList.Create;
     240  XMLDocument := TXMLDocument.Create;
     241  Mem := TMemoryStream.Create;
     242  try
     243    FioAPI.Format := DataFormat;
     244    FioAPI.Token := Account.Token;
     245    Account.Time := Now;
     246    if FioAPI.DownloadInterval(Now, Now, List) then begin
     247      List.SaveToStream(Mem);
     248      ReadXMLFile(XMLDocument, Mem);
     249      RootNode := XMLDocument.DocumentElement;
     250      ShowMessage(RootNode.NodeName);
     251    end else begin
     252      Account.Number := '';
     253      Account.Balance := 0;
     254    end;
     255  finally
     256    Mem.Free;
     257    XMLDocument.Free;
     258    FioAPI.Free;
     259    List.Free;
     260  end;
     261end;
     262
     263procedure TCore.LoadAccounts;
     264var
     265  I: Integer;
     266begin
     267  for I := 0 to Accounts.Count - 1 do
     268    LoadAccount(TAccount(Accounts[I]));
    94269end;
    95270
     
    108283    WriteString('TargetDir', TargetDirectory);
    109284    WriteInteger('DataFormat', Integer(DataFormat));
    110     WriteString('Token', Token);
    111285    WriteDate('ReportTimeFrom', ReportTimeFrom);
    112286    WriteDateTime('ReportTimeTo', ReportTimeTo);
     
    117291    Free;
    118292  end;
     293  Accounts.SaveToRegistry(RegContext(Context.RootKey, Context.Key + '\Account'));
    119294end;
    120295
     
    127302    TargetDirectory := ReadStringWithDefault('TargetDir', ExtractFileDir(Application.ExeName));
    128303    DataFormat := TFioDataFormat(ReadIntegerWithDefault('DataFormat', Integer(dfXML)));
    129     Token := ReadStringWithDefault('Token', '');
    130304    ReportTimeFrom := ReadDateTimeWithDefault('ReportTimeFrom', Now);
    131305    ReportTimeTo := ReadDateTimeWithDefault('ReportTimeTo', Now);
     
    136310    Free;
    137311  end;
     312  Accounts.LoadFromRegistry(RegContext(Context.RootKey, Context.Key + '\Account'));
    138313end;
    139314
     
    162337  try
    163338    FioAPI.Format := DataFormat;
    164     FioAPI.Token := Token;
     339    FioAPI.Token := CurrentAccount.Token;
    165340    if FioAPI.DownloadLast(List) then begin
    166341      FileName := TargetDirectory + DirectorySeparator + Format(SReport, [FormatDateTime('yyyy-mm-dd hh-nn-ss', Now) +
     
    185360  try
    186361    FioAPI.Format := DataFormat;
    187     FioAPI.Token := Token;
     362    FioAPI.Token := CurrentAccount.Token;
    188363    if FioAPI.DownloadMonthly(ReportYear, ReportId, List) then begin
    189364      FileName := TargetDirectory + DirectorySeparator + Format(SReport, [IntToStr(ReportYear) +
     
    209384  try
    210385    FioAPI.Format := DataFormat;
    211     FioAPI.Token := Token;
     386    FioAPI.Token := CurrentAccount.Token;
    212387    if FioAPI.DownloadInterval(ReportTimeFrom, ReportTimeTo, List) then begin
    213388      FileName := TargetDirectory + DirectorySeparator + Format(SReport, [FormatDateTime('yyyy-mm-dd', ReportTimeFrom) +
     
    223398end;
    224399
     400procedure TCore.AAccountsExecute(Sender: TObject);
     401begin
     402  TAccountList(FormAccounts.Accounts).Assign(Accounts);
     403  if FormAccounts.ShowModal = mrOk then begin
     404    Accounts.Assign(TAccountList(FormAccounts.Accounts));
     405  end;
     406end;
     407
    225408end.
    226409
Note: See TracChangeset for help on using the changeset viewer.