Advertisement
Guest User

Untitled

a guest
Aug 1st, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 6.41 KB | None | 0 0
  1. program test9;
  2.  
  3. uses
  4.   Crt,
  5.   Classes, Strings, RegExpr, Sysutils, Sqlite3, Sqlite3db,
  6.   XMLReader, XMLTextReader, XMLUtils, LibCurl, UnixType, Pipes,
  7.   App, Objects, Menus, Drivers, Views, Dialogs, MsgBox, StdDlg;
  8.  
  9. type
  10.   TMyApp = object(TApplication)
  11.     procedure InitMenuBar; virtual;
  12.     procedure HandleEvent(var Event: TEvent); virtual;
  13.   end;
  14.  
  15.   TDisplaySQLDialog = object(TDialog)
  16.     constructor Init (FileName : String);
  17.   end;
  18.   PDisplaySQLDialog = ^TDisplaySQLDialog;
  19.  
  20.   TUpdateDialog = object(TDialog)
  21.     constructor Init (FileName : String);
  22.   end;
  23.   PUpdateDialog = ^TUpdateDialog;
  24.  
  25.  
  26. procedure TMyApp.InitMenuBar;
  27. var
  28.   R : TRect;
  29.   pFileMenu, pHelpMenu : PMenuItem;
  30. begin
  31.   GetExtent(R);
  32.   R.B.Y := R.A.Y + 1;
  33.   pHelpMenu := NewSubMenu('~H~elp', hcNoContext, NewMenu(
  34.         NewItem('~A~bout', '', 0, cmHelp, hcNoContext, nil)),
  35.         nil);
  36.   pFileMenu := NewSubMenu('~F~ile', hcNoContext, NewMenu(
  37.         NewItem('~O~pen', 'F2', kbF2, cmOpen, hcNoContext,
  38.         NewItem('~C~lose', 'F3', kbF3, cmClose, hcNoContext,
  39.         NewLine(
  40.         NewItem('E~x~it', 'Alt-X', kbAltX, cmQuit, hcNoContext, nil))))),
  41.         pHelpMenu);
  42.   MenuBar := new (PMenuBar, Init(R,
  43.       NewMenu(pFileMenu)));
  44. end;
  45.  
  46. procedure TMyApp.HandleEvent(var Event: TEvent);
  47. var
  48.   FileName : String;
  49.   result : integer;
  50.   pOpen : PFileDialog;
  51.   R : TRect;
  52.   pDisplay : PDisplaySQLDialog;
  53.   pUpdate : PUpdateDialog;
  54. begin
  55.   inherited HandleEvent(Event);
  56.   if Event.What = evCommand then
  57.   begin
  58.     if Event.Command = cmHelp then
  59.     begin
  60.           MessageBox('About:'+#13#10+'This is my test app'+#13#10+'Alan Ward (C) 2016',
  61.            nil, mfInformation or mfOKButton);
  62.     end;
  63.     if Event.Command = cmOpen then
  64.     begin
  65.         pOpen := New(PFileDialog, Init('*.db', 'Open', 'File ~N~ame',
  66.           fdOpenButton, hcNoContext));
  67.         result := ExecuteDialog (pOpen, @FileName);
  68.         if not (result = cmCancel) then
  69.         begin
  70.                 pUpdate := New(PUpdateDialog, Init(FileName));
  71.                     ExecuteDialog(pUpdate, nil);
  72.             pDisplay := New(PDisplaySQLDialog, Init(FileName));
  73.             ExecuteDialog (pDisplay, nil);
  74.         end;
  75.     end;    
  76.   end;
  77. end;
  78.  
  79.  
  80. Function DoWrite(Ptr : Pointer; Size : size_t; nmemb: size_t; Data : Pointer) : size_t; cdecl;
  81.  
  82. begin
  83.   DoWrite := Classes.TStream(Data).Write(Ptr^,Size*nmemb);
  84. end;
  85.  
  86.  
  87. constructor TUpdateDialog.Init (FileName : String);
  88. var
  89.   R : TRect;
  90.   msgLabel : PLabel;
  91.  
  92.   URL : Pchar = 'http://fullcirclemagazine.org/author/ronnie-2/feed/';
  93.   hCurl : pCurl;
  94.    
  95.     inPipe : TInputPipeStream;
  96.     outPipe : TOutputPipeStream;
  97.    
  98.   reader : TXMLReader;
  99.   settings : TXMLReaderSettings;
  100.   input : TXMLInputSource;
  101.  
  102.   sql : TSQLite;
  103.   n : integer;
  104.  
  105.   issue, articleTitle, articleLink : string;
  106.   nextTextIsTitle, nextTextIsLink : boolean;
  107.  
  108.   re : TRegExpr;
  109.   pos, len : integer;
  110.   newItems : integer;
  111. begin
  112.   R.Assign(20, 4, 60, 14);
  113.   inherited Init (R, 'Update');
  114.  
  115.   R.Assign(3, 3, 33, 4);
  116.   msgLabel := New(PLabel, Init(R, 'Connecting to the Internet...', nil));
  117.   Insert (msgLabel);
  118.  
  119.   R.Assign(25, 8, 35, 9);
  120.   Insert (New(PButton, Init(R, '~C~lose', cmCancel, 0)));
  121.   DrawView;
  122.  
  123.  
  124.   settings := TXMLReaderSettings.Create;
  125.   settings.PreserveWhiteSpace := false;
  126.   settings.Namespaces := true;
  127.  
  128.   CreatePipeStreams (inPipe, outPipe);
  129.  
  130.   hCurl:= curl_easy_init;
  131.   if Assigned(hCurl) then
  132.   begin
  133.     curl_easy_setopt(hCurl,CURLOPT_VERBOSE, [False]);
  134.     curl_easy_setopt(hCurl,CURLOPT_URL,[URL]);
  135.  
  136.     curl_easy_setopt(hCurl,CURLOPT_WRITEFUNCTION,[@DoWrite]);
  137.   curl_easy_setopt(hCurl,CURLOPT_WRITEDATA,[Pointer(outPipe)]);
  138.  
  139.     curl_easy_perform(hCurl);
  140.     curl_easy_cleanup(hCurl);
  141.   end;
  142.  
  143.   msgLabel^.Text := NewStr('Retrieved information...');
  144.   DrawView;
  145.  
  146.  
  147.     re := TRegExpr.Create;
  148.     re.Expression := '#[0-9]*';
  149.    
  150.   newItems := 0;
  151.   sql := TSQLite.Create(FileName);
  152.  
  153.   input := TXMLInputSource.Create(inPipe);
  154.   reader := TXMLTextReader.Create(input, settings);
  155.   while reader.Read do
  156.   begin
  157.         if reader.NodeType = ntElement then
  158.             if reader.name = 'title' then begin
  159.                 nextTextIsTitle := true;
  160.                 nextTextIsLink := false;
  161.             end else if reader.name = 'link' then begin
  162.               nextTextIsTitle := false;
  163.               nextTextIsLink := true;
  164.             end else begin
  165.                 nextTextIsTitle := false;
  166.                 nextTextIsLink := false;
  167.             end;
  168.         if reader.NodeType = ntText then
  169.             if nextTextIsTitle then
  170.             begin
  171.                 if re.Exec(reader.value) then
  172.                 begin
  173.                     pos := re.MatchPos[0];
  174.                     len := re.MatchLen[0];
  175.                     issue := Copy(reader.value, pos+1, len-1);
  176.                     if issue <> '' then begin
  177.                         articleTitle := reader.value;
  178.                         nextTextIsTitle := false;
  179.                     end;
  180.                 end;
  181.             end else if nextTextIsLink then begin
  182.                 articleLink := reader.value;
  183.                 nextTextIsLink := false;
  184.                
  185.                 sql.Query('select id from issues where id="' + issue + '"', nil);
  186.                 n := sql.List_FieldName.count;
  187.                 if n = 0 then begin
  188.                     inc(newItems);
  189.                    
  190.                     sql.Query('insert into issues values("' + issue + '", "' + articleTitle + '", "", "' +
  191.                         articleLink + '", "")', nil);
  192.                 end;
  193.             end;
  194.   end;
  195.  
  196.   sql.Free;
  197.   reader.Free;
  198.     input.Free;
  199.     settings.Free;
  200.  
  201.   msgLabel^.Text := NewStr('Found ' + IntToStr(newItems) + ' new issues...');
  202.   DrawView;
  203. end;
  204.  
  205.  
  206. constructor TDisplaySQLDialog.Init (FileName : String);
  207. const
  208.   dbquery = 'select title, download from issues order by id desc';
  209. var  
  210.   sql : TSQLite;
  211.   i, n : Integer;
  212.   res : Classes.TStringList;
  213.   id, downloadURL : String;
  214.  
  215.   R : TRect;
  216.   Scroll : PScrollBar;
  217.   Items : PStringCollection;
  218.   List : PListBox;
  219. begin
  220.   R.Assign(0, 0, 78, 17);
  221.   inherited Init (R, 'Display SQL');
  222.  
  223.   sql := TSQLite.Create(FileName);
  224.     sql.Query(dbquery, nil);
  225.     n := sql.List_Field.count;
  226.    
  227.   Items := New(PStringCollection, Init(10, 1));
  228.     for i := 1 to n do
  229.     begin
  230.         res := Classes.TStringList(sql.List_Field.items[i-1]);
  231.         id := res[0];
  232.         downloadURL := res[1];
  233.         Items^.Insert(NewStr(id + ' | ' + downloadURL));
  234.     end;
  235.    
  236.     sql.Free;
  237.  
  238.   R.Assign(67, 2, 68, 12);
  239.   Scroll := New(PScrollBar, Init(R));
  240.   Insert (Scroll);
  241.  
  242.   R.Assign(2, 2, 66, 12);
  243.   List := New(PListBox, Init(R, 1, Scroll));
  244.   List^.NewList(Items);
  245.   Insert (List);
  246.  
  247.   R.Assign(60, 14, 70, 15);
  248.   Insert (New(PButton, Init(R, '~C~lose', cmCancel, 0)));
  249. end;
  250.  
  251.  
  252. var MyApp : TMyApp;
  253.  
  254. begin
  255.   MyApp.Init;
  256.   MyApp.Run;
  257.   MyApp.Done;
  258. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement