2 Copyright (c) 2013-2015 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "notesmanager.h"
19 #include "notesharedglobalconfig.h"
20 #include "NoteShared/NotesNetworkReceiver"
21 #include "NoteShared/CreateNewNoteJob"
22 #include "NoteShared/NotesChangeRecorder"
23 #include "NoteShared/NotesAkonadiTreeModel"
24 #include "NoteShared/NoteAlarmAttribute"
25 #include "notesagentalarmdialog.h"
27 #include <AkonadiCore/Session>
28 #include <AkonadiCore/Collection>
29 #include <AkonadiCore/Item>
30 #include <AkonadiCore/ChangeRecorder>
32 #include <KMime/KMimeMessage>
34 #include <KNotification>
35 #include <KIconLoader>
36 #include <KLocalizedString>
43 NotesManager::NotesManager(QObject
*parent
)
46 mCheckAlarm(Q_NULLPTR
)
48 Akonadi::Session
*session
= new Akonadi::Session("KNotes Session", this);
49 mNoteRecorder
= new NoteShared::NotesChangeRecorder(this);
50 mNoteRecorder
->changeRecorder()->setSession(session
);
51 mNoteTreeModel
= new NoteShared::NotesAkonadiTreeModel(mNoteRecorder
->changeRecorder(), this);
53 connect(mNoteTreeModel
, &NoteShared::NotesAkonadiTreeModel::rowsInserted
, this, &NotesManager::slotRowInserted
);
55 connect(mNoteRecorder
->changeRecorder(), &Akonadi::Monitor::itemChanged
, this, &NotesManager::slotItemChanged
);
56 connect(mNoteRecorder
->changeRecorder(), &Akonadi::Monitor::itemRemoved
, this, &NotesManager::slotItemRemoved
);
59 NotesManager::~NotesManager()
64 void NotesManager::clear()
67 mListener
= Q_NULLPTR
;
68 if (mCheckAlarm
&& mCheckAlarm
->isActive()) {
73 void NotesManager::slotItemRemoved(const Akonadi::Item
&item
)
75 if (mListItem
.contains(item
)) {
76 mListItem
.removeAll(item
);
80 void NotesManager::slotItemChanged(const Akonadi::Item
&item
, const QSet
<QByteArray
> &set
)
82 if (set
.contains("ATR:NoteAlarmAttribute")) {
83 mListItem
.removeAll(item
);
84 if (item
.hasAttribute
<NoteShared::NoteAlarmAttribute
>()) {
85 mListItem
.append(item
);
88 mAlarmDialog
->removeAlarm(item
);
94 void NotesManager::slotRowInserted(const QModelIndex
&parent
, int start
, int end
)
96 for (int i
= start
; i
<= end
; ++i
) {
97 if (mNoteTreeModel
->hasIndex(i
, 0, parent
)) {
98 const QModelIndex child
= mNoteTreeModel
->index(i
, 0, parent
);
100 mNoteTreeModel
->data(child
, Akonadi::EntityTreeModel::ItemRole
).value
<Akonadi::Item
>();
101 if (!item
.hasPayload
<KMime::Message::Ptr
>()) {
104 if (item
.hasAttribute
<NoteShared::NoteAlarmAttribute
>()) {
105 mListItem
.append(item
);
111 void NotesManager::slotCheckAlarm()
113 QDateTime from
= NoteShared::NoteSharedGlobalConfig::self()->alarmsLastChecked().addSecs(1);
114 if (!from
.isValid()) {
118 const QDateTime now
= QDateTime::currentDateTime();
119 NoteShared::NoteSharedGlobalConfig::self()->setAlarmsLastChecked(now
);
121 Akonadi::Item::List lst
;
122 Q_FOREACH (const Akonadi::Item
&item
, mListItem
) {
123 NoteShared::NoteAlarmAttribute
*attrAlarm
= item
.attribute
<NoteShared::NoteAlarmAttribute
>();
125 if (attrAlarm
->dateTime() < QDateTime::currentDateTime()) {
130 if (!lst
.isEmpty()) {
132 mAlarmDialog
= new NotesAgentAlarmDialog
;
134 mAlarmDialog
->addListAlarm(lst
);
135 mAlarmDialog
->show();
137 mCheckAlarm
->start();
140 void NotesManager::load()
142 updateNetworkListener();
144 mCheckAlarm
= new QTimer(this);
145 } else if (mCheckAlarm
->isActive()) {
149 mCheckAlarm
->setInterval(1000 * 60 * NoteShared::NoteSharedGlobalConfig::checkInterval());
150 connect(mCheckAlarm
, &QTimer::timeout
, this, &NotesManager::slotCheckAlarm
);
154 void NotesManager::stopAll()
159 void NotesManager::slotAcceptConnection()
161 // Accept the connection and make KNotesNetworkReceiver do the job
162 QTcpSocket
*s
= mListener
->nextPendingConnection();
165 NoteShared::NotesNetworkReceiver
*recv
= new NoteShared::NotesNetworkReceiver(s
);
166 connect(recv
, &NoteShared::NotesNetworkReceiver::sigNoteReceived
, this, &NotesManager::slotNewNote
);
170 void NotesManager::slotNewNote(const QString
&name
, const QString
&text
)
172 const QPixmap pixmap
= QIcon::fromTheme(QStringLiteral("knotes")).pixmap(KIconLoader::SizeSmall
, KIconLoader::SizeSmall
);
173 KNotification::event(QStringLiteral("receivednotes"),
174 i18n("Note Received"),
177 KNotification::CloseOnTimeout
,
178 QStringLiteral("akonadi_notes_agent"));
179 NoteShared::CreateNewNoteJob
*job
= new NoteShared::CreateNewNoteJob(this, Q_NULLPTR
);
180 //For the moment it doesn't support richtext.
181 job
->setRichText(false);
182 job
->setNote(name
, text
);
186 void NotesManager::updateNetworkListener()
189 mListener
= Q_NULLPTR
;
191 if (NoteShared::NoteSharedGlobalConfig::receiveNotes()) {
192 // create the socket and start listening for connections
193 mListener
= new QTcpServer
;
194 mListener
->listen(QHostAddress::Any
, NoteShared::NoteSharedGlobalConfig::port());
195 connect(mListener
, &QTcpServer::newConnection
, this, &NotesManager::slotAcceptConnection
);