• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • akonadi
  • xml
xmlwritejob.cpp
1 /*
2  Copyright (c) 2009 Volker Krause <vkrause@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "xmlwritejob.h"
21 #include "xmldocument.h"
22 #include "xmlwriter.h"
23 
24 #include <akonadi/collection.h>
25 #include <akonadi/collectionfetchjob.h>
26 #include <akonadi/item.h>
27 #include <akonadi/itemfetchjob.h>
28 #include <akonadi/itemfetchscope.h>
29 
30 #include <KDebug>
31 
32 #include <QDomElement>
33 #include <QFile>
34 #include <QStack>
35 
36 using namespace Akonadi;
37 
38 namespace Akonadi {
39 
40 class XmlWriteJobPrivate {
41  public:
42  XmlWriteJobPrivate( XmlWriteJob* parent ) : q( parent ) {}
43 
44  XmlWriteJob* const q;
45  Collection::List roots;
46  QStack<Collection::List> pendingSiblings;
47  QStack<QDomElement> elementStack;
48  QString fileName;
49  XmlDocument document;
50 
51  void collectionFetchResult( KJob* job );
52  void processCollection();
53  void itemFetchResult( KJob* job );
54  void processItems();
55 };
56 
57 }
58 
59 void XmlWriteJobPrivate::collectionFetchResult(KJob* job)
60 {
61  if ( job->error() )
62  return;
63  CollectionFetchJob *fetch = dynamic_cast<CollectionFetchJob*>( job );
64  Q_ASSERT( fetch );
65  if ( fetch->collections().isEmpty() ) {
66  processItems();
67  } else {
68  pendingSiblings.push( fetch->collections() );
69  processCollection();
70  }
71 }
72 
73 void XmlWriteJobPrivate::processCollection()
74 {
75  if ( !pendingSiblings.isEmpty() && pendingSiblings.top().isEmpty() ) {
76  pendingSiblings.pop();
77  if ( pendingSiblings.isEmpty() ) {
78  q->done();
79  return;
80  }
81  processItems();
82  return;
83  }
84 
85  if ( pendingSiblings.isEmpty() ) {
86  q->done();
87  return;
88  }
89 
90  const Collection current = pendingSiblings.top().first();
91  kDebug() << "Writing " << current.name() << "into" << elementStack.top().attribute( QLatin1String("name") );
92  elementStack.push( XmlWriter::writeCollection( current, elementStack.top() ) );
93  CollectionFetchJob *subfetch = new CollectionFetchJob( current, CollectionFetchJob::FirstLevel, q );
94  q->connect( subfetch, SIGNAL(result(KJob*)), q, SLOT(collectionFetchResult(KJob*)) );
95 }
96 
97 void XmlWriteJobPrivate::processItems()
98 {
99  const Collection collection = pendingSiblings.top().first();
100  ItemFetchJob *fetch = new ItemFetchJob( collection, q );
101  fetch->fetchScope().fetchAllAttributes();
102  fetch->fetchScope().fetchFullPayload();
103  q->connect( fetch, SIGNAL(result(KJob*)), q, SLOT(itemFetchResult(KJob*)) );
104 }
105 
106 void XmlWriteJobPrivate::itemFetchResult(KJob* job)
107 {
108  if ( job->error() )
109  return;
110  ItemFetchJob *fetch = dynamic_cast<ItemFetchJob*>( job );
111  Q_ASSERT( fetch );
112  foreach ( const Item &item, fetch->items() )
113  XmlWriter::writeItem( item, elementStack.top() );
114  pendingSiblings.top().removeFirst();
115  elementStack.pop();
116  processCollection();
117 }
118 
119 
120 XmlWriteJob::XmlWriteJob(const Collection& root, const QString& fileName, QObject* parent) :
121  Job( parent ),
122  d( new XmlWriteJobPrivate( this ) )
123 {
124  d->roots.append( root );
125  d->fileName = fileName;
126 }
127 
128 
129 XmlWriteJob::XmlWriteJob(const Collection::List& roots, const QString& fileName, QObject* parent) :
130  Job( parent ),
131  d( new XmlWriteJobPrivate( this ) )
132 {
133  d->roots = roots;
134  d->fileName = fileName;
135 }
136 
137 
138 XmlWriteJob::~XmlWriteJob()
139 {
140  delete d;
141 }
142 
143 void XmlWriteJob::doStart()
144 {
145  d->elementStack.push( d->document.document().documentElement() );
146  CollectionFetchJob *job = new CollectionFetchJob( d->roots, this );
147  connect( job, SIGNAL(result(KJob*)), SLOT(collectionFetchResult(KJob*)) );
148 }
149 
150 void XmlWriteJob::done() // cannot be in the private class due to emitResult()
151 {
152  if ( !d->document.writeToFile( d->fileName ) ) {
153  setError( Unknown );
154  setErrorText( d->document.lastError() );
155  }
156  emitResult();
157 }
158 
159 #include "moc_xmlwritejob.cpp"
Akonadi::CollectionFetchJob::collections
Collection::List collections() const
Returns the list of fetched collection.
Definition: collectionfetchjob.cpp:175
Akonadi::Collection::name
QString name() const
Returns the i18n'ed name of the collection.
Definition: collection.cpp:81
Akonadi::ItemFetchScope::fetchAllAttributes
void fetchAllAttributes(bool fetch=true)
Sets whether all available attributes should be fetched.
Definition: itemfetchscope.cpp:91
Akonadi::Job::Unknown
Unknown error.
Definition: job.h:109
Akonadi::XmlWriteJob
Serializes a given Akonadi collection into a XML file.
Definition: xmlwritejob.h:35
Akonadi::CollectionFetchJob::FirstLevel
Only list direct sub-collections of the base collection.
Definition: collectionfetchjob.h:63
Akonadi::XmlDocument
Represents a document of the KNUT XML serialization format for Akonadi objects.
Definition: xmldocument.h:37
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::CollectionFetchJob
Job that fetches collections from the Akonadi storage.
Definition: collectionfetchjob.h:53
Akonadi::Job
Base class for all actions in the Akonadi storage.
Definition: job.h:86
Akonadi::XmlWriter::writeCollection
AKONADI_XML_EXPORT QDomElement writeCollection(const Collection &collection, QDomElement &parentElem)
Serializes the given collection into a DOM element with the given parent.
Definition: xmlwriter.cpp:69
Akonadi::ItemFetchScope::fetchFullPayload
void fetchFullPayload(bool fetch=true)
Sets whether the full payload shall be fetched.
Definition: itemfetchscope.cpp:68
Akonadi::ItemFetchJob::items
Item::List items() const
Returns the fetched items.
Definition: itemfetchjob.cpp:228
Akonadi::ItemFetchJob::fetchScope
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Definition: itemfetchjob.cpp:256
Akonadi::XmlWriteJob::doStart
void doStart()
This method must be reimplemented in the concrete jobs.
Definition: xmlwritejob.cpp:143
Akonadi::XmlWriter::writeItem
AKONADI_XML_EXPORT QDomElement writeItem(const Akonadi::Item &item, QDomElement &parentElem)
Serializes the given item into a DOM element and attaches it to the given item.
Akonadi::ItemFetchJob
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:82
Akonadi::Collection::List
QList< Collection > List
Describes a list of collections.
Definition: collection.h:81
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:28 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal