Sponsored links


Go Back   Sat Industry Forums > Dreambox > Dreambox Development
Register FAQ Members List Calendar Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
  #11 (permalink)  
Old 09-02-2006
bacicciosat's Avatar
coder
 
Join Date: Sep 2006
Posts: 47
Thanks: 0
Thanked 40 Times in 14 Posts
bacicciosat is on a distinguished road
Lesson8: Http Connection and Download

/*
+--------------------------------------------------------------------------
| The Bible Enigma1 Tutorial
| ========================================
| By: Bacicciosat aka Meo aka Luponero
|
| Enigma 1 API GUI tutorial with Sources Code and Comments
| (c) august 2006 by Meo
|
+---------------------------------------------------------------------------
*/

Lesson8: Http Connection and Download

Ok this is the FINAL lesson
We will create a little application.
This plugin will connect to Wikipedia Site and will Download the page about Mkportal.

If you followed this tutorial you are now able to understand code and Enigma Api.
I will not explain other.
i will post here only the complete commented source.

Code:
/*
+--------------------------------------------------------------------------
|   Bible Enigma1 Tutorial
|   ========================================
|   By: Bacicciosat aka Meo aka Luponero
|
|   Lesson 8: http connection and download demonstration.
|   (c) august 2006 by Meo
|
+---------------------------------------------------------------------------
*/

// include files nedeed by our code.
#include <stdio.h>
#include <stdlib.h>
#include <plugin.h>
#include <math.h>
#include <dirent.h>
#include <string.h>
#include <lib/gui/ewindow.h>
#include <lib/gui/ebutton.h>
#include <lib/gui/emessage.h>
#include <lib/gui/textinput.h>
#include <lib/gui/combobox.h>
#include <lib/gui/statusbar.h>
#include <lib/gui/eskin.h>
#include <lib/gdi/font.h>
#include <lib/gdi/epng.h>
#include <lib/gdi/gfbdc.h>
#include <lib/system/httpd.h>
#include <lib/gui/eprogress.h>
#include <upgrade.h>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <lib/base/thread.h>
#include <lib/base/estring.h>


// The Class declaration of our Http Connection system
class Fetcher : public Object
{    
    // The temp file in which we will store the download data
    eString tempPath;
    // internal pointers and functions of download connection
    eHTTPConnection * connectionP;
    eHTTPDataSource * dataSinkP;
    void transferDone(int err);
    eHTTPDataSource * createDownloadSink(eHTTPConnection *conn);    
public:
    Signal1<void,int> downloadDone;
    void fetch();
};

// The Class declaration of our Main Window
class eBibleMainWindow: public eWindow
{
    // the label to show the text
    eLabel *label;
    // the button to start connection
    eButton *ok;
    // the connection object
    Fetcher theFetcher;
    // flag to indicate the download state
    int downloadDoneFlag;
    int inDownloadFlag;
public:
        // the constructor.
    eBibleMainWindow();
    // function to call when button is pushed 
    void Wconnect();
    // function to call when download is completed.
    void downloadDone(int err);
        // the destructor.
    ~eBibleMainWindow();
};

// The definition of the C function we will use to remove Html Tags from downloaded text
eString removeTags(eString in);
    
// The plugin entry point, Here start the code execution
extern "C" int plugin_exec( PluginParam *par )
{
    // our demo dialog instance.
    eBibleMainWindow dlg;
        // show the dialog...
    dlg.show();
        // give control to dialog.. (the dialog is modal!)
    int result=dlg.exec();
        // and after it, hide it again.
    dlg.hide();
    return result;
}


eBibleMainWindow::eBibleMainWindow(): eWindow(1)
{
    inDownloadFlag = 0;
        // move our dialog to 100.100...
    cmove(ePoint(100, 70));
        // ...and give x and y dimensions.
    cresize(eSize(520, 436));
        // set a title.
    setText("Enigma Bible Lesson 8: http connection");
    
    // create a label to show a text.
    label=new eLabel(this);
    // give a position
    label->move(ePoint(10, 0));
    // set the label dimensions
    label->resize(eSize(490, 380));
    // set the label text
    label->setText("This a demo http connection. We will connect to Wikipedia Site and will Download the page about Mkportal that is a CMS wrote and distributed by Meo aka Bacicciosat. We will parse the Wikipedia page and show in this window the Wikpedia definition of Mkportal.");
    // set the wrap flag to the label
    label->setFlags(RS_WRAP);

    // create button and set properties
    ok = new eButton(this);
    ok->setText("Connect");
    ok->move(ePoint((clientrect.width() - 150)/2, clientrect.height() - 60));
    ok->resize(eSize(150, 40));
    ok->setShortcut("green");
    ok->setShortcutPixmap("green");
    ok->loadDeco();

    // function to call when button is pushed
    CONNECT(ok->selected, eBibleMainWindow::Wconnect);

}

