2 Copyright (C) 2010 Klarälvdalens Datakonsult AB,
3 a KDAB Group company, info@kdab.net,
4 author Stephen Kelly <stephen@kdab.com>
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.
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.
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
22 #include "notecreatorandselector.h"
26 #include <KMime/Message>
28 #include <akonadi/item.h>
29 #include <akonadi/entitydisplayattribute.h>
30 #include <akonadi/itemcreatejob.h>
33 #include <Akonadi/EntityTreeModel>
35 using namespace Akonadi
;
37 using namespace Akonotes
;
39 NoteCreatorAndSelector::NoteCreatorAndSelector(QItemSelectionModel
* primaryModel
, QItemSelectionModel
* secondaryModel
, QObject
* parent
)
41 m_primarySelectionModel(primaryModel
),
42 m_secondarySelectionModel(secondaryModel
== 0 ? primaryModel
: secondaryModel
),
43 m_containerCollectionId(-1),
45 m_giveupTimer(new QTimer(this))
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()));
52 NoteCreatorAndSelector::~NoteCreatorAndSelector()
57 void NoteCreatorAndSelector::createNote(const Akonadi::Collection
& containerCollection
)
59 m_containerCollectionId
= containerCollection
.id();
61 if (m_primarySelectionModel
== m_secondarySelectionModel
)
65 m_giveupTimer
->start();
66 connect(m_primarySelectionModel
->model(), SIGNAL(rowsInserted(QModelIndex
,int,int)), SLOT(trySelectCollection()));
67 trySelectCollection();
71 void NoteCreatorAndSelector::trySelectCollection()
73 QModelIndex idx
= EntityTreeModel::modelIndexForCollection(m_primarySelectionModel
->model(), Collection(m_containerCollectionId
));
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()));
83 void NoteCreatorAndSelector::doCreateNote()
86 newItem
.setMimeType( Note::mimeType() );
88 KMime::Message::Ptr newPage
= KMime::Message::Ptr( new KMime::Message() );
90 QString title
= i18nc( "The default name for new pages.", "New Page" );
91 QByteArray
encoding( "utf-8" );
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( " " ) );
104 newItem
.setPayload( newPage
);
106 Akonadi::EntityDisplayAttribute
*eda
= new Akonadi::EntityDisplayAttribute();
107 eda
->setIconName( QString::fromLatin1( "text-plain" ) );
108 newItem
.addAttribute(eda
);
110 Akonadi::ItemCreateJob
*job
= new Akonadi::ItemCreateJob( newItem
, Collection(m_containerCollectionId
), this );
111 connect( job
, SIGNAL(result(KJob
*)), SLOT(noteCreationFinished(KJob
*)) );
115 void NoteCreatorAndSelector::noteCreationFinished(KJob
* job
)
119 kWarning() << job
->errorString();
122 Akonadi::ItemCreateJob
*createJob
= qobject_cast
<Akonadi::ItemCreateJob
*>(job
);
125 Item newItem
= createJob
->item();
126 m_newNoteId
= newItem
.id();
128 m_giveupTimer
->start();
129 connect(m_primarySelectionModel
->model(), SIGNAL(rowsInserted(QModelIndex
,int,int)), SLOT(trySelectNote()));
133 void NoteCreatorAndSelector::trySelectNote()
135 QModelIndexList list
= EntityTreeModel::modelIndexesForItem(m_secondarySelectionModel
->model(), Item(m_newNoteId
));
139 const QModelIndex idx
= list
.first();
140 m_secondarySelectionModel
->select(QItemSelection(idx
, idx
), QItemSelectionModel::ClearAndSelect
);