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

kjots

  • sources
  • kde-4.14
  • kdepim
  • kjots
localresourcecreator.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Klarälvdalens Datakonsult AB,
3  a KDAB Group company, info@kdab.net,
4  author Stephen Kelly <stephen@kdab.com>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "localresourcecreator.h"
23 
24 #include <Akonadi/Notes/NoteUtils>
25 
26 #include <KDebug>
27 #include <KLocalizedString>
28 #include <KRandom>
29 #include <Akonadi/CollectionFetchJob>
30 #include <Akonadi/AgentInstance>
31 #include <Akonadi/AgentManager>
32 #include <Akonadi/CollectionCreateJob>
33 #include <Akonadi/ItemCreateJob>
34 #include <akonadi/item.h>
35 #include <KMime/KMimeMessage>
36 #include <Akonadi/EntityDisplayAttribute>
37 
38 LocalResourceCreator::LocalResourceCreator(QObject* parent)
39  : NoteShared::LocalResourceCreator(parent)
40 {
41 
42 }
43 
44 void LocalResourceCreator::finishCreateResource()
45 {
46  Akonadi::CollectionFetchJob *collectionFetchJob = new Akonadi::CollectionFetchJob( Akonadi::Collection::root(), Akonadi::CollectionFetchJob::FirstLevel, this );
47  connect( collectionFetchJob, SIGNAL(result(KJob*)), SLOT(rootFetchFinished(KJob*)) );
48 }
49 
50 void LocalResourceCreator::rootFetchFinished(KJob* job)
51 {
52  if (job->error()) {
53  kWarning() << job->errorString();
54  deleteLater();
55  return;
56  }
57 
58  Akonadi::CollectionFetchJob *lastCollectionFetchJob = qobject_cast<Akonadi::CollectionFetchJob*>(job);
59  if (!lastCollectionFetchJob) {
60  deleteLater();
61  return;
62  }
63 
64  Akonadi::Collection::List list = lastCollectionFetchJob->collections();
65 
66  if (list.isEmpty())
67  {
68  kWarning() << "Couldn't find new collection in resource";
69  deleteLater();
70  return;
71  }
72 
73  foreach (const Akonadi::Collection &col, list)
74  {
75  Akonadi::AgentInstance instance = Akonadi::AgentManager::self()->instance(col.resource());
76  if (instance.type().identifier() == akonadiNotesInstanceName())
77  {
78  Akonadi::CollectionFetchJob *collectionFetchJob = new Akonadi::CollectionFetchJob( col, Akonadi::CollectionFetchJob::FirstLevel, this );
79  collectionFetchJob->setProperty("FetchedCollection", col.id());
80  connect( collectionFetchJob, SIGNAL(result(KJob*)), SLOT(topLevelFetchFinished(KJob*)) );
81  return;
82  }
83  }
84  Q_ASSERT(!"Couldn't find new collection");
85  deleteLater();
86 }
87 
88 void LocalResourceCreator::topLevelFetchFinished(KJob* job)
89 {
90  if (job->error()) {
91  kWarning() << job->errorString();
92  deleteLater();
93  return;
94  }
95 
96  Akonadi::CollectionFetchJob *lastCollectionFetchJob = qobject_cast<Akonadi::CollectionFetchJob*>(job);
97  if (!lastCollectionFetchJob) {
98  deleteLater();
99  return;
100  }
101 
102  Akonadi::Collection::List list = lastCollectionFetchJob->collections();
103 
104  if (!list.isEmpty())
105  {
106  deleteLater();
107  return;
108  }
109 
110  Akonadi::Collection::Id id = lastCollectionFetchJob->property("FetchedCollection").toLongLong();
111 
112  Akonadi::Collection collection;
113  collection.setParentCollection( Akonadi::Collection(id) );
114  QString title = i18nc( "The default name for new books.", "New Book" );
115  collection.setName( KRandom::randomString( 10 ) );
116  collection.setContentMimeTypes( QStringList() << Akonadi::Collection::mimeType() << Akonadi::NoteUtils::noteMimeType() );
117 
118  Akonadi::EntityDisplayAttribute *eda = new Akonadi::EntityDisplayAttribute();
119  eda->setIconName( QLatin1String("x-office-address-book") );
120  eda->setDisplayName( title );
121  collection.addAttribute(eda);
122 
123  Akonadi::CollectionCreateJob *createJob = new Akonadi::CollectionCreateJob( collection, this );
124  connect( createJob, SIGNAL(result(KJob*)), this, SLOT(createFinished(KJob*)) );
125 
126 }
127 
128 void LocalResourceCreator::createFinished(KJob* job)
129 {
130  if (job->error()) {
131  kWarning() << job->errorString();
132  deleteLater();
133  return;
134  }
135 
136  Akonadi::CollectionCreateJob* collectionCreateJob = qobject_cast<Akonadi::CollectionCreateJob*>(job);
137  if (!collectionCreateJob)
138  {
139  deleteLater();
140  return;
141  }
142 
143  Akonadi::Item item;
144  item.setParentCollection(collectionCreateJob->collection());
145  item.setMimeType( Akonadi::NoteUtils::noteMimeType() );
146 
147  KMime::Message::Ptr note( new KMime::Message() );
148 
149  QString title = i18nc( "The default name for new pages.", "New Page" );
150  QByteArray encoding( "utf-8" );
151 
152  note->subject( true )->fromUnicodeString( title, encoding );
153  note->contentType( true )->setMimeType( "text/plain" );
154  note->date( true )->setDateTime( KDateTime::currentLocalDateTime() );
155  note->from( true )->fromUnicodeString( QLatin1String("Kjots@kde4"), encoding );
156  // Need a non-empty body part so that the serializer regards this as a valid message.
157  note->mainBodyPart()->fromUnicodeString( QLatin1String(" ") );
158 
159  note->assemble();
160 
161  item.setPayload(note);
162  Akonadi::EntityDisplayAttribute *eda = new Akonadi::EntityDisplayAttribute();
163  eda->setIconName( QLatin1String("text-plain") );
164  item.addAttribute(eda);
165 
166  Akonadi::ItemCreateJob *itemCreateJob = new Akonadi::ItemCreateJob( item, collectionCreateJob->collection(), this);
167  connect( itemCreateJob, SIGNAL(result(KJob*)), SLOT(itemCreateFinished(KJob*)) );
168 }
169 
170 void LocalResourceCreator::itemCreateFinished(KJob* job)
171 {
172  if (job->error()) {
173  kWarning() << job->errorString();
174  }
175  deleteLater();
176 }
177 
178 
179 
180 
181 
182 
LocalResourceCreator
Creates a notes resource, a book and a page if one does not already exist.
Definition: localresourcecreator.h:34
QByteArray
LocalResourceCreator::LocalResourceCreator
LocalResourceCreator(QObject *parent=0)
Definition: localresourcecreator.cpp:38
localresourcecreator.h
LocalResourceCreator::finishCreateResource
virtual void finishCreateResource()
Definition: localresourcecreator.cpp:44
QObject
QString
QStringList
QLatin1String
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:12 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kjots

Skip menu "kjots"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

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