[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdepim/akonadi/resources/mbox/libmbox
From:       Bertjan Broeksema <b.broeksema () home ! nl>
Date:       2009-06-18 14:35:58
Message-ID: 1245335758.292395.21086.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 983605 by bbroeksema:

- Make sure that entries are only added when a file is loaded.
- Add tests for appendEntry.


 M  +24 -7     mbox.cpp  
 M  +8 -2      mbox.h  
 M  +36 -1     tests/mboxtest.cpp  
 M  +5 -0      tests/mboxtest.h  


--- trunk/KDE/kdepim/akonadi/resources/mbox/libmbox/mbox.cpp #983604:983605
@@ -88,7 +88,7 @@
 {
   // Set some sane defaults
   d->mFileLocked = false;
-  d->mLockType = None;
+  d->mLockType = None; //
 }
 
 MBox::~MBox()
@@ -103,6 +103,9 @@
 
 qint64 MBox::appendEntry( const MessagePtr &entry )
 {
+  if ( d->mMboxFile.fileName().isEmpty() )
+    return -1; // It doesn't make sense to add entries when we don't have an reference file.
+
   const QByteArray rawEntry = escapeFrom( entry->encodedContent() );
 
   if ( rawEntry.size() <= 0 ) {
@@ -115,9 +118,20 @@
 
   // Make sure the byte array is large enough to check for an end character.
   // Then check if the required newlines are there.
-  if ( nextOffset >= 2 ) {
-    if ( nextOffset > 0 && d->mAppendedEntries.at( nextOffset - 1 ) != '\n' ) {
-      if ( d->mAppendedEntries.at( nextOffset - 1 ) != '\n' ) {
+  if ( nextOffset < 1 ) { // Empty, add one empty line
+    if ( d->mMboxFile.size() == 0 ) {
+      d->mAppendedEntries.append( "\n");
+      ++nextOffset;
+    }
+  } else if ( nextOffset == 1 && d->mAppendedEntries.at( 0 ) != '\n' ) {
+    // This should actually not happen, but catch it anyway.
+    if (d->mMboxFile.size() < 0 ) {
+      d->mAppendedEntries.append( "\n");
+      ++nextOffset;
+    }
+  } else {
+    if ( d->mAppendedEntries.at( nextOffset - 1 ) != '\n' ) {
+      if ( d->mAppendedEntries.at( nextOffset ) != '\n' ) {
         d->mAppendedEntries.append( "\n\n" );
         nextOffset += 2;
       } else {
@@ -133,6 +147,11 @@
     d->mAppendedEntries.append( "\n\n" );
   }
 
+  MsgInfo info;
+  info.first = d->mInitialMboxFileSize + nextOffset;
+  info.second = rawEntry.size();
+  d->mEntries << info;
+
   return d->mInitialMboxFileSize + nextOffset;
 }
 
@@ -195,9 +214,7 @@
     }
   }
 
-  unlock(); // FIXME: What if unlock fails?
-
-  return true;
+  return unlock(); // FIXME: What if unlock fails?
 }
 
 bool MBox::lock()
--- trunk/KDE/kdepim/akonadi/resources/mbox/libmbox/mbox.h #983604:983605
@@ -45,14 +45,20 @@
     MBox();
 
     /**
-     * Closes the file if it is still open.
+     * Unlocks the file if it is still open.
      */
     ~MBox();
 
     /**
      * Appends @param entry to the MBox. Returns the offset in the file
      * where the added message starts or -1 if the entry was not added (e.g.
-     * when it doesn't contain data).
+     * when it doesn't contain data). Entries are only added after a call to
+     * load( const QString& ). The returned offset is <em>only</em> valid for
+     * that particular file.
+     *
+     * @param entry The message to append to the mbox.
+     * @return the offset of the entry in the file or -1 if the entry was not
+     *         added.
      */
     qint64 appendEntry( const MessagePtr &entry );
 
--- trunk/KDE/kdepim/akonadi/resources/mbox/libmbox/tests/mboxtest.cpp #983604:983605
@@ -29,7 +29,7 @@
 
 QTEST_KDEMAIN_CORE(MboxTest)
 
-#include "../mbox.h"
+#include "test-entries.h"
 
 static const char * testDir = "libmbox-unit-test";
 static const char * testFile = "test-mbox-file";
@@ -56,6 +56,15 @@
   mboxfile.open( QIODevice::WriteOnly );
   mboxfile.close();
   QVERIFY(mboxfile.exists());
+
+  mMail1 = MessagePtr( new KMime::Message );
+  mMail1->setContent( KMime::CRLFtoLF( sEntry1 ) );
+  mMail1->parse();
+
+  mMail2 = MessagePtr( new KMime::Message );
+  mMail2->setContent( KMime::CRLFtoLF( sEntry2 ) );
+  mMail2->parse();
+
 }
 
 void MboxTest::testSetLockMethod()
@@ -134,6 +143,32 @@
   QVERIFY( !QFile( lockFileName() ).exists() );
 }
 
+void MboxTest::testAppend()
+{
+  QFileInfo info( fileName() );
+  QCOMPARE( info.size(), static_cast<qint64>( 0 ) );
+
+  MBox mbox;
+  mbox.setLockType( MBox::None );
+
+  // When no file is loaded no entries should get added to the mbox.
+  QCOMPARE( mbox.entryList().size(), 0 );
+  QCOMPARE( mbox.appendEntry( mMail1 ), static_cast<qint64>( -1 ) );
+  QCOMPARE( mbox.entryList().size(), 0 );
+
+  QVERIFY( mbox.load( fileName() ) );
+
+  // First message added to an emtpy file should be at offset 0
+  QCOMPARE( mbox.entryList().size(), 0 );
+  QCOMPARE( mbox.appendEntry( mMail1 ), static_cast<qint64>( 0 ) );
+  QCOMPARE( mbox.entryList().size(), 1 );
+  QCOMPARE( mbox.entryList().first().second, static_cast<quint64>( sEntry1.size() ) );
+
+  QVERIFY( mbox.appendEntry( mMail2 ) > sEntry1.size() );
+  QCOMPARE( mbox.entryList().size(), 2 );
+  QCOMPARE( mbox.entryList().last().second, static_cast<quint64>( sEntry2.size() ) );
+}
+
 void MboxTest::cleanupTestCase()
 {
   mTempDir->unlink();
--- trunk/KDE/kdepim/akonadi/resources/mbox/libmbox/tests/mboxtest.h #983604:983605
@@ -22,6 +22,8 @@
 
 #include <QObject>
 
+#include "../mbox.h"
+
 class KTempDir;
 
 class MboxTest : public QObject
@@ -32,6 +34,7 @@
     void testSetLockMethod();
     void testLockBeforeLoad();
     void testProcMailLock();
+    void testAppend();
     void cleanupTestCase();
 
   private:
@@ -40,6 +43,8 @@
 
   private:
     KTempDir *mTempDir;
+    MessagePtr mMail1;
+    MessagePtr mMail2;
 };
 
 #endif // MBOXTEST_H
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic