french -> French
[kdepim.git] / akonadi_next / notecreatorandselector.cpp
blob687fc9be87909ba63bd7e428b6b37c101381bff4
1 /*
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
19 02110-1301, USA.
22 #include "notecreatorandselector.h"
24 #include <KLocale>
26 #include <KMime/Message>
28 #include <akonadi/item.h>
29 #include <akonadi/entitydisplayattribute.h>
30 #include <akonadi/itemcreatejob.h>
32 #include "note.h"
33 #include <Akonadi/EntityTreeModel>
35 using namespace Akonadi;
37 using namespace Akonotes;
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))
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)
62 doCreateNote();
63 else
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));
74 if (!idx.isValid())
75 return;
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();
83 void NoteCreatorAndSelector::doCreateNote()
85 Item newItem;
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( " " ) );
102 newPage->assemble();
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)
117 if (job->error())
119 kWarning() << job->errorString();
120 return;
122 Akonadi::ItemCreateJob *createJob = qobject_cast<Akonadi::ItemCreateJob*>(job);
123 Q_ASSERT(createJob);
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()));
130 trySelectNote();
133 void NoteCreatorAndSelector::trySelectNote()
135 QModelIndexList list = EntityTreeModel::modelIndexesForItem(m_secondarySelectionModel->model(), Item(m_newNoteId));
136 if (list.isEmpty())
137 return;
139 const QModelIndex idx = list.first();
140 m_secondarySelectionModel->select(QItemSelection(idx, idx), QItemSelectionModel::ClearAndSelect);