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

List:       kde-commits
Subject:    playground/pim/akonadi/filter
From:       Szymon Stefanek <pragma () kvirc ! net>
Date:       2009-08-21 4:09:37
Message-ID: 1250827777.684033.10524.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 1013885 by stefanek:

Support for cloning the filtering tree. Quite useful if we plan to execute the same \
program on multiple threads.

 M  +1 -1      akonadi/filter/componentfactory.cpp  
 M  +14 -1     akonadi/filter/componentfactory.h  
 M  +34 -2     akonadi/filter/program.cpp  
 M  +27 -1     akonadi/filter/program.h  
 M  +9 -1      console/mainwindow.cpp  


--- trunk/playground/pim/akonadi/filter/akonadi/filter/componentfactory.cpp \
#1013884:1013885 @@ -346,7 +346,7 @@
 
 Program * ComponentFactory::createProgram()
 {
-  return new Program();
+  return new Program( this );
 }
 
 Rule * ComponentFactory::createRule( Component * parent )
--- trunk/playground/pim/akonadi/filter/akonadi/filter/componentfactory.h \
#1013884:1013885 @@ -68,13 +68,26 @@
   class RuleList;
 } // namespace Action
 
+/**
+ * The feature bits used for logically matching components in the editors and
+ * the IO classes.
+ */
 enum Features
 {
+  /**
+   * The component/object contains a QDateTime field.
+   */
   FeatureContainsDate = 1,
 
+  /**
+   * The first bit that can be used for custom features.
+   * Use this bit in your own enumeration and shift it one
+   * position left at each feature you add.
+   */
   FeatureCustomFirstBit = (1 << 1)
-};
 
+}; // enum Features
+
 /**
  * The Akonadi::Filter::ComponentFactory class plays a very central role in the \
                filter management and customisation.
  *
--- trunk/playground/pim/akonadi/filter/akonadi/filter/program.cpp #1013884:1013885
@@ -27,16 +27,23 @@
 
 #include <QObject>
 
+#include <KLocale>
 #include <KDebug>
 
+#include "componentfactory.h"
+
+#include "io/sieveencoder.h"
+#include "io/sievedecoder.h"
+
 namespace Akonadi
 {
 namespace Filter 
 {
 
-Program::Program()
-  : Action::RuleList( 0 ), PropertyBag()
+Program::Program( ComponentFactory * factory )
+  : Action::RuleList( 0 ), PropertyBag(), mComponentFactory( factory )
 {
+  Q_ASSERT( mComponentFactory );
 }
 
 Program::~Program()
@@ -64,6 +71,31 @@
   dumpRuleList( prefix );
 }
 
+Program * Program::clone()
+{
+  errorStack().clearErrors();
+
+  IO::SieveEncoder encoder;
+  QByteArray serialized = encoder.run( this );
+  if( serialized.isEmpty() )
+  {
+    errorStack().pushErrorStack( encoder.errorStack() );
+    errorStack().pushError( i18n( "Encoding of the program into Sieve source failed" \
) ); +    return 0;
+  }
+
+  IO::SieveDecoder decoder( mComponentFactory );
+  Program * prog = decoder.run( serialized );
+  if( !prog )
+  {
+    errorStack().pushErrorStack( decoder.errorStack() );
+    errorStack().pushError( i18n( "Decoding of the program from Sieve source failed" \
) ); +    return 0;
+  }
+
+  return prog;
+}
+
 } // namespace Filter
 
 } // namespace Akonadi
--- trunk/playground/pim/akonadi/filter/akonadi/filter/program.h #1013884:1013885
@@ -46,6 +46,8 @@
 namespace Filter
 {
 
+class ComponentFactory;
+
 /**
  * @class Akonadi::Filter::Program
  * @brief A complete filtering program.
@@ -104,14 +106,26 @@
 
   /**
    * Creates an empty filtering program.
+   *
+   * @param factory The ComponentFactory used to create this Program. The ownership
+   *        of the factory is NOT transferred and the caller must ensure the \
validity +   *        of the pointer through the entire lifetime of the Program \
                object.
    */
-  Program();
+  Program( ComponentFactory * factory );
 
   /**
    * Destroys the filtering program including any included rules.
    */
   virtual ~Program();
 
+protected:
+  /**
+   * The component factory used to create this Program object.
+   * This is a shallow pointer: the creator of this object must ensure
+   * its validity through the entire lifetime.
+   */
+  ComponentFactory * mComponentFactory;
+
 public:
 
   /**
@@ -127,6 +141,18 @@
   void setName( const QString &name );
 
   /**
+   * Creates an exact clone of this Program. 
+   * The ownership of the newly created tree is given to the caller.
+   * If cloning fails for some reason then 0 is returned and a detailed
+   * error is made available via errorStack().
+   * 
+   * You shouldn't need to override this as long as all your components can be \
serialized +   * via IO::SieveEncoder / IO::SieveDecoder. This is true for all the \
"naked" components +   * provided by this library, even if customized via \
ComponentFactory. +   */
+  virtual Program * clone();
+
+  /**
    * Reimplemented from Component. Returns true.
    */
   virtual bool isProgram() const;
--- trunk/playground/pim/akonadi/filter/console/mainwindow.cpp #1013884:1013885
@@ -449,8 +449,16 @@
   filter.setMimeType( mimeType );
   filter.setComponentFactory( componentFactory );
   filter.setEditorFactory( editorFactory );
-  filter.setProgram( new Akonadi::Filter::Program() );
 
+  Akonadi::Filter::Program * prog = componentFactory->createProgram();
+  if( !prog )
+  {
+    KMessageBox::error( this, i18n( "The component factory failed to create the \
filtering program instance" ), i18n( "Internal error" ) ); +    return;
+  }
+
+  filter.setProgram( prog );
+
   FilterEditor ed( this, &filter, lbb );
   if( ed.exec() != KDialog::Accepted )
     return;


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

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