Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program test9;
- uses
- Crt,
- Classes, Strings, RegExpr, Sysutils, Sqlite3, Sqlite3db,
- XMLReader, XMLTextReader, XMLUtils, LibCurl, UnixType, Pipes,
- App, Objects, Menus, Drivers, Views, Dialogs, MsgBox, StdDlg;
- type
- TMyApp = object(TApplication)
- procedure InitMenuBar; virtual;
- procedure HandleEvent(var Event: TEvent); virtual;
- end;
- TDisplaySQLDialog = object(TDialog)
- constructor Init (FileName : String);
- end;
- PDisplaySQLDialog = ^TDisplaySQLDialog;
- TUpdateDialog = object(TDialog)
- constructor Init (FileName : String);
- end;
- PUpdateDialog = ^TUpdateDialog;
- procedure TMyApp.InitMenuBar;
- var
- R : TRect;
- pFileMenu, pHelpMenu : PMenuItem;
- begin
- GetExtent(R);
- R.B.Y := R.A.Y + 1;
- pHelpMenu := NewSubMenu('~H~elp', hcNoContext, NewMenu(
- NewItem('~A~bout', '', 0, cmHelp, hcNoContext, nil)),
- nil);
- pFileMenu := NewSubMenu('~F~ile', hcNoContext, NewMenu(
- NewItem('~O~pen', 'F2', kbF2, cmOpen, hcNoContext,
- NewItem('~C~lose', 'F3', kbF3, cmClose, hcNoContext,
- NewLine(
- NewItem('E~x~it', 'Alt-X', kbAltX, cmQuit, hcNoContext, nil))))),
- pHelpMenu);
- MenuBar := new (PMenuBar, Init(R,
- NewMenu(pFileMenu)));
- end;
- procedure TMyApp.HandleEvent(var Event: TEvent);
- var
- FileName : String;
- result : integer;
- pOpen : PFileDialog;
- R : TRect;
- pDisplay : PDisplaySQLDialog;
- pUpdate : PUpdateDialog;
- begin
- inherited HandleEvent(Event);
- if Event.What = evCommand then
- begin
- if Event.Command = cmHelp then
- begin
- MessageBox('About:'+#13#10+'This is my test app'+#13#10+'Alan Ward (C) 2016',
- nil, mfInformation or mfOKButton);
- end;
- if Event.Command = cmOpen then
- begin
- pOpen := New(PFileDialog, Init('*.db', 'Open', 'File ~N~ame',
- fdOpenButton, hcNoContext));
- result := ExecuteDialog (pOpen, @FileName);
- if not (result = cmCancel) then
- begin
- pUpdate := New(PUpdateDialog, Init(FileName));
- ExecuteDialog(pUpdate, nil);
- pDisplay := New(PDisplaySQLDialog, Init(FileName));
- ExecuteDialog (pDisplay, nil);
- end;
- end;
- end;
- end;
- Function DoWrite(Ptr : Pointer; Size : size_t; nmemb: size_t; Data : Pointer) : size_t; cdecl;
- begin
- DoWrite := Classes.TStream(Data).Write(Ptr^,Size*nmemb);
- end;
- constructor TUpdateDialog.Init (FileName : String);
- var
- R : TRect;
- msgLabel : PLabel;
- URL : Pchar = 'http://fullcirclemagazine.org/author/ronnie-2/feed/';
- hCurl : pCurl;
- inPipe : TInputPipeStream;
- outPipe : TOutputPipeStream;
- reader : TXMLReader;
- settings : TXMLReaderSettings;
- input : TXMLInputSource;
- sql : TSQLite;
- n : integer;
- issue, articleTitle, articleLink : string;
- nextTextIsTitle, nextTextIsLink : boolean;
- re : TRegExpr;
- pos, len : integer;
- newItems : integer;
- begin
- R.Assign(20, 4, 60, 14);
- inherited Init (R, 'Update');
- R.Assign(3, 3, 33, 4);
- msgLabel := New(PLabel, Init(R, 'Connecting to the Internet...', nil));
- Insert (msgLabel);
- R.Assign(25, 8, 35, 9);
- Insert (New(PButton, Init(R, '~C~lose', cmCancel, 0)));
- DrawView;
- settings := TXMLReaderSettings.Create;
- settings.PreserveWhiteSpace := false;
- settings.Namespaces := true;
- CreatePipeStreams (inPipe, outPipe);
- hCurl:= curl_easy_init;
- if Assigned(hCurl) then
- begin
- curl_easy_setopt(hCurl,CURLOPT_VERBOSE, [False]);
- curl_easy_setopt(hCurl,CURLOPT_URL,[URL]);
- curl_easy_setopt(hCurl,CURLOPT_WRITEFUNCTION,[@DoWrite]);
- curl_easy_setopt(hCurl,CURLOPT_WRITEDATA,[Pointer(outPipe)]);
- curl_easy_perform(hCurl);
- curl_easy_cleanup(hCurl);
- end;
- msgLabel^.Text := NewStr('Retrieved information...');
- DrawView;
- re := TRegExpr.Create;
- re.Expression := '#[0-9]*';
- newItems := 0;
- sql := TSQLite.Create(FileName);
- input := TXMLInputSource.Create(inPipe);
- reader := TXMLTextReader.Create(input, settings);
- while reader.Read do
- begin
- if reader.NodeType = ntElement then
- if reader.name = 'title' then begin
- nextTextIsTitle := true;
- nextTextIsLink := false;
- end else if reader.name = 'link' then begin
- nextTextIsTitle := false;
- nextTextIsLink := true;
- end else begin
- nextTextIsTitle := false;
- nextTextIsLink := false;
- end;
- if reader.NodeType = ntText then
- if nextTextIsTitle then
- begin
- if re.Exec(reader.value) then
- begin
- pos := re.MatchPos[0];
- len := re.MatchLen[0];
- issue := Copy(reader.value, pos+1, len-1);
- if issue <> '' then begin
- articleTitle := reader.value;
- nextTextIsTitle := false;
- end;
- end;
- end else if nextTextIsLink then begin
- articleLink := reader.value;
- nextTextIsLink := false;
- sql.Query('select id from issues where id="' + issue + '"', nil);
- n := sql.List_FieldName.count;
- if n = 0 then begin
- inc(newItems);
- sql.Query('insert into issues values("' + issue + '", "' + articleTitle + '", "", "' +
- articleLink + '", "")', nil);
- end;
- end;
- end;
- sql.Free;
- reader.Free;
- input.Free;
- settings.Free;
- msgLabel^.Text := NewStr('Found ' + IntToStr(newItems) + ' new issues...');
- DrawView;
- end;
- constructor TDisplaySQLDialog.Init (FileName : String);
- const
- dbquery = 'select title, download from issues order by id desc';
- var
- sql : TSQLite;
- i, n : Integer;
- res : Classes.TStringList;
- id, downloadURL : String;
- R : TRect;
- Scroll : PScrollBar;
- Items : PStringCollection;
- List : PListBox;
- begin
- R.Assign(0, 0, 78, 17);
- inherited Init (R, 'Display SQL');
- sql := TSQLite.Create(FileName);
- sql.Query(dbquery, nil);
- n := sql.List_Field.count;
- Items := New(PStringCollection, Init(10, 1));
- for i := 1 to n do
- begin
- res := Classes.TStringList(sql.List_Field.items[i-1]);
- id := res[0];
- downloadURL := res[1];
- Items^.Insert(NewStr(id + ' | ' + downloadURL));
- end;
- sql.Free;
- R.Assign(67, 2, 68, 12);
- Scroll := New(PScrollBar, Init(R));
- Insert (Scroll);
- R.Assign(2, 2, 66, 12);
- List := New(PListBox, Init(R, 1, Scroll));
- List^.NewList(Items);
- Insert (List);
- R.Assign(60, 14, 70, 15);
- Insert (New(PButton, Init(R, '~C~lose', cmCancel, 0)));
- end;
- var MyApp : TMyApp;
- begin
- MyApp.Init;
- MyApp.Run;
- MyApp.Done;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement