/*************************************************************************** CInformationElement.cpp - description ------------------- begin : Fri Jul 19 2002 copyright : (C) 2002 by Alexander Theel email : alex.theel@gmx.net ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include "CInformationElement.h" #include "../global.h" #include "../utilities/strings.h" #include "../gui/dialogs/searchDialog/searchlistitem.h" #include #include "../utilities/crypt/StringCrypter.h" //QTextEdit* CInformationElement::tmpRTFEditor = 0; // ------------------------------------------------------------------------------- CInformationElement::CInformationElement( IParent* pParent, QString sDescription, QString sInformation, InformationFormat* pFormat, QString sIconFileName, bool bExpires, QDate expiryDate ) : mpParent( pParent ) , mbBatched( false ) , mDescription( sDescription ) , mpInformationFormat( pFormat ) , mInformation( sInformation ) , mIcon() , mIconFilename( sIconFileName ) , mpChildObjects( NULLPTR ) , mExpiryDate( expiryDate ) , mbExpires( bExpires ) , miInformationYPos( 0 ) , mbIsEncryptionEnabled( false ) , msTmpPasswd( "" ) , mEncryptedData() // ------------------------------------------------------------------------------- { mpChildObjects = new QPtrList(); if ( NULLPTR == mpChildObjects ) { std::cout<<"TuxCards-ERROR: in constructor 'CInformationElement'\n" <<"NULLPTR == mpChildObjects\n" <<"Not enough memory to create objects. TuxCards might crash." <setAutoDelete( true ); if (!mbBatched) emit propertyChanged(); } // ------------------------------------------------------------------------------- CInformationElement::~CInformationElement( void ) // ------------------------------------------------------------------------------- { //std::cout<<"\t~CIE: "<clear(); /* while ( NULL < mpChildObjects->count() ) { CInformationElement* pIE = mpChildObjects->first(); removeChild( pIE ); pIE = NULLPTR; std::cout<<"\t\tpIE="<aboutToRemoveElement(this); mpParent = NULLPTR; } } // **************************** IParent ****************************************** // ------------------------------------------------------------------------------- QString CInformationElement::getDescription( void ) // ------------------------------------------------------------------------------- { return mDescription; } // ------------------------------------------------------------------------------- IParent* CInformationElement::getParent( void ) // ------------------------------------------------------------------------------- { return mpParent; } // ------------------------------------------------------------------------------- void CInformationElement::removeChild( CInformationElement* pChild ) // ------------------------------------------------------------------------------- { if ( NULLPTR == mpChildObjects ) return; // autodeletion is set to true (look at constructor) mpChildObjects->removeRef( pChild ); } // ------------------------------------------------------------------------------- void CInformationElement::aboutToRemoveElement( CInformationElement* pIE ) // ------------------------------------------------------------------------------- { if ( NULLPTR != mpParent ) mpParent->aboutToRemoveElement(pIE); } // **************************** IParent - End ************************************ // ------------------------------------------------------------------------------- void CInformationElement::deleteSelf( void ) // ------------------------------------------------------------------------------- { //cout<<"IE::deleteSelf(); parent="<removeChild(this); } // ------------------------------------------------------------------------------- void CInformationElement::setParent( IParent* pParent ) // ------------------------------------------------------------------------------- { mpParent = pParent; } // ------------------------------------------------------------------------------- void CInformationElement::setBatched( bool b ) // ------------------------------------------------------------------------------- { mbBatched = b; if (!mbBatched) emit propertyChanged(); } // ------------------------------------------------------------------------------- bool CInformationElement::isBatched( void ) const // ------------------------------------------------------------------------------- { return mbBatched; } // ------------------------------------------------------------------------------- void CInformationElement::addChild( CInformationElement* pElement ) // ------------------------------------------------------------------------------- { if ( (NULLPTR == pElement) || (NULLPTR == mpChildObjects) ) return; mpChildObjects->append( pElement ); if (!mbBatched) emit childAdded( pElement ); } // ------------------------------------------------------------------------------- QPtrList* CInformationElement::getChildren( void ) // ------------------------------------------------------------------------------- { return mpChildObjects; } // ------------------------------------------------------------------------------- int CInformationElement::childCount( void ) const // ------------------------------------------------------------------------------- { if ( NULLPTR == mpChildObjects ) return 0; return mpChildObjects->count(); } // ------------------------------------------------------------------------------- void CInformationElement::setDescription( QString description ) // ------------------------------------------------------------------------------- { mDescription = description; if (!mbBatched) emit propertyChanged(); } // ------------------------------------------------------------------------------- QString CInformationElement::getInformation( void ) const // ------------------------------------------------------------------------------- { return mInformation; } // ------------------------------------------------------------------------------- void CInformationElement::setInformation( const QString& information ) // ------------------------------------------------------------------------------- { mInformation = information; if (!mbBatched) emit propertyChanged(); } // ------------------------------------------------------------------------------- bool CInformationElement::hasIcon( void ) const // ------------------------------------------------------------------------------- { return !mIcon.isNull(); } // ------------------------------------------------------------------------------- void CInformationElement::setIcon( QPixmap icon ) // ------------------------------------------------------------------------------- { mIcon = icon; if (!mbBatched) emit propertyChanged(); } // ------------------------------------------------------------------------------- void CInformationElement::setIconFileName( const QString& file ) // ------------------------------------------------------------------------------- { mIconFilename = file; if (!mbBatched) emit propertyChanged(); } // ------------------------------------------------------------------------------- QString CInformationElement::getIconFileName( void ) const // ------------------------------------------------------------------------------- { return (mIconFilename ? mIconFilename : QString("none")); } // ------------------------------------------------------------------------------- InformationFormat* CInformationElement::getInformationFormat( void ) const // ------------------------------------------------------------------------------- { return mpInformationFormat; } // ------------------------------------------------------------------------------- void CInformationElement::setInformationFormat( InformationFormat* pFormat ) // ------------------------------------------------------------------------------- { mpInformationFormat = pFormat; if (!mbBatched) emit propertyChanged(); } // ------------------------------------------------------------------------------- QString CInformationElement::toString( void ) const // ------------------------------------------------------------------------------- { QString result = mDescription+" ["+mpInformationFormat->toString()+"]\n" +mInformation; return result; } // ------------------------------------------------------------------------------- QString CInformationElement::getTreeString( int tab ) const // ------------------------------------------------------------------------------- { QString result = Strings::spaces(tab)+mDescription+"\n"; tab++; if ( NULLPTR == mpChildObjects ) return result; QPtrListIterator it(*mpChildObjects); CInformationElement* x; while( (x = it.current()) != 0 ){ ++it; result += x->getTreeString(tab); } return result; } // ------------------------------------------------------------------------------- void CInformationElement::toXML( QDomDocument xmlDocument, QDomNode parent ) // ------------------------------------------------------------------------------- { QDomElement thisElement = xmlDocument.createElement("InformationElement"); thisElement.setAttribute("informationFormat", getInformationFormat()->toString()); thisElement.setAttribute("iconFileName", getIconFileName()); thisElement.setAttribute("expires", expires() ? QString("true") : QString("false") ); thisElement.setAttribute("expiryDate", getExpiryDate().toString()); // add description QDomElement description = xmlDocument.createElement("Description"); QDomText text = xmlDocument.createTextNode(getDescription()); description.appendChild(text); thisElement.appendChild(description); // add information QDomElement information = xmlDocument.createElement("Information"); text = xmlDocument.createTextNode(getInformation()); information.appendChild(text); thisElement.appendChild(information); // add children QPtrListIterator it(*mpChildObjects); CInformationElement* x; while( (x = it.current()) != 0 ) { ++it; x->toXML(xmlDocument, thisElement); } parent.appendChild(thisElement); } // ------------------------------------------------------------------------------- CInformationElement* CInformationElement::findChildWithDescription( QString desc ) // ------------------------------------------------------------------------------- { QPtrListIterator it(*mpChildObjects); CInformationElement* x; while( (x = it.current()) != 0 ) { ++it; if (x->getDescription() == desc) return x; } return NULLPTR; } /** * find the specified 'QString pattern' within the text/information of the * appropriate 'CInformationElement' (evtl. recursive) and append the found * "places" as 'SearchListItem's at the list's end. */ // ------------------------------------------------------------------------------- void CInformationElement::search( QString pattern, bool recursive, bool caseSensitive, QListView& list) // ------------------------------------------------------------------------------- { searchDescription(pattern, caseSensitive, list); searchInformation(pattern, caseSensitive, list); // if recursive -> do so if (recursive) { QPtrListIterator it(*mpChildObjects); CInformationElement* x; while( (x = it.current()) != 0 ) { ++it; x->search( pattern, true, caseSensitive, list ); } } } // ------------------------------------------------------------------------------- void CInformationElement::searchLine( QString pattern, bool caseSensitive, QListView& list, QString line, int lineNumber, int searchLocation ) // ------------------------------------------------------------------------------- { int pos = line.find(pattern, 0, caseSensitive); while (pos >= 0) { // found something -> add it to 'list' (void) new SearchListItem( &list, new Path(this), searchLocation, lineNumber, pos, pattern.length(), line ); // prepare for next evtl. occurance within this 'oneLine' pos = line.find( pattern, pos+pattern.length(), caseSensitive ); } } // ------------------------------------------------------------------------------- void CInformationElement::searchDescription( QString pattern, bool caseSensitive, QListView& list) // ------------------------------------------------------------------------------- { searchLine( pattern, caseSensitive, list, mDescription, -1, SearchPosition::SP_NAME ); } // ------------------------------------------------------------------------------- void CInformationElement::searchInformation( QString pattern, bool caseSensitive, QListView& list ) // ------------------------------------------------------------------------------- { QString text = getInformationText() +"\n"; // add "\n" -> so, the last line is also searched QString oneLine; int line = -1; //because first line is 0 while ( ( oneLine = Strings::removeAndReturnFirstLine(text) ) != 0 ) { line++; searchLine( pattern, caseSensitive, list, oneLine, line, SearchPosition::SP_INFORMATION); } /* SearchRTF-Idee (somit kann man regexp. auch in rtf-text suchen/finden) 1) mit converter.cpp in ascii umwandeln 2) paragraphenweise suchen, dabei gefundene Stellen als SearchListItem in die Liste eintragen. */ } // ------------------------------------------------------------------------------- QString CInformationElement::getInformationText( void ) const // ------------------------------------------------------------------------------- { QString text; if ( getInformationFormat() == &InformationFormat::ASCII ) { text = getInformation(); } else { text = Strings::removeHTMLTags(getInformation()); } return text; } /** * Appends the specified text to the end of the information * of this element. */ // ------------------------------------------------------------------------------- void CInformationElement::appendInformation( QString text ) // ------------------------------------------------------------------------------- { if ( mpInformationFormat == &InformationFormat::RTF ) { text.replace( QRegExp("\n"), "
\n" ); } mInformation += text; emit informationHasChanged(); } // ------------------------------------------------------------------------------- bool CInformationElement::expires( void ) const // ------------------------------------------------------------------------------- { return mbExpires; } // ------------------------------------------------------------------------------- QDate CInformationElement::getExpiryDate( void ) const // ------------------------------------------------------------------------------- { return mExpiryDate; } /** * This method set the expire date to 'date'. If 'expires' == false, this * information element will not expire at all (independently of a given date). */ // ------------------------------------------------------------------------------- void CInformationElement::setExpiryDate( bool expires, QDate date ) // ------------------------------------------------------------------------------- { mbExpires = expires; mExpiryDate = date; } // ------------------------------------------------------------------------------- void CInformationElement::setInformationYPos( int iPos ) // ------------------------------------------------------------------------------- { miInformationYPos = iPos; } // ------------------------------------------------------------------------------- int CInformationElement::getInformationYPos( void ) const // ------------------------------------------------------------------------------- { return miInformationYPos; } // ------------------------------------------------------------------------------- // Adds information so that the element has the capability to be encrypted. // To encrypt the element call 'encrypt()'. // ------------------------------------------------------------------------------- void CInformationElement::enableEncryption( bool bIsEncryptionEnabled, const QString& sTmpPasswd ) // ------------------------------------------------------------------------------- { mbIsEncryptionEnabled = bIsEncryptionEnabled; msTmpPasswd = mbIsEncryptionEnabled ? sTmpPasswd : QString(""); } // ------------------------------------------------------------------------------- // States whether this element has the capability to be encrypted. // To encrypt the element call 'encrypt()'. // ------------------------------------------------------------------------------- bool CInformationElement::isEncryptionEnabled( void ) const // ------------------------------------------------------------------------------- { return mbIsEncryptionEnabled; } // ------------------------------------------------------------------------------- // States whether this element is currently encrypted. // ------------------------------------------------------------------------------- bool CInformationElement::isCurrentlyEncrypted( void ) const // ------------------------------------------------------------------------------- { return ( 0 != mEncryptedData.size() ); } // ------------------------------------------------------------------------------- // This encrypts the element and makes its contents unreadable. // ------------------------------------------------------------------------------- void CInformationElement::encrypt( void ) // ------------------------------------------------------------------------------- { if ( !mbIsEncryptionEnabled ) return; StringCrypter::encryptString( mInformation, msTmpPasswd, mEncryptedData ); mInformation = ""; } // ------------------------------------------------------------------------------- // This decrypts the element and makes its contents readable. // Returns 'true' if everything is correct and element could be decrypted. // ------------------------------------------------------------------------------- bool CInformationElement::decrypt( const QString& sPasswd ) // ------------------------------------------------------------------------------- { int iError = StringCrypter::decryptString( mEncryptedData, sPasswd, mInformation ); if ( StringCrypter::NO_ERROR == iError ) { msTmpPasswd = sPasswd; mEncryptedData.resize(0); } else if ( StringCrypter::ERROR_INVALID_FILEHEADER == iError ) { // this should never happen (if the user does not touch the data) std::cout<<"StringCrypter::ERROR_INVALID_FILEHEADER"<