aboutsummaryrefslogtreecommitdiff
path: root/notebookmodel.cc
blob: b9c897ea5eb76eeeb3b4accab107c4cf4125a880 (plain)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * scribiu -- read notebooks and voice memos from Livescribe pens
 * Copyright (C) 2015 Javier S. Pedro <javier@javispedro.com>
 *
 * 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 3 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 program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QtCore/QDebug>
#include <QtCore/QSettings>
#include <QtGui/QIcon>
#include <QtGui/QDesktopServices>
#include <QtWidgets/QApplication>
#include <QtWidgets/QStyle>
#include "paperreplay.h"
#include "afdnotebook.h"
#include "notebookmodel.h"

#define NUM_COLUMNS 3
#define PEN_INDEX_ID 0xFFFFFFFFU

NotebookModel::NotebookModel(QObject *parent) :
    QAbstractItemModel(parent),
    _dataDir(userDataDirectory()),
    _watcher()
{
	if (!_dataDir.exists()) {
		if (!_dataDir.mkpath(".")) {
			qWarning() << "Cannot create my data directory:" << _dataDir.absolutePath();
		}
	}
	_watcher.addPath(_dataDir.absolutePath());
	connect(&_watcher, SIGNAL(directoryChanged(QString)), SLOT(handleChangedDirectory(QString)));
	refresh();
}

QString NotebookModel::defaultDataDirectory()
{
	QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
	if (path.isEmpty()) {
		path = QDir::home().absoluteFilePath(".scribiu");
	}
	return path;
}

QString NotebookModel::userDataDirectory()
{
	QSettings settings;
	QString path = settings.value("data/directory").toString();
	if (path.isEmpty()) {
		path = defaultDataDirectory();
		settings.setValue("data/directory", QString());
	}
	return path;
}

QString NotebookModel::penDirectory(const QString &name) const
{
	return _dataDir.filePath(name + ".pen");
}

QString NotebookModel::notebookDirectory(const QString &penName, const QString &nbName) const
{
	return _dataDir.filePath(penName + ".pen" + "/" + nbName + ".afd");
}

QString NotebookModel::notebookDirectory(const QModelIndex &index) const
{
	if (!index.isValid()) return QString();
	quint32 id = index.internalId();
	if (id != PEN_INDEX_ID) {
		const QString &penName = _pens[id];
		const QStringList &notebooks = _notebooks[penName];
		const QString &notebookName = notebooks.at(index.row());
		return notebookDirectory(penName, notebookName);
	}
	return QString();
}

QString NotebookModel::paperReplayDirectory(const QString &name) const
{
	return _dataDir.filePath(name + ".pen" + "/"+ PAPER_REPLAY);
}

QVariant NotebookModel::data(const QModelIndex &index, int role) const
{
	if (!index.isValid()) return QVariant();
	quint32 id = index.internalId();
	if (id == PEN_INDEX_ID) {
		int penIndex = index.row();
		if (penIndex < 0 || penIndex >= _pens.size()) return QVariant();
		switch (role) {
		case Qt::DisplayRole:
			switch (index.column()) {
			case 0:
				return _pens[penIndex];
			}
			break;
		case Qt::DecorationRole:
			switch (index.column()) {
			case 2:
				if (isPenLocked(_pens[penIndex])) {
					return QApplication::style()->standardIcon(QStyle::SP_BrowserReload);
				}
				break;
			}
			break;
		}
	} else {
		const QString &penName = _pens[id];
		const QStringList &notebooks = _notebooks[penName];
		const QString &notebookName = notebooks.at(index.row());
		switch (role) {
		case Qt::DisplayRole:
			switch (index.column()) {
			case 0:
				return notebooks.at(index.row());
			case 1:
				if (notebookName != PAPER_REPLAY) {
					return estimatePagesOfNotebook(penName, notebookName);
				}
				break;
			}
			break;
		case Qt::DecorationRole:
			switch (index.column()) {
			case 0:
				return getNotebookIcon(penName, notebookName);
			case 1:
				return QVariant();
			case 2:
				if (notebookName != PAPER_REPLAY) {
					if (isNotebookLocked(penName, notebookName)) {
						return QApplication::style()->standardIcon(QStyle::SP_BrowserReload);
					}
				} else {
					if (isPaperReplayLocked(penName)) {
						return QApplication::style()->standardIcon(QStyle::SP_BrowserReload);
					}
				}
				break;
			}
			break;
		case Qt::TextAlignmentRole:
			switch (index.column()) {
			case 0:
				return Qt::AlignLeft;
			case 1:
			case 2:
				return Qt::AlignCenter;
			}
			break;
		}
	}
	return QVariant();
}

Qt::ItemFlags NotebookModel::flags(const QModelIndex &index) const
{
	switch (index.column()) {
	case 0:
		return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
	default:
		return Qt::ItemIsSelectable;
	}
}

QModelIndex NotebookModel::index(int row, int column, const QModelIndex &parent) const
{
	if (parent.isValid()) {
		quint32 id = parent.internalId();
		if (id == PEN_INDEX_ID) {
			// Pen index
			const quint32 penIndex = parent.row();
			const QString &penName = _pens.at(penIndex);
			const QStringList &notebooks = _notebooks[penName];
			if (row >= 0 && row < notebooks.size() && column >= 0 && column < NUM_COLUMNS) {
				return createIndex(row, column, penIndex);
			} else {
				return QModelIndex();
			}
		} else {
			// Notebook index
			return QModelIndex(); // Nothing inside notebooks
		}
	} else if (row >= 0 && row < _pens.size() && column >= 0 && column < NUM_COLUMNS) {
		return createIndex(row, column, PEN_INDEX_ID);
	} else {
		return QModelIndex();
	}
}

QModelIndex NotebookModel::parent(const QModelIndex &child) const
{
	if (child.isValid()) {
		quint32 id = child.internalId();
		if (id == PEN_INDEX_ID) {
			return QModelIndex(); // Pens have no parent
		} else {
			return createIndex(id, 0, PEN_INDEX_ID);
		}
	} else {
		return QModelIndex();
	}
}

int NotebookModel::rowCount(const QModelIndex &parent) const
{
	if (parent.isValid()) {
		quint32 id = parent.internalId();
		if (id == PEN_INDEX_ID) {
			const quint32 penIndex = parent.row();
			const QString &penName = _pens.at(penIndex);
			const QStringList &notebooks = _notebooks[penName];
			return notebooks.size();
		} else {
			return 0; // Nothing inside notebooks
		}
	} else {
		return _pens.size();
	}
}

int NotebookModel::columnCount(const QModelIndex &parent) const
{
	Q_UNUSED(parent);
	return NUM_COLUMNS;
}

void NotebookModel::refresh()
{
	QStringList pens = _dataDir.entryList(QStringList("*.pen"), QDir::Dirs, QDir::Name);
	for (int i = 0; i < pens.size(); i++) {
		pens[i].chop(4); // Remove .pen extension
	}

	// Insert/remove new/deleted pens
	int i = 0, j = 0;
	while (i < _pens.size() && j < pens.size()) {
		int comp = QString::compare(_pens[i], pens[j], Qt::CaseInsensitive);
		if (comp == 0) {
			++i;
			++j;
		} else if (comp > 0) { /* _pens[i] > pens[j] */
			beginInsertRows(QModelIndex(), i, i);
			_pens.insert(i, pens[j]);
			endInsertRows();
			++i;
			++j;
		} else { /* _pens[i] < pens[j] */
			beginRemoveRows(QModelIndex(), i, i);
			_notebooks.remove(_pens[i]);
			_pens.removeAt(i);
			endRemoveRows();
		}
	}
	if (i < _pens.size()) {
		Q_ASSERT(j == pens.size());
		beginRemoveRows(QModelIndex(), i, _pens.size() - 1);
		while (i < _pens.size()) {
			_notebooks.remove(_pens[i]);
			_pens.removeAt(i);
		}
		endRemoveRows();
	} else if (j < pens.size()) {
		Q_ASSERT(i == _pens.size());
		beginInsertRows(QModelIndex(), i, i + (pens.size() - j));
		_pens.append(pens.mid(j));
		endInsertRows();
	}

	foreach (const QString &pen, _pens) {
		refreshPen(pen);
	}
}

