Recent

Author Topic: Adding "Automatic updates" feature  (Read 11287 times)

WildMan

  • New Member
  • *
  • Posts: 22
Adding "Automatic updates" feature
« on: July 06, 2012, 11:10:46 am »
I'm working on a update feature to allow my app users to click on a button and download zip files that contain new program content.  It seems like I'm close but I got stuck; the stripped-down code below downloads the zip file from the given URL and stores on the local drive.   That works and I can unzip the file by clicking on in in Finder. But when I try to unzip it via code, I get an error #127 (whatever that is)...

(uses HTTPSend)

  with THTTPSend.Create do
  begin
     if HTTPMethod('GET','http://www.MySite.Com/Updates/Update.zip') then
     begin
        document.SaveToFile(DestPath+'Update.zip');   
        ExecuteProcess('FUnzip','"'+DestPath+'Update.zip"',[]);
     end;
     Free;
  end;   

I've tried executing Unzip and Funzip, tried UTF8ToSys on the parameters passed to ExecuteProcess, tried messing with the Zlibar zip package (but it doesn't handle compressed zip files), and tried banging my head against the wall (hurt, didn't help). 

The double quotes in there is because DestPath contains spaces, in my case it points to a directory in the Applications Support directory; tried it without them, still got an error #127.

I also tried calling Unzip through a tProcess without luck.

Can anyone provide any pointers that may help me with this little hurdle?

kamischi

  • Full Member
  • ***
  • Posts: 177
Re: Adding "Automatic updates" feature
« Reply #1 on: July 06, 2012, 11:18:30 am »
Did you consider using Sparkle (http://sparkle.andymatuschak.org/) or even have some experience with it?
fpc 2.6.4, lazarus 1.4.0, Mac OS X, fink

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Adding "Automatic updates" feature
« Reply #2 on: July 06, 2012, 01:07:33 pm »
Don't know what FUnzip and Unzip are...

Have you tried using the FCL zipper unit?

Something like (NOTE: NOT TESTED, written by hand:
Code: [Select]
uses
Zipper
...
var
UnzipFile: TUnZipper; 
...
begin
...
  try
    UnzipFile.FileName := 'c:\test\myzip.zip';
    UnzipFile.OutputPath := 'c:\windows\temp\' //or perhaps without trailing \;
    UnzipFile.UnZipAllFiles;
  finally
    UnzipFile.Free;
  end;

  UnzipFile := TUnZipper.Create();
See e.g. the checkrideutil unit in the checkridehelper project in my Checkride project (link in signature) for the full code

Edit: this unit supports several decompression methods (deflate,...) but does not support one that I occassionally see... can't remember which one.
Most files actually decompress ok with this though.
« Last Edit: July 06, 2012, 01:09:51 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

WildMan

  • New Member
  • *
  • Posts: 22
Re: Adding "Automatic updates" feature
« Reply #3 on: July 07, 2012, 06:33:15 am »
Kamischi, Sparkle looks really cool but it appears to be geared toward C programers using Xcode.  It's a bit more than I need too; my UI lets the user select the files they're interested in and I can grab them from the web so I just need to unzip them and I'd be all set.  Have you already used Sparkle with a Lazarus project?

BigChimp, I followed the link and found the source for CheckRideUntil but can't find a Zipper.pas file.  Your example seams to be Windows based and I'm working on a Mac, is that perhaps why I don't have this zipper unit?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8114
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Adding "Automatic updates" feature
« Reply #4 on: July 07, 2012, 03:16:17 pm »
Quote
Your example seams to be Windows based and I'm working on a Mac, is that perhaps why I don't have this zipper unit?
zipper is part of paszlib package, look for that under your compiler's units folder (I only use windows and linux, so I don't know the layout in mac).

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Adding "Automatic updates" feature
« Reply #5 on: July 08, 2012, 10:59:34 am »
Yep, what leledumbo said: it's part of the FCL so should be distributed with your FPC version...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

WildMan

  • New Member
  • *
  • Posts: 22
Re: Adding "Automatic updates" feature
« Reply #6 on: July 08, 2012, 12:54:32 pm »
Thanks guys, that does the trick nicely.

IPguy

  • Sr. Member
  • ****
  • Posts: 385
Re: Adding "Automatic updates" feature
« Reply #7 on: July 09, 2012, 02:00:26 pm »
WildMan,
Can you share the completed procedure you developed to implement the upgrade feature?

WildMan

  • New Member
  • *
  • Posts: 22
Re: Adding "Automatic updates" feature
« Reply #8 on: July 10, 2012, 08:32:27 am »
I put the download process in a tThread so that users can download files while they keep working.  There's more than 300 image libraries (zip files full of JPG images) that they may download.  So, when they select some libraries, it creates a thread to download the first and when that thread is complete it queues the next thread and so forth.  Anyway, here's the execute process for the thread, the bit that does the work...

(uses HTTPSend, Zipper)

     tDownloadThread = Class(tThread)
     Private
       Ffilename: string;
       FCountryIndex: Integer;
     Protected
       Procedure Execute; Override;
     Public
       Constructor Create(CreateSuspended: Boolean);
       Destructor Destroy; Override;
       Property FileName: String read FFileName write FfileName;
     end;   


procedure TDownloadThread.Execute;
var UnzipFile: TUnZipper;
begin
  with THTTPSend.Create do
  begin
    if HTTPMethod('GET','http://www.MySite.com/Downloads/'+FileName+'.zip') then
    begin
      document.SaveToFile(AppSupportPath+FileName+'.zip');

      UnZipFile:=tUnZipper.Create;
      try
        UnzipFile.FileName := AppSupportPath+FileName+'.zip';
        UnzipFile.OutputPath := AppSupportPath;
        UnzipFile.UnZipAllFiles;
      finally
        UnzipFile.Free;
      end;
      Deletefile(AppSupportPath+FileName+'.zip');
    end;
    Free;
  end;
end; 


{and the bit to create the tThead...}

     DownloadThread:=tDownloadThread.Create(true);
     DownloadThread.FileName:=  //insert your file name
     DownloadThread.Resume;

This bit of code uses tips from a lot of people on this forum so thanks for all the help.

IPguy

  • Sr. Member
  • ****
  • Posts: 385
Re: Adding "Automatic updates" feature
« Reply #9 on: July 11, 2012, 02:58:57 am »
Thank you.

WildMan

  • New Member
  • *
  • Posts: 22
Re: Adding "Automatic updates" feature
« Reply #10 on: July 20, 2012, 08:49:44 am »
I'm hoping to extend this process now to add on-line activation to my app.  With the above code, I can grab a file from the web page; if I could then delete that file off the web site, I'd have everything I need to implement this.  My app comes with a "DVD code" which is a serial number encrypted with OnGuard.  So, the user enters the DVD code and the program reads that as, say, Serial number 1000.  It then checks the web site to download a file named (for example) 1000.Lic  (a file that I'd place on the site when the order is received) and it would then delete the file.   

THTTPSend made it easy to download the file but I'm not having such luck in deleting the file.  Any suggestions there?  Also, I had no luck in generating a unique machine identifier which would make this process more secure (otherwise that that file 1000.Lic could just be copied to another machine)

Odacir

  • New Member
  • *
  • Posts: 27
Re: Adding "Automatic updates" feature
« Reply #11 on: February 01, 2013, 01:41:59 pm »
I put the download process in a tThread so that users can download files while they keep working.  There's more than 300 image libraries (zip files full of JPG images) that they may download.  So, when they select some libraries, it creates a thread to download the first and when that thread is complete it queues the next thread and so forth.  Anyway, here's the execute process for the thread, the bit that does the work...

(uses HTTPSend, Zipper)

     tDownloadThread = Class(tThread)
     Private
       Ffilename: string;
       FCountryIndex: Integer;
     Protected
       Procedure Execute; Override;
     Public
       Constructor Create(CreateSuspended: Boolean);
       Destructor Destroy; Override;
       Property FileName: String read FFileName write FfileName;
     end;   


procedure TDownloadThread.Execute;
var UnzipFile: TUnZipper;
begin
  with THTTPSend.Create do
  begin
    if HTTPMethod('GET','http://www.MySite.com/Downloads/'+FileName+'.zip') then
    begin
      document.SaveToFile(AppSupportPath+FileName+'.zip');

      UnZipFile:=tUnZipper.Create;
      try
        UnzipFile.FileName := AppSupportPath+FileName+'.zip';
        UnzipFile.OutputPath := AppSupportPath;
        UnzipFile.UnZipAllFiles;
      finally
        UnzipFile.Free;
      end;
      Deletefile(AppSupportPath+FileName+'.zip');
    end;
    Free;
  end;
end; 


{and the bit to create the tThead...}

     DownloadThread:=tDownloadThread.Create(true);
     DownloadThread.FileName:=  //insert your file name
     DownloadThread.Resume;

This bit of code uses tips from a lot of people on this forum so thanks for all the help.

Wildman,

Is possible you post a simple project with your code and your httpsend and zipper unit? I tryed, but on my sinapse unit, don't have the httpmethod.

Thanks a lot

Leledumbo

  • Hero Member
  • *****
  • Posts: 8114
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Adding "Automatic updates" feature
« Reply #12 on: February 01, 2013, 02:26:12 pm »
Quote
I tryed, but on my sinapse unit, don't have the httpmethod.
It's a method of THTTPSend class, see that wildman calls the method inside a with block:
Code: [Select]
with THTTPSend.Create do
  begin
    if HTTPMethod('GET','http://www.MySite.com/Downloads/'+FileName+'.zip') then

Odacir

  • New Member
  • *
  • Posts: 27
Re: Adding "Automatic updates" feature
« Reply #13 on: February 04, 2013, 07:18:02 pm »
Thanks,

The code is running. Only problem with the thread. I will solve in other time.