kshowmail: kshowmail/showrecord.cpp Source File

- Main Page
- Namespaces
- Classes
- Files
- Directories

# showrecord.cpp

    00001 /***************************************************************************
    00002                           showrecord.cpp  -  description
    00003                              -------------------
    00004     begin                : Mon Dec 3 2001
    00005     copyright            : (C) 2001 by Eggert Ehmke
    00006     email                : eggert.ehmke@berlin.de
    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 "showrecord.h"
    00019
    00020 int const ShowRecord::continueShowHeaders( 0 );
    00021 int const ShowRecord::cancelShowHeaders( 1 );
    00022
    00023 ShowRecord::ShowRecord()
    00024 {
    00025   //set auto delete to true. This is a function of the parent class, which deletes
    00026   //all items when the list is deleted or cleared.
    00027   setAutoDelete( true );
    00028 }
    00029
    00030 ShowRecord::~ShowRecord()
    00031 {
    00032 }
    00033
    00034 void ShowRecord::saveOptions( TQDomDocument& doc, TQDomElement& parent )
    00035 {
    00036   //Loop over all mail items
    00037   for( ShowRecordElem* pElem = first(); pElem; pElem = next() )
    00038   {
    00039     //call the method of the mail to save it
    00040     pElem->saveOptions( doc, parent );
    00041   }
    00042 }
    00043
    00044 void ShowRecord::readStoredMails( TQDomElement& parent )
    00045 {
    00046   //clear the list
    00047   clear();
    00048
    00049   //get first DOM node (mail)
    00050   TQDomNode n = parent.firstChild();
    00051
    00052   //iterate over all mail items stored in the given account
    00053   while( !n.isNull() )
    00054   {
    00055     //get element of the current node
    00056     TQDomElement e = n.toElement();
    00057
    00058     //create mail object
    00059     ShowRecordElem* pElem = new ShowRecordElem();
    00060
    00061     //store the currently read mail data in the new object
    00062     pElem->readOptions( e );
    00063
    00064     //store the new mail object in this list
    00065     append( pElem );
    00066
    00067     //get next DOM node
    00068     n = n.nextSibling();
    00069   }
    00070 }
    00071
    00072 void ShowRecord::applyFilters ()
    00073 {
    00074   //Loop over all mails in this list
    00075   for( ShowRecordElem* pElem = first(); pElem; pElem = next() )
    00076   {
    00077     //apply the filters to the current mail
    00078     pElem->applyFilters ();
    00079   }
    00080 }
    00081
    00082 bool ShowRecord::hasSelectedMails( )
    00083 {
    00084   bool selected = false;    //TRUE when a selected mail was found
    00085   ShowRecordElem* mail;     //mail which we want to check
    00086
    00087   //get first mail
    00088   mail = first();
    00089
    00090   //iterate over all mails until we have found a selected mail
    00091   while( mail != NULL && !selected )
    00092   {
    00093     //check the current mail
    00094     selected = mail->isSelected();
    00095
    00096     //get next mail
    00097     mail = next();
    00098   }
    00099
    00100   return selected;
    00101 }
    00102
    00103 MailNumberList_Type ShowRecord::getSelectedMails( )
    00104 {
    00105   MailNumberList_Type list;                     //contains the numbers of selected mails
    00106   ShowRecordElem* mail;                         //mail from which we want to get the number (if selected)
    00107   TQPtrListIterator<ShowRecordElem> it( *this ); //iterator for the mail list
    00108
    00109   //iterate over all mails
    00110   while( ( mail = it.current() ) != NULL )
    00111   {
    00112     //increment iterator to next mail
    00113     ++it;
    00114
    00115     //if current mail is selected append its number to the mail number list
    00116     if( mail->isSelected() )
    00117       list.append( mail->number() );
    00118   }
    00119
    00120   return list;
    00121 }
    00122
    00123 void ShowRecord::removeMail( int number )
    00124 {
    00125   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00126   ShowRecordElem* mail;                           //current mail
    00127
    00128   //looking for the mail with the number 'number'
    00129   while( ( mail = it.current() ) != NULL )
    00130   {
    00131     //increment iterator to next mail
    00132     ++it;
    00133
    00134     //if the current mail has the given number, remove it
    00135     if( mail->number() == number )
    00136       remove( mail );
    00137   }
    00138 }
    00139
    00140 TQStringList ShowRecord::getSelectedSubjects( ) const
    00141 {
    00142   TQStringList subjects;                           //contains the subjects
    00143   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00144   ShowRecordElem* mail;                           //current mail
    00145
    00146   //iterate over all mails
    00147   while( ( mail = it.current() ) != NULL )
    00148   {
    00149     //increment iterator to next mail
    00150     ++it;
    00151
    00152     //if the mail is selected, append subject to list
    00153     if( mail->isSelected() )
    00154       subjects.append( mail->subject() );
    00155   }
    00156
    00157   return subjects;
    00158 }
    00159
    00160 TQString ShowRecord::getSenderOf( int number ) const
    00161 {
    00162   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00163   ShowRecordElem* mail;                           //current mail
    00164   bool found = false;                             //True, when the wanted mail was found
    00165   TQString sender;                                 //sender of the wanted mail
    00166
    00167   //looking for the mail with the number 'number'
    00168   while( ( mail = it.current() ) != NULL && !found )
    00169   {
    00170     //increment iterator to next mail
    00171     ++it;
    00172
    00173     //if the current mail has the given number, remove it
    00174     if( mail->number() == number )
    00175     {
    00176       sender = mail->from();
    00177       found = true;
    00178     }
    00179   }
    00180   return sender;
    00181 }
    00182
    00183 TQString ShowRecord::getDateOf( int number ) const
    00184 {
    00185   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00186   ShowRecordElem* mail;                           //current mail
    00187   bool found = false;                             //True, when the wanted mail was found
    00188   TQString date;                                   //sent date of the wanted mail
    00189
    00190   //looking for the mail with the number 'number'
    00191   while( ( mail = it.current() ) != NULL && !found )
    00192   {
    00193     //increment iterator to next mail
    00194     ++it;
    00195
    00196     //if the current mail has the given number, remove it
    00197     if( mail->number() == number )
    00198     {
    00199       date = mail->date();
    00200       found = true;
    00201     }
    00202   }
    00203   return date;
    00204
    00205 }
    00206
    00207 TQString ShowRecord::getSizeOf( int number ) const
    00208 {
    00209   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00210   ShowRecordElem* mail;                           //current mail
    00211   bool found = false;                             //True, when the wanted mail was found
    00212   TQString size;                                   //size of the wanted mail
    00213
    00214   //looking for the mail with the number 'number'
    00215   while( ( mail = it.current() ) != NULL && !found )
    00216   {
    00217     //increment iterator to next mail
    00218     ++it;
    00219
    00220     //if the current mail has the given number, remove it
    00221     if( mail->number() == number )
    00222     {
    00223       size = mail->strSizePrefix();
    00224       found = true;
    00225     }
    00226   }
    00227   return size;
    00228
    00229 }
    00230
    00231 TQString ShowRecord::getSubjectOf( int number ) const
    00232 {
    00233   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00234   ShowRecordElem* mail;                           //current mail
    00235   bool found = false;                             //True, when the wanted mail was found
    00236   TQString subject;                                //subject of the wanted mail
    00237
    00238   //looking for the mail with the number 'number'
    00239   while( ( mail = it.current() ) != NULL && !found )
    00240   {
    00241     //increment iterator to next mail
    00242     ++it;
    00243
    00244     //if the current mail has the given number, get the subject
    00245     if( mail->number() == number )
    00246     {
    00247       subject = mail->subject();
    00248       found = true;
    00249     }
    00250   }
    00251   return subject;
    00252
    00253 }
    00254
    00255 TQString ShowRecord::decodeMailBody( TQByteArray body, int number, bool preferHTML ) const
    00256 {
    00257   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00258   ShowRecordElem* mail;                           //current mail
    00259   bool found = false;                             //True, when the wanted mail was found
    00260   TQString mailbody;                               //decoded mail
    00261
    00262   //looking for the mail with the number 'number'
    00263   while( ( mail = it.current() ) != NULL && !found )
    00264   {
    00265     //increment iterator to next mail
    00266     ++it;
    00267
    00268     //if the current mail has the given number, decode the mail
    00269     if( mail->number() == number )
    00270     {
    00271       mailbody = mail->decodeMailBody( body, preferHTML );
    00272       found = true;
    00273     }
    00274   }
    00275   return mailbody;
    00276
    00277 }
    00278
    00279 bool ShowRecord::hasMail( TQString uid )
    00280 {
    00281   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00282   ShowRecordElem* mail;                           //current mail
    00283   bool found = false;                             //True, when the wanted mail was found
    00284
    00285   while( ( mail = it.current() ) != NULL && !found )
    00286   {
    00287     //increment iterator to next mail
    00288     ++it;
    00289
    00290     //compare the uid
    00291     if( mail->uidl() == uid )
    00292     {
    00293      found = true;
    00294     }
    00295   }
    00296   return found;
    00297 }
    00298
    00299 void ShowRecord::appendNewMail( int number, TQString uid, bool isNew )
    00300 {
    00301   //create new mail
    00302   ShowRecordElem* newMail = new ShowRecordElem( number, uid, isNew );
    00303
    00304   //append new mail
    00305   if( newMail != NULL )
    00306     append( newMail );
    00307 }
    00308
    00309 void ShowRecord::printMailList( )
    00310 {
    00311   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00312   ShowRecordElem* mail;                           //current mail
    00313
    00314   while( ( mail = it.current() ) )
    00315   {
    00316     //increment iterator to next mail
    00317     ++it;
    00318
    00319     //print mail
    00320     cout << mail->number() << " - UID: " << mail->uidl() << "; Size: " << mail->size() << "; Subject: " << mail->subject() << "; New: " << mail->isNew() << endl;
    00321   }
    00322
    00323 }
    00324
    00325 void ShowRecord::setSize( int number, long size )
    00326 {
    00327   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00328   ShowRecordElem* mail;                           //current mail
    00329   bool found = false;                             //True, when the wanted mail was found
    00330
    00331   //looking for the mail with the number 'number'
    00332   while( ( mail = it.current() ) != NULL && !found )
    00333   {
    00334     //increment iterator to next mail
    00335     ++it;
    00336
    00337     //if the current mail has the given number, decode the mail
    00338     if( mail->number() == number )
    00339     {
    00340       mail->setSize( size );
    00341       found = true;
    00342     }
    00343   }
    00344 }
    00345
    00346 Types::MailNumberList_Type ShowRecord::getNewMails( )
    00347 {
    00348   MailNumberList_Type list;                     //contains the numbers of the new mails
    00349   ShowRecordElem* mail;                         //mail from which we want to get the number (if new)
    00350   TQPtrListIterator<ShowRecordElem> it( *this ); //iterator for the mail list
    00351
    00352   //iterate over all mails
    00353   while( ( mail = it.current() ) != NULL )
    00354   {
    00355     //increment iterator to next mail
    00356     ++it;
    00357
    00358     //if current mail is new append its number to the mail number list
    00359     if( mail->isNew() )
    00360       list.append( mail->number() );
    00361   }
    00362
    00363   return list;
    00364 }
    00365
    00366 void ShowRecord::setHeader( int number, TQString header )
    00367 {
    00368   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00369   ShowRecordElem* mail;                           //current mail
    00370   bool found = false;                             //True, when the wanted mail was found
    00371
    00372   //looking for the mail with the number 'number'
    00373   while( ( mail = it.current() ) != NULL && !found )
    00374   {
    00375     //increment iterator to next mail
    00376     ++it;
    00377
    00378     //if the current mail has the given number, set the header
    00379     if( mail->number() == number )
    00380     {
    00381       mail->setHeader( header );
    00382       found = true;
    00383     }
    00384   }
    00385 }
    00386
    00387 TQStringList ShowRecord::getUIDsOfOldMails( )
    00388 {
    00389   TQStringList list;                             //contains the UIDs of the old mails
    00390   ShowRecordElem* mail;                         //mail from which we want to get the uid (if old)
    00391   TQPtrListIterator<ShowRecordElem> it( *this ); //iterator for the mail list
    00392
    00393   //iterate over all mails
    00394   while( ( mail = it.current() ) != NULL )
    00395   {
    00396     //increment iterator to next mail
    00397     ++it;
    00398
    00399     //if current mail is new append its number to the mail number list
    00400     if( !mail->isNew() )
    00401       list.append( mail->uidl() );
    00402   }
    00403
    00404   return list;
    00405 }
    00406
    00407 TQString ShowRecord::getHeaderOf( TQString uid )
    00408 {
    00409   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00410   ShowRecordElem* mail;                           //current mail
    00411   bool found = false;                             //True, when the wanted mail was found
    00412   TQString header;                                 //header of the wanted mail
    00413
    00414   //looking for the mail with the UID 'uid'
    00415   while( ( mail = it.current() ) != NULL && !found )
    00416   {
    00417     //increment iterator to next mail
    00418     ++it;
    00419
    00420     //if the current mail has the given uid, get the header
    00421     if( mail->uidl() == uid )
    00422     {
    00423       header = mail->header();
    00424       found = true;
    00425     }
    00426   }
    00427   return header;
    00428 }
    00429
    00430 void ShowRecord::setHeader( TQString uid, TQString header )
    00431 {
    00432   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00433   ShowRecordElem* mail;                           //current mail
    00434   bool found = false;                             //True, when the wanted mail was found
    00435
    00436   //looking for the mail with the UID 'uid'
    00437   while( ( mail = it.current() ) != NULL && !found )
    00438   {
    00439     //increment iterator to next mail
    00440     ++it;
    00441
    00442     //if the current mail has the given UID, set the header
    00443     if( mail->uidl() == uid )
    00444     {
    00445       mail->setHeader( header );
    00446       found = true;
    00447     }
    00448   }
    00449 }
    00450
    00451 int ShowRecord::getNumberNewMails( )
    00452 {
    00453   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00454   ShowRecordElem* mail;                           //current mail
    00455   int number = 0;                                 //number of new mails
    00456
    00457   while( ( mail = it.current() ) )
    00458   {
    00459     //increment iterator to next mail
    00460     ++it;
    00461
    00462     //increment number, if this mail is new
    00463     if( mail->isNew() )
    00464       number++;
    00465   }
    00466
    00467   return number;
    00468 }
    00469
    00470 int ShowRecord::getNumberMails( )
    00471 {
    00472   return count();
    00473 }
    00474
    00475 long ShowRecord::getTotalSize( )
    00476 {
    00477   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00478   ShowRecordElem* mail;                           //current mail
    00479   long size = 0;                                   //total size of all mails
    00480
    00481   while( ( mail = it.current() ) )
    00482   {
    00483     //increment iterator to next mail
    00484     ++it;
    00485
    00486     size += mail->size();
    00487   }
    00488
    00489   return size;
    00490 }
    00491
    00492 void ShowRecord::fillMailListView( KshowmailView * view, TQString & account )
    00493 {
    00494   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00495   ShowRecordElem* mail;                           //current mail
    00496
    00497   //iterate over all mails and order the mail to create a list view item
    00498   while( ( mail = it.current() ) )
    00499   {
    00500     //increment iterator to next mail
    00501     ++it;
    00502
    00503     //insert list view item
    00504     TQString number = TQString( "%1" ).arg( mail->number() );
    00505     TQString from = mail->from();
    00506     TQString to = mail->to();
    00507     TQString subject = mail->subject();
    00508     TQString date = mail->date();
    00509     TQString size = TQString( "%1" ).arg( mail->size() );
    00510     TQString content = mail->content();
    00511     TQString state = mail->state();
    00512     TQString time = mail->strUnixTime();
    00513     mail->setViewItem( view->insertMail( number, account, from, to, subject, date, size, content, state, time ) );
    00514   }
    00515
    00516 }
    00517
    00518 int ShowRecord::showSelectedHeaders( TQString& account )
    00519 {
    00520   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00521   ShowRecordElem* mail;                           //current mail
    00522   int showNextHeader = ShowRecordElem::continueShowHeaders; //return value of ShowRecordElem::showHeader()
    00523
    00524   while( ( mail = it.current() ) && showNextHeader == ShowRecordElem::continueShowHeaders )
    00525   {
    00526     //increment iterator to next mail
    00527     ++it;
    00528
    00529     //order the mail to show its header
    00530     if( mail->isSelected() )
    00531       showNextHeader = mail->showHeader( account );
    00532   }
    00533
    00534   return showNextHeader == ShowRecordElem::continueShowHeaders ? ShowRecord::continueShowHeaders : ShowRecord::cancelShowHeaders;
    00535 }
    00536
    00537 bool ShowRecord::isNew( TQString uid ) const
    00538 {
    00539   TQPtrListIterator<ShowRecordElem> it( *this );   //iterator for the mail list
    00540   ShowRecordElem* mail;                           //current mail
    00541   bool found = false;                             //True, when the wanted mail was found
    00542   bool newMail = false;                           //at time we have not found it, therefore the return value is false
    00543
    00544   while( ( mail = it.current() ) != NULL && !found )
    00545   {
    00546     //increment iterator to next mail
    00547     ++it;
    00548
    00549     //compare the uid
    00550     if( mail->uidl() == uid )
    00551     {
    00552       found = true;
    00553       newMail = mail->isNew();
    00554     }
    00555   }
    00556
    00557   return newMail;
    00558 }