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.
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.
+--------------------------------------------------------------------------
| 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. }
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.