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

akonadi_next

  • sources
  • kde-4.12
  • kdepim
  • akonadi_next
notecreatorandselector.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 "notecreatorandselector.h"
23 
24 #include <KLocale>
25 
26 #include <KMime/Message>
27 
28 #include <akonadi/item.h>
29 #include <akonadi/entitydisplayattribute.h>
30 #include <akonadi/itemcreatejob.h>
31 
32 #include "note.h"
33 #include <Akonadi/EntityTreeModel>
34 
35 using namespace Akonadi;
36 
37 using namespace Akonotes;
38 
39 NoteCreatorAndSelector::NoteCreatorAndSelector(QItemSelectionModel* primaryModel, QItemSelectionModel* secondaryModel, QObject* parent)
40  : QObject(parent),
41  m_primarySelectionModel(primaryModel),
42  m_secondarySelectionModel(secondaryModel == 0 ? primaryModel : secondaryModel),
43  m_containerCollectionId(-1),
44  m_newNoteId(-1),
45  m_giveupTimer(new QTimer(this))
46 {
47  // If the note doesn't exist after 5 seconds, give up waiting for it.
48  m_giveupTimer->setInterval(5000);
49  connect(m_giveupTimer, SIGNAL(timeout()), SLOT(deleteLater()));
50 }
51 
52 NoteCreatorAndSelector::~NoteCreatorAndSelector()
53 {
54 }
55 
56 
57 void NoteCreatorAndSelector::createNote(const Akonadi::Collection& containerCollection)
58 {
59  m_containerCollectionId = containerCollection.id();
60 
61  if (m_primarySelectionModel == m_secondarySelectionModel)
62  doCreateNote();
63  else
64  {
65  m_giveupTimer->start();
66  connect(m_primarySelectionModel->model(), SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(trySelectCollection()));
67  trySelectCollection();
68  }
69 }
70 
71 void NoteCreatorAndSelector::trySelectCollection()
72 {
73  QModelIndex idx = EntityTreeModel::modelIndexForCollection(m_primarySelectionModel->model(), Collection(m_containerCollectionId));
74  if (!idx.isValid())
75  return;
76 
77  m_giveupTimer->stop();
78  m_primarySelectionModel->select(QItemSelection(idx, idx), QItemSelectionModel::Select);
79  disconnect(m_primarySelectionModel->model(), SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(trySelectCollection()));
80  doCreateNote();
81 }
82 
83 void NoteCreatorAndSelector::doCreateNote()
84 {
85  Item newItem;
86  newItem.setMimeType( Note::mimeType() );
87 
88  KMime::Message::Ptr newPage = KMime::Message::Ptr( new KMime::Message() );
89 
90  QString title = i18nc( "The default name for new pages.", "New Page" );
91  QByteArray encoding( "utf-8" );
92 
93  newPage->subject( true )->fromUnicodeString( title, encoding );
94  newPage->contentType( true )->setMimeType( "text/plain" );
95  newPage->contentType()->setCharset("utf-8");
96  newPage->contentTransferEncoding(true)->setEncoding(KMime::Headers::CEquPr);
97  newPage->date( true )->setDateTime( KDateTime::currentLocalDateTime() );
98  newPage->from( true )->fromUnicodeString( QString::fromLatin1( "Kjots@kde4" ), encoding );
99  // Need a non-empty body part so that the serializer regards this as a valid message.
100  newPage->mainBodyPart()->fromUnicodeString( QString::fromLatin1( " " ) );
101 
102  newPage->assemble();
103 
104  newItem.setPayload( newPage );
105 
106  Akonadi::EntityDisplayAttribute *eda = new Akonadi::EntityDisplayAttribute();
107  eda->setIconName( QString::fromLatin1( "text-plain" ) );
108  newItem.addAttribute(eda);
109 
110  Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob( newItem, Collection(m_containerCollectionId), this );
111  connect( job, SIGNAL(result(KJob*)), SLOT(noteCreationFinished(KJob*)) );
112 
113 }
114 
115 void NoteCreatorAndSelector::noteCreationFinished(KJob* job)
116 {
117  if (job->error())
118  {
119  kWarning() << job->errorString();
120  return;
121  }
122  Akonadi::ItemCreateJob *createJob = qobject_cast<Akonadi::ItemCreateJob*>(job);
123  Q_ASSERT(createJob);
124 
125  Item newItem = createJob->item();
126  m_newNoteId = newItem.id();
127 
128  m_giveupTimer->start();
129  connect(m_primarySelectionModel->model(), SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(trySelectNote()));
130  trySelectNote();
131 }
132 
133 void NoteCreatorAndSelector::trySelectNote()
134 {
135  QModelIndexList list = EntityTreeModel::modelIndexesForItem(m_secondarySelectionModel->model(), Item(m_newNoteId));
136  if (list.isEmpty())
137  return;
138 
139  const QModelIndex idx = list.first();
140  m_secondarySelectionModel->select(QItemSelection(idx, idx), QItemSelectionModel::ClearAndSelect);
141 }
142 
notecreatorandselector.h
QObject
Akonotes::Note::mimeType
static QString mimeType()
Definition: note.cpp:24
Akonotes::NoteCreatorAndSelector::createNote
void createNote(const Akonadi::Collection &containerCollection)
Definition: notecreatorandselector.cpp:57
Akonotes::NoteCreatorAndSelector::~NoteCreatorAndSelector
virtual ~NoteCreatorAndSelector()
Definition: notecreatorandselector.cpp:52
note.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:54:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi_next

Skip menu "akonadi_next"
  • 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

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