void eBibleMainWindow::Wconnect()
{
    // Set the flag that indicates we are in downlading status
    if(!inDownloadFlag) {    
        inDownloadFlag = 1;
        // Hide Label to change text
        label->hide();
        // Set new text for the label
        label->setText("Wait please connection to Wikipedia site in progress...");
        // Show the label with New Text
        label->show();
        // Function to call when donwload is complete
        CONNECT(theFetcher.downloadDone, eBibleMainWindow::downloadDone);
        // Set the flag indicates that we are starting connections
        downloadDoneFlag = 0;
        // Call function to start http connection
        theFetcher.fetch();
    }
}

void Fetcher::fetch()
{    
      // declare variable
    eString url;
    // assign to the variable the url we want to connect
    url = "http://en.wikipedia.org/wiki/Mkportal";
    // assign to class variable the temporary file we will use to store the dowanloaded data
    tempPath = "/var/tmp/bdemo.tmp";
    // inizialize the control error flag
    int error = 0;
    // start connection
    connectionP = eHTTPConnection::doRequest(url.c_str(), eApp, &error);
    // Show a Messagebox in case of connection error
    if(!connectionP || error)
    {    eMessageBox msg("Error downloading " + url + "(" + eString().sprintf("%d", error) + ")", _("Details"), eMessageBox::btOK);
        msg.show();     msg.exec();     msg.hide();
    }
    else
    {
        //if the connection is estabilished start the download funcions
        CONNECT(connectionP->transferDone, Fetcher::transferDone);
        CONNECT(connectionP->createDataSource, Fetcher::createDownloadSink);
        // set the user-agent name
        connectionP->local_header["User-Agent"] = "PLBOT";
        connectionP->start();
    }
}

void Fetcher::transferDone(int err)
{    
    // If no error we can call the downloadDone function
    if(!err)
    {    connectionP = NULL;
        // Tell caller download is ready
        /*emit*/ downloadDone(err);
    }
    else
    {
        //else show a Messagebox with Error description.
        eString sMsg = "Error " + eString().sprintf("%d", err);
        eMessageBox msg(sMsg, _("User Abort"), eMessageBox::iconWarning|eMessageBox::btOK);
        msg.show();     msg.exec();     msg.hide();
    }
}
// internal download procedure (standard)
eHTTPDataSource * Fetcher::createDownloadSink(eHTTPConnection *conn)
{    dataSinkP = new eHTTPDownload(connectionP, (char *)tempPath.c_str());

    return(dataSinkP);

}

void eBibleMainWindow::downloadDone(int err)
{
    // set the flag that indicates that download is complete
    if(!downloadDoneFlag)
    {    downloadDoneFlag = 1;
        // create a buffer to read the temp file with download data.
        char buf[512];
        // declare the variables we will use in this function
        eString strview, strview1;
        // open temporary file cotaining the downloaded data 
          FILE *f = fopen("/var/tmp/bdemo.tmp", "rt");
           if (f)
           {
            //Add an introduction text
            strview = "Mkportal CMS. Author Meo aka Bacicciosat aka Luponero. www.mkportal.it\n\nWikipedia:\n";
            // find the line we want (The mkportal definition in the wiki page we downloaded)
            while (fgets(buf, 512, f))
             {
                if (strstr(buf, "is a free Portal"))
                break;
            }
            // store this line in the variable
            strview1 = eString(buf);
            // remove html tags
            strview1 = removeTags(strview1);
            //  concate strings to compose our output text
            strview += strview1;
            // read the next lines .....
              while (fgets(buf, 512, f))
              {
            // if we found this string the defnition is ended so we can stop to read the file
                if (strstr(buf, "See also"))
                break;
            // else store the line in the variable
                strview1 = eString(buf);
            // remove html tags
                strview1 = removeTags(strview1);
            //  concate strings to compose our output text
                strview += strview1; 
              }
            // close file
              fclose(f);
           }
        //Delete the temporary file we used to store downloaded data
           ::unlink("/var/tmp/bdemo.tmp");
        // Hide label to change the text
        label->hide();
        // Insert into the label the output we have stored in the variable strview
        label->setText(strview);
        // Show the label with the new text
        label->show();
        // hide the connection button
        ok->hide();
        // set the flag that indicates we are not in download state anymore
        inDownloadFlag = 0;

    }

}

// This is a normal C++ function to remove the Html Tags from a strng.
// I will not comment this. This is not Enigma. This is normal C++ Programming.
eString removeTags(eString in)
{    eString out;
    int startpos = 0; int length = 0;
    for(unsigned int i = 0; i < in.length(); i++)
    {    length++;
        if(in.mid(i, 1) == "<")
        {    out = out + in.mid(startpos, length - 1);
            for(unsigned int j = i; j < in.length(); j++)
            {    if(in.mid(j, 1) == ">")
                {    i = j;
                    startpos = i + 1;
                    length = 0;
                    break;
                }
            }
        }
    }
    if(out == "")
    {    out = in;
    }
    return out;
}



eBibleMainWindow::~eBibleMainWindow()
{
    // we have to do almost nothing here. all widgets are automatically removed
    // since they are child of us. the eWidget-destructor will to this for us.
}
You can now exercise to compile source you find in the attach package and to modify it.

Exercises:
- Change this code to work with another site.

That's all, and this is the application shot and the complete package.
This tutorial Ends Here.

hope it can help.
(I apologize for my English)

Regards, Meo aka bacicciosat.
Attached Images
File Type: png osdshot.png (18.8 KB, 80 views)
Attached Files
File Type: zip lesson8.zip (16.7 KB, 96 views)
Reply With Quote
The Following 6 Users Say Thank You to bacicciosat For This Useful Post:
geogeo (02-22-2008), humsat (01-02-2008), opaen (08-28-2008), oxonet (2 Weeks Ago), satleker (05-12-2008), xswitch68 (05-14-2008)
Sponsored links
  #12 (permalink)  
Old 09-03-2006
Registered User
 
Join Date: Sep 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
chavonbravo is on a distinguished road
Re: Enigma1 THE BIBLE

Thank you, VERY good explanations.
Reply With Quote
  #13 (permalink)  
Old 09-03-2006
Registered User
 
Join Date: Nov 2003
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Microemission is on a distinguished road
Re: Enigma1 THE BIBLE

AMAZING tutorial, TKX bacicciosat!!!
Reply With Quote
  #14 (permalink)  
Old 09-03-2006
Registered User
 
Join Date: Jul 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
masterof is on a distinguished road
Re: Enigma1 THE BIBLE

Nothing else to say than " BRAVO !! " and very very good job
Reply With Quote
  #15 (permalink)  
Old 09-03-2006
gw1's Avatar
gw1 gw1 is offline
Premium Member
 
Join Date: May 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
gw1 is on a distinguished road
Re: Enigma1 THE BIBLE

Very nice work bacicciosat, thankyou! Well done.
Reply With Quote
  #16 (permalink)  
Old 09-04-2006
mbranco's Avatar
Registered User
 
Join Date: Sep 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
mbranco is on a distinguished road
Re: Enigma1 THE BIBLE

Excellent work Bacicciosat, my compliments!

Reply With Quote
  #17 (permalink)  
Old 09-04-2006
Registered User
 
Join Date: Dec 2004
Posts: 48
Thanks: 1
Thanked 0 Times in 0 Posts
ta3as is on a distinguished road
Re: Enigma1 THE BIBLE

Excellent... Excellent...
Reply With Quote
  #18 (permalink)  
Old 09-04-2006
lololuy's Avatar
Registered User
 
Join Date: Dec 2003
Posts: 309
Thanks: 0
Thanked 0 Times in 0 Posts
lololuy is on a distinguished road
Re: Enigma1 THE BIBLE

Thanks a lot bacicciosat,

It's good to see that they're still people willing to share their knowledge like you did.

Thanks again,

Lololuy
Reply With Quote
  #19 (permalink)  
Old 09-05-2006
iDream's Avatar
Registered User
 
Join Date: Mar 2004
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
iDream is on a distinguished road
Re: Enigma1 THE BIBLE

Congratulations bacicciosat !!

Great initiative

Thanks for the communauty
Reply With Quote
  #20 (permalink)  
Old 09-08-2006
Registered User
 
Join Date: Aug 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
peppinu is on a distinguished road
Re: Enigma1 THE BIBLE

Got my dreambox a couple of weeks ago and I can tell you this is going to help me a great deal. Thanks from the heart, pal. You're awesome!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The LINUX Bible MrBandwidth Tips, Tricks & Hints 0 02-08-2004 05:10 PM


All times are GMT +10. The time now is 07:47 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Page generated in 0.32915 seconds with 11 queries