• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

keduca

fileread.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           fileread.cpp  -  description
00003                              -------------------
00004     begin                : Wed May 23 2001
00005     copyright            : (C) 2001 by Javier Campos
00006     email                : javi@asyris.org
00007 ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "fileread.h"
00019 
00020 #include <assert.h>
00021 
00022 #include <kio/netaccess.h>
00023 #include <kstandarddirs.h>
00024 #include <kmimetype.h>
00025 #include <kdebug.h>
00026 #include <ktempfile.h>
00027 
00028 #include <qfileinfo.h>
00029 #include <qpixmap.h>
00030 #include <qbuffer.h>
00031 
00032 FileRead::FileRead( QObject *parent, const char *name )
00033     :QObject(parent, name),
00034      _changed(false),
00035      _tmpfile(0)
00036 {
00037 }
00038 
00039 FileRead::~FileRead()
00040 {
00041 }
00042 
00043 bool FileRead::openFile(const KURL &url) {
00044     QString tmpFile;
00045     bool returnval=false;
00046     if( KIO::NetAccess::download( url, tmpFile, 0 ) )
00047     {
00048         returnval=loadFile( tmpFile );
00049         if (returnval) {
00050             _currentURL = url;
00051             kdDebug()<<"... load successful: "<<_currentURL.url()<<endl;
00052         }
00053 
00054         KIO::NetAccess::removeTempFile( tmpFile );
00055     }else
00056         kdDebug()<<"FileRead::openFile(): download NOT successful: "<<url.url()<<endl;
00057 
00058     return returnval;
00059 }
00060 
00061 bool FileRead::loadFile(const QString &filename)
00062 {
00063     QDomDocument doc("document.xml");
00064 
00065     KMimeType::Ptr type = KMimeType::findByFileContent(filename);
00066 
00067     kdDebug() << "FileRead::loadFile(): MIME-Type is " << type->name() << endl;
00068 
00069     QFile file(filename);
00070 
00071     if(!file.open(IO_ReadOnly))
00072     {
00073         return false;
00074     }
00075 
00076     if (type->name() == "text/html") // Non-compressed files are recognized as HTML...
00077     {
00078         doc.setContent(&file);
00079     }
00080     else
00081     {
00082         doc.setContent(qUncompress(file.readAll()));
00083     }
00084 
00085      QDomElement docElem = doc.documentElement();
00086    if( (doc.doctype().isNull()) || (doc.doctype().name() != "educa") )   { 
00087         file.close(); 
00088     return false;
00089     }
00090 
00091     QDomNode n = docElem.firstChild();
00092 
00093     QDomNodeList dnList = n.childNodes();
00094     for( unsigned int i = 0; i < dnList.count(); ++i)
00095     {
00096         // ------------------- INFORMATION BODY -----------------------
00097         QDomElement element = dnList.item(i).toElement();
00098         if( element.tagName() == "default" || element.tagName() == "author" )
00099         {
00100             if( element.tagName() == "default" ) {  _header.insert( "image", element.attribute( "image", "default.png" ) ); }
00101             if( element.tagName() == "author" ) {
00102                 QDomNodeList AuthordnList = element.childNodes();
00103                 for( unsigned int i = 0; i < AuthordnList.count(); ++i) {
00104                     QDomElement authorelement = AuthordnList.item(i).toElement();
00105                     _header.insert( authorelement.tagName(), authorelement.text() );
00106                 }
00107             }
00108         } else {
00109             _header.insert( element.tagName(), element.text() );
00110         }
00111     }
00112 
00113     n = n.nextSibling();
00114 
00115     dnList = n.childNodes();
00116     for( unsigned int i = 0; i < dnList.count(); ++i)
00117     {
00118         insertQuestion();
00119         // --------------------- QUESTION ATTRIBUTE------------------------
00120         QDomElement elementNODE = dnList.item(i).toElement();
00121         setQuestion( QF_TYPE,       elementNODE.attribute( "type", "1" ).toInt() );
00122         setQuestion( QF_PICTURE,    elementNODE.attribute( "image", "" ) );
00123         setQuestion( QF_TIME,       elementNODE.attribute( "time", "0" ).toInt() );
00124         setQuestion( QF_POINTS,     elementNODE.attribute( "points", "0" ).toInt() );
00125 
00126         QDomNodeList quList = elementNODE.childNodes();
00127         for( unsigned int x = 0; x < quList.count(); ++x)
00128         {
00129             // --------------------- QUESTION AND RESPONSES------------------
00130             QDomElement element = quList.item(x).toElement();
00131             if( element.tagName() == "text" )       setQuestion( QF_TEXT, element.text() );
00132             if( element.tagName() == "true" )         setAnswer( element.text(), true, element.attribute( "points", "0" ).toInt() );
00133             if( element.tagName() == "false" )      setAnswer( element.text(), false,element.attribute( "points", "0" ).toInt() );
00134             if( element.tagName() == "tip" )            setQuestion( QF_TIP, element.text() );
00135             if( element.tagName() == "explain" )    setQuestion( QF_EXPLAIN, element.text() );
00136         }
00137     }
00138 
00139     n = n.nextSibling();
00140 
00141     dnList = n.childNodes();
00142 
00143     if( dnList.count() > 0 )
00144       {
00145       for( unsigned int i = 0; i < dnList.count(); ++i)
00146         {
00147         insertResult();
00148         // --------------------- QUESTION ATTRIBUTE------------------------
00149         QDomElement elementNODE = dnList.item(i).toElement();
00150         setResult( RS_TEXT,         elementNODE.text() );
00151         setResult( RS_PICTURE,  elementNODE.attribute( "image", "" ) );
00152         setResult( RS_MIN,        elementNODE.attribute( "min", "0" ).toInt() );
00153         setResult( RS_MAX,      elementNODE.attribute( "max", "0" ).toInt() );
00154         }
00155       }
00156     
00157     file.close();
00158 
00159     refreshData();
00160     
00161     _changed=false;
00162     return true;
00163 }
00164 
00165 void FileRead::setQuestion( QuestionField field, const QString& text)
00166 {
00167 // QF_text, QF_picture, QF_type, QF_time, QF_tip, QF_explain
00168     switch( field )
00169     {
00170     case QF_TEXT:
00171         (*_recordQuestions).text = text;
00172         break;
00173     case QF_PICTURE:
00174         (*_recordQuestions).picture = text;
00175         break;
00176     case QF_TIP:
00177         (*_recordQuestions).tip = text;
00178         break;
00179     case QF_EXPLAIN:
00180         (*_recordQuestions).explain = text;
00181         break;
00182     default:
00183         kdDebug()<<"FileRead::setQuestion(QuestionField field, QString text) called for not handled field value "<<field <<endl;
00184         break;
00185     }
00186     _changed=true;
00187 }
00188 
00189 void FileRead::setQuestion( QuestionField field, int value )
00190 {
00191     switch( field )
00192     {
00193     case QF_TYPE:
00194         (*_recordQuestions).type = value;
00195         break;
00196     case QF_TIME:
00197         (*_recordQuestions).time = value;
00198         break;
00199     case QF_POINTS:
00200         (*_recordQuestions).points = value;
00201         break;
00202     default:
00203         kdDebug()<<"FileRead::setQuestion(QuestionField field, int value) called for not handled field value "<<field <<endl;
00204         break;
00205     }
00206     _changed=true;
00207 }
00208 
00209 void FileRead::setResult( ResultField field, const QString& text)
00210 {
00211 // RS_text, QF_picture
00212     switch( field )
00213     {
00214     case RS_TEXT:
00215         (*_recordResults).text = text;
00216         break;
00217     case RS_PICTURE:
00218         (*_recordResults).picture = text;
00219         break;
00220     default:
00221         kdDebug()<<"FileRead::setResult(ResultField field, QString text) called for not handled field value "<<field <<endl;
00222         break;
00223     }
00224     _changed=true;
00225 }
00226 
00227 void FileRead::setResult( ResultField field, int value )
00228 {
00229     switch( field )
00230     {
00231     case RS_MIN:
00232         (*_recordResults).min = value;
00233         break;
00234     case RS_MAX:
00235         (*_recordResults).max = value;
00236         break;
00237     default:
00238         kdDebug()<<"FileRead::setResultInt(ResultField field, int value) called for not handled field value "<<field <<endl;
00239         break;
00240     }
00241     _changed=true;
00242 }
00243 
00244 void FileRead::setAnswer( const QString& text, bool value, int points)
00245 {
00246     Answers tmpAnswers;
00247 
00248     tmpAnswers.text = text;
00249     tmpAnswers.value = value;
00250     tmpAnswers.points = points;
00251 
00252     (*_recordQuestions).listAnswers.append( tmpAnswers );
00253     _changed=true;
00254 }
00255 
00256 void FileRead::insertQuestion()
00257 {
00258     Questions tempQuestions;
00259     tempQuestions.text = "";
00260     _listQuestions.append( tempQuestions );
00261     recordLast();
00262     _changed=true;
00263 }
00264 
00265 void FileRead::insertResult()
00266 {
00267     Results tempResults;
00268     tempResults.text = "";
00269     _listResults.append( tempResults );
00270     recordResultLast();
00271     _changed=true;
00272 }
00273 
00274 void FileRead::recordFirst()
00275 {
00276     if( _fileEOF = true ) _fileEOF = false;
00277     if( _fileBOF = false ) _fileBOF = true;
00278     _recordQuestions = _listQuestions.begin();
00279 }
00280 
00281 void FileRead::recordLast()
00282 {
00283     if( _fileBOF = true )   _fileBOF = false;
00284     if( _fileEOF = false ) _fileEOF = true;
00285     _recordQuestions = _listQuestions.end();
00286     --_recordQuestions;
00287 }
00288 
00289 void FileRead::recordNext()
00290 {
00291     ++_recordQuestions;
00292     if( _recordQuestions == _listQuestions.end() )
00293       {
00294       _fileEOF = true;
00295       --_recordQuestions;
00296       }
00297     else
00298       if( _fileBOF = true ) _fileBOF = false;
00299 }
00300 
00301 void FileRead::recordPrevious()
00302 {
00303     if( _recordQuestions == _listQuestions.begin() )
00304       _fileBOF = true;
00305     else
00306     {
00307       if( _fileEOF = true ) _fileEOF = false;
00308       --_recordQuestions;
00309     }
00310 }
00311 
00312 void FileRead::recordResultFirst()
00313 {
00314     if( _fileResultEOF = true ) _fileResultEOF = false;
00315     if( _fileResultBOF = false ) _fileResultBOF = true;
00316     _recordResults = _listResults.begin();
00317 }
00318 
00319 void FileRead::recordResultLast()
00320 {
00321     if( _fileResultBOF = true )     _fileResultBOF = false;
00322     if( _fileResultEOF = false ) _fileResultEOF = true;
00323     _recordResults = _listResults.end();
00324     --_recordResults;
00325 }
00326 
00327 void FileRead::recordResultNext()
00328 {
00329     ++_recordResults;
00330     if( _recordResults == _listResults.end() )
00331     {
00332         _fileResultEOF = true;
00333         --_recordResults;
00334     }
00335     else
00336     {
00337         if( _fileBOF = true ) _fileBOF = false;
00338     }
00339 }
00340 
00341 void FileRead::recordResultPrevious()
00342 {
00343     if( _recordResults == _listResults.begin() )
00344     {
00345         _fileResultBOF = true;
00346     }
00347     else
00348     {
00349         if( _fileResultEOF = true ) _fileResultEOF = false;
00350         --_recordResults;
00351     }
00352 }
00353 
00354 void FileRead::recordAnswerFirst()
00355 {
00356     if( _fileAnswerEOF = true ) _fileAnswerEOF = false;
00357     if( _fileAnswerBOF = false ) _fileAnswerBOF = true;
00358     (*_recordQuestions).recordAnswers = (*_recordQuestions).listAnswers.begin();
00359 }
00360 
00361 void FileRead::recordAnswerLast()
00362 {
00363     if( _fileAnswerBOF = true )     _fileAnswerBOF = false;
00364     if( _fileAnswerEOF = false ) _fileAnswerEOF = true;
00365     (*_recordQuestions).recordAnswers = (*_recordQuestions).listAnswers.end();
00366     --(*_recordQuestions).recordAnswers;
00367 }
00368 
00369 void FileRead::recordAnswerNext()
00370 {
00371     ++(*_recordQuestions).recordAnswers;
00372     if( (*_recordQuestions).recordAnswers == (*_recordQuestions).listAnswers.end() )
00373     {
00374         _fileAnswerEOF = true;
00375         --(*_recordQuestions).recordAnswers;
00376     }
00377     else
00378     {
00379         if( _fileAnswerBOF = true ) _fileAnswerBOF = false;
00380     }
00381 }
00382 
00383 void FileRead::recordAnswerPrevious()
00384 {
00385     if( (*_recordQuestions).recordAnswers == (*_recordQuestions).listAnswers.begin() )
00386     {
00387         _fileBOF = true;
00388     }
00389     else
00390     {
00391         if( _fileAnswerEOF = true ) _fileAnswerEOF = false;
00392         --(*_recordQuestions).recordAnswers;
00393     }
00394 }
00395 
00396 void FileRead::recordAnswerAt( unsigned int index)
00397 {
00398     (*_recordQuestions).recordAnswers = (*_recordQuestions).listAnswers.begin();
00399     for( unsigned int i = 0; i < index; ++i)
00400       ++(*_recordQuestions).recordAnswers;
00401 }
00402 
00403 unsigned int FileRead::recordAnswerCount()
00404 {
00405   return (*_recordQuestions).listAnswers.count();
00406 }
00407 
00408 QString FileRead::getQuestion( QuestionField field )
00409 {
00410 // QF_text, QF_picture, QF_type, QF_time, QF_tip, QF_explain
00411     switch( field )
00412     {
00413     case QF_TEXT:
00414         return (*_recordQuestions).text;
00415         break;
00416     case QF_PICTURE:
00417 //        return getPictureLocal( (*_recordQuestions).picture );
00418         return( (*_recordQuestions).picture );
00419         break;
00420     case QF_POINTS:
00421         return QString().setNum( (*_recordQuestions).points );
00422         break;
00423     case QF_TIME:
00424         return QString().setNum( (*_recordQuestions).time );
00425         break;
00426     case QF_TIP:
00427         return (*_recordQuestions).tip;
00428         break;
00429     case QF_EXPLAIN:
00430         return (*_recordQuestions).explain;
00431         break;
00432     default:
00433         kdDebug()<<"FileRead::getQuestion() called for not handled field value "<<field <<endl;
00434         return "";
00435         break;
00436     }
00437 }
00438 
00439 int FileRead::getQuestionInt( QuestionField field )
00440 {
00441     switch( field )
00442     {
00443     case QF_TYPE:
00444         return (*_recordQuestions).type;
00445         break;
00446     case QF_TIME:
00447         return (*_recordQuestions).time;
00448         break;
00449     case QF_POINTS:
00450         return (*_recordQuestions).points;
00451         break;
00452     default:
00453         kdDebug()<<"FileRead::getQuestionInt() called for not handled field value "<<field <<endl;
00454         return 0;
00455     }
00456 }
00457 
00458 QString FileRead::getAnswer( AnswerField field )
00459 {
00460     // AField { AF_text, AF_value, AF_picture, AF_point };
00461     switch( field )
00462     {
00463     case AF_TEXT:
00464         return (*(*_recordQuestions).recordAnswers).text;
00465         break;
00466 //      case AF_VALUE:
00467 //          (*(*_recordQuestions).RecordAnswers).Value ? return i18n("True") : return i18n("False");
00468 //          break;
00469     case AF_POINT:
00470         return QString().setNum( (*(*_recordQuestions).recordAnswers).points );
00471         break;
00472     default:
00473         kdDebug()<<"FileRead::getAnswer() called for not handled field value "<<field <<endl;
00474         return "";
00475     }
00476 }
00477 
00478 bool FileRead::getAnswerValue()
00479 {
00480     return (*(*_recordQuestions).recordAnswers).value;
00481 }
00482 
00483 int FileRead::getAnswerPoints()
00484 {
00485     return (*(*_recordQuestions).recordAnswers).points;
00486 }
00487 
00488 QString FileRead::getResult( ResultField field )
00489 {
00490     switch( field )
00491     {
00492     case RS_TEXT:
00493         return (*_recordResults).text;
00494         break;
00495     case RS_PICTURE:
00496         return( (*_recordResults).picture );
00497         break;
00498     case RS_MIN:
00499         return QString().setNum( (*_recordResults).min );
00500         break;
00501     case RS_MAX:
00502         return QString().setNum( (*_recordResults).max );
00503         break;
00504     default:
00505         kdDebug()<<"FileRead::getResult() called for not handled field value "<<field <<endl;
00506         return "";
00507         break;
00508     }
00509 }
00510 
00511 int FileRead::getResultInt( ResultField field )
00512 {
00513     switch( field )
00514     {
00515     case RS_MIN:
00516         return (*_recordResults).min;
00517         break;
00518     case RS_MAX:
00519         return (*_recordResults).max;
00520         break;
00521     default:
00522         kdDebug()<<"FileRead::getResult() called for not handled field value "<<field <<endl;
00523         return 0;
00524         break;
00525     }
00526 }
00527 
00528 bool FileRead::recordEOF()
00529 {
00530     return _fileEOF;
00531 }
00532 
00533 bool FileRead::recordBOF()
00534 {
00535     return _fileBOF;
00536 }
00537 
00538 bool FileRead::recordResultEOF()
00539 {
00540     return _fileResultEOF;
00541 }
00542 
00543 bool FileRead::recordResultBOF()
00544 {
00545     return _fileResultBOF;
00546 }
00547 
00548 bool FileRead::recordAnswerEOF()
00549 {
00550     return _fileAnswerEOF;
00551 }
00552 
00553 bool FileRead::recordAnswerBOF()
00554 {
00555     return _fileAnswerBOF;
00556 }
00557 
00558 QString FileRead::getPicture()
00559 {
00560   QString picture;
00561 
00562     if( !getQuestion(QF_PICTURE).isEmpty() )
00563       picture = getQuestion(QF_PICTURE);
00564       else if( !(_header["image"]).isEmpty() )
00565         picture = _header["image"];
00566           else
00567             return locate("data","keduca/pics/default.png");
00568 
00569   if( _currentURL.isLocalFile() && !KURL(picture).isValid() )
00570     {
00571     if( !QFileInfo(picture).exists() )
00572       picture = _currentURL.directory(false,true) + picture;
00573     } else if( !_currentURL.isLocalFile() && !KURL(picture).isValid() )
00574        picture = _currentURL.protocol() + "://" + _currentURL.host() + _currentURL.directory(false,true) + picture;
00575     
00576   kdDebug()<< picture <<endl;
00577             
00578   return picture;
00579 }
00580 
00581 QPixmap FileRead::getPicturePixmap()
00582 {
00583   
00584   KURL picture ( getPicture() );
00585   QPixmap pict;
00586     
00587   if( KIO::NetAccess::download( picture, _tmpfileImage, 0 ) )
00588   {
00589     kdDebug()<<"... load successful: "<< _tmpfileImage <<endl;
00590     pict = QPixmap( _tmpfileImage );
00591     KIO::NetAccess::removeTempFile( _tmpfileImage );
00592   }
00593   else
00594   {
00595     kdDebug()<<"FileRead::openFile(): download NOT successful: "<< _tmpfileImage <<endl;
00596     pict = NULL;
00597   }
00598 
00599   return pict;
00600 }
00601 
00602 void FileRead::clearAnswers()
00603 {
00604     ((*_recordQuestions).listAnswers).clear();
00605     _changed=true;
00606 }
00607 
00608 void FileRead::recordDelete()
00609 {
00610     _listQuestions.remove( _recordQuestions );
00611     _changed=true;
00612 }
00613 
00614 void FileRead::recordSwap( bool moveup )
00615 {
00616     Questions listTMP;
00617     Questions listNEW;
00618 
00619     listTMP = (*_recordQuestions);
00620 
00621     if( moveup )
00622     {
00623         recordPrevious();
00624         listNEW = (*_recordQuestions);
00625         (*_recordQuestions) = listTMP;
00626         recordNext();
00627         (*_recordQuestions) = listNEW;
00628     }
00629     else
00630     {
00631         recordNext();
00632         listNEW = (*_recordQuestions);
00633         (*_recordQuestions) = listTMP;
00634         recordPrevious();
00635         (*_recordQuestions) = listNEW;
00636     }
00637     _changed=true;
00638 }
00639 
00640 bool FileRead::saveFile( const KURL &url, bool copyimages, bool saveCompressed )
00641 {
00642     if (url.isValid())
00643     {
00644         _currentURL = url;
00645     }
00646     kdDebug()<<"FileRead::saveFile() to "<<_currentURL.url()<<endl;
00647     // Local file
00648     if ( _currentURL.isLocalFile() )
00649     {
00650         if ( _tmpfile != 0 ) // get rid of a possible temp file first
00651         {              // (happens if previous _currentURL was remote)
00652              _tmpfile->unlink();
00653              delete _tmpfile;
00654              _tmpfile = 0;
00655         }
00656         if( saveFile(_currentURL.path(), copyimages, saveCompressed) ) {
00657             emit completed();
00658             emit setWindowCaption( _currentURL.prettyURL() );
00659             return true;
00660         }
00661     }
00662     else
00663     { // Remote file
00664         // We haven't saved yet, or we did but locally - provide a temp file
00665         if ( _tmpfile == 0 )
00666         {
00667              _tmpfile = new KTempFile;
00668         }
00669         // otherwise, we already had a temp file
00670         if( saveFile(_tmpfile->name(), copyimages, saveCompressed) ) {
00671             // upload the file
00672              KIO::Job * job = KIO::file_copy( KURL::fromPathOrURL( _tmpfile->name() ), _currentURL, -1, true /*overwrite*/ );
00673              connect( job, SIGNAL( result( KIO::Job * ) ), this, SLOT( slotUploadFinished (KIO::Job *) ) );
00674             return true;
00675         }
00676     }
00677 
00678     // Save local file and upload local file
00679     return false;
00680 }
00681 
00682 bool FileRead::saveFile( const QString &filename, bool copyimages, bool saveCompressed )
00683 {
00684     QDomDocument doc("document.xml");
00685     QTextStream stream;
00686     QString line;
00687     QByteArray data;
00688     QBuffer buffer(data);
00689     QFile file(filename);
00690     QStringList copyJOB;
00691 
00692     stream.setDevice(&buffer);
00693 
00694     if ( (!file.open(IO_WriteOnly)) || (!buffer.open(IO_WriteOnly)) )
00695     {
00696         return false;
00697     }
00698     /*
00699     stream.setDevice(&file);
00700 
00701     if(!file.open(IO_WriteOnly))
00702     {
00703         // No puede abrir la base
00704         return false;
00705     }
00706     */
00707 
00708     QString head( "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><!DOCTYPE educa>" );
00709     doc.setContent( head );
00710 
00711     QDomElement Root = doc.createElement("Document");
00712     doc.appendChild( Root );
00713 
00714     QDomElement NodeInfo = doc.createElement("Info");
00715     Root.appendChild( NodeInfo );
00716 
00717     insertXML( doc, NodeInfo, "title", _header["title"] );
00718     insertXML( doc, NodeInfo, "category", _header["category"] );
00719     insertXML( doc, NodeInfo, "type", _header["type"] );
00720     insertXML( doc, NodeInfo, "level", _header["level"] );
00721     insertXML( doc, NodeInfo, "language", _header["language"] );
00722 
00723     if( !(_header["image"]).isEmpty() )
00724     {
00725         QDomElement Nodedefault = doc.createElement("default");
00726 
00727         if( copyimages  )
00728         {
00729             copyJOB.append( _header["image"] );
00730             Nodedefault.setAttribute( "image", QFileInfo(_header["image"]).fileName() );
00731         } else {
00732             Nodedefault.setAttribute( "image", _header["image"]);
00733         }
00734         NodeInfo.appendChild( Nodedefault );
00735     }
00736 
00737     if( !_header["name"].isEmpty() || !_header["email"].isEmpty() || !_header["www"].isEmpty() )
00738     {
00739         QDomElement Nodeauthor = doc.createElement("author");
00740         NodeInfo.appendChild( Nodeauthor );
00741         if( !_header["name"].isEmpty() )    insertXML( doc, Nodeauthor, "name", _header["name"] );
00742         if( !_header["email"].isEmpty() )   insertXML( doc, Nodeauthor, "email", _header["email"] );
00743         if( !_header["www"].isEmpty() )     insertXML( doc, Nodeauthor, "www", _header["www"] );
00744     }
00745 
00746     QDomElement NodeData = doc.createElement("Data");
00747     Root.appendChild( NodeData );
00748 
00749     recordFirst();
00750     while ( !recordEOF() )
00751     {
00752         QDomElement question = doc.createElement("question");
00753         if( !getQuestion( QF_PICTURE ).isEmpty() )
00754         {
00755             if( copyimages )
00756             {
00757                 copyJOB.append( getQuestion( QF_PICTURE ) );
00758                 question.setAttribute("image", QFileInfo( getQuestion( QF_PICTURE ) ).fileName() );
00759             } else {
00760                 question.setAttribute("image",  getQuestion( QF_PICTURE ) );
00761             }
00762         }
00763         question.setAttribute( "type", getQuestionInt( QF_TYPE ) );
00764         if( getQuestionInt( QF_POINTS ) > 0 ) question.setAttribute( "points", getQuestion( QF_POINTS ) );
00765         if( getQuestionInt( QF_TIME ) > 0 ) question.setAttribute( "time", getQuestion( QF_TIME ) );
00766         insertXML( doc, question, "text", getQuestion( QF_TEXT ) );
00767 
00768         recordAnswerFirst();
00769         while( !recordAnswerEOF() )
00770         {
00771             if( getAnswerValue() )
00772             {
00773                 QDomElement domELEMENT = doc.createElement( "true" );
00774                 if( getAnswerPoints() > 0 ) domELEMENT.setAttribute("points", getAnswerPoints() );
00775                 QDomText DATAelement = doc.createTextNode( getAnswer( AF_TEXT ) );
00776                 question.appendChild( domELEMENT );
00777                 domELEMENT.appendChild( DATAelement );
00778             }   else {
00779 //              insertXML( doc, question, "false", getAnswer( AF_text ) );
00780                 QDomElement domELEMENT = doc.createElement( "false" );
00781                 if( getAnswerPoints() > 0 ) domELEMENT.setAttribute("points", getAnswerPoints() );
00782                 QDomText DATAelement = doc.createTextNode( getAnswer( AF_TEXT ) );
00783                 question.appendChild( domELEMENT );
00784                 domELEMENT.appendChild( DATAelement );
00785             }
00786             recordAnswerNext();
00787         };
00788 
00789         if( !getQuestion( QF_TIP ).isEmpty() ) insertXML( doc, question, "tip", getQuestion( QF_TIP ) );
00790         if( !getQuestion( QF_EXPLAIN ).isEmpty() ) insertXML( doc, question, "explain", getQuestion( QF_EXPLAIN ) );
00791 
00792         NodeData.appendChild( question );
00793         recordNext();
00794     }
00795 
00796     doc.save( stream, 4);
00797     buffer.close();
00798     if ( saveCompressed )
00799         file.writeBlock(qCompress(data));
00800     else
00801         file.writeBlock(data);
00802     file.close();
00803 
00804     if( copyimages == true && copyJOB.count() > 0 )
00805     {
00806         KURL::List  KurlLIST( copyJOB );
00807         KIO::CopyJob *copyjob;
00808 
00809         copyjob = KIO::copy( KurlLIST, KURL( _currentURL.directory(false,true) ), true);
00810     }
00811     _changed=false;
00812     return true;
00813 
00814 }
00815 
00817 bool FileRead::saveResults( const KURL &url, const QString &results )
00818 {
00819     if (url.isValid())
00820     {
00821         _currentURL = url;
00822     }
00823     kdDebug()<<"FileRead::saveResults() to "<<_currentURL.url()<<endl;
00824     // Local file
00825     if ( _currentURL.isLocalFile() )
00826     {
00827         if ( _tmpfile != 0 ) // get rid of a possible temp file first
00828         {              // (happens if previous _currentURL was remote)
00829              _tmpfile->unlink();
00830              delete _tmpfile;
00831              _tmpfile = 0;
00832         }
00833         if( saveResults(_currentURL.path(), results) ) {
00834             emit completed();
00835             emit setWindowCaption( _currentURL.prettyURL() );
00836             return true;
00837         }
00838     }
00839     else
00840     { // Remote file
00841         // We haven't saved yet, or we did but locally - provide a temp file
00842         if ( _tmpfile == 0 )
00843         {
00844              _tmpfile = new KTempFile;
00845         }
00846         // otherwise, we already had a temp file
00847         if( saveResults(_tmpfile->name(), results) ) {
00848             // upload the file
00849              KIO::Job * job = KIO::file_copy( KURL::fromPathOrURL( _tmpfile->name() ), _currentURL, -1, true /*overwrite*/ );
00850              connect( job, SIGNAL( result( KIO::Job * ) ), this, SLOT( slotUploadFinished (KIO::Job *) ) );
00851             return true;
00852         }
00853     }
00854 
00855     // Save local file and upload local file
00856     return false;
00857 }
00858 
00859 bool FileRead::saveResults( const QString &filename, const QString &results )
00860 {
00861     QTextStream stream;
00862     QFile file(filename);
00863     QStringList copyJOB;
00864 
00865     stream.setDevice(&file);
00866 
00867     if(!file.open(IO_WriteOnly))
00868     {
00869         // Cannot open
00870         return false;
00871     }
00872     stream << results;
00873     file.close();
00874     return true;
00875 }
00876 
00878 void FileRead::insertXML( QDomDocument &doc, QDomElement &parent, const QString &tagName, const QString &data)
00879 {
00880     QDomElement domELEMENT = doc.createElement( tagName );
00881     QDomText DATAelement = doc.createTextNode( data );
00882 
00883     parent.appendChild( domELEMENT );
00884     domELEMENT.appendChild( DATAelement );
00885 }
00886 
00888 void FileRead::insertXML( QDomDocument &doc, QDomElement &parent, const QString &data)
00889 {
00890     QDomText DATAelement = doc.createTextNode( data );
00891     parent.appendChild( DATAelement );
00892 }
00893 
00895 QString FileRead::getHeader(const QString &head)
00896 {
00897     return _header[head];
00898 }
00899 
00901 void FileRead::setHeader( const QString field, const QString value)
00902 {
00903     _changed = _header[field]!=value;
00904 
00905     if( (_header[field]).isEmpty() )
00906         _header.insert( field, value );
00907     else
00908         _header.replace( field, value );
00909 }
00910 
00912 bool FileRead::isMultiAnswer()
00913 {
00914     int numOKanswer = 0;
00915 
00916     recordAnswerFirst();
00917 
00918     while( !recordAnswerEOF() )
00919     {
00920         if( (*(*_recordQuestions).recordAnswers).value == true ) numOKanswer++;
00921         recordAnswerNext();
00922     }
00923 
00924     if( numOKanswer > 1 )   {
00925         return true;
00926     }   else {
00927         return false;
00928     }
00929 }
00930 
00932 bool FileRead::isResult()
00933 {
00934   return _listResults.count() > 0 ? true : false; 
00935 }
00936 
00937 void FileRead::slotUploadFinished( KIO::Job * job )
00938 {
00939   if (job->error()) {
00940     emit canceled( job->errorString() );
00941     kdDebug()<< "FileRead::slotUploadFinished(): " <<job->errorString()<<endl;
00942   }
00943   else
00944   {
00945     if ( _tmpfile!=0 ) // We're finished with this document -> remove temp file
00946     {
00947        _tmpfile->unlink();
00948        delete _tmpfile;
00949        _tmpfile=0;
00950     }
00951     emit setWindowCaption( _currentURL.prettyURL() );
00952     emit completed();
00953   }
00954 }
00955 
00957 void FileRead::recordAt( uint index )
00958 {
00959     _recordQuestions = _listQuestions.begin();
00960     for( unsigned int i = 0; i < index; ++i)
00961       ++_recordQuestions;   
00962 }
00963 
00965 uint FileRead::getTotalQuestions()
00966 {
00967   return _totalQuestions;
00968 }
00969 
00971 uint FileRead::getTotalPoints()
00972 {
00973   return _totalPoints;
00974 }
00975 
00977 uint FileRead::getTotalTime()
00978 {
00979   return _totalTime;
00980 }
00981 
00983 void FileRead::refreshData()
00984 {
00985   _totalTime = 0;
00986   _totalPoints = 0;
00987   _totalQuestions = 0;
00988 
00989   _recordQuestions = _listQuestions.begin();
00990   while ( _recordQuestions != _listQuestions.end() )
00991     {
00992     _totalQuestions++;
00993     _totalPoints += (*_recordQuestions).points;
00994     _totalTime += (*_recordQuestions).time;
00995     ++_recordQuestions;
00996     } 
00997 }
00998 
00999 #include "fileread.moc"

keduca

Skip menu "keduca"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal