#include "CDatabase.h" #include #include #include #include #include //#include #include #include //Added by qt3to4: #include #include #include int mystrcasecmp( const char *s1, const char *s2, int n) {// this function compares the two strings upto n characters // the case of the characters is not taken into account a == A // and then returns a zero if they are equal otherwise // none zero value for (int i=0;i < n;i++) { char ch1 = toupper( s1[ i ] ); char ch2 = toupper( s2[ i ] ); if ( ch1 != ch2 ) { return (int) (ch1 - ch2); } } return (0); } // the object which allows the progam to communicate with the PDB CDatabaseCom::CDatabaseCom(std::string user, std::string password, std::string SN){ m_SN = SN; m_qhttp = new Q3Http(NULL,"PWE_COM"); m_qhttp->setHost("wacap.unige.ch",3146); std::string tobe_encoded = user + ":" + password; Q3CString result = KCodecs::base64Encode(Q3CString(tobe_encoded.c_str())); m_encoded_password = (const char*)result; QObject::connect(m_qhttp, SIGNAL(requestStarted(int)),(QObject*)this,SLOT(Request_Started(int))); QObject::connect(m_qhttp, SIGNAL(requestFinished(int,bool)),(QObject*)this,SLOT(Read_Incoming(int,bool))); m_complete = true; } CDatabaseCom::~CDatabaseCom(){// the destructor } void CDatabaseCom::Request_Started(int){// this function is called when the request is commenced m_htmlfile.clear(); m_complete = false; } void CDatabaseCom::Read_Incoming(int , bool ){ // this function is connected to the signal // that is sent when there is data to be read int i; std::string error_str,html_line=""; QByteArray data; Q3Http::Error val = m_qhttp->error(); switch(val){ case (Q3Http::NoError):{ data = m_qhttp->readAll(); error_str = "No error occurred"; for (i=0;i<(int)data.count();i++){ html_line += data[i]; if (data[i] == '\n'){ m_htmlfile.push_back(html_line); //printf("%s",html_line.c_str()); html_line = ""; } } if(m_htmlfile.size()>0 && ( (int)(*(m_htmlfile.end()-1)).find("")!=(int)std::string::npos || (int)(*(m_htmlfile.end()-1)).find("")!=(int)std::string::npos)){ emit DownloadComplete(error_str.c_str()); m_complete = true; } return;} case (Q3Http::HostNotFound): error_str = "The host name lookup failed";break; case (Q3Http::ConnectionRefused): error_str = "The server refused the connection";break; case (Q3Http::UnexpectedClose): error_str ="The server closed the connection unexpectedly";break; case (Q3Http::InvalidResponseHeader): error_str ="The server sent an invalid response header";break; case (Q3Http::WrongContentLength): error_str ="The client could not read the content correctly because an error with respect to the content length occurred";break; case (Q3Http::Aborted): error_str ="The request was aborted with abort()";break; default: case (Q3Http::UnknownError): error_str ="An error other than those specified above occurred";break; } emit DownloadComplete(error_str.c_str()); m_complete = true; } void CDatabaseCom::Write_Socket(std::string command){ // the user has press the button and we will send the query Q3HttpRequestHeader http_header(command.c_str()); m_id = m_qhttp->request(http_header); } std::vector CDatabaseCom::getStaveSNs(QApplication &app){ std::vector retvec; m_complete = false; std::string checkStr = m_SN; checkStr.erase(11,14); if(checkStr=="20212000018" || checkStr=="20212001120") // get sectors for disk or bistaves for half-shell GetHtmlAss(false); else GetHtml(); while(!m_complete ) app.processEvents(); // QString inError = error_msg; // if("No error occurred"!=inError){ // QApplication::restoreOverrideCursor(); // QMessageBox::warning(this,"tmperr","Error in html reading:\n"+inError); // return; // } // if(m_cdb->m_htmlfile.size()==0){ // QApplication::restoreOverrideCursor(); // QMessageBox::warning(this,"tmperr","Error in html reading:\nno data returned"); // return; // } int pos; for(unsigned int i=0;i"); searchp.erase(0,pos+9); pos = searchp.find(""); searchp.erase(pos,searchp.length()-pos); //printf("object is of type %s\n",searchp.c_str()); break; } } for(unsigned int j=0;j 76); if ( insertLFs ) out_len += ((out_len-1)/76); int count = 0; out.resize( out_len ); out[didx] = '\n'; // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion if ( len > 1 ) { while (sidx < (int)len-2) { if ( insertLFs ) { if ( count && (count%76) == 0 ) { out[didx++] = '\n'; } count += 4; } out[didx++] = Base64EncMap[(data[sidx] >> 2) & 077]; out[didx++] = Base64EncMap[((data[sidx+1] >> 4) & 017) | ((data[sidx] << 4) & 077)]; out[didx++] = Base64EncMap[((data[sidx+2] >> 6) & 003) | ((data[sidx+1] << 2) & 077)]; out[didx++] = Base64EncMap[data[sidx+2] & 077]; sidx += 3; } } if (sidx < (int)len) { if ( insertLFs && (count > 0) && (count%76) == 0 ) out[didx++] = '\n'; out[didx++] = Base64EncMap[(data[sidx] >> 2) & 077]; if (sidx < (int)len-1) { out[didx++] = Base64EncMap[((data[sidx+1] >> 4) & 017) | ((data[sidx] << 4) & 077)]; out[didx++] = Base64EncMap[(data[sidx+1] << 2) & 077]; } else { out[didx++] = Base64EncMap[(data[sidx] << 4) & 077]; } } // Add padding while (didx < (int)out.size()) { out[didx] = '='; didx++; } } Q3CString KBase64::base64Decode( const Q3CString& str ) { if ( str.isEmpty() ) return ""; QByteArray in( str.length() ); memcpy( in.data(), str.data(), str.length() ); return base64Decode( in ); } Q3CString KBase64::base64Decode( const QByteArray& in ) { QByteArray out; base64Decode( in, out ); return Q3CString( out.data(), out.size()+1 ); } void KBase64::base64Decode( const QByteArray& in, QByteArray& out ) { out.resize(0); if ( in.isEmpty() ) return; unsigned int count = 0; unsigned int len = in.size(), tail = len; const char* data = in.data(); // Deal with possible *nix "BEGIN" marker!! while ( count < len && (data[count] == '\n' || data[count] == '\r' || data[count] == '\t' || data[count] == ' ') ) count++; // int mystrcasecmp( const char *s1, const char *s2, int n) // if ( strncasecmp(data+count, "begin", 5) == 0 ) if ( mystrcasecmp(data+count, "begin", 5) == 0 ) { count += 5; while ( count < len && data[count] != '\n' && data[count] != '\r' ) count++; while ( count < len && (data[count] == '\n' || data[count] == '\r') ) count ++; data += count; tail = (len -= count); } // Find the tail end of the actual encoded data even if // there is/are trailing CR and/or LF. while ( data[tail-1] == '=' || data[tail-1] == '\n' || data[tail-1] == '\r' ) if ( data[--tail] != '=' ) len = tail; int outIdx = 0; // unsigned int outIdx = 0; out.resize( (count=len) ); // for (unsigned int idx = 0; idx < count; idx++) for (int idx = 0; idx < (int)count; idx++) { // Adhere to RFC 2045 and ignore characters // that are not part of the encoding table. char ch = data[idx]; if ((ch > 47 && ch < 58) || (ch > 64 && ch < 91) || (ch > 96 && ch < 123) || ch == '+' || ch == '/' || ch == '=') { out[outIdx++] = (char)Base64DecMap[(int)ch]; } else { len--; tail--; } } // kdDebug() << "Tail size = " << tail << ", Length size = " << len << endl; // 4-byte to 3-byte conversion len = (tail>(len/4)) ? tail-(len/4) : 0; // unsigned int sidx = 0, didx = 0; int sidx = 0, didx = 0; if ( len > 1 ) { while (didx < (int)len-2) { out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx+1] >> 4) & 003)); out[didx+1] = (((out[sidx+1] << 4) & 255) | ((out[sidx+2] >> 2) & 017)); out[didx+2] = (((out[sidx+2] << 6) & 255) | (out[sidx+3] & 077)); sidx += 4; didx += 3; } } if (didx < (int)len) out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx+1] >> 4) & 003)); if (++didx < (int)len ) out[didx] = (((out[sidx+1] << 4) & 255) | ((out[sidx+2] >> 2) & 017)); // Resize the output buffer if ( len == 0 || (int)len < (int)out.size() ) out.resize(len); } RDBManager::RDBManager(QApplication *app) : m_app(app){ m_html_line = ""; m_qhttp = 0; m_fout = 0; } RDBManager::~RDBManager(){ } std::vector RDBManager::getRootUrls(QString modSN, QString measType){ std::vector retvec; QString searchString = modSN+"/"+measType; QString host = "atlas1.ge.infn.it"; QString url = "/pixelprod/ListFiles2.php?Site=ALL&Item="+searchString; QString rname, totalRname, path_line, old_path="nn"; getRawData(url,host); // wait for request to finish while(m_qhttp!=0){ if(m_app!=0) m_app->processEvents(); } int pos=-1, pos2=-1, offs=0, pos3=-1; QString html_line = m_html_line; pos3 = html_line.find(searchString+"/"); while(pos3>=0){ path_line = html_line; path_line.remove(0,pos3); path_line.remove(searchString.length()+3, path_line.length()-(searchString.length()+3)); if(path_line!=old_path){ url = "/pixelprod/ListFiles2.php?Site=ALL&Item=" + path_line; m_html_line=""; getRawData(url,host); // wait for request to finish while(m_qhttp!=0){ if(m_app!=0) m_app->processEvents(); } QString tmp_url = m_html_line; pos = tmp_url.find(".root"); while(pos>=0){ for(offs=10;offsprocessEvents(); } int pos=-1, pos2=-1, offs=0; pos = m_html_line.find(searchString+"/"); if(pos>=0){ m_html_line.remove(0,pos); m_html_line.remove(searchString.length()+3, m_html_line.length()-(searchString.length()+3)); searchString = m_html_line; url = "/pixelprod/ListFiles2.php?Site=ALL&Item="+searchString; getRawData(url,host); // wait for request to finish while(m_qhttp!=0){ if(m_app!=0) m_app->processEvents(); } pos = m_html_line.find(".root"); if(pos>=0){ for(offs=10;offs=0){ host = m_html_line.left(pos); url = m_html_line.right(m_html_line.length()-pos); if(retainName){ rname = url; pos = rname.find("/"); pos2 = pos; while(pos>=0){ pos2 = pos; pos = rname.find("/",pos+1); } rname.remove(0,pos2+1); // printf("host: %s\nURL: %s\nFilename: %s\n",host.latin1(),url.latin1(),rname.latin1()); rname = outPath+"/"+rname; } else rname = outPath; getRawData(url,host,rname); // wait for request to finish while(m_qhttp!=0){ if(m_app!=0) m_app->processEvents(); } return 0; } else return -4; } else return -3; } else return -2; } else return -1; } void RDBManager::getRootFromUrl(QString input_url, QString outPath){ QString host, url; int pos = input_url.find("/"); if(pos>=0){ host = input_url.left(pos); url = input_url.right(input_url.length()-pos); getRawData(url,host,outPath); // wait for request to finish while(m_qhttp!=0){ if(m_app!=0) m_app->processEvents(); } } return; } void RDBManager::getRawData(QString url, QString host, QString path){ delete m_qhttp; m_qhttp = new Q3Http(NULL,"RDB_COM"); m_qhttp->setHost(host); QObject::connect(m_qhttp, SIGNAL(requestFinished(int,bool)),(QObject*)this,SLOT(readRawData())); QObject::connect(m_qhttp, SIGNAL(done(bool)),(QObject*)this,SLOT(procRawData())); // let's send the request m_html_line=""; if(path!=QString::null){ m_fout = new QFile(path); m_fout->open(QIODevice::WriteOnly); } m_qhttp->get(url,m_fout); } void RDBManager::readRawData(){ Q3Http::Error val = m_qhttp->error(); QString error_str; QByteArray data; switch(val){ case (Q3Http::NoError):{ if(m_fout==0){ data = m_qhttp->readAll(); for (int i=0;i<(int)data.count();i++){ m_html_line += data[i]; } } return ; } case (Q3Http::HostNotFound): error_str = "The host name lookup failed";break; case (Q3Http::ConnectionRefused): error_str = "The server refused the connection";break; case (Q3Http::UnexpectedClose): error_str ="The server closed the connection unexpectedly";break; case (Q3Http::InvalidResponseHeader): error_str ="The server sent an invalid response header";break; case (Q3Http::WrongContentLength): error_str ="The client could not read the content correctly because an error with respect to the content length occurred";break; case (Q3Http::Aborted): error_str ="The request was aborted with abort()";break; default: case (Q3Http::UnknownError): error_str ="An unknown error occurred";break; } } void RDBManager::procRawData(){ delete m_qhttp; m_qhttp = 0; delete m_fout; m_fout = 0; }