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;
|
45
|
50
|
72
|
51 import qt.core.QtAlgorithms;
|
|
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;
|
|
59 import qt.core.QDebug;
|
|
60 import qt.core.QDateTime;
|
|
61 import qt.core.QHash;
|
|
62 import qt.core.QObject;
|
|
63 import qt.core.QTimer;
|
|
64 import qt.core.QUrl;
|
45
|
65
|
73
|
66 import qt.webkit.QWebHistoryInterface;
|
74
|
67 import qt.webkit.QWebSettings;
|
45
|
68
|
|
69 import QWebHistoryInterface;
|
|
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) {
|
|
294 qWarning() << "Unable to open history file for saving"
|
|
295 << (saveAll ? tempFile.fileName() : historyFile.fileName());
|
|
296 return;
|
|
297 }
|
45
|
298
|
65
|
299 QDataStream out_ = (saveAll ? tempFile : historyFile);
|
|
300 for (int i = first; i >= 0; --i) {
|
|
301 QByteArray data;
|
|
302 auto stream = new QDataStream(data, QIODevice.WriteOnly);
|
77
|
303 HistoryItem item = m_history[i];
|
65
|
304 stream << HISTORY_VERSION << item.url << item.dateTime << item.title;
|
|
305 out_ << data;
|
|
306 }
|
|
307 tempFile.close();
|
45
|
308
|
65
|
309 if (saveAll) {
|
|
310 if (historyFile.exists() && !historyFile.remove())
|
|
311 qWarning() << "History: error removing old history." << historyFile.errorString();
|
|
312 if (!tempFile.rename(historyFile.fileName()))
|
|
313 qWarning() << "History: error moving new history over old." << tempFile.errorString() << historyFile.fileName();
|
|
314 }
|
|
315 m_lastSavedUrl = m_history.value(0).url;
|
|
316 }
|
45
|
317
|
65
|
318 void checkForExpired()
|
|
319 {
|
77
|
320 if (m_historyLimit < 0 || m_history.length == 0)
|
65
|
321 return;
|
45
|
322
|
65
|
323 QDateTime now = QDateTime.currentDateTime();
|
|
324 int nextTimeout = 0;
|
45
|
325
|
77
|
326 while (m_history.length) {
|
65
|
327 QDateTime checkForExpired = m_history.last().dateTime;
|
|
328 checkForExpired.setDate(checkForExpired.date().addDays(m_historyLimit));
|
|
329 if (now.daysTo(checkForExpired) > 7) {
|
|
330 // check at most in a week to prevent int overflows on the timer
|
|
331 nextTimeout = 7 * 86400;
|
|
332 } else {
|
|
333 nextTimeout = now.secsTo(checkForExpired);
|
|
334 }
|
|
335 if (nextTimeout > 0)
|
|
336 break;
|
|
337 HistoryItem item = m_history.takeLast();
|
|
338 // remove from saved file also
|
77
|
339 m_lastSavedUrl = null;
|
|
340 entryRemoved.emit(item);
|
65
|
341 }
|
45
|
342
|
65
|
343 if (nextTimeout > 0)
|
|
344 m_expiredTimer.start(nextTimeout * 1000);
|
|
345 }
|
45
|
346
|
|
347 protected:
|
|
348
|
65
|
349 void addHistoryItem(HistoryItem item)
|
|
350 {
|
|
351 QWebSettings globalSettings = QWebSettings.globalSettings();
|
|
352 if (globalSettings.testAttribute(QWebSettings.PrivateBrowsingEnabled))
|
|
353 return;
|
|
354
|
77
|
355 m_history = [item] ~ m_history;
|
|
356 entryAdded.emit(item);
|
|
357 if (m_history.length == 1)
|
65
|
358 checkForExpired();
|
|
359 }
|
45
|
360
|
|
361 private:
|
|
362
|
65
|
363 void load()
|
|
364 {
|
|
365 loadSettings();
|
45
|
366
|
80
|
367 historyFile = new QFile(QDesktopServices.storageLocation(QDesktopServices.DataLocation) ~ "/history");
|
65
|
368 if (!historyFile.exists())
|
|
369 return;
|
|
370 if (!historyFile.open(QFile.ReadOnly)) {
|
|
371 qWarning() << "Unable to open history file" << historyFile.fileName();
|
|
372 return;
|
|
373 }
|
45
|
374
|
74
|
375 HistoryItem[] list;
|
65
|
376 auto in_ = new QDataStream(&historyFile);
|
|
377 // Double check that the history file is sorted as it is read in
|
|
378 bool needToSort = false;
|
77
|
379 auto lastInsertedItem = new HistoryItem;
|
|
380 auto data = new QByteArray;
|
|
381 auto stream = new QDataStream;
|
|
382 auto buffer = new QBuffer;
|
65
|
383 stream.setDevice(buffer);
|
|
384 while (!historyFile.atEnd()) {
|
|
385 in_ >> data;
|
|
386 buffer.close();
|
|
387 buffer.setBuffer(data);
|
|
388 buffer.open(QIODevice.ReadOnly);
|
77
|
389 uint ver;
|
65
|
390 stream >> ver;
|
|
391 if (ver != HISTORY_VERSION)
|
|
392 continue;
|
77
|
393 auto item = new HistoryItem;
|
65
|
394 stream >> item.url;
|
|
395 stream >> item.dateTime;
|
|
396 stream >> item.title;
|
45
|
397
|
65
|
398 if (!item.dateTime.isValid())
|
|
399 continue;
|
45
|
400
|
65
|
401 if (item == lastInsertedItem) {
|
|
402 if (lastInsertedItem.title.isEmpty() && !list.isEmpty())
|
|
403 list[0].title = item.title;
|
|
404 continue;
|
|
405 }
|
45
|
406
|
65
|
407 if (!needToSort && !list.isEmpty() && lastInsertedItem < item)
|
|
408 needToSort = true;
|
45
|
409
|
65
|
410 list.prepend(item);
|
|
411 lastInsertedItem = item;
|
|
412 }
|
|
413
|
|
414 if (needToSort)
|
|
415 qSort(list.begin(), list.end());
|
45
|
416
|
65
|
417 setHistory(list, true);
|
45
|
418
|
65
|
419 // If we had to sort re-write the whole history sorted
|
|
420 if (needToSort) {
|
77
|
421 m_lastSavedUrl = null;
|
65
|
422 m_saveTimer.changeOccurred();
|
|
423 }
|
|
424 }
|
45
|
425
|
65
|
426 AutoSaver m_saveTimer;
|
|
427 int m_historyLimit;
|
|
428 QTimer m_expiredTimer;
|
74
|
429 HistoryItem[] m_history;
|
77
|
430 string m_lastSavedUrl;
|
45
|
431
|
65
|
432 HistoryModel m_historyModel;
|
|
433 HistoryFilterModel m_historyFilterModel;
|
|
434 HistoryTreeModel m_historyTreeModel;
|
|
435 }
|
45
|
436
|
73
|
437
|
45
|
438 class HistoryModel : public QAbstractTableModel
|
|
439 {
|
65
|
440 public:
|
45
|
441
|
65
|
442 void historyReset()
|
|
443 {
|
|
444 reset();
|
|
445 }
|
45
|
446
|
65
|
447 void entryAdded()
|
|
448 {
|
|
449 beginInsertRows(QModelIndex(), 0, 0);
|
|
450 endInsertRows();
|
|
451 }
|
45
|
452
|
65
|
453 void entryUpdated(int offset)
|
|
454 {
|
|
455 QModelIndex idx = index(offset, 0);
|
77
|
456 dataChanged.emit(idx, idx);
|
65
|
457 }
|
45
|
458
|
|
459 public:
|
|
460
|
|
461 enum Roles {
|
|
462 DateRole = Qt.UserRole + 1,
|
|
463 DateTimeRole = Qt.UserRole + 2,
|
|
464 UrlRole = Qt.UserRole + 3,
|
|
465 UrlStringRole = Qt.UserRole + 4
|
73
|
466 };
|
45
|
467
|
65
|
468 this(HistoryManager history, QObject parent = null)
|
45
|
469 {
|
|
470 super(parent);
|
|
471 m_history = history;
|
65
|
472 assert(m_history);
|
|
473
|
|
474 m_history.historyReset.connect(&this.historyReset);
|
|
475 m_history.entryRemoved.connect(&this.historyReset);
|
|
476 m_history.entryAdded.connect(&this.entryAdded);
|
|
477 m_history.entryUpdated.connect(&this.entryUpdated);
|
45
|
478 }
|
|
479
|
|
480 QVariant headerData(int section, Qt.Orientation orientation, int role = Qt.DisplayRole)
|
|
481 {
|
|
482 if (orientation == Qt.Horizontal && role == Qt.DisplayRole) {
|
|
483 switch (section) {
|
|
484 case 0: return tr("Title");
|
|
485 case 1: return tr("Address");
|
|
486 }
|
|
487 }
|
65
|
488 return QAbstractTableModel.headerData(section, orientation, role);
|
45
|
489 }
|
|
490
|
65
|
491 QVariant data(QModelIndex index, int role = Qt.DisplayRole)
|
45
|
492 {
|
74
|
493 HistoryItem[] lst = m_history.history();
|
77
|
494 if (index.row() < 0 || index.row() >= lst.length)
|
45
|
495 return QVariant();
|
|
496
|
77
|
497 HistoryItem item = lst[index.row()];
|
45
|
498 switch (role) {
|
|
499 case DateTimeRole:
|
|
500 return item.dateTime;
|
|
501 case DateRole:
|
|
502 return item.dateTime.date();
|
|
503 case UrlRole:
|
|
504 return QUrl(item.url);
|
|
505 case UrlStringRole:
|
|
506 return item.url;
|
|
507 case Qt.DisplayRole:
|
|
508 case Qt.EditRole: {
|
|
509 switch (index.column()) {
|
|
510 case 0:
|
|
511 // when there is no title try to generate one from the url
|
|
512 if (item.title.isEmpty()) {
|
77
|
513 string page = QFileInfo(QUrl(item.url).path()).fileName();
|
45
|
514 if (!page.isEmpty())
|
|
515 return page;
|
|
516 return item.url;
|
|
517 }
|
|
518 return item.title;
|
|
519 case 1:
|
|
520 return item.url;
|
|
521 }
|
|
522 }
|
|
523 case Qt.DecorationRole:
|
|
524 if (index.column() == 0) {
|
65
|
525 return BrowserApplication.instance().icon(item.url);
|
45
|
526 }
|
|
527 }
|
|
528 return QVariant();
|
|
529 }
|
|
530
|
65
|
531 int columnCount(QModelIndex parent = QModelIndex())
|
45
|
532 {
|
|
533 return (parent.isValid()) ? 0 : 2;
|
|
534 }
|
|
535
|
65
|
536 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
537 {
|
|
538 return (parent.isValid()) ? 0 : m_history.history().count();
|
|
539 }
|
|
540
|
65
|
541 bool removeRows(int row, int count, QModelIndex parent = QModelIndex())
|
45
|
542 {
|
|
543 if (parent.isValid())
|
|
544 return false;
|
|
545 int lastRow = row + count - 1;
|
|
546 beginRemoveRows(parent, row, lastRow);
|
74
|
547 HistoryItem[] lst = m_history.history();
|
45
|
548 for (int i = lastRow; i >= row; --i)
|
|
549 lst.removeAt(i);
|
65
|
550 m_history.historyReset.disconnect(&this.historyReset);
|
45
|
551 m_history.setHistory(lst);
|
65
|
552 m_history.historyReset.connect(&this.historyReset);
|
45
|
553 endRemoveRows();
|
|
554 return true;
|
|
555 }
|
|
556
|
77
|
557 private:
|
45
|
558
|
65
|
559 HistoryManager m_history;
|
45
|
560 }
|
|
561
|
66
|
562
|
45
|
563 const uint MOVEDROWS = 15;
|
|
564
|
66
|
565
|
45
|
566 /*!
|
65
|
567 Proxy model that will remove any duplicate entries.
|
|
568 Both m_sourceRow and m_historyHash store their offsets not from
|
|
569 the front of the list, but as offsets from the back.
|
|
570 */
|
45
|
571 class HistoryFilterModel : public QAbstractProxyModel
|
|
572 {
|
|
573 public:
|
|
574
|
65
|
575 this(QAbstractItemModel sourceModel, QObject parent = null)
|
45
|
576 {
|
|
577 super(parent);
|
|
578 m_loaded = false;
|
|
579 setSourceModel(sourceModel);
|
|
580 }
|
|
581
|
77
|
582 bool historyContains(string url)
|
45
|
583 {
|
|
584 load();
|
77
|
585 return ((url in m_historyHash) != null);
|
45
|
586 }
|
|
587
|
77
|
588 int historyLocation(string url)
|
65
|
589 {
|
|
590 load();
|
77
|
591 if (!(url in m_historyHash))
|
65
|
592 return 0;
|
|
593 return sourceModel().rowCount() - m_historyHash.value(url);
|
|
594 }
|
45
|
595
|
65
|
596 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
|
597 {
|
|
598 load();
|
77
|
599 string url = sourceIndex.data(HistoryModel.UrlStringRole).toString();
|
|
600 if (!(url in m_historyHash))
|
65
|
601 return QModelIndex();
|
45
|
602
|
65
|
603 // This can be done in a binary search, but we can't use qBinary find
|
|
604 // because it can't take: qBinaryFind(m_sourceRow.end(), m_sourceRow.begin(), v);
|
|
605 // so if this is a performance bottlneck then convert to binary search, until then
|
|
606 // the cleaner/easier to read code wins the day.
|
|
607 int realRow = -1;
|
|
608 int sourceModelRow = sourceModel().rowCount() - sourceIndex.row();
|
45
|
609
|
65
|
610 for (int i = 0; i < m_sourceRow.count(); ++i) {
|
77
|
611 if (m_sourceRow[i] == sourceModelRow) {
|
65
|
612 realRow = i;
|
|
613 break;
|
|
614 }
|
|
615 }
|
|
616 if (realRow == -1)
|
|
617 return QModelIndex();
|
45
|
618
|
65
|
619 return createIndex(realRow, sourceIndex.column(), sourceModel().rowCount() - sourceIndex.row());
|
|
620 }
|
45
|
621
|
65
|
622 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
623 {
|
|
624 load();
|
|
625 int sourceRow = sourceModel().rowCount() - proxyIndex.internalId();
|
|
626 return sourceModel().index(sourceRow, proxyIndex.column());
|
|
627 }
|
|
628
|
65
|
629 void setSourceModel(QAbstractItemModel newSourceModel)
|
45
|
630 {
|
|
631 if (sourceModel()) {
|
65
|
632 sourceModel.modelReset.disconnect(&this.sourceReset);
|
|
633 sourceModel.dataChanged.disconnect(&this.dataChanged);
|
|
634 sourceModel.rowsInserted.disconnect(&this.sourceRowsInserted);
|
|
635 sourceModel.rowsRemoved.disconnect(&this.sourceRowsRemoved);
|
45
|
636 }
|
|
637
|
65
|
638 QAbstractProxyModel.setSourceModel(newSourceModel);
|
45
|
639
|
|
640 if (sourceModel()) {
|
|
641 m_loaded = false;
|
65
|
642 sourceModel.modelReset.connect(&this.sourceReset);
|
|
643 sourceModel.dataChanged.connect(&this.sourceDataChanged);
|
|
644 sourceModel.rowsInserted.connect(&this.sourceRowsInserted);
|
|
645 sourceModel.rowsRemoved.connect(&this.sourceRowsRemoved);
|
45
|
646 }
|
|
647 }
|
|
648
|
74
|
649 QVariant headerData(int section, Qt.Orientation orientation, int role = Qt.DisplayRole)
|
45
|
650 {
|
|
651 return sourceModel().headerData(section, orientation, role);
|
|
652 }
|
|
653
|
65
|
654 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
655 {
|
|
656 load();
|
|
657 if (parent.isValid())
|
|
658 return 0;
|
77
|
659 return m_historyHash.length;
|
45
|
660 }
|
|
661
|
74
|
662 int columnCount(QModelIndex parent = QModelIndex())
|
45
|
663 {
|
|
664 return (parent.isValid()) ? 0 : 2;
|
|
665 }
|
|
666
|
65
|
667 QModelIndex index(int, int, QModelIndex = QModelIndex())
|
45
|
668 {
|
|
669 load();
|
|
670 if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
|
|
671 return QModelIndex();
|
|
672
|
|
673 return createIndex(row, column, m_sourceRow[row]);
|
|
674 }
|
|
675
|
85
|
676 QModelIndex parent(QModelIndex index = QModelIndex())
|
45
|
677 {
|
|
678 return QModelIndex();
|
|
679 }
|
|
680
|
65
|
681 /*
|
|
682 Removing a continuous block of rows will remove filtered rows too as this is
|
|
683 the users intention.
|
|
684 */
|
74
|
685 bool removeRows(int row, int count, QModelIndex parent = QModelIndex())
|
45
|
686 {
|
|
687 if (row < 0 || count <= 0 || row + count > rowCount(parent) || parent.isValid())
|
|
688 return false;
|
|
689 int lastRow = row + count - 1;
|
65
|
690 sourceModel.rowsRemoved,disconnect(&this.sourceRowsRemoved);
|
45
|
691 beginRemoveRows(parent, row, lastRow);
|
|
692 int oldCount = rowCount();
|
|
693 int start = sourceModel().rowCount() - m_sourceRow.value(row);
|
|
694 int end = sourceModel().rowCount() - m_sourceRow.value(lastRow);
|
|
695 sourceModel().removeRows(start, end - start + 1);
|
|
696 endRemoveRows();
|
65
|
697 sourceModel.rowsRemoved.connect(&this.sourceRowsRemoved);
|
45
|
698 m_loaded = false;
|
|
699 if (oldCount - count != rowCount())
|
|
700 reset();
|
|
701 return true;
|
|
702 }
|
|
703
|
74
|
704 QVariant data(QModelIndex index, int role = Qt.DisplayRole)
|
45
|
705 {
|
65
|
706 return QAbstractProxyModel.data(index, role);
|
45
|
707 }
|
|
708
|
73
|
709 private:
|
45
|
710
|
|
711 void sourceReset()
|
|
712 {
|
|
713 m_loaded = false;
|
|
714 reset();
|
|
715 }
|
|
716
|
65
|
717 void sourceDataChanged(QModelIndex topLeft, QModelIndex bottomRight)
|
45
|
718 {
|
74
|
719 dataChanged.emit(mapFromSource(topLeft), mapFromSource(bottomRight));
|
45
|
720 }
|
|
721
|
65
|
722 void sourceRowsRemoved(QModelIndex , int start, int end)
|
45
|
723 {
|
|
724 sourceReset();
|
|
725 }
|
|
726
|
65
|
727 void sourceRowsInserted(QModelIndex parent, int start, int end)
|
45
|
728 {
|
65
|
729 assert(start == end && start == 0);
|
77
|
730
|
45
|
731 if (!m_loaded)
|
|
732 return;
|
|
733 QModelIndex idx = sourceModel().index(start, 0, parent);
|
77
|
734 string url = idx.data(HistoryModel.UrlStringRole).toString();
|
|
735 if (url in m_historyHash) {
|
45
|
736 int sourceRow = sourceModel().rowCount() - m_historyHash[url];
|
|
737 int realRow = mapFromSource(sourceModel().index(sourceRow, 0)).row();
|
|
738 beginRemoveRows(QModelIndex(), realRow, realRow);
|
|
739 m_sourceRow.removeAt(realRow);
|
|
740 m_historyHash.remove(url);
|
|
741 endRemoveRows();
|
|
742 }
|
|
743 beginInsertRows(QModelIndex(), 0, 0);
|
77
|
744 m_historyHash[url] = (sourceModel().rowCount() - start);
|
45
|
745 m_sourceRow.insert(0, sourceModel().rowCount());
|
|
746 endInsertRows();
|
|
747 }
|
|
748
|
|
749 private:
|
|
750 void load()
|
|
751 {
|
|
752 if (m_loaded)
|
|
753 return;
|
|
754 m_sourceRow.clear();
|
77
|
755 m_historyHash = null;
|
|
756 m_historyHash.length = sourceModel().rowCount();
|
|
757 m_historyHash.length = 0;
|
45
|
758 for (int i = 0; i < sourceModel().rowCount(); ++i) {
|
|
759 QModelIndex idx = sourceModel().index(i, 0);
|
77
|
760 string url = idx.data(HistoryModel.UrlStringRole).toString();
|
|
761 if (!(url in m_historyHash)) {
|
45
|
762 m_sourceRow.append(sourceModel().rowCount() - i);
|
|
763 m_historyHash[url] = sourceModel().rowCount() - i;
|
|
764 }
|
|
765 }
|
|
766 m_loaded = true;
|
|
767 }
|
|
768
|
74
|
769 int[] m_sourceRow;
|
77
|
770 int[string] m_historyHash;
|
65
|
771 bool m_loaded;
|
45
|
772 }
|
|
773
|
73
|
774
|
45
|
775 /*
|
65
|
776 The history menu
|
|
777 - Removes the first twenty entries and puts them as children of the top level.
|
|
778 - If there are less then twenty entries then the first folder is also removed.
|
45
|
779
|
65
|
780 The mapping is done by knowing that HistoryTreeModel is over a table
|
|
781 We store that row offset in our index's private data.
|
45
|
782 */
|
|
783 class HistoryMenuModel : public QAbstractProxyModel
|
|
784 {
|
|
785 public:
|
|
786
|
65
|
787 /*
|
|
788 Maps the first bunch of items of the source model to the root
|
|
789 */
|
74
|
790 this(HistoryTreeModel sourceModel, QObject parent = null)
|
65
|
791 {
|
|
792 super(parent);
|
|
793 m_treeModel = sourceModel;
|
|
794 setSourceModel(sourceModel);
|
|
795 }
|
45
|
796
|
65
|
797 int columnCount(QModelIndex parent)
|
|
798 {
|
|
799 return m_treeModel.columnCount(mapToSource(parent));
|
|
800 }
|
45
|
801
|
74
|
802 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
803 {
|
|
804 if (parent.column() > 0)
|
|
805 return 0;
|
|
806
|
|
807 if (!parent.isValid()) {
|
|
808 int folders = sourceModel().rowCount();
|
|
809 int bumpedItems = bumpedRows();
|
|
810 if (bumpedItems <= MOVEDROWS && bumpedItems == sourceModel().rowCount(sourceModel().index(0, 0)))
|
|
811 --folders;
|
|
812 return bumpedItems + folders;
|
|
813 }
|
|
814
|
|
815 if (parent.internalId() == -1) {
|
|
816 if (parent.row() < bumpedRows())
|
|
817 return 0;
|
|
818 }
|
|
819
|
|
820 QModelIndex idx = mapToSource(parent);
|
|
821 int defaultCount = sourceModel().rowCount(idx);
|
|
822 if (idx == sourceModel().index(0, 0))
|
|
823 return defaultCount - bumpedRows();
|
|
824 return defaultCount;
|
|
825 }
|
|
826
|
65
|
827 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
45
|
828 {
|
|
829 // currently not used or autotested
|
65
|
830 assert(false);
|
45
|
831 int sr = m_treeModel.mapToSource(sourceIndex).row();
|
|
832 return createIndex(sourceIndex.row(), sourceIndex.column(), sr);
|
|
833 }
|
|
834
|
65
|
835 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
836 {
|
|
837 if (!proxyIndex.isValid())
|
|
838 return QModelIndex();
|
|
839
|
|
840 if (proxyIndex.internalId() == -1) {
|
|
841 int bumpedItems = bumpedRows();
|
|
842 if (proxyIndex.row() < bumpedItems)
|
|
843 return m_treeModel.index(proxyIndex.row(), proxyIndex.column(), m_treeModel.index(0, 0));
|
|
844 if (bumpedItems <= MOVEDROWS && bumpedItems == sourceModel().rowCount(m_treeModel.index(0, 0)))
|
|
845 --bumpedItems;
|
|
846 return m_treeModel.index(proxyIndex.row() - bumpedItems, proxyIndex.column());
|
|
847 }
|
|
848
|
|
849 QModelIndex historyIndex = m_treeModel.sourceModel().index(proxyIndex.internalId(), proxyIndex.column());
|
|
850 QModelIndex treeIndex = m_treeModel.mapFromSource(historyIndex);
|
|
851 return treeIndex;
|
|
852 }
|
|
853
|
74
|
854 QModelIndex index(int row, int column, QModelIndex parent = QModelIndex())
|
45
|
855 {
|
65
|
856 if (row < 0 || column < 0 || column >= columnCount(parent) || parent.column() > 0)
|
45
|
857 return QModelIndex();
|
65
|
858
|
45
|
859 if (!parent.isValid())
|
|
860 return createIndex(row, column, -1);
|
|
861
|
|
862 QModelIndex treeIndexParent = mapToSource(parent);
|
|
863
|
|
864 int bumpedItems = 0;
|
|
865 if (treeIndexParent == m_treeModel.index(0, 0))
|
|
866 bumpedItems = bumpedRows();
|
|
867 QModelIndex treeIndex = m_treeModel.index(row + bumpedItems, column, treeIndexParent);
|
|
868 QModelIndex historyIndex = m_treeModel.mapToSource(treeIndex);
|
|
869 int historyRow = historyIndex.row();
|
|
870 if (historyRow == -1)
|
|
871 historyRow = treeIndex.row();
|
|
872 return createIndex(row, column, historyRow);
|
|
873 }
|
|
874
|
74
|
875 QModelIndex parent(QModelIndex index = QModelIndex())
|
45
|
876 {
|
|
877 int offset = index.internalId();
|
|
878 if (offset == -1 || !index.isValid())
|
|
879 return QModelIndex();
|
|
880
|
|
881 QModelIndex historyIndex = m_treeModel.sourceModel().index(index.internalId(), 0);
|
|
882 QModelIndex treeIndex = m_treeModel.mapFromSource(historyIndex);
|
|
883 QModelIndex treeIndexParent = treeIndex.parent();
|
|
884
|
|
885 int sr = m_treeModel.mapToSource(treeIndexParent).row();
|
|
886 int bumpedItems = bumpedRows();
|
|
887 if (bumpedItems <= MOVEDROWS && bumpedItems == sourceModel().rowCount(sourceModel().index(0, 0)))
|
|
888 --bumpedItems;
|
|
889 return createIndex(bumpedItems + treeIndexParent.row(), treeIndexParent.column(), sr);
|
|
890 }
|
|
891
|
|
892 int bumpedRows()
|
|
893 {
|
|
894 QModelIndex first = m_treeModel.index(0, 0);
|
|
895 if (!first.isValid())
|
|
896 return 0;
|
|
897 return qMin(m_treeModel.rowCount(first), MOVEDROWS);
|
|
898 }
|
|
899
|
|
900 private:
|
|
901
|
65
|
902 HistoryTreeModel m_treeModel;
|
45
|
903 }
|
|
904
|
73
|
905
|
45
|
906 // Menu that is dynamically populated from the history
|
|
907 class HistoryMenu : public ModelMenu
|
|
908 {
|
74
|
909 mixin Signal!("openUrl", QUrl /*url*/);
|
65
|
910
|
45
|
911 public:
|
|
912
|
65
|
913 this(QWidget parent = null)
|
45
|
914 {
|
|
915 super(parent);
|
|
916 m_history = 0;
|
66
|
917 activated.connect(&this.activated);
|
45
|
918 setHoverRole(HistoryModel.UrlStringRole);
|
|
919 }
|
|
920
|
74
|
921 void setInitialActions(QAction[] actions)
|
66
|
922 {
|
|
923 m_initialActions = actions;
|
77
|
924 for (int i = 0; i < m_initialActions.length; ++i)
|
|
925 addAction(m_initialActions[i]);
|
66
|
926 }
|
45
|
927
|
|
928 protected:
|
|
929
|
|
930 bool prePopulated()
|
|
931 {
|
|
932 if (!m_history) {
|
65
|
933 m_history = BrowserApplication.historyManager();
|
45
|
934 m_historyMenuModel = new HistoryMenuModel(m_history.historyTreeModel(), this);
|
|
935 setModel(m_historyMenuModel);
|
|
936 }
|
|
937 // initial actions
|
77
|
938 for (int i = 0; i < m_initialActions.length; ++i)
|
|
939 addAction(m_initialActions[i]);
|
|
940 if (m_initialActions.length)
|
45
|
941 addSeparator();
|
|
942 setFirstSeparator(m_historyMenuModel.bumpedRows());
|
|
943
|
|
944 return false;
|
|
945 }
|
|
946
|
|
947 void postPopulated()
|
|
948 {
|
|
949 if (m_history.history().count() > 0)
|
|
950 addSeparator();
|
|
951
|
65
|
952 QAction showAllAction = new QAction(tr("Show All History"), this);
|
|
953 showAllAction.triggered.connect(&this.showHistoryDialog);
|
45
|
954 addAction(showAllAction);
|
|
955
|
65
|
956 QAction clearAction = new QAction(tr("Clear History"), this);
|
|
957 clearAction.triggered.connect(&m_history.clear);
|
45
|
958 addAction(clearAction);
|
|
959 }
|
|
960
|
73
|
961 private:
|
|
962
|
65
|
963 void activated(QModelIndex index)
|
45
|
964 {
|
74
|
965 openUrl.emit(index.data(HistoryModel.UrlRole).toUrl());
|
45
|
966 }
|
|
967
|
|
968 void showHistoryDialog()
|
|
969 {
|
65
|
970 auto dialog = new HistoryDialog(this);
|
|
971 dialog.openUrl(QUrl).connect(&this.openUrl(QUrl));
|
45
|
972 dialog.show();
|
|
973 }
|
|
974
|
|
975 private:
|
|
976
|
65
|
977 HistoryManager m_history;
|
|
978 HistoryMenuModel m_historyMenuModel;
|
74
|
979 QAction[] m_initialActions;
|
45
|
980 }
|
|
981
|
66
|
982
|
45
|
983 // proxy model for the history model that
|
|
984 // exposes each url http://www.foo.com and it url starting at the host www.foo.com
|
|
985 class HistoryCompletionModel : public QAbstractProxyModel
|
|
986 {
|
73
|
987 public:
|
45
|
988
|
65
|
989 this(QObject parent = null)
|
45
|
990 {
|
|
991 super(parent);
|
|
992 }
|
|
993
|
65
|
994 QVariant data(QModelIndex index, int role)
|
45
|
995 {
|
73
|
996 if (sourceModel() && (role == Qt.EditRole || role == Qt.DisplayRole) && index.isValid()) {
|
45
|
997 QModelIndex idx = mapToSource(index);
|
|
998 idx = idx.sibling(idx.row(), 1);
|
77
|
999 string urlString = idx.data(HistoryModel.UrlStringRole).toString();
|
45
|
1000 if (index.row() % 2) {
|
|
1001 QUrl url = urlString;
|
77
|
1002 string s = url.toString(QUrl.RemoveScheme | QUrl.RemoveUserInfo | QUrl.StripTrailingSlash);
|
45
|
1003 return s.mid(2); // strip // from the front
|
|
1004 }
|
|
1005 return urlString;
|
|
1006 }
|
65
|
1007 return QAbstractProxyModel.data(index, role);
|
45
|
1008 }
|
|
1009
|
65
|
1010 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
1011 {
|
|
1012 return (parent.isValid() || !sourceModel()) ? 0 : sourceModel().rowCount(parent) * 2;
|
|
1013 }
|
|
1014
|
65
|
1015 int columnCount(QModelIndex parent = QModelIndex())
|
45
|
1016 {
|
|
1017 return (parent.isValid()) ? 0 : 1;
|
|
1018 }
|
|
1019
|
65
|
1020 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
45
|
1021 {
|
|
1022 int row = sourceIndex.row() * 2;
|
|
1023 return index(row, sourceIndex.column());
|
|
1024 }
|
|
1025
|
65
|
1026 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
1027 {
|
|
1028 if (!sourceModel())
|
|
1029 return QModelIndex();
|
|
1030 int row = proxyIndex.row() / 2;
|
|
1031 return sourceModel().index(row, proxyIndex.column());
|
|
1032 }
|
|
1033
|
65
|
1034 QModelIndex index(int, int, QModelIndex = QModelIndex())
|
45
|
1035 {
|
65
|
1036 if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
|
45
|
1037 return QModelIndex();
|
|
1038 return createIndex(row, column, 0);
|
|
1039 }
|
|
1040
|
85
|
1041 QModelIndex parent(QModelIndex index = QModelIndex())
|
45
|
1042 {
|
|
1043 return QModelIndex();
|
|
1044 }
|
|
1045
|
74
|
1046 void setSourceModel(QAbstractItemModel sourceModel)
|
45
|
1047 {
|
|
1048 if (sourceModel()) {
|
65
|
1049 sourceModel.modelReset.disconnect(&this.sourceReset);
|
73
|
1050 sourceModel.rowsInserted.disconnect(&this.sourceReset);
|
|
1051 sourceModel.rowsRemoved.disconnect(&this.sourceReset);
|
45
|
1052 }
|
|
1053
|
65
|
1054 QAbstractProxyModel.setSourceModel(newSourceModel);
|
45
|
1055
|
|
1056 if (newSourceModel) {
|
65
|
1057 newSourceModel.modelReset.connect(&this.sourceReset);
|
73
|
1058 sourceModel.rowsInserted.connect(&this.sourceReset);
|
|
1059 sourceModel.rowsRemoved.connect(&this.sourceReset);
|
45
|
1060 }
|
|
1061
|
|
1062 reset();
|
|
1063 }
|
|
1064
|
73
|
1065 private:
|
45
|
1066
|
|
1067 void sourceReset()
|
|
1068 {
|
|
1069 reset();
|
|
1070 }
|
|
1071 }
|
|
1072
|
73
|
1073
|
45
|
1074 // proxy model for the history model that converts the list
|
|
1075 // into a tree, one top level node per day.
|
|
1076 // Used in the HistoryDialog.
|
|
1077 class HistoryTreeModel : public QAbstractProxyModel
|
|
1078 {
|
|
1079 public:
|
|
1080
|
65
|
1081 this(QAbstractItemModel sourceModel, QObject parent = null)
|
45
|
1082 {
|
|
1083 super(parent);
|
|
1084 setSourceModel(sourceModel);
|
|
1085 }
|
|
1086
|
74
|
1087 QVariant data(QModelIndex index, int role = Qt.DisplayRole)
|
45
|
1088 {
|
|
1089 if ((role == Qt.EditRole || role == Qt.DisplayRole)) {
|
|
1090 int start = index.internalId();
|
|
1091 if (start == 0) {
|
|
1092 int offset = sourceDateRow(index.row());
|
|
1093 if (index.column() == 0) {
|
|
1094 QModelIndex idx = sourceModel().index(offset, 0);
|
|
1095 QDate date = idx.data(HistoryModel.DateRole).toDate();
|
65
|
1096 if (date == QDate.currentDate())
|
45
|
1097 return tr("Earlier Today");
|
80
|
1098 return date.toString("dddd, MMMM d, yyyy");
|
45
|
1099 }
|
|
1100 if (index.column() == 1) {
|
77
|
1101 return Format(tr("{} items"), rowCount(index.sibling(index.row(), 0)));
|
45
|
1102 }
|
|
1103 }
|
|
1104 }
|
|
1105 if (role == Qt.DecorationRole && index.column() == 0 && !index.parent().isValid())
|
80
|
1106 return new QIcon(":history.png");
|
45
|
1107 if (role == HistoryModel.DateRole && index.column() == 0 && index.internalId() == 0) {
|
|
1108 int offset = sourceDateRow(index.row());
|
|
1109 QModelIndex idx = sourceModel().index(offset, 0);
|
|
1110 return idx.data(HistoryModel.DateRole);
|
|
1111 }
|
|
1112
|
65
|
1113 return QAbstractProxyModel.data(index, role);
|
45
|
1114 }
|
|
1115
|
74
|
1116 int columnCount(QModelIndex parent)
|
45
|
1117 {
|
|
1118 return sourceModel().columnCount(mapToSource(parent));
|
|
1119 }
|
|
1120
|
65
|
1121 int rowCount(QModelIndex parent = QModelIndex())
|
|
1122 {
|
|
1123 if ( parent.internalId() != 0 || parent.column() > 0 || !sourceModel())
|
|
1124 return 0;
|
45
|
1125
|
65
|
1126 // row count OF dates
|
|
1127 if (!parent.isValid()) {
|
77
|
1128 if (m_sourceRowCache.length)
|
|
1129 return m_sourceRowCache.length;
|
65
|
1130 QDate currentDate;
|
|
1131 int rows = 0;
|
|
1132 int totalRows = sourceModel().rowCount();
|
45
|
1133
|
65
|
1134 for (int i = 0; i < totalRows; ++i) {
|
|
1135 QDate rowDate = sourceModel().index(i, 0).data(HistoryModel.DateRole).toDate();
|
|
1136 if (rowDate != currentDate) {
|
77
|
1137 m_sourceRowCache ~= i;
|
65
|
1138 currentDate = rowDate;
|
|
1139 ++rows;
|
|
1140 }
|
|
1141 }
|
77
|
1142 assert(m_sourceRowCache.length == rows);
|
65
|
1143 return rows;
|
|
1144 }
|
45
|
1145
|
65
|
1146 // row count FOR a date
|
|
1147 int start = sourceDateRow(parent.row());
|
|
1148 int end = sourceDateRow(parent.row() + 1);
|
|
1149 return (end - start);
|
|
1150 }
|
45
|
1151
|
74
|
1152 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
45
|
1153 {
|
|
1154 if (!sourceIndex.isValid())
|
|
1155 return QModelIndex();
|
|
1156
|
77
|
1157 if (m_sourceRowCache.length == 0)
|
66
|
1158 rowCount(QModelIndex());
|
45
|
1159
|
85
|
1160 //int[].iterator it;
|
|
1161 //it = qLowerBound(m_sourceRowCache.begin(), m_sourceRowCache.end(), sourceIndex.row());
|
|
1162 int it = 0;
|
|
1163 it = qLowerBound(m_sourceRowCache, sourceIndex.row());
|
|
1164 if (it != sourceIndex.row())
|
66
|
1165 --it;
|
85
|
1166 int dateRow = qMax(0, it);
|
77
|
1167 int row = sourceIndex.row() - m_sourceRowCache[dateRow];
|
45
|
1168 return createIndex(row, sourceIndex.column(), dateRow + 1);
|
|
1169 }
|
|
1170
|
65
|
1171 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
1172 {
|
|
1173 int offset = proxyIndex.internalId();
|
|
1174 if (offset == 0)
|
|
1175 return QModelIndex();
|
|
1176 int startDateRow = sourceDateRow(offset - 1);
|
|
1177 return sourceModel().index(startDateRow + proxyIndex.row(), proxyIndex.column());
|
|
1178 }
|
|
1179
|
65
|
1180 QModelIndex index(int row, int column, QModelIndex parent = QModelIndex())
|
45
|
1181 {
|
65
|
1182 if (row < 0 || column < 0 || column >= columnCount(parent) || parent.column() > 0)
|
|
1183 return QModelIndex();
|
45
|
1184
|
65
|
1185 if (!parent.isValid())
|
|
1186 return createIndex(row, column, 0);
|
|
1187 return createIndex(row, column, parent.row() + 1);
|
45
|
1188 }
|
|
1189
|
65
|
1190 QModelIndex parent(QModelIndex index = QModelIndex())
|
45
|
1191 {
|
|
1192 int offset = index.internalId();
|
|
1193 if (offset == 0 || !index.isValid())
|
|
1194 return QModelIndex();
|
|
1195 return createIndex(offset - 1, 0, 0);
|
|
1196 }
|
|
1197
|
65
|
1198 bool hasChildren(QModelIndex parent = QModelIndex())
|
45
|
1199 {
|
|
1200 QModelIndex grandparent = parent.parent();
|
|
1201 if (!grandparent.isValid())
|
|
1202 return true;
|
|
1203 return false;
|
|
1204 }
|
|
1205
|
65
|
1206 Qt.ItemFlags flags(QModelIndex index)
|
45
|
1207 {
|
|
1208 if (!index.isValid())
|
|
1209 return Qt.NoItemFlags;
|
|
1210 return Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled;
|
|
1211 }
|
|
1212
|
74
|
1213 bool removeRows(int row, int count, QModelIndex parent = QModelIndex())
|
65
|
1214 {
|
|
1215 if (row < 0 || count <= 0 || row + count > rowCount(parent))
|
|
1216 return false;
|
45
|
1217
|
65
|
1218 if (parent.isValid()) {
|
|
1219 // removing pages
|
|
1220 int offset = sourceDateRow(parent.row());
|
|
1221 return sourceModel().removeRows(offset + row, count);
|
|
1222 } else {
|
|
1223 // removing whole dates
|
|
1224 for (int i = row + count - 1; i >= row; --i) {
|
|
1225 QModelIndex dateParent = index(i, 0);
|
|
1226 int offset = sourceDateRow(dateParent.row());
|
|
1227 if (!sourceModel().removeRows(offset, rowCount(dateParent)))
|
|
1228 return false;
|
|
1229 }
|
|
1230 }
|
|
1231 return true;
|
|
1232 }
|
45
|
1233
|
65
|
1234 QVariant headerData(int section, Qt.Orientation orientation, int role = Qt.DisplayRole)
|
|
1235 {
|
|
1236 return sourceModel().headerData(section, orientation, role);
|
|
1237 }
|
45
|
1238
|
65
|
1239 void setSourceModel(QAbstractItemModel newSourceModel)
|
|
1240 {
|
|
1241 if (sourceModel()) {
|
|
1242 sourceModel.modelReset.disconnect(&this.sourceReset);
|
|
1243 sourceModel.layoutChanged.disconnect(&this.sourceReset);
|
|
1244 sourceModel.rowsInserted.disconnect(&this.sourceRowsInserted);
|
|
1245 sourceModel.rowsRemoved.disconnect(&this.sourceRowsRemoved);
|
|
1246 }
|
45
|
1247
|
65
|
1248 QAbstractProxyModel.setSourceModel(newSourceModel);
|
45
|
1249
|
65
|
1250 if (newSourceModel) {
|
|
1251 sourceModel.modelReset.connect(&this.sourceReset);
|
|
1252 sourceModel.layoutChanged.connect(&this.sourceReset);
|
|
1253 sourceModel.rowsInserted.connect(&this.sourceRowsInserted);
|
|
1254 sourceModel.rowsRemoved.connect(&this.sourceRowsRemoved);
|
|
1255 }
|
45
|
1256
|
65
|
1257 reset();
|
|
1258 }
|
45
|
1259
|
73
|
1260 private:
|
45
|
1261
|
|
1262 void sourceReset()
|
|
1263 {
|
77
|
1264 m_sourceRowCache.length = 0;
|
45
|
1265 reset();
|
|
1266 }
|
|
1267
|
74
|
1268 void sourceRowsInserted(QModelIndex parent, int start, int end)
|
45
|
1269 {
|
65
|
1270 assert(!parent.isValid());
|
45
|
1271 if (start != 0 || start != end) {
|
77
|
1272 m_sourceRowCache.length = 0;
|
45
|
1273 reset();
|
|
1274 return;
|
|
1275 }
|
|
1276
|
77
|
1277 m_sourceRowCache.length = 0;
|
45
|
1278 QModelIndex treeIndex = mapFromSource(sourceModel().index(start, 0));
|
|
1279 QModelIndex treeParent = treeIndex.parent();
|
|
1280 if (rowCount(treeParent) == 1) {
|
|
1281 beginInsertRows(QModelIndex(), 0, 0);
|
|
1282 endInsertRows();
|
|
1283 } else {
|
|
1284 beginInsertRows(treeParent, treeIndex.row(), treeIndex.row());
|
|
1285 endInsertRows();
|
|
1286 }
|
|
1287 }
|
|
1288
|
77
|
1289 void sourceRowsRemoved(QModelIndex parent, int start, int end)
|
65
|
1290 {
|
|
1291 assert(!parent.isValid());
|
77
|
1292 if (m_sourceRowCache.length == 0)
|
65
|
1293 return;
|
|
1294 for (int i = end; i >= start;) {
|
85
|
1295 //int[]::iterator it;
|
|
1296 //it = qLowerBound(m_sourceRowCache.begin(), m_sourceRowCache.end(), i);
|
|
1297 int it;
|
|
1298 it = qLowerBound(m_sourceRowCache, i);
|
65
|
1299 // playing it safe
|
85
|
1300 if (it == m_sourceRowCache.length) {
|
|
1301 m_sourceRowCache.length = 0;
|
65
|
1302 reset();
|
|
1303 return;
|
|
1304 }
|
45
|
1305
|
85
|
1306 if (it != i)
|
65
|
1307 --it;
|
85
|
1308 int row = qMax(0, it);
|
65
|
1309 int offset = m_sourceRowCache[row];
|
|
1310 QModelIndex dateParent = index(row, 0);
|
|
1311 // If we can remove all the rows in the date do that and skip over them
|
|
1312 int rc = rowCount(dateParent);
|
|
1313 if (i - rc + 1 == offset && start <= i - rc + 1) {
|
|
1314 beginRemoveRows(QModelIndex(), row, row);
|
|
1315 m_sourceRowCache.removeAt(row);
|
|
1316 i -= rc + 1;
|
|
1317 } else {
|
|
1318 beginRemoveRows(dateParent, i - offset, i - offset);
|
|
1319 ++row;
|
|
1320 --i;
|
|
1321 }
|
77
|
1322 for (int j = row; j < m_sourceRowCache.length; ++j)
|
65
|
1323 --m_sourceRowCache[j];
|
|
1324 endRemoveRows();
|
|
1325 }
|
|
1326 }
|
45
|
1327
|
|
1328 private:
|
|
1329
|
|
1330 // Translate the top level date row into the offset where that date starts
|
|
1331 int sourceDateRow(int row)
|
|
1332 {
|
|
1333 if (row <= 0)
|
|
1334 return 0;
|
|
1335
|
77
|
1336 if (m_sourceRowCache.length == 0)
|
45
|
1337 rowCount(QModelIndex());
|
|
1338
|
77
|
1339 if (row >= m_sourceRowCache.length) {
|
45
|
1340 if (!sourceModel())
|
|
1341 return 0;
|
|
1342 return sourceModel().rowCount();
|
|
1343 }
|
77
|
1344 return m_sourceRowCache[row];
|
45
|
1345 }
|
|
1346
|
74
|
1347 int[] m_sourceRowCache;
|
45
|
1348 }
|
|
1349
|
73
|
1350
|
45
|
1351 // A modified QSortFilterProxyModel that always accepts the root nodes in the tree
|
|
1352 // so filtering is only done on the children.
|
|
1353 // Used in the HistoryDialog
|
|
1354 class TreeProxyModel : public QSortFilterProxyModel
|
|
1355 {
|
65
|
1356 public:
|
45
|
1357
|
65
|
1358 this(QObject parent = null)
|
45
|
1359 {
|
|
1360 super(parent);
|
|
1361 setSortRole(HistoryModel.DateTimeRole);
|
|
1362 setFilterCaseSensitivity(Qt.CaseInsensitive);
|
|
1363 }
|
65
|
1364
|
77
|
1365 protected:
|
|
1366
|
65
|
1367 bool filterAcceptsRow(int source_row, QModelIndex source_parent)
|
45
|
1368 {
|
|
1369 if (!source_parent.isValid())
|
|
1370 return true;
|
65
|
1371 return QSortFilterProxyModel.filterAcceptsRow(source_row, source_parent);
|
45
|
1372 }
|
|
1373 }
|
|
1374
|
73
|
1375
|
65
|
1376 import ui_history;
|
45
|
1377
|
|
1378 class HistoryDialog : public QDialog, public Ui_HistoryDialog
|
|
1379 {
|
74
|
1380 mixin Signal!("openUrl", QUrl /*url*/);
|
45
|
1381
|
|
1382 public:
|
|
1383
|
74
|
1384 this(QWidget parent = null, HistoryManager history = null)
|
|
1385 //: QDialog(parent)
|
45
|
1386 {
|
65
|
1387 HistoryManager history = setHistory;
|
45
|
1388 if (!history)
|
65
|
1389 history = BrowserApplication.historyManager();
|
45
|
1390 setupUi(this);
|
|
1391 tree.setUniformRowHeights(true);
|
65
|
1392 tree.setSelectionBehavior(QAbstractItemView.SelectRows);
|
45
|
1393 tree.setTextElideMode(Qt.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);
|
45
|
1408 tree.setContextMenuPolicy(Qt.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 }
|