Vocabulary.cpp

Go to the documentation of this file.
00001 #include "Vocabulary.h"
00002 
00003 Vocabulary::Vocabulary( int id, const QString& title = QString::null ) 
00004     : Base(), id( id ), markedForStudy( false ), markedForDeletion( false ), title( title ), description( QString::null ), author( QString::null ), 
00005         dirty( false ) {
00006     QDateTime now( QDateTime::currentDateTime() );
00007     creationDate = modificationDate = now;
00008 }
00009 
00010 Vocabulary::Vocabulary( const Vocabulary& voc ) 
00011     : Base(), id( voc.id ), markedForStudy( voc.markedForStudy ), markedForDeletion( voc.markedForDeletion ), 
00012         title( voc.title ), description( voc.description ), author( voc.author ), 
00013             creationDate( voc.creationDate ), modificationDate( voc.modificationDate ), dirty( voc.dirty ), parent( voc.parent ) {
00014     for( TermMap::ConstIterator it = voc.terms.begin(); it != voc.terms.end(); it++ ) {
00015         const Term& term = it.data();
00016         addTerm( term );
00017     }
00018 }
00019 
00020 Vocabulary::~Vocabulary() {
00021 }
00022 
00023 int Vocabulary::getId() const {
00024     return( id );
00025 }
00026 
00027 bool Vocabulary::isMarkedForStudy() const {
00028     return( markedForStudy );
00029 }
00030 
00031 void Vocabulary::setMarkedForStudy( bool isMarkedForStudy ) {
00032     markedForStudy = isMarkedForStudy;
00033 }
00034 
00035 bool Vocabulary::isMarkedForDeletion() const {
00036     return( markedForDeletion );
00037 }
00038 
00039 void Vocabulary::setMarkedForDeletion( bool isMarkedForDeletion ) {
00040     markedForDeletion = isMarkedForDeletion;
00041 }
00042 
00043 const QString Vocabulary::getTitle() const {
00044     return( title );
00045     //return( title + QString( "(" ) + QString::number( getId() ) + QString( ")" ) ); // For debugging.
00046 }
00047 
00048 void Vocabulary::setTitle( const QString& title ) {
00049     this->title = title;
00050 }
00051 
00052 const QString Vocabulary::getDescription() const {
00053     return( description );
00054 }
00055 
00056 void Vocabulary::setDescription( const QString& desc ) {
00057     this->description = desc;
00058 }
00059 
00060 const QString Vocabulary::getAuthor() const {
00061     return( author );
00062 }
00063 
00064 void Vocabulary::setAuthor( const QString& author ) {
00065     this->author = author;
00066 }
00067 
00068 const QDateTime Vocabulary::getCreationDate() const {
00069     return( creationDate );
00070 }
00071 
00072 void Vocabulary::setCreationDate( const QDateTime& creationDate ) {
00073     this->creationDate = creationDate;
00074 }
00075 
00076 const QDateTime Vocabulary::getModificationDate() const {
00077     return( modificationDate );
00078 }
00079 
00080 void Vocabulary::setModificationDate( const QDateTime& modificationDate ) {
00081     this->modificationDate = modificationDate;
00082 }
00083 
00084 void Vocabulary::addTerm( const Term& term ) {
00085     terms.insert( term.getId(), term );
00086 }
00087 
00088 void Vocabulary::removeTerm( const int& id ) {
00089     terms.remove( id );
00090 }
00091 
00092 bool Vocabulary::isTermExists( const int& id ) const {
00093     return( terms.contains( id ) );
00094 }
00095 
00096 Term& Vocabulary::getTerm( const int& id ) {
00097     return( terms[ id ] );
00098 }
00099 
00100 bool Vocabulary::isEmpty() const {
00101     return( terms.isEmpty() );
00102 }
00103 
00104 const uint Vocabulary::getSize() const {
00105     return( terms.count() );
00106 }
00107 
00108 void Vocabulary::getItemsCount( uint* termCount, uint* checkedTermCount, uint* selectedTermCount, bool isReachableFromRoot, const QString& firstLang = QString::null, const QString& testLang = QString::null ) const {
00109     if( !isMarkedForDeletion() ) {
00110         for( TermMap::ConstIterator it = terms.begin(); it != terms.end(); it++ ) {
00111             const Term& term = it.data();
00112             if( !firstLang.isNull() && !testLang.isNull() ) {
00113                 if( term.isTranslationExists( firstLang ) && term.isTranslationExists( testLang ) ) {
00114                     *termCount += 1;
00115                     if( term.isMarkedForStudy() ) {
00116                         *checkedTermCount += 1;
00117                         if( isReachableFromRoot )
00118                             *selectedTermCount += 1;
00119                     }
00120                 }
00121             }
00122             else {
00123                 *termCount += 1;
00124                 if( term.isMarkedForStudy() ) {
00125                     *checkedTermCount += 1;
00126                     if( isReachableFromRoot )
00127                         *selectedTermCount += 1;
00128                 }
00129             }
00130         }
00131     }
00132 }
00133 
00134 int Vocabulary::getMaxTermId() const {
00135     int maxId = 0;
00136     for( TermMap::ConstIterator it = terms.begin(); it != terms.end(); it++ ) {
00137         const Term& term = it.data();
00138         if( term.getId() > maxId )
00139             maxId = term.getId();
00140     }
00141     return( maxId );
00142 }
00143 
00144 bool Vocabulary::containsTermWithTranslations( const QString& lang1, const QString& lang2 ) const {
00145     for( Vocabulary::TermMap::ConstIterator it = terms.begin(); it != terms.end(); it++ ) {
00146         const Term& term = it.data();
00147         if( term.isTranslationExists( lang1 ) && term.isTranslationExists( lang2 ) )
00148             return( true );
00149     }
00150     return( false );
00151 }
00152 
00153 QStringList Vocabulary::getTranslationLanguages() const {
00154     QStringList languages;
00155     for( TermMap::ConstIterator it = begin(); it != end(); it++ ) {
00156         const Term& term = it.data();
00157         for( Term::TranslationMap::ConstIterator it2 = term.translationsBegin(); it2 != term.translationsEnd(); it2++ ) {
00158             const Translation& trans = it2.data();
00159             if( !languages.contains( trans.getLanguage() ) )
00160                 languages.append( trans.getLanguage() );
00161         }
00162     }
00163     return( languages );
00164 }
00165 
00166 void Vocabulary::removeTranslations( const QStringList& languages ) {
00167     for( TermMap::Iterator it = begin(); it != end(); it++ ) {
00168         Term& term = it.data();
00169         for( QStringList::ConstIterator it2 = languages.begin(); it2 != languages.end(); it2++ ) {
00170             const QString& lang = *it2;
00171             term.removeTranslation( lang );
00172         }
00173     }
00174 }
00175 
00176 Vocabulary::TermMap::ConstIterator Vocabulary::begin() const {
00177     return( terms.begin() );
00178 }
00179 
00180 Vocabulary::TermMap::ConstIterator Vocabulary::end() const {
00181     return( terms.end() );
00182 }
00183 
00184 Vocabulary::TermMap::Iterator Vocabulary::begin() {
00185     return( terms.begin() );
00186 }
00187 
00188 Vocabulary::TermMap::Iterator Vocabulary::end() {
00189     return( terms.end() );
00190 }
00191 
00192 bool Vocabulary::isDirty() const {
00193     return( dirty );
00194 }
00195 
00196 void Vocabulary::setDirty( bool isDirty ) {
00197     dirty = isDirty;
00198 }
00199 
00200 Folder* Vocabulary::getParent() const {
00201     return( parent );
00202 }
00203 
00204 void Vocabulary::setParent( Folder* parent ) {
00205     this->parent = parent;
00206 }
00207 
00208 bool Vocabulary::load( const QString& filename ) {
00209     QFile dataFile( filename );
00210     if( !dataFile.open( IO_ReadOnly ) )
00211         return( false );
00212     
00213     QByteArray compressedData( dataFile.readAll() );
00214     QByteArray data( Util::qUncompress( compressedData ) );
00215 
00216     QDataStream in( data, IO_ReadOnly );
00217 
00218     Q_UINT32 tempMagicNumber;
00219     Q_UINT16 tempVersion;
00220     Vocabulary tempVocab;
00221 
00222     in >> tempMagicNumber >> tempVersion;
00223 
00224     if( tempMagicNumber != Vocabulary::magicNumber ) {
00225         cerr << "Wrong magic number: Incompatible vocabulary data file." << endl;
00226         return( false );
00227     }
00228     if( tempVersion > 0x0010 ) {
00229         cerr << "Vocabulary data file is from a more recent version.  Upgrade toMOTko." << endl;
00230         return( false );
00231     }
00232 
00233     in.setVersion( 3 );
00234     in >> tempVocab;
00235 
00236     dataFile.close();
00237 
00238     id = tempVocab.getId();
00239     markedForStudy = tempVocab.isMarkedForStudy();
00240     title = tempVocab.getTitle();
00241     description = tempVocab.getDescription();
00242     author = tempVocab.getAuthor();
00243     creationDate = tempVocab.getCreationDate();
00244     modificationDate = tempVocab.getModificationDate();
00245     dirty = tempVocab.isDirty();
00246     for( TermMap::ConstIterator it = tempVocab.begin(); it != tempVocab.end(); it++ ) {
00247         const Term& term = it.data();
00248         addTerm( term );
00249     }
00250     return( true );
00251 }
00252 
00253 bool Vocabulary::save( const QString& filename ) const {
00254     QByteArray data;
00255 
00256     QDataStream out( data, IO_WriteOnly );
00257     out.setVersion( 3 /* QDataStream::Qt_3 ? */ );
00258 
00259     // 0x0010 means 0.10.x version.  
00260     out << Q_UINT32( Vocabulary::magicNumber ) << Q_UINT16( 0x0010 ) << *this;
00261 
00262     QByteArray compressedData( Util::qCompress( data ) ); 
00263 
00264     QFile dataFile( filename );
00265     QFileInfo dataFileInfo( dataFile );
00266 
00267     if( !Util::makeDirectory( dataFileInfo.dirPath() ) )
00268         return( false );
00269 
00270     if( !dataFile.open( IO_WriteOnly ) )
00271         return( false );
00272 
00273     int ret = dataFile.writeBlock( compressedData );
00274     dataFile.close();
00275 
00276     if( ret == -1 || dataFile.status() != IO_Ok ) {
00277         dataFile.resetStatus();
00278         return( false );
00279     }
00280 
00281     return( true );
00282 }
00283 
00284 QDataStream& operator<<( QDataStream& out, const Vocabulary& vocab ) {
00285     out << vocab.id << vocab.title << vocab.terms;
00286     out << vocab.description << vocab.author << vocab.creationDate << vocab.modificationDate;
00287 
00288     return( out );
00289 }
00290 
00291 QDataStream& operator>>( QDataStream& in, Vocabulary& vocab ) {
00292     int tempId;
00293     QString tempTitle;
00294     Vocabulary::TermMap tempTerms;
00295     QString tempDescription;
00296     QString tempAuthor;
00297     QDateTime tempCreationDate;
00298     QDateTime tempModificationDate;
00299 
00300     in >> tempId >> tempTitle >> tempTerms;
00301 
00302     in >> tempDescription >> tempAuthor >> tempCreationDate >> tempModificationDate;
00303 
00304     vocab = Vocabulary( tempId, tempTitle );
00305     for( Vocabulary::TermMap::ConstIterator it = tempTerms.begin(); it != tempTerms.end(); it++ ) {
00306         const Term& term = it.data();
00307         vocab.addTerm( term );
00308     }
00309    
00310     vocab.setDescription( tempDescription );
00311     vocab.setAuthor( tempAuthor );
00312     vocab.setCreationDate( tempCreationDate );
00313     vocab.setModificationDate( tempModificationDate );
00314 
00315     return( in );
00316 }

Generated on Sun Mar 1 17:30:47 2009 for toMOTko by  doxygen 1.5.6