SVN_SILENT made messages (.desktop file)
[kdepim.git] / akonadi_next / notecreatorandselector.cpp
blobce95e3b822f71edea39e36df112c19b552a330f4
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 <KLocalizedString>
26 #include <KMime/Message>
28 #include <item.h>
29 #include <entitydisplayattribute.h>
30 #include <itemcreatejob.h>
32 #include "note.h"
33 #include <EntityTreeModel>
34 #include <QDebug>
36 using namespace Akonadi;
38 using namespace Akonotes;
40 NoteCreatorAndSelector::NoteCreatorAndSelector(QItemSelectionModel *primaryModel, QItemSelectionModel *secondaryModel, QObject *parent)
41 : QObject(parent),
42 m_primarySelectionModel(primaryModel),
43 m_secondarySelectionModel(secondaryModel == 0 ? primaryModel : secondaryModel),
44 m_containerCollectionId(-1),
45 m_newNoteId(-1),
46 m_giveupTimer(new QTimer(this))
48 // If the note doesn't exist after 5 seconds, give up waiting for it.
49 m_giveupTimer->setInterval(5000);
50 connect(m_giveupTimer, &QTimer::timeout, this, &NoteCreatorAndSelector::deleteLater);
53 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 {
64 m_giveupTimer->start();
65 connect(m_primarySelectionModel->model(), &QAbstractItemModel::rowsInserted, this, &NoteCreatorAndSelector::trySelectCollection);
66 trySelectCollection();
70 void NoteCreatorAndSelector::trySelectCollection()
72 QModelIndex idx = EntityTreeModel::modelIndexForCollection(m_primarySelectionModel->model(), Collection(m_containerCollectionId));
73 if (!idx.isValid()) {
74 return;
77 m_giveupTimer->stop();
78 m_primarySelectionModel->select(QItemSelection(idx, idx), QItemSelectionModel::Select);
79 disconnect(m_primarySelectionModel->model(), &QAbstractItemModel::rowsInserted, this, &NoteCreatorAndSelector::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(QDateTime::currentDateTime());
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(QStringLiteral("text-plain"));
108 newItem.addAttribute(eda);
110 Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob(newItem, Collection(m_containerCollectionId), this);
111 connect(job, &Akonadi::ItemCreateJob::result, this, &NoteCreatorAndSelector::noteCreationFinished);
115 void NoteCreatorAndSelector::noteCreationFinished(KJob *job)
117 if (job->error()) {
118 qWarning() << job->errorString();
119 return;
121 Akonadi::ItemCreateJob *createJob = qobject_cast<Akonadi::ItemCreateJob *>(job);
122 Q_ASSERT(createJob);
124 Item newItem = createJob->item();
125 m_newNoteId = newItem.id();
127 m_giveupTimer->start();
128 connect(m_primarySelectionModel->model(), &QAbstractItemModel::rowsInserted, this, &NoteCreatorAndSelector::trySelectNote);
129 trySelectNote();
132 void NoteCreatorAndSelector::trySelectNote()
134 QModelIndexList list = EntityTreeModel::modelIndexesForItem(m_secondarySelectionModel->model(), Item(m_newNoteId));
135 if (list.isEmpty()) {
136 return;
139 const QModelIndex idx = list.first();
140 m_secondarySelectionModel->select(QItemSelection(idx, idx), QItemSelectionModel::ClearAndSelect);