45
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the demonstration applications of the Qt Toolkit.
|
|
7 **
|
|
8 ** $QT_BEGIN_LICENSE:LGPL$
|
|
9 ** Commercial Usage
|
|
10 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11 ** accordance with the Qt Commercial License Agreement provided with the
|
|
12 ** Software or, alternatively, in accordance with the terms contained in
|
|
13 ** a written agreement between you and Nokia.
|
|
14 **
|
|
15 ** GNU Lesser General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
17 ** General Public License version 2.1 as published by the Free Software
|
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
19 ** packaging of this file. Please review the following information to
|
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
22 **
|
|
23 ** In addition, as a special exception, Nokia gives you certain
|
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL
|
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|
26 ** package.
|
|
27 **
|
|
28 ** GNU General Public License Usage
|
|
29 ** Alternatively, this file may be used under the terms of the GNU
|
|
30 ** General Public License version 3.0 as published by the Free Software
|
|
31 ** Foundation and appearing in the file LICENSE.GPL included in the
|
|
32 ** packaging of this file. Please review the following information to
|
|
33 ** ensure the GNU General Public License version 3.0 requirements will be
|
|
34 ** met: http://www.gnu.org/copyleft/gpl.html.
|
|
35 **
|
|
36 ** If you are unsure which license is appropriate for your use, please
|
|
37 ** contact the sales department at qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
72
|
41
|
45
|
42 module history;
|
|
43
|
|
44
|
72
|
45 import qt.gui.QClipboard;
|
|
46 import qt.gui.QDesktopServices;
|
|
47 import qt.gui.QHeaderView;
|
|
48 import qt.gui.QStyle;
|
|
49 import qt.gui.QSortFilterProxyModel;
|
94
|
50 import qt.gui.QAbstractTableModel;
|
|
51 //import qt.core.QtAlgorithms;
|
72
|
52 import qt.core.QBuffer;
|
|
53 import qt.core.QDir;
|
|
54 import qt.core.QFile;
|
|
55 import qt.core.QFileInfo;
|
|
56 import qt.core.QSettings;
|
|
57 import qt.core.QTemporaryFile;
|
|
58 import qt.core.QTextStream;
|
94
|
59 //import qt.core.QDebug;
|
72
|
60 import qt.core.QDateTime;
|
94
|
61 //import qt.core.QHash;
|
72
|
62 import qt.core.QObject;
|
|
63 import qt.core.QTimer;
|
|
64 import qt.core.QUrl;
|
73
|
65 import qt.webkit.QWebHistoryInterface;
|
74
|
66 import qt.webkit.QWebSettings;
|
100
|
67 //import QWebHistoryInterface;
|
45
|
68
|
100
|
69 import tango.text.convert.Format;
|
45
|
70
|
74
|
71 import autosaver;
|
65
|
72 import browserapplication;
|
|
73 import modelmenu;
|
|
74
|
|
75
|
74
|
76 static const uint HISTORY_VERSION = 23;
|
45
|
77
|
|
78
|
|
79 class HistoryItem
|
|
80 {
|
|
81 public:
|
65
|
82
|
|
83 this() {}
|
45
|
84
|
85
|
85 this(string u, QDateTime d = null, string t = null)
|
65
|
86 {
|
|
87 title = t;
|
|
88 url = u;
|
85
|
89 dateTime = d ? d : new QDateTime();
|
45
|
90 }
|
|
91
|
65
|
92 //bool operator==(HistoryItem &other)
|
|
93 int opEquals(HistoryItem other)
|
|
94 {
|
|
95 return cast(int) (other.title == title && other.url == url && other.dateTime == dateTime);
|
|
96 }
|
45
|
97
|
65
|
98 // history is sorted in reverse
|
|
99 //bool operator <(HistoryItem &other)
|
|
100 int opCmp(HistoryItem other)
|
|
101 {
|
|
102 return cast(int) (dateTime > other.dateTime);
|
|
103 }
|
|
104
|
77
|
105 string title;
|
|
106 string url;
|
65
|
107 QDateTime dateTime;
|
45
|
108 }
|
|
109
|
|
110
|
|
111 class HistoryManager : public QWebHistoryInterface
|
|
112 {
|
65
|
113 mixin Signal!("historyReset");
|
74
|
114 mixin Signal!("entryAdded", HistoryItem /*item*/);
|
|
115 mixin Signal!("entryRemoved", HistoryItem /*item*/);
|
|
116 mixin Signal!("entryUpdated", int /*offset*/);
|
45
|
117
|
|
118 public:
|
|
119
|
65
|
120 this(QObject parent = null)
|
45
|
121 {
|
|
122 super(parent);
|
|
123 m_saveTimer = new AutoSaver(this);
|
|
124 m_historyLimit = 30;
|
|
125 m_historyModel = 0;
|
|
126 m_historyFilterModel = 0;
|
|
127 m_historyTreeModel = 0;
|
|
128
|
|
129 m_expiredTimer.setSingleShot(true);
|
65
|
130
|
|
131 m_expiredTimer.timeout.connect(&this.checkForExpired);
|
|
132 this.entryAdded.connect(&m_saveTimer.changeOccurred);
|
|
133 this.entryRemoved.connect(&m_saveTimer.changeOccurred);
|
|
134
|
45
|
135 load();
|
|
136
|
|
137 m_historyModel = new HistoryModel(this, this);
|
|
138 m_historyFilterModel = new HistoryFilterModel(m_historyModel, this);
|
|
139 m_historyTreeModel = new HistoryTreeModel(m_historyFilterModel, this);
|
|
140
|
|
141 // QWebHistoryInterface will delete the history manager
|
65
|
142 QWebHistoryInterface.setDefaultInterface(this);
|
45
|
143 }
|
|
144
|
|
145 ~this()
|
|
146 {
|
|
147 m_saveTimer.saveIfNeccessary();
|
|
148 }
|
|
149
|
77
|
150 bool historyContains(string url)
|
45
|
151 {
|
|
152 return m_historyFilterModel.historyContains(url);
|
|
153 }
|
|
154
|
77
|
155 void addHistoryEntry(string url)
|
45
|
156 {
|
|
157 QUrl cleanUrl(url);
|
77
|
158 cleanUrl.setPassword(null);
|
|
159 cleanUrl.setHost(toLower(cleanUrl.host()));
|
65
|
160 auto item = new HistoryItem(cleanUrl.toString(), QDateTime.currentDateTime());
|
45
|
161 addHistoryItem(item);
|
|
162 }
|
|
163
|
77
|
164 void updateHistoryItem(QUrl url, string title)
|
45
|
165 {
|
77
|
166 for (int i = 0; i < m_history.length; ++i) {
|
|
167 if (url == m_history[i].url) {
|
45
|
168 m_history[i].title = title;
|
|
169 m_saveTimer.changeOccurred();
|
|
170 if (m_lastSavedUrl.isEmpty())
|
77
|
171 m_lastSavedUrl = m_history[i].url;
|
|
172 entryUpdated.emit(i);
|
45
|
173 break;
|
|
174 }
|
|
175 }
|
|
176 }
|
|
177
|
|
178 int historyLimit()
|
|
179 {
|
|
180 return m_historyLimit;
|
|
181 }
|
|
182
|
|
183 void setHistoryLimit(int limit)
|
|
184 {
|
|
185 if (m_historyLimit == limit)
|
|
186 return;
|
|
187 m_historyLimit = limit;
|
|
188 checkForExpired();
|
|
189 m_saveTimer.changeOccurred();
|
|
190 }
|
|
191
|
74
|
192 HistoryItem[] history()
|
45
|
193 {
|
|
194 return m_history;
|
|
195 }
|
|
196
|
74
|
197 void setHistory(HistoryItem[] history, bool loadedAndSorted = false)
|
45
|
198 {
|
|
199 m_history = history;
|
|
200
|
|
201 // verify that it is sorted by date
|
|
202 if (!loadedAndSorted)
|
|
203 qSort(m_history.begin(), m_history.end());
|
|
204
|
|
205 checkForExpired();
|
|
206
|
|
207 if (loadedAndSorted) {
|
|
208 m_lastSavedUrl = m_history.value(0).url;
|
|
209 } else {
|
77
|
210 m_lastSavedUrl = null;
|
45
|
211 m_saveTimer.changeOccurred();
|
|
212 }
|
77
|
213 historyReset.emit();
|
45
|
214 }
|
|
215
|
|
216 // History manager keeps around these models for use by the completer and other classes
|
74
|
217 HistoryModel historyModel()
|
45
|
218 {
|
|
219 return m_historyModel;
|
|
220 }
|
|
221
|
65
|
222 HistoryFilterModel historyFilterModel()
|
45
|
223 {
|
|
224 return m_historyFilterModel;
|
|
225 }
|
|
226
|
65
|
227 HistoryTreeModel historyTreeModel()
|
45
|
228 {
|
|
229 return m_historyTreeModel;
|
|
230 }
|
|
231
|
73
|
232 public:
|
45
|
233
|
|
234 void clear()
|
|
235 {
|
77
|
236 m_history = null;
|
|
237 m_lastSavedUrl = null;
|
45
|
238 m_saveTimer.changeOccurred();
|
|
239 m_saveTimer.saveIfNeccessary();
|
|
240 historyReset();
|
|
241 }
|
|
242
|
|
243 void loadSettings()
|
|
244 {
|
|
245 // load settings
|
|
246 QSettings settings;
|
80
|
247 settings.beginGroup("history");
|
|
248 m_historyLimit = settings.value("historyLimit", 30).toInt();
|
45
|
249 }
|
|
250
|
65
|
251 private:
|
|
252
|
|
253 void save()
|
|
254 {
|
|
255 QSettings settings;
|
80
|
256 settings.beginGroup("history");
|
|
257 settings.setValue("historyLimit", m_historyLimit);
|
45
|
258
|
65
|
259 bool saveAll = m_lastSavedUrl.isEmpty();
|
77
|
260 int first = m_history.length - 1;
|
65
|
261 if (!saveAll) {
|
|
262 // find the first one to save
|
77
|
263 for (int i = 0; i < m_history.length; ++i) {
|
|
264 if (m_history[i].url == m_lastSavedUrl) {
|
65
|
265 first = i - 1;
|
|
266 break;
|
|
267 }
|
|
268 }
|
|
269 }
|
66
|
270
|
77
|
271 if (first == m_history.length - 1)
|
65
|
272 saveAll = true;
|
45
|
273
|
77
|
274 string directory = QDesktopServices.storageLocation(QDesktopServices.DataLocation);
|
65
|
275 if (directory.isEmpty())
|
80
|
276 directory = QDir.homePath() ~ "/." ~ QCoreApplication.applicationName();
|
65
|
277 if (!QFile.exists(directory)) {
|
|
278 QDir dir;
|
|
279 dir.mkpath(directory);
|
|
280 }
|
45
|
281
|
80
|
282 auto historyFile = new QFile(directory ~ "/history");
|
65
|
283 // When saving everything use a temporary file to prevent possible data loss.
|
74
|
284 auto tempFile = new QTemporaryFile;
|
65
|
285 tempFile.setAutoRemove(false);
|
|
286 bool open = false;
|
|
287 if (saveAll) {
|
|
288 open = tempFile.open();
|
|
289 } else {
|
|
290 open = historyFile.open(QFile.Append);
|
|
291 }
|
45
|
292
|
65
|
293 if (!open) {
|
94
|
294 qWarning("Unable to open history file for saving" ~ (saveAll ? tempFile.fileName() : historyFile.fileName()));
|
65
|
295 return;
|
|
296 }
|
45
|
297
|
65
|
298 QDataStream out_ = (saveAll ? tempFile : historyFile);
|
|
299 for (int i = first; i >= 0; --i) {
|
|
300 QByteArray data;
|
|
301 auto stream = new QDataStream(data, QIODevice.WriteOnly);
|
77
|
302 HistoryItem item = m_history[i];
|
65
|
303 stream << HISTORY_VERSION << item.url << item.dateTime << item.title;
|
|
304 out_ << data;
|
|
305 }
|
|
306 tempFile.close();
|
45
|
307
|
65
|
308 if (saveAll) {
|
|
309 if (historyFile.exists() && !historyFile.remove())
|
94
|
310 qWarning("History: error removing old history." ~ historyFile.errorString());
|
65
|
311 if (!tempFile.rename(historyFile.fileName()))
|
94
|
312 qWarning("History: error moving new history over old." ~ tempFile.errorString() ~ historyFile.fileName());
|
65
|
313 }
|
|
314 m_lastSavedUrl = m_history.value(0).url;
|
|
315 }
|
45
|
316
|
65
|
317 void checkForExpired()
|
|
318 {
|
77
|
319 if (m_historyLimit < 0 || m_history.length == 0)
|
65
|
320 return;
|
45
|
321
|
65
|
322 QDateTime now = QDateTime.currentDateTime();
|
|
323 int nextTimeout = 0;
|
45
|
324
|
77
|
325 while (m_history.length) {
|
65
|
326 QDateTime checkForExpired = m_history.last().dateTime;
|
|
327 checkForExpired.setDate(checkForExpired.date().addDays(m_historyLimit));
|
|
328 if (now.daysTo(checkForExpired) > 7) {
|
|
329 // check at most in a week to prevent int overflows on the timer
|
|
330 nextTimeout = 7 * 86400;
|
|
331 } else {
|
|
332 nextTimeout = now.secsTo(checkForExpired);
|
|
333 }
|
|
334 if (nextTimeout > 0)
|
|
335 break;
|
|
336 HistoryItem item = m_history.takeLast();
|
|
337 // remove from saved file also
|
77
|
338 m_lastSavedUrl = null;
|
|
339 entryRemoved.emit(item);
|
65
|
340 }
|
45
|
341
|
65
|
342 if (nextTimeout > 0)
|
|
343 m_expiredTimer.start(nextTimeout * 1000);
|
|
344 }
|
45
|
345
|
|
346 protected:
|
|
347
|
65
|
348 void addHistoryItem(HistoryItem item)
|
|
349 {
|
|
350 QWebSettings globalSettings = QWebSettings.globalSettings();
|
|
351 if (globalSettings.testAttribute(QWebSettings.PrivateBrowsingEnabled))
|
|
352 return;
|
|
353
|
77
|
354 m_history = [item] ~ m_history;
|
|
355 entryAdded.emit(item);
|
|
356 if (m_history.length == 1)
|
65
|
357 checkForExpired();
|
|
358 }
|
45
|
359
|
|
360 private:
|
|
361
|
65
|
362 void load()
|
|
363 {
|
|
364 loadSettings();
|
45
|
365
|
80
|
366 historyFile = new QFile(QDesktopServices.storageLocation(QDesktopServices.DataLocation) ~ "/history");
|
65
|
367 if (!historyFile.exists())
|
|
368 return;
|
|
369 if (!historyFile.open(QFile.ReadOnly)) {
|
94
|
370 qWarning("Unable to open history file" ~ historyFile.fileName());
|
65
|
371 return;
|
|
372 }
|
45
|
373
|
74
|
374 HistoryItem[] list;
|
65
|
375 auto in_ = new QDataStream(&historyFile);
|
|
376 // Double check that the history file is sorted as it is read in
|
|
377 bool needToSort = false;
|
77
|
378 auto lastInsertedItem = new HistoryItem;
|
|
379 auto data = new QByteArray;
|
|
380 auto stream = new QDataStream;
|
|
381 auto buffer = new QBuffer;
|
65
|
382 stream.setDevice(buffer);
|
|
383 while (!historyFile.atEnd()) {
|
|
384 in_ >> data;
|
|
385 buffer.close();
|
|
386 buffer.setBuffer(data);
|
|
387 buffer.open(QIODevice.ReadOnly);
|
77
|
388 uint ver;
|
65
|
389 stream >> ver;
|
|
390 if (ver != HISTORY_VERSION)
|
|
391 continue;
|
77
|
392 auto item = new HistoryItem;
|
65
|
393 stream >> item.url;
|
|
394 stream >> item.dateTime;
|
|
395 stream >> item.title;
|
45
|
396
|
65
|
397 if (!item.dateTime.isValid())
|
|
398 continue;
|
45
|
399
|
65
|
400 if (item == lastInsertedItem) {
|
|
401 if (lastInsertedItem.title.isEmpty() && !list.isEmpty())
|
|
402 list[0].title = item.title;
|
|
403 continue;
|
|
404 }
|
45
|
405
|
65
|
406 if (!needToSort && !list.isEmpty() && lastInsertedItem < item)
|
|
407 needToSort = true;
|
45
|
408
|
65
|
409 list.prepend(item);
|
|
410 lastInsertedItem = item;
|
|
411 }
|
|
412
|
|
413 if (needToSort)
|
|
414 qSort(list.begin(), list.end());
|
45
|
415
|
65
|
416 setHistory(list, true);
|
45
|
417
|
65
|
418 // If we had to sort re-write the whole history sorted
|
|
419 if (needToSort) {
|
77
|
420 m_lastSavedUrl = null;
|
65
|
421 m_saveTimer.changeOccurred();
|
|
422 }
|
|
423 }
|
45
|
424
|
65
|
425 AutoSaver m_saveTimer;
|
|
426 int m_historyLimit;
|
|
427 QTimer m_expiredTimer;
|
74
|
428 HistoryItem[] m_history;
|
77
|
429 string m_lastSavedUrl;
|
45
|
430
|
65
|
431 HistoryModel m_historyModel;
|
|
432 HistoryFilterModel m_historyFilterModel;
|
|
433 HistoryTreeModel m_historyTreeModel;
|
|
434 }
|
45
|
435
|
73
|
436
|
45
|
437 class HistoryModel : public QAbstractTableModel
|
|
438 {
|
65
|
439 public:
|
45
|
440
|
65
|
441 void historyReset()
|
|
442 {
|
|
443 reset();
|
|
444 }
|
45
|
445
|
65
|
446 void entryAdded()
|
|
447 {
|
|
448 beginInsertRows(QModelIndex(), 0, 0);
|
|
449 endInsertRows();
|
|
450 }
|
45
|
451
|
65
|
452 void entryUpdated(int offset)
|
|
453 {
|
|
454 QModelIndex idx = index(offset, 0);
|
77
|
455 dataChanged.emit(idx, idx);
|
65
|
456 }
|
45
|
457
|
|
458 public:
|
|
459
|
|
460 enum Roles {
|
94
|
461 DateRole = Qt_ItemDataRole.UserRole + 1,
|
|
462 DateTimeRole = Qt_ItemDataRole.UserRole + 2,
|
|
463 UrlRole = Qt_ItemDataRole.UserRole + 3,
|
|
464 UrlStringRole = Qt_ItemDataRole.UserRole + 4
|
73
|
465 };
|
45
|
466
|
65
|
467 this(HistoryManager history, QObject parent = null)
|
45
|
468 {
|
|
469 super(parent);
|
|
470 m_history = history;
|
65
|
471 assert(m_history);
|
|
472
|
|
473 m_history.historyReset.connect(&this.historyReset);
|
|
474 m_history.entryRemoved.connect(&this.historyReset);
|
|
475 m_history.entryAdded.connect(&this.entryAdded);
|
|
476 m_history.entryUpdated.connect(&this.entryUpdated);
|
45
|
477 }
|
|
478
|
94
|
479 QVariant headerData(int section, Qt_Orientation orientation, int role = Qt_ItemDataRole.DisplayRole)
|
45
|
480 {
|
94
|
481 if (orientation == Qt_Orientation.Horizontal && role == Qt_Orientation.DisplayRole) {
|
45
|
482 switch (section) {
|
|
483 case 0: return tr("Title");
|
|
484 case 1: return tr("Address");
|
|
485 }
|
|
486 }
|
65
|
487 return QAbstractTableModel.headerData(section, orientation, role);
|
45
|
488 }
|
|
489
|
94
|
490 QVariant data(QModelIndex index, int role = Qt_ItemDataRole.DisplayRole)
|
45
|
491 {
|
74
|
492 HistoryItem[] lst = m_history.history();
|
77
|
493 if (index.row() < 0 || index.row() >= lst.length)
|
94
|
494 return new QVariant();
|
45
|
495
|
77
|
496 HistoryItem item = lst[index.row()];
|
45
|
497 switch (role) {
|
|
498 case DateTimeRole:
|
|
499 return item.dateTime;
|
|
500 case DateRole:
|
|
501 return item.dateTime.date();
|
|
502 case UrlRole:
|
94
|
503 return new QUrl(item.url);
|
45
|
504 case UrlStringRole:
|
|
505 return item.url;
|
94
|
506 case Qt_ItemDataRole.DisplayRole:
|
|
507 case Qt_ItemDataRole.EditRole: {
|
45
|
508 switch (index.column()) {
|
|
509 case 0:
|
|
510 // when there is no title try to generate one from the url
|
|
511 if (item.title.isEmpty()) {
|
94
|
512 string page = (new QFileInfo((new QUrl(item.url)).path())).fileName();
|
45
|
513 if (!page.isEmpty())
|
|
514 return page;
|
|
515 return item.url;
|
|
516 }
|
|
517 return item.title;
|
|
518 case 1:
|
|
519 return item.url;
|
|
520 }
|
|
521 }
|
94
|
522 case Qt_ItemDataRole.DecorationRole:
|
45
|
523 if (index.column() == 0) {
|
65
|
524 return BrowserApplication.instance().icon(item.url);
|
45
|
525 }
|
|
526 }
|
94
|
527 return new QVariant();
|
45
|
528 }
|
|
529
|
65
|
530 int columnCount(QModelIndex parent = QModelIndex())
|
45
|
531 {
|
|
532 return (parent.isValid()) ? 0 : 2;
|
|
533 }
|
|
534
|
65
|
535 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
536 {
|
|
537 return (parent.isValid()) ? 0 : m_history.history().count();
|
|
538 }
|
|
539
|
65
|
540 bool removeRows(int row, int count, QModelIndex parent = QModelIndex())
|
45
|
541 {
|
|
542 if (parent.isValid())
|
|
543 return false;
|
|
544 int lastRow = row + count - 1;
|
|
545 beginRemoveRows(parent, row, lastRow);
|
74
|
546 HistoryItem[] lst = m_history.history();
|
45
|
547 for (int i = lastRow; i >= row; --i)
|
|
548 lst.removeAt(i);
|
65
|
549 m_history.historyReset.disconnect(&this.historyReset);
|
45
|
550 m_history.setHistory(lst);
|
65
|
551 m_history.historyReset.connect(&this.historyReset);
|
45
|
552 endRemoveRows();
|
|
553 return true;
|
|
554 }
|
|
555
|
77
|
556 private:
|
45
|
557
|
65
|
558 HistoryManager m_history;
|
45
|
559 }
|
|
560
|
66
|
561
|
45
|
562 const uint MOVEDROWS = 15;
|
|
563
|
66
|
564
|
45
|
565 /*!
|
65
|
566 Proxy model that will remove any duplicate entries.
|
|
567 Both m_sourceRow and m_historyHash store their offsets not from
|
|
568 the front of the list, but as offsets from the back.
|
|
569 */
|
45
|
570 class HistoryFilterModel : public QAbstractProxyModel
|
|
571 {
|
|
572 public:
|
|
573
|
65
|
574 this(QAbstractItemModel sourceModel, QObject parent = null)
|
45
|
575 {
|
|
576 super(parent);
|
|
577 m_loaded = false;
|
|
578 setSourceModel(sourceModel);
|
|
579 }
|
|
580
|
77
|
581 bool historyContains(string url)
|
45
|
582 {
|
|
583 load();
|
77
|
584 return ((url in m_historyHash) != null);
|
45
|
585 }
|
|
586
|
77
|
587 int historyLocation(string url)
|
65
|
588 {
|
|
589 load();
|
77
|
590 if (!(url in m_historyHash))
|
65
|
591 return 0;
|
|
592 return sourceModel().rowCount() - m_historyHash.value(url);
|
|
593 }
|
45
|
594
|
65
|
595 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
|
596 {
|
|
597 load();
|
77
|
598 string url = sourceIndex.data(HistoryModel.UrlStringRole).toString();
|
|
599 if (!(url in m_historyHash))
|
65
|
600 return QModelIndex();
|
45
|
601
|
65
|
602 // This can be done in a binary search, but we can't use qBinary find
|
|
603 // because it can't take: qBinaryFind(m_sourceRow.end(), m_sourceRow.begin(), v);
|
|
604 // so if this is a performance bottlneck then convert to binary search, until then
|
|
605 // the cleaner/easier to read code wins the day.
|
|
606 int realRow = -1;
|
|
607 int sourceModelRow = sourceModel().rowCount() - sourceIndex.row();
|
45
|
608
|
65
|
609 for (int i = 0; i < m_sourceRow.count(); ++i) {
|
77
|
610 if (m_sourceRow[i] == sourceModelRow) {
|
65
|
611 realRow = i;
|
|
612 break;
|
|
613 }
|
|
614 }
|
|
615 if (realRow == -1)
|
|
616 return QModelIndex();
|
45
|
617
|
65
|
618 return createIndex(realRow, sourceIndex.column(), sourceModel().rowCount() - sourceIndex.row());
|
|
619 }
|
45
|
620
|
65
|
621 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
622 {
|
|
623 load();
|
|
624 int sourceRow = sourceModel().rowCount() - proxyIndex.internalId();
|
|
625 return sourceModel().index(sourceRow, proxyIndex.column());
|
|
626 }
|
|
627
|
65
|
628 void setSourceModel(QAbstractItemModel newSourceModel)
|
45
|
629 {
|
|
630 if (sourceModel()) {
|
65
|
631 sourceModel.modelReset.disconnect(&this.sourceReset);
|
|
632 sourceModel.dataChanged.disconnect(&this.dataChanged);
|
|
633 sourceModel.rowsInserted.disconnect(&this.sourceRowsInserted);
|
|
634 sourceModel.rowsRemoved.disconnect(&this.sourceRowsRemoved);
|
45
|
635 }
|
|
636
|
65
|
637 QAbstractProxyModel.setSourceModel(newSourceModel);
|
45
|
638
|
|
639 if (sourceModel()) {
|
|
640 m_loaded = false;
|
65
|
641 sourceModel.modelReset.connect(&this.sourceReset);
|
|
642 sourceModel.dataChanged.connect(&this.sourceDataChanged);
|
|
643 sourceModel.rowsInserted.connect(&this.sourceRowsInserted);
|
|
644 sourceModel.rowsRemoved.connect(&this.sourceRowsRemoved);
|
45
|
645 }
|
|
646 }
|
|
647
|
94
|
648 QVariant headerData(int section, Qt_Orientation orientation, int role = Qt_ItemDataRole.DisplayRole)
|
45
|
649 {
|
|
650 return sourceModel().headerData(section, orientation, role);
|
|
651 }
|
|
652
|
65
|
653 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
654 {
|
|
655 load();
|
|
656 if (parent.isValid())
|
|
657 return 0;
|
77
|
658 return m_historyHash.length;
|
45
|
659 }
|
|
660
|
74
|
661 int columnCount(QModelIndex parent = QModelIndex())
|
45
|
662 {
|
|
663 return (parent.isValid()) ? 0 : 2;
|
|
664 }
|
|
665
|
65
|
666 QModelIndex index(int, int, QModelIndex = QModelIndex())
|
45
|
667 {
|
|
668 load();
|
|
669 if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
|
|
670 return QModelIndex();
|
|
671
|
|
672 return createIndex(row, column, m_sourceRow[row]);
|
|
673 }
|
|
674
|
85
|
675 QModelIndex parent(QModelIndex index = QModelIndex())
|
45
|
676 {
|
|
677 return QModelIndex();
|
|
678 }
|
|
679
|
65
|
680 /*
|
|
681 Removing a continuous block of rows will remove filtered rows too as this is
|
|
682 the users intention.
|
|
683 */
|
74
|
684 bool removeRows(int row, int count, QModelIndex parent = QModelIndex())
|
45
|
685 {
|
|
686 if (row < 0 || count <= 0 || row + count > rowCount(parent) || parent.isValid())
|
|
687 return false;
|
|
688 int lastRow = row + count - 1;
|
65
|
689 sourceModel.rowsRemoved,disconnect(&this.sourceRowsRemoved);
|
45
|
690 beginRemoveRows(parent, row, lastRow);
|
|
691 int oldCount = rowCount();
|
|
692 int start = sourceModel().rowCount() - m_sourceRow.value(row);
|
|
693 int end = sourceModel().rowCount() - m_sourceRow.value(lastRow);
|
|
694 sourceModel().removeRows(start, end - start + 1);
|
|
695 endRemoveRows();
|
65
|
696 sourceModel.rowsRemoved.connect(&this.sourceRowsRemoved);
|
45
|
697 m_loaded = false;
|
|
698 if (oldCount - count != rowCount())
|
|
699 reset();
|
|
700 return true;
|
|
701 }
|
|
702
|
94
|
703 QVariant data(QModelIndex index, int role = Qt_ItemDataRole.DisplayRole)
|
45
|
704 {
|
65
|
705 return QAbstractProxyModel.data(index, role);
|
45
|
706 }
|
|
707
|
73
|
708 private:
|
45
|
709
|
|
710 void sourceReset()
|
|
711 {
|
|
712 m_loaded = false;
|
|
713 reset();
|
|
714 }
|
|
715
|
65
|
716 void sourceDataChanged(QModelIndex topLeft, QModelIndex bottomRight)
|
45
|
717 {
|
74
|
718 dataChanged.emit(mapFromSource(topLeft), mapFromSource(bottomRight));
|
45
|
719 }
|
|
720
|
65
|
721 void sourceRowsRemoved(QModelIndex , int start, int end)
|
45
|
722 {
|
|
723 sourceReset();
|
|
724 }
|
|
725
|
65
|
726 void sourceRowsInserted(QModelIndex parent, int start, int end)
|
45
|
727 {
|
65
|
728 assert(start == end && start == 0);
|
77
|
729
|
45
|
730 if (!m_loaded)
|
|
731 return;
|
|
732 QModelIndex idx = sourceModel().index(start, 0, parent);
|
77
|
733 string url = idx.data(HistoryModel.UrlStringRole).toString();
|
|
734 if (url in m_historyHash) {
|
45
|
735 int sourceRow = sourceModel().rowCount() - m_historyHash[url];
|
|
736 int realRow = mapFromSource(sourceModel().index(sourceRow, 0)).row();
|
|
737 beginRemoveRows(QModelIndex(), realRow, realRow);
|
|
738 m_sourceRow.removeAt(realRow);
|
|
739 m_historyHash.remove(url);
|
|
740 endRemoveRows();
|
|
741 }
|
|
742 beginInsertRows(QModelIndex(), 0, 0);
|
77
|
743 m_historyHash[url] = (sourceModel().rowCount() - start);
|
45
|
744 m_sourceRow.insert(0, sourceModel().rowCount());
|
|
745 endInsertRows();
|
|
746 }
|
|
747
|
|
748 private:
|
|
749 void load()
|
|
750 {
|
|
751 if (m_loaded)
|
|
752 return;
|
|
753 m_sourceRow.clear();
|
77
|
754 m_historyHash = null;
|
|
755 m_historyHash.length = sourceModel().rowCount();
|
|
756 m_historyHash.length = 0;
|
45
|
757 for (int i = 0; i < sourceModel().rowCount(); ++i) {
|
|
758 QModelIndex idx = sourceModel().index(i, 0);
|
77
|
759 string url = idx.data(HistoryModel.UrlStringRole).toString();
|
|
760 if (!(url in m_historyHash)) {
|
45
|
761 m_sourceRow.append(sourceModel().rowCount() - i);
|
|
762 m_historyHash[url] = sourceModel().rowCount() - i;
|
|
763 }
|
|
764 }
|
|
765 m_loaded = true;
|
|
766 }
|
|
767
|
74
|
768 int[] m_sourceRow;
|
77
|
769 int[string] m_historyHash;
|
65
|
770 bool m_loaded;
|
45
|
771 }
|
|
772
|
73
|
773
|
45
|
774 /*
|
65
|
775 The history menu
|
|
776 - Removes the first twenty entries and puts them as children of the top level.
|
|
777 - If there are less then twenty entries then the first folder is also removed.
|
45
|
778
|
65
|
779 The mapping is done by knowing that HistoryTreeModel is over a table
|
|
780 We store that row offset in our index's private data.
|
45
|
781 */
|
|
782 class HistoryMenuModel : public QAbstractProxyModel
|
|
783 {
|
|
784 public:
|
|
785
|
65
|
786 /*
|
|
787 Maps the first bunch of items of the source model to the root
|
|
788 */
|
74
|
789 this(HistoryTreeModel sourceModel, QObject parent = null)
|
65
|
790 {
|
|
791 super(parent);
|
|
792 m_treeModel = sourceModel;
|
|
793 setSourceModel(sourceModel);
|
|
794 }
|
45
|
795
|
65
|
796 int columnCount(QModelIndex parent)
|
|
797 {
|
|
798 return m_treeModel.columnCount(mapToSource(parent));
|
|
799 }
|
45
|
800
|
74
|
801 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
802 {
|
|
803 if (parent.column() > 0)
|
|
804 return 0;
|
|
805
|
|
806 if (!parent.isValid()) {
|
|
807 int folders = sourceModel().rowCount();
|
|
808 int bumpedItems = bumpedRows();
|
|
809 if (bumpedItems <= MOVEDROWS && bumpedItems == sourceModel().rowCount(sourceModel().index(0, 0)))
|
|
810 --folders;
|
|
811 return bumpedItems + folders;
|
|
812 }
|
|
813
|
|
814 if (parent.internalId() == -1) {
|
|
815 if (parent.row() < bumpedRows())
|
|
816 return 0;
|
|
817 }
|
|
818
|
|
819 QModelIndex idx = mapToSource(parent);
|
|
820 int defaultCount = sourceModel().rowCount(idx);
|
|
821 if (idx == sourceModel().index(0, 0))
|
|
822 return defaultCount - bumpedRows();
|
|
823 return defaultCount;
|
|
824 }
|
|
825
|
65
|
826 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
45
|
827 {
|
|
828 // currently not used or autotested
|
65
|
829 assert(false);
|
45
|
830 int sr = m_treeModel.mapToSource(sourceIndex).row();
|
|
831 return createIndex(sourceIndex.row(), sourceIndex.column(), sr);
|
|
832 }
|
|
833
|
65
|
834 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
835 {
|
|
836 if (!proxyIndex.isValid())
|
|
837 return QModelIndex();
|
|
838
|
|
839 if (proxyIndex.internalId() == -1) {
|
|
840 int bumpedItems = bumpedRows();
|
|
841 if (proxyIndex.row() < bumpedItems)
|
|
842 return m_treeModel.index(proxyIndex.row(), proxyIndex.column(), m_treeModel.index(0, 0));
|
|
843 if (bumpedItems <= MOVEDROWS && bumpedItems == sourceModel().rowCount(m_treeModel.index(0, 0)))
|
|
844 --bumpedItems;
|
|
845 return m_treeModel.index(proxyIndex.row() - bumpedItems, proxyIndex.column());
|
|
846 }
|
|
847
|
|
848 QModelIndex historyIndex = m_treeModel.sourceModel().index(proxyIndex.internalId(), proxyIndex.column());
|
|
849 QModelIndex treeIndex = m_treeModel.mapFromSource(historyIndex);
|
|
850 return treeIndex;
|
|
851 }
|
|
852
|
74
|
853 QModelIndex index(int row, int column, QModelIndex parent = QModelIndex())
|
45
|
854 {
|
65
|
855 if (row < 0 || column < 0 || column >= columnCount(parent) || parent.column() > 0)
|
45
|
856 return QModelIndex();
|
65
|
857
|
45
|
858 if (!parent.isValid())
|
|
859 return createIndex(row, column, -1);
|
|
860
|
|
861 QModelIndex treeIndexParent = mapToSource(parent);
|
|
862
|
|
863 int bumpedItems = 0;
|
|
864 if (treeIndexParent == m_treeModel.index(0, 0))
|
|
865 bumpedItems = bumpedRows();
|
|
866 QModelIndex treeIndex = m_treeModel.index(row + bumpedItems, column, treeIndexParent);
|
|
867 QModelIndex historyIndex = m_treeModel.mapToSource(treeIndex);
|
|
868 int historyRow = historyIndex.row();
|
|
869 if (historyRow == -1)
|
|
870 historyRow = treeIndex.row();
|
|
871 return createIndex(row, column, historyRow);
|
|
872 }
|
|
873
|
74
|
874 QModelIndex parent(QModelIndex index = QModelIndex())
|
45
|
875 {
|
|
876 int offset = index.internalId();
|
|
877 if (offset == -1 || !index.isValid())
|
|
878 return QModelIndex();
|
|
879
|
|
880 QModelIndex historyIndex = m_treeModel.sourceModel().index(index.internalId(), 0);
|
|
881 QModelIndex treeIndex = m_treeModel.mapFromSource(historyIndex);
|
|
882 QModelIndex treeIndexParent = treeIndex.parent();
|
|
883
|
|
884 int sr = m_treeModel.mapToSource(treeIndexParent).row();
|
|
885 int bumpedItems = bumpedRows();
|
|
886 if (bumpedItems <= MOVEDROWS && bumpedItems == sourceModel().rowCount(sourceModel().index(0, 0)))
|
|
887 --bumpedItems;
|
|
888 return createIndex(bumpedItems + treeIndexParent.row(), treeIndexParent.column(), sr);
|
|
889 }
|
|
890
|
|
891 int bumpedRows()
|
|
892 {
|
|
893 QModelIndex first = m_treeModel.index(0, 0);
|
|
894 if (!first.isValid())
|
|
895 return 0;
|
|
896 return qMin(m_treeModel.rowCount(first), MOVEDROWS);
|
|
897 }
|
|
898
|
|
899 private:
|
|
900
|
65
|
901 HistoryTreeModel m_treeModel;
|
45
|
902 }
|
|
903
|
73
|
904
|
45
|
905 // Menu that is dynamically populated from the history
|
|
906 class HistoryMenu : public ModelMenu
|
|
907 {
|
74
|
908 mixin Signal!("openUrl", QUrl /*url*/);
|
65
|
909
|
45
|
910 public:
|
|
911
|
65
|
912 this(QWidget parent = null)
|
45
|
913 {
|
|
914 super(parent);
|
|
915 m_history = 0;
|
66
|
916 activated.connect(&this.activated);
|
45
|
917 setHoverRole(HistoryModel.UrlStringRole);
|
|
918 }
|
|
919
|
74
|
920 void setInitialActions(QAction[] actions)
|
66
|
921 {
|
|
922 m_initialActions = actions;
|
77
|
923 for (int i = 0; i < m_initialActions.length; ++i)
|
|
924 addAction(m_initialActions[i]);
|
66
|
925 }
|
45
|
926
|
|
927 protected:
|
|
928
|
|
929 bool prePopulated()
|
|
930 {
|
|
931 if (!m_history) {
|
65
|
932 m_history = BrowserApplication.historyManager();
|
45
|
933 m_historyMenuModel = new HistoryMenuModel(m_history.historyTreeModel(), this);
|
|
934 setModel(m_historyMenuModel);
|
|
935 }
|
|
936 // initial actions
|
77
|
937 for (int i = 0; i < m_initialActions.length; ++i)
|
|
938 addAction(m_initialActions[i]);
|
|
939 if (m_initialActions.length)
|
45
|
940 addSeparator();
|
|
941 setFirstSeparator(m_historyMenuModel.bumpedRows());
|
|
942
|
|
943 return false;
|
|
944 }
|
|
945
|
|
946 void postPopulated()
|
|
947 {
|
|
948 if (m_history.history().count() > 0)
|
|
949 addSeparator();
|
|
950
|
65
|
951 QAction showAllAction = new QAction(tr("Show All History"), this);
|
|
952 showAllAction.triggered.connect(&this.showHistoryDialog);
|
45
|
953 addAction(showAllAction);
|
|
954
|
65
|
955 QAction clearAction = new QAction(tr("Clear History"), this);
|
|
956 clearAction.triggered.connect(&m_history.clear);
|
45
|
957 addAction(clearAction);
|
|
958 }
|
|
959
|
73
|
960 private:
|
|
961
|
65
|
962 void activated(QModelIndex index)
|
45
|
963 {
|
74
|
964 openUrl.emit(index.data(HistoryModel.UrlRole).toUrl());
|
45
|
965 }
|
|
966
|
|
967 void showHistoryDialog()
|
|
968 {
|
65
|
969 auto dialog = new HistoryDialog(this);
|
|
970 dialog.openUrl(QUrl).connect(&this.openUrl(QUrl));
|
45
|
971 dialog.show();
|
|
972 }
|
|
973
|
|
974 private:
|
|
975
|
65
|
976 HistoryManager m_history;
|
|
977 HistoryMenuModel m_historyMenuModel;
|
74
|
978 QAction[] m_initialActions;
|
45
|
979 }
|
|
980
|
66
|
981
|
45
|
982 // proxy model for the history model that
|
|
983 // exposes each url http://www.foo.com and it url starting at the host www.foo.com
|
|
984 class HistoryCompletionModel : public QAbstractProxyModel
|
|
985 {
|
73
|
986 public:
|
45
|
987
|
65
|
988 this(QObject parent = null)
|
45
|
989 {
|
|
990 super(parent);
|
|
991 }
|
|
992
|
65
|
993 QVariant data(QModelIndex index, int role)
|
45
|
994 {
|
94
|
995 if (sourceModel() && (role == Qt_ItemDataRole.EditRole || role == Qt_ItemDataRole.DisplayRole) && index.isValid()) {
|
45
|
996 QModelIndex idx = mapToSource(index);
|
|
997 idx = idx.sibling(idx.row(), 1);
|
77
|
998 string urlString = idx.data(HistoryModel.UrlStringRole).toString();
|
45
|
999 if (index.row() % 2) {
|
|
1000 QUrl url = urlString;
|
77
|
1001 string s = url.toString(QUrl.RemoveScheme | QUrl.RemoveUserInfo | QUrl.StripTrailingSlash);
|
45
|
1002 return s.mid(2); // strip // from the front
|
|
1003 }
|
|
1004 return urlString;
|
|
1005 }
|
65
|
1006 return QAbstractProxyModel.data(index, role);
|
45
|
1007 }
|
|
1008
|
65
|
1009 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
1010 {
|
|
1011 return (parent.isValid() || !sourceModel()) ? 0 : sourceModel().rowCount(parent) * 2;
|
|
1012 }
|
|
1013
|
65
|
1014 int columnCount(QModelIndex parent = QModelIndex())
|
45
|
1015 {
|
|
1016 return (parent.isValid()) ? 0 : 1;
|
|
1017 }
|
|
1018
|
65
|
1019 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
45
|
1020 {
|
|
1021 int row = sourceIndex.row() * 2;
|
|
1022 return index(row, sourceIndex.column());
|
|
1023 }
|
|
1024
|
65
|
1025 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
1026 {
|
|
1027 if (!sourceModel())
|
|
1028 return QModelIndex();
|
|
1029 int row = proxyIndex.row() / 2;
|
|
1030 return sourceModel().index(row, proxyIndex.column());
|
|
1031 }
|
|
1032
|
65
|
1033 QModelIndex index(int, int, QModelIndex = QModelIndex())
|
45
|
1034 {
|
65
|
1035 if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
|
45
|
1036 return QModelIndex();
|
|
1037 return createIndex(row, column, 0);
|
|
1038 }
|
|
1039
|
85
|
1040 QModelIndex parent(QModelIndex index = QModelIndex())
|
45
|
1041 {
|
|
1042 return QModelIndex();
|
|
1043 }
|
|
1044
|
74
|
1045 void setSourceModel(QAbstractItemModel sourceModel)
|
45
|
1046 {
|
|
1047 if (sourceModel()) {
|
65
|
1048 sourceModel.modelReset.disconnect(&this.sourceReset);
|
73
|
1049 sourceModel.rowsInserted.disconnect(&this.sourceReset);
|
|
1050 sourceModel.rowsRemoved.disconnect(&this.sourceReset);
|
45
|
1051 }
|
|
1052
|
65
|
1053 QAbstractProxyModel.setSourceModel(newSourceModel);
|
45
|
1054
|
|
1055 if (newSourceModel) {
|
65
|
1056 newSourceModel.modelReset.connect(&this.sourceReset);
|
73
|
1057 sourceModel.rowsInserted.connect(&this.sourceReset);
|
|
1058 sourceModel.rowsRemoved.connect(&this.sourceReset);
|
45
|
1059 }
|
|
1060
|
|
1061 reset();
|
|
1062 }
|
|
1063
|
73
|
1064 private:
|
45
|
1065
|
|
1066 void sourceReset()
|
|
1067 {
|
|
1068 reset();
|
|
1069 }
|
|
1070 }
|
|
1071
|
73
|
1072
|
45
|
1073 // proxy model for the history model that converts the list
|
|
1074 // into a tree, one top level node per day.
|
|
1075 // Used in the HistoryDialog.
|
|
1076 class HistoryTreeModel : public QAbstractProxyModel
|
|
1077 {
|
|
1078 public:
|
|
1079
|
65
|
1080 this(QAbstractItemModel sourceModel, QObject parent = null)
|
45
|
1081 {
|
|
1082 super(parent);
|
|
1083 setSourceModel(sourceModel);
|
|
1084 }
|
|
1085
|
94
|
1086 QVariant data(QModelIndex index, int role = Qt_ItemDataRole.DisplayRole)
|
45
|
1087 {
|
94
|
1088 if ((role == Qt_ItemDataRole.EditRole || role == Qt_ItemDataRole.DisplayRole)) {
|
45
|
1089 int start = index.internalId();
|
|
1090 if (start == 0) {
|
|
1091 int offset = sourceDateRow(index.row());
|
|
1092 if (index.column() == 0) {
|
|
1093 QModelIndex idx = sourceModel().index(offset, 0);
|
|
1094 QDate date = idx.data(HistoryModel.DateRole).toDate();
|
65
|
1095 if (date == QDate.currentDate())
|
45
|
1096 return tr("Earlier Today");
|
80
|
1097 return date.toString("dddd, MMMM d, yyyy");
|
45
|
1098 }
|
|
1099 if (index.column() == 1) {
|
77
|
1100 return Format(tr("{} items"), rowCount(index.sibling(index.row(), 0)));
|
45
|
1101 }
|
|
1102 }
|
|
1103 }
|
94
|
1104 if (role == Qt_ItemDataRole.DecorationRole && index.column() == 0 && !index.parent().isValid())
|
80
|
1105 return new QIcon(":history.png");
|
45
|
1106 if (role == HistoryModel.DateRole && index.column() == 0 && index.internalId() == 0) {
|
|
1107 int offset = sourceDateRow(index.row());
|
|
1108 QModelIndex idx = sourceModel().index(offset, 0);
|
|
1109 return idx.data(HistoryModel.DateRole);
|
|
1110 }
|
|
1111
|
65
|
1112 return QAbstractProxyModel.data(index, role);
|
45
|
1113 }
|
|
1114
|
74
|
1115 int columnCount(QModelIndex parent)
|
45
|
1116 {
|
|
1117 return sourceModel().columnCount(mapToSource(parent));
|
|
1118 }
|
|
1119
|
65
|
1120 int rowCount(QModelIndex parent = QModelIndex())
|
|
1121 {
|
|
1122 if ( parent.internalId() != 0 || parent.column() > 0 || !sourceModel())
|
|
1123 return 0;
|
45
|
1124
|
65
|
1125 // row count OF dates
|
|
1126 if (!parent.isValid()) {
|
77
|
1127 if (m_sourceRowCache.length)
|
|
1128 return m_sourceRowCache.length;
|
65
|
1129 QDate currentDate;
|
|
1130 int rows = 0;
|
|
1131 int totalRows = sourceModel().rowCount();
|
45
|
1132
|
65
|
1133 for (int i = 0; i < totalRows; ++i) {
|
|
1134 QDate rowDate = sourceModel().index(i, 0).data(HistoryModel.DateRole).toDate();
|
|
1135 if (rowDate != currentDate) {
|
77
|
1136 m_sourceRowCache ~= i;
|
65
|
1137 currentDate = rowDate;
|
|
1138 ++rows;
|
|
1139 }
|
|
1140 }
|
77
|
1141 assert(m_sourceRowCache.length == rows);
|
65
|
1142 return rows;
|
|
1143 }
|
45
|
1144
|
65
|
1145 // row count FOR a date
|
|
1146 int start = sourceDateRow(parent.row());
|
|
1147 int end = sourceDateRow(parent.row() + 1);
|
|
1148 return (end - start);
|
|
1149 }
|
45
|
1150
|
74
|
1151 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
45
|
1152 {
|
|
1153 if (!sourceIndex.isValid())
|
|
1154 return QModelIndex();
|
|
1155
|
77
|
1156 if (m_sourceRowCache.length == 0)
|
66
|
1157 rowCount(QModelIndex());
|
45
|
1158
|
85
|
1159 //int[].iterator it;
|
|
1160 //it = qLowerBound(m_sourceRowCache.begin(), m_sourceRowCache.end(), sourceIndex.row());
|
|
1161 int it = 0;
|
|
1162 it = qLowerBound(m_sourceRowCache, sourceIndex.row());
|
|
1163 if (it != sourceIndex.row())
|
66
|
1164 --it;
|
85
|
1165 int dateRow = qMax(0, it);
|
77
|
1166 int row = sourceIndex.row() - m_sourceRowCache[dateRow];
|
45
|
1167 return createIndex(row, sourceIndex.column(), dateRow + 1);
|
|
1168 }
|
|
1169
|
65
|
1170 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
1171 {
|
|
1172 int offset = proxyIndex.internalId();
|
|
1173 if (offset == 0)
|
|
1174 return QModelIndex();
|
|
1175 int startDateRow = sourceDateRow(offset - 1);
|
|
1176 return sourceModel().index(startDateRow + proxyIndex.row(), proxyIndex.column());
|
|
1177 }
|
|
1178
|
65
|
1179 QModelIndex index(int row, int column, QModelIndex parent = QModelIndex())
|
45
|
1180 {
|
65
|
1181 if (row < 0 || column < 0 || column >= columnCount(parent) || parent.column() > 0)
|
|
1182 return QModelIndex();
|
45
|
1183
|
65
|
1184 if (!parent.isValid())
|
|
1185 return createIndex(row, column, 0);
|
|
1186 return createIndex(row, column, parent.row() + 1);
|
45
|
1187 }
|
|
1188
|
65
|
1189 QModelIndex parent(QModelIndex index = QModelIndex())
|
45
|
1190 {
|
|
1191 int offset = index.internalId();
|
|
1192 if (offset == 0 || !index.isValid())
|
|
1193 return QModelIndex();
|
|
1194 return createIndex(offset - 1, 0, 0);
|
|
1195 }
|
|
1196
|
65
|
1197 bool hasChildren(QModelIndex parent = QModelIndex())
|
45
|
1198 {
|
|
1199 QModelIndex grandparent = parent.parent();
|
|
1200 if (!grandparent.isValid())
|
|
1201 return true;
|
|
1202 return false;
|
|
1203 }
|
|
1204
|
94
|
1205 int flags(QModelIndex index)
|
45
|
1206 {
|
|
1207 if (!index.isValid())
|
94
|
1208 return Qt_ItemFlag.NoItemFlags;
|
|
1209 return Qt_ItemFlag.ItemIsSelectable | Qt_ItemFlag.ItemIsEnabled | Qt_ItemFlag.ItemIsDragEnabled;
|
45
|
1210 }
|
|
1211
|
74
|
1212 bool removeRows(int row, int count, QModelIndex parent = QModelIndex())
|
65
|
1213 {
|
|
1214 if (row < 0 || count <= 0 || row + count > rowCount(parent))
|
|
1215 return false;
|
45
|
1216
|
65
|
1217 if (parent.isValid()) {
|
|
1218 // removing pages
|
|
1219 int offset = sourceDateRow(parent.row());
|
|
1220 return sourceModel().removeRows(offset + row, count);
|
|
1221 } else {
|
|
1222 // removing whole dates
|
|
1223 for (int i = row + count - 1; i >= row; --i) {
|
|
1224 QModelIndex dateParent = index(i, 0);
|
|
1225 int offset = sourceDateRow(dateParent.row());
|
|
1226 if (!sourceModel().removeRows(offset, rowCount(dateParent)))
|
|
1227 return false;
|
|
1228 }
|
|
1229 }
|
|
1230 return true;
|
|
1231 }
|
45
|
1232
|
94
|
1233 QVariant headerData(int section, Qt_Orientation orientation, int role = Qt_ItemDataRole.DisplayRole)
|
65
|
1234 {
|
|
1235 return sourceModel().headerData(section, orientation, role);
|
|
1236 }
|
45
|
1237
|
65
|
1238 void setSourceModel(QAbstractItemModel newSourceModel)
|
|
1239 {
|
|
1240 if (sourceModel()) {
|
|
1241 sourceModel.modelReset.disconnect(&this.sourceReset);
|
|
1242 sourceModel.layoutChanged.disconnect(&this.sourceReset);
|
|
1243 sourceModel.rowsInserted.disconnect(&this.sourceRowsInserted);
|
|
1244 sourceModel.rowsRemoved.disconnect(&this.sourceRowsRemoved);
|
|
1245 }
|
45
|
1246
|
65
|
1247 QAbstractProxyModel.setSourceModel(newSourceModel);
|
45
|
1248
|
65
|
1249 if (newSourceModel) {
|
|
1250 sourceModel.modelReset.connect(&this.sourceReset);
|
|
1251 sourceModel.layoutChanged.connect(&this.sourceReset);
|
|
1252 sourceModel.rowsInserted.connect(&this.sourceRowsInserted);
|
|
1253 sourceModel.rowsRemoved.connect(&this.sourceRowsRemoved);
|
|
1254 }
|
45
|
1255
|
65
|
1256 reset();
|
|
1257 }
|
45
|
1258
|
73
|
1259 private:
|
45
|
1260
|
|
1261 void sourceReset()
|
|
1262 {
|
77
|
1263 m_sourceRowCache.length = 0;
|
45
|
1264 reset();
|
|
1265 }
|
|
1266
|
74
|
1267 void sourceRowsInserted(QModelIndex parent, int start, int end)
|
45
|
1268 {
|
65
|
1269 assert(!parent.isValid());
|
45
|
1270 if (start != 0 || start != end) {
|
77
|
1271 m_sourceRowCache.length = 0;
|
45
|
1272 reset();
|
|
1273 return;
|
|
1274 }
|
|
1275
|
77
|
1276 m_sourceRowCache.length = 0;
|
45
|
1277 QModelIndex treeIndex = mapFromSource(sourceModel().index(start, 0));
|
|
1278 QModelIndex treeParent = treeIndex.parent();
|
|
1279 if (rowCount(treeParent) == 1) {
|
|
1280 beginInsertRows(QModelIndex(), 0, 0);
|
|
1281 endInsertRows();
|
|
1282 } else {
|
|
1283 beginInsertRows(treeParent, treeIndex.row(), treeIndex.row());
|
|
1284 endInsertRows();
|
|
1285 }
|
|
1286 }
|
|
1287
|
77
|
1288 void sourceRowsRemoved(QModelIndex parent, int start, int end)
|
65
|
1289 {
|
|
1290 assert(!parent.isValid());
|
77
|
1291 if (m_sourceRowCache.length == 0)
|
65
|
1292 return;
|
|
1293 for (int i = end; i >= start;) {
|
85
|
1294 //int[]::iterator it;
|
|
1295 //it = qLowerBound(m_sourceRowCache.begin(), m_sourceRowCache.end(), i);
|
|
1296 int it;
|
|
1297 it = qLowerBound(m_sourceRowCache, i);
|
65
|
1298 // playing it safe
|
85
|
1299 if (it == m_sourceRowCache.length) {
|
|
1300 m_sourceRowCache.length = 0;
|
65
|
1301 reset();
|
|
1302 return;
|
|
1303 }
|
45
|
1304
|
85
|
1305 if (it != i)
|
65
|
1306 --it;
|
85
|
1307 int row = qMax(0, it);
|
65
|
1308 int offset = m_sourceRowCache[row];
|
|
1309 QModelIndex dateParent = index(row, 0);
|
|
1310 // If we can remove all the rows in the date do that and skip over them
|
|
1311 int rc = rowCount(dateParent);
|
|
1312 if (i - rc + 1 == offset && start <= i - rc + 1) {
|
|
1313 beginRemoveRows(QModelIndex(), row, row);
|
|
1314 m_sourceRowCache.removeAt(row);
|
|
1315 i -= rc + 1;
|
|
1316 } else {
|
|
1317 beginRemoveRows(dateParent, i - offset, i - offset);
|
|
1318 ++row;
|
|
1319 --i;
|
|
1320 }
|
77
|
1321 for (int j = row; j < m_sourceRowCache.length; ++j)
|
65
|
1322 --m_sourceRowCache[j];
|
|
1323 endRemoveRows();
|
|
1324 }
|
|
1325 }
|
45
|
1326
|
|
1327 private:
|
|
1328
|
|
1329 // Translate the top level date row into the offset where that date starts
|
|
1330 int sourceDateRow(int row)
|
|
1331 {
|
|
1332 if (row <= 0)
|
|
1333 return 0;
|
|
1334
|
77
|
1335 if (m_sourceRowCache.length == 0)
|
45
|
1336 rowCount(QModelIndex());
|
|
1337
|
77
|
1338 if (row >= m_sourceRowCache.length) {
|
45
|
1339 if (!sourceModel())
|
|
1340 return 0;
|
|
1341 return sourceModel().rowCount();
|
|
1342 }
|
77
|
1343 return m_sourceRowCache[row];
|
45
|
1344 }
|
|
1345
|
74
|
1346 int[] m_sourceRowCache;
|
45
|
1347 }
|
|
1348
|
73
|
1349
|
45
|
1350 // A modified QSortFilterProxyModel that always accepts the root nodes in the tree
|
|
1351 // so filtering is only done on the children.
|
|
1352 // Used in the HistoryDialog
|
|
1353 class TreeProxyModel : public QSortFilterProxyModel
|
|
1354 {
|
65
|
1355 public:
|
45
|
1356
|
65
|
1357 this(QObject parent = null)
|
45
|
1358 {
|
|
1359 super(parent);
|
|
1360 setSortRole(HistoryModel.DateTimeRole);
|
94
|
1361 setFilterCaseSensitivity(Qt_CaseSensitivity.CaseInsensitive);
|
45
|
1362 }
|
65
|
1363
|
77
|
1364 protected:
|
|
1365
|
65
|
1366 bool filterAcceptsRow(int source_row, QModelIndex source_parent)
|
45
|
1367 {
|
|
1368 if (!source_parent.isValid())
|
|
1369 return true;
|
65
|
1370 return QSortFilterProxyModel.filterAcceptsRow(source_row, source_parent);
|
45
|
1371 }
|
|
1372 }
|
|
1373
|
73
|
1374
|
150
|
1375 import ui_historydialog;
|
45
|
1376
|
113
|
1377 class HistoryDialog : public QDialog //, public Ui_HistoryDialog
|
45
|
1378 {
|
113
|
1379 mixin Ui_HistoryDialog;
|
74
|
1380 mixin Signal!("openUrl", QUrl /*url*/);
|
45
|
1381
|
|
1382 public:
|
|
1383
|
74
|
1384 this(QWidget parent = null, HistoryManager history = null)
|
45
|
1385 {
|
94
|
1386 super(parent);
|
65
|
1387 HistoryManager history = setHistory;
|
45
|
1388 if (!history)
|
65
|
1389 history = BrowserApplication.historyManager();
|
113
|
1390 setupUi(this);
|
45
|
1391 tree.setUniformRowHeights(true);
|
65
|
1392 tree.setSelectionBehavior(QAbstractItemView.SelectRows);
|
94
|
1393 tree.setTextElideMode(Qt_TextElideMode.ElideMiddle);
|
65
|
1394 auto model = history.historyTreeModel();
|
|
1395 auto proxyModel = new TreeProxyModel(this);
|
77
|
1396 search.textChanged.connect(&proxyModel.setFilterFixedString);
|
65
|
1397 removeButton.clicked.connect(&tree.removeOne);
|
|
1398 removeAllButton.clicked.connect(&history.clear);
|
45
|
1399 proxyModel.setSourceModel(model);
|
|
1400 tree.setModel(proxyModel);
|
|
1401 tree.setExpanded(proxyModel.index(0, 0), true);
|
|
1402 tree.setAlternatingRowColors(true);
|
65
|
1403 QFontMetrics fm = font();
|
45
|
1404 int header = fm.width(QLatin1Char('m')) * 40;
|
|
1405 tree.header().resizeSection(0, header);
|
|
1406 tree.header().setStretchLastSection(true);
|
65
|
1407 tree.activated(QModelIndex).connect(this.open);
|
94
|
1408 tree.setContextMenuPolicy(Qt_ContextMenuPolicy.CustomContextMenu);
|
85
|
1409 tree.customContextMenuRequested.connect(&this.customContextMenuRequested);
|
45
|
1410 }
|
|
1411
|
65
|
1412 private:
|
45
|
1413
|
65
|
1414 void customContextMenuRequested(QPoint pos)
|
45
|
1415 {
|
77
|
1416 auto menu = new QMenu;
|
45
|
1417 QModelIndex index = tree.indexAt(pos);
|
|
1418 index = index.sibling(index.row(), 0);
|
|
1419 if (index.isValid() && !tree.model().hasChildren(index)) {
|
|
1420 menu.addAction(tr("Open"), this, SLOT(open()));
|
|
1421 menu.addSeparator();
|
|
1422 menu.addAction(tr("Copy"), this, SLOT(copy()));
|
|
1423 }
|
|
1424 menu.addAction(tr("Delete"), tree, SLOT(removeOne()));
|
65
|
1425 menu.exec(QCursor.pos());
|
45
|
1426 }
|
|
1427
|
|
1428 void open()
|
|
1429 {
|
|
1430 QModelIndex index = tree.currentIndex();
|
|
1431 if (!index.parent().isValid())
|
|
1432 return;
|
77
|
1433 openUrl.emit(index.data(HistoryModel.UrlRole).toUrl());
|
45
|
1434 }
|
|
1435
|
|
1436 void copy()
|
|
1437 {
|
|
1438 QModelIndex index = tree.currentIndex();
|
|
1439 if (!index.parent().isValid())
|
|
1440 return;
|
77
|
1441 string url = index.data(HistoryModel.UrlStringRole).toString();
|
45
|
1442
|
65
|
1443 QClipboard clipboard = QApplication.clipboard();
|
45
|
1444 clipboard.setText(url);
|
|
1445 }
|
|
1446 }
|