File: notes.cpp

package info (click to toggle)
psi-plugins 1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,368 kB
  • sloc: cpp: 42,063; xml: 714; ansic: 84; makefile: 61; sh: 12
file content (273 lines) | stat: -rw-r--r-- 7,777 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * notes.cpp - plugin
 * Copyright (C) 2010  Evgeny Khryukin
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include "notes.h"
#include "editnote.h"
#include "notesviewdelegate.h"
#include "storagenotesplugin.h"

#include <QMessageBox>


Notes::Notes(StorageNotesPlugin *storageNotes, int acc, QWidget *parent)
	: QDialog(parent, Qt::Window)
	, account_(acc)
	, storageNotes_(storageNotes)
	, tagModel_(new TagModel(this))
	, noteModel_(new NoteModel(this))
	, proxyModel_(new ProxyModel(this))
	, updateTagsTimer_(new QTimer(this))
	, newNotes(false)
	, waitForSave(false)

{
	setModal(false);
	ui_.setupUi(this);

	setWindowTitle(tr("Notebook") + " - " + storageNotes_->accInfo->getJid(account_));

	setWindowIcon(storageNotes_->iconHost->getIcon("storagenotes/storagenotes"));
	ui_.pb_add->setIcon(storageNotes_->iconHost->getIcon("psi/action_templates_edit"));
	ui_.pb_delete->setIcon(storageNotes_->iconHost->getIcon("psi/remove"));
	ui_.pb_edit->setIcon(storageNotes_->iconHost->getIcon("psi/options"));
	ui_.pb_load->setIcon(storageNotes_->iconHost->getIcon("psi/reload"));
	ui_.pb_save->setIcon(storageNotes_->iconHost->getIcon("psi/save"));
	ui_.pb_close->setIcon(storageNotes_->iconHost->getIcon("psi/cancel"));

	ui_.tv_tags->setModel(tagModel_);	
	proxyModel_->setSourceModel(noteModel_);
	ui_.lv_notes->setResizeMode(QListView::Adjust);
	ui_.lv_notes->setItemDelegate(new NotesViewDelegate(this));
	ui_.lv_notes->setModel(proxyModel_);

	connect(ui_.tv_tags, SIGNAL(clicked(QModelIndex)), this, SLOT(selectTag()));
	connect(ui_.lv_notes, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(edit()));
	connect(ui_.pb_save, SIGNAL(released()), this, SLOT(save()));
	connect(ui_.pb_close, SIGNAL(released()), this, SLOT(close()));
	connect(ui_.pb_load, SIGNAL(released()), this, SLOT(load()));
	connect(ui_.pb_add, SIGNAL(released()), this, SLOT(add()));
	connect(ui_.pb_delete, SIGNAL(released()), this, SLOT(del()));
	connect(ui_.pb_edit, SIGNAL(released()), this, SLOT(edit()));

	ui_.tv_tags->installEventFilter(this);

	updateTagsTimer_->setSingleShot(true);
	updateTagsTimer_->setInterval(100);
	connect(updateTagsTimer_, SIGNAL(timeout()), this, SLOT(updateTags()));
}

Notes::~Notes()
{
}

void Notes::closeEvent(QCloseEvent *e)
{
	if(newNotes) {
		int rez = QMessageBox::question(this, tr("Notebook"),
						tr("Some changes are not saved. Are you sure you want to quit?"),
						QMessageBox::Ok | QMessageBox::Cancel);
		if(rez == QMessageBox::Cancel) {
			e->ignore();
			return;
		}
	}

	emit notesDeleted(account_);
	e->ignore();
}

void Notes::keyPressEvent(QKeyEvent *e)
{
	if(e->key() == Qt::Key_Escape) {
		e->ignore();
		close();
	}
	else {
		QDialog::keyPressEvent(e);
		e->accept();
	}
}

bool Notes::eventFilter(QObject *obj, QEvent *e)
{
	if(obj == ui_.tv_tags && e->type() == QEvent::KeyPress)	{
		QTimer::singleShot(0, this, SLOT(selectTag()));
	}

	return QDialog::eventFilter(obj, e);
}

void Notes::save()
{
	QList<QDomElement> notesList = noteModel_->getAllNotes();
	QString notes;
	foreach(const QDomElement& note, notesList) {
		QString tag = note.attribute("tags");
		QString text = note.firstChildElement("text").text();
		QString title = note.firstChildElement("title").text();
		tag = replaceSymbols(tag);
		text = replaceSymbols(text);
		title = replaceSymbols(title);
		notes+=QString("<note tags=\"%1\"><title>%2</title><text>%3</text></note>")
				.arg(tag)
				.arg(title)
				.arg(text);
	}
	QString xml = QString("<iq type=\"set\" id=\"%2\"><query xmlns=\"jabber:iq:private\"><storage xmlns=\"http://miranda-im.org/storage#notes\">%1</storage></query></iq>")
			.arg(notes)
			.arg(NOTES_ID);

	storageNotes_->stanzaSender->sendStanza(account_, xml);

	newNotes = false;
	waitForSave = true;
}

void Notes::add()
{
	QString tag = ui_.tv_tags->currentIndex().data(Qt::DisplayRole).toString();
	if(tag == TagModel::allTagsName())
		tag = QString();

	EditNote *editNote = new EditNote(this, tag);
	connect(editNote, SIGNAL(newNote(QDomElement)), this, SLOT(addNote(QDomElement)));
	editNote->show();

	newNotes = true;
}

void Notes::del()
{    
	noteModel_->delNote(proxyModel_->mapToSource(ui_.lv_notes->currentIndex()));
	updateTagsTimer_->start();

	newNotes = true;
}

void Notes::updateTags()
{
	QStringList tags = noteModel_->getAllTags();
	QString currentTag = ui_.tv_tags->currentIndex().data(Qt::DisplayRole).toString();

	tagModel_->clear();

	foreach(const QString& tag, tags) {
		if(!tag.isEmpty())
			tagModel_->addTag(tag);
	}

	QModelIndex ind = tagModel_->indexByTag(currentTag);
	if(ind.isValid())
		ui_.tv_tags->setCurrentIndex(tagModel_->indexByTag(currentTag));
	else
		ui_.tv_tags->setCurrentIndex(tagModel_->index(0));
	selectTag();
	ui_.tv_tags->expandToDepth(2);
}

void Notes::edit()
{
	QModelIndex index = proxyModel_->mapToSource(ui_.lv_notes->currentIndex());
	if(!index.isValid())
		return;

	QString text = index.data(NoteModel::NoteRole).toString();
	QString title = index.data(NoteModel::TitleRole).toString();
	QString tags = index.data(NoteModel::TagRole).toString();

	EditNote *editNote = new EditNote( this, tags, title, text, index);
	connect(editNote, SIGNAL(editNote(QDomElement, QModelIndex)), this, SLOT(noteEdited(QDomElement, QModelIndex)));
	editNote->show();
}

void Notes::noteEdited(const QDomElement& note, const QModelIndex& index)
{
	noteModel_->editNote(note, index);
	updateTagsTimer_->start();

	newNotes = true;
}

void Notes::load()
{
	if(storageNotes_->accInfo->getStatus(account_) == "offline")
		return;

	if(newNotes) {
		int rez = QMessageBox::question(this, tr("Notebook"),
						tr("Some changes are not saved. Are you sure you want to continue?"),
						QMessageBox::Ok | QMessageBox::Cancel);
		if(rez == QMessageBox::Cancel) {
			return;
		}
	}

	tagModel_->clear();
	ui_.tv_tags->setCurrentIndex(tagModel_->index(0));
	selectTag();
	noteModel_->clear();
	QString str = QString("<iq type=\"get\" id=\"%1\"><query xmlns=\"jabber:iq:private\"><storage xmlns=\"%2\" /></query></iq>")
			.arg(NOTES_ID).arg("http://miranda-im.org/storage#notes");
	storageNotes_->stanzaSender->sendStanza(account_, str);

	newNotes = false;
}

void Notes::incomingNotes(const QList<QDomElement>& notes)
{
	foreach(const QDomElement& note, notes) {
		addNote(note);
	}
}

void Notes::addNote(const QDomElement& note)
{
	QString tag = note.attribute("tags");
	noteModel_->addNote(note);
	updateTagsTimer_->start();
}

void Notes::selectTag()
{    
	proxyModel_->setFilterFixedString(ui_.tv_tags->currentIndex().data(Qt::DisplayRole).toString());
}

void Notes::error()
{
	storageNotes_->popup->initPopup(tr("Error! Perhaps the function is not implemented on the server."),
					tr("Storage Notes Plugin"), "storagenotes/storagenotes", 7); // 7 - AlertHeadline
	close();
}

void Notes::saved()
{
	if(!waitForSave)
		return;

	storageNotes_->popup->initPopup(tr("Notes has been saved."),
					tr("Storage Notes Plugin"), "storagenotes/storagenotes", 7); // 7 - AlertHeadline

	waitForSave = false;
}

QString Notes::replaceSymbols(const QString& str)
{
	return storageNotes_->stanzaSender->escape(str);
}