void NotebookModel::refreshPen(const QString &name)
{
	QDir penDir(penDirectory(name));
	if (!penDir.exists()) {
		return;
	}

	_watcher.addPath(penDir.canonicalPath());

	QStringList &curNotebooks = _notebooks[name];
	QStringList diskNotebooks = penDir.entryList(QStringList("*.afd"), QDir::Dirs, QDir::Name);
	for (int i = 0; i < diskNotebooks.size(); i++) {
		diskNotebooks[i].chop(4);
	}
	if (penDir.exists(PAPER_REPLAY)) diskNotebooks.append(PAPER_REPLAY);

	QModelIndex penIndex = index(indexOfPen(name), 0, QModelIndex());

	emit dataChanged(penIndex, index(penIndex.row(), NUM_COLUMNS - 1, QModelIndex()));

	int i = 0, j = 0;
	while (i < curNotebooks.size() && j < diskNotebooks.size()) {
		int comp = QString::compare(curNotebooks[i], diskNotebooks[j], Qt::CaseInsensitive);
		if (comp == 0) {
			++i;
			++j;
		} else if (comp > 0) { /* _pens[i] > pens[j] */
			beginInsertRows(penIndex, i, i);
			curNotebooks.insert(i, diskNotebooks[j]);
			endInsertRows();
			++i;
			++j;
		} else { /* _pens[i] < pens[j] */
			beginRemoveRows(penIndex, i, i);
			curNotebooks.removeAt(i);
			endRemoveRows();
		}
	}
	if (i < curNotebooks.size()) {
		Q_ASSERT(j == diskNotebooks.size());
		beginRemoveRows(penIndex, i, curNotebooks.size() - 1);
		curNotebooks.erase(curNotebooks.begin() + i, curNotebooks.end());
		endRemoveRows();
	} else if (j < diskNotebooks.size()) {
		Q_ASSERT(i == curNotebooks.size());
		beginInsertRows(penIndex, i, i + (diskNotebooks.size() - j));
		curNotebooks.append(diskNotebooks.mid(j));
		endInsertRows();
	}

	qDebug() << "Found" << curNotebooks.size() << "notebooks for pen" << name;
}

int NotebookModel::indexOfPen(const QString &name)
{
	auto it = std::lower_bound(_pens.begin(), _pens.end(), name);
	if (it == _pens.end()) {
		return -1;
	} else {
		return it - _pens.begin();
	}
}

QDir NotebookModel::penDir(const QString &pen) const
{
	return QDir(penDirectory(pen));
}

QDir NotebookModel::notebookDir(const QString &pen, const QString &notebook) const
{
	return QDir(notebookDirectory(pen, notebook));
}

QIcon NotebookModel::getNotebookIcon(const QString &pen, const QString &notebook) const
{
	static QStringList candidates;
	if (candidates.isEmpty()) {
		candidates << "userdata/icon/Notebook.png"
		           << "userdata/icon/active_64x64.png"
		           << "userdata/icon/active_32x32.png"
		           << "userdata/icon/active_16x16.png";
	}
	QIcon icon;

	QDir dir = notebookDir(pen, notebook);
	if (dir.exists()) {
		foreach (const QString &candidate, candidates) {
			if (dir.exists(candidate)) {
				icon.addFile(dir.filePath(candidate));
			}
		}
	}

	return icon;
}

bool NotebookModel::isPenLocked(const QString &pen) const
{
	QDir dir = penDir(pen);
	if (dir.exists(".sync.lck")) {
		return true; // TODO check if stale
	} else {
		return false;
	}
}

bool NotebookModel::isNotebookLocked(const QString &pen, const QString &notebook) const
{
	QDir dir = notebookDir(pen, notebook);
	if (dir.exists(".sync.lck")) {
		return true;
	} else {
		return false;
	}
}

bool NotebookModel::isPaperReplayLocked(const QString &pen) const
{
	QDir dir(paperReplayDirectory(pen));
	if (dir.exists(".sync.lck")) {
		return true;
	} else {
		return false;
	}
}

int NotebookModel::estimatePagesOfNotebook(const QString &pen, const QString &notebook) const
{
	QDir dataDir(notebookDirectory(pen, notebook) + "/data");
	QStringList pages = dataDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
	return pages.count();
}

void NotebookModel::handleChangedDirectory(const QString &path)
{
	qDebug() << "changed" << path;
	if (path == _dataDir.absolutePath()) {
		refresh();
	} else if (path.endsWith(".pen")) {
		QFileInfo finfo(path);
		refreshPen(finfo.baseName());
	}
}