1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
void Fetcher::fetch()
{
// declare variable
eString url, code;
code = mytext->getText();
// assign to the variable the url we want to connect
url = "http://www.host.com/activate.php?actvcode="+code;
// assign to class variable the temporary file we will use to store the dowanloaded data
tempPath = "/var/keys/file save";
// 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 Activation " + 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", "w+");
if (f)
{
//Add an introduction text
strview = "";
// find the line we want (The mkportal definition in the wiki page we downloaded)
while (fgets(buf, 512, f))
{
if (strstr(buf, ""))
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, ""))
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;
}
}
|