1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
|
/* ============================================================
*
* This file is a part of the rekonq project
*
* Copyright (C) 2009-2011 by Andrea Diamantini <adjam7 at gmail dot com>
* Copyright (C) 2010 by Matthieu Gicquel <matgic78 at gmail dot com>
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ============================================================ */
// Self Includes
#include "newtabpage.h"
#include "newtabpage.moc"
// Auto Includes
#include "rekonq.h"
// Local Includes
#include "application.h"
#include "bookmarkmanager.h"
#include "downloadmanager.h"
#include "iconmanager.h"
#include "historymodels.h"
#include "mainview.h"
#include "mainwindow.h"
#include "previewselectorbar.h"
#include "websnap.h"
#include "webpage.h"
#include "webtab.h"
// KDE Includes
#include <KBookmarkManager>
#include <KIconLoader>
#include <KLocale>
#include <KMimeType>
#include <KStandardDirs>
// Qt Includes
#include <QFile>
#include <QAction>
#include <QWebFrame>
NewTabPage::NewTabPage(QWebFrame *frame)
: QObject(frame)
, m_root(frame->documentElement())
{
QString htmlFilePath = KStandardDirs::locate("data", "rekonq/htmls/home.html");
QString imagesPath = QL1S("file://") + KGlobal::dirs()->findResourceDir("data", "rekonq/pics/bg.png") + QL1S("rekonq/pics");
QFile file(htmlFilePath);
bool isOpened = file.open(QIODevice::ReadOnly);
if (!isOpened)
{
kDebug() << "Couldn't open the home.html file";
}
else
{
m_html = file.readAll();
m_html.replace(QL1S("%2"), imagesPath);
}
}
void NewTabPage::generate(const KUrl &url)
{
if (KUrl("about:preview").isParentOf(url))
{
if (url.fileName() == QL1S("add"))
{
QStringList names = ReKonfig::previewNames();
QStringList urls = ReKonfig::previewUrls();
names.append("");
urls.append("");
ReKonfig::setPreviewNames(names);
ReKonfig::setPreviewUrls(urls);
// Why doesn't it work well ?
// m_root.appendInside(emptyPreview(names.length() - 1));
// Replacing with this :
generate(KUrl("about:favorites"));
return;
}
if (url.directory() == QL1S("preview/remove"))
{
removePreview(url.fileName().toInt());
return;
}
if (url.directory() == QL1S("preview/modify"))
{
int index = url.fileName().toInt();
rApp->mainWindow()->currentTab()->createPreviewSelectorBar(index);
return;
}
}
if (KUrl("about:tabs").isParentOf(url))
{
if (url.fileName() == QL1S("show"))
{
const int winIndex = url.queryItem(QL1S("win")).toInt();
const int tabIndex = url.queryItem(QL1S("tab")).toInt();
kDebug() << "win = " << winIndex;
kDebug() << "tab = " << tabIndex;
MainWindow *w = rApp->mainWindowList().at(winIndex).data();
// close about:tabs tab
rApp->mainWindow()->mainView()->closeTab(rApp->mainWindow()->mainView()->currentIndex());
// show requested tab
w->mainView()->setCurrentIndex(tabIndex);
if (w != rApp->mainWindow())
w->raise();
return;
}
if (url.fileName() == QL1S("remove"))
{
const int winIndex = url.queryItem(QL1S("win")).toInt();
const int tabIndex = url.queryItem(QL1S("tab")).toInt();
kDebug() << "win = " << winIndex;
kDebug() << "tab = " << tabIndex;
MainWindow *w = rApp->mainWindowList().at(winIndex).data();
w->mainView()->closeTab(tabIndex);
generate(KUrl("about:tabs"));
return;
}
}
if (url == KUrl("about:downloads/clear"))
{
rApp->downloadManager()->clearDownloadsHistory();
generate(KUrl("about:downloads"));
return;
}
if (url == KUrl("about:history/clear"))
{
rApp->historyManager()->clear();
generate(KUrl("about:history"));
return;
}
if (url == KUrl("about:bookmarks/edit"))
{
rApp->bookmarkManager()->slotEditBookmarks();
return;
}
// webFrame can be null. See bug:282092
if (!m_root.webFrame())
return;
WebPage *page = qobject_cast <WebPage *>(m_root.webFrame()->page());
page->mainFrame()->setHtml(m_html);
page->setIsOnRekonqPage(true);
m_root = page->mainFrame()->documentElement().findFirst(QL1S("#content"));
browsingMenu(url);
QString title;
QByteArray encodedUrl = url.toEncoded();
if (encodedUrl == QByteArray("about:favorites"))
{
favoritesPage();
updateWindowIcon();
title = i18n("Favorites");
}
else if (encodedUrl == QByteArray("about:closedTabs"))
{
closedTabsPage();
updateWindowIcon();
title = i18n("Closed Tabs");
}
else if (encodedUrl == QByteArray("about:history"))
{
historyPage();
updateWindowIcon();
title = i18n("History");
}
else if (encodedUrl == QByteArray("about:bookmarks"))
{
bookmarksPage();
updateWindowIcon();
title = i18n("Bookmarks");
}
else if (encodedUrl == QByteArray("about:downloads"))
{
downloadsPage();
updateWindowIcon();
title = i18n("Downloads");
}
else if (encodedUrl == QByteArray("about:tabs"))
{
tabsPage();
updateWindowIcon();
title = i18n("Tabs");
}
m_root.document().findFirst(QL1S("title")).setPlainText(title);
}
// ----------------------------------------------------------------------------
// HIGH-LEVEL FUNCTIONS
void NewTabPage::browsingMenu(const KUrl ¤tUrl)
{
QList<QWebElement> navItems;
// Favorites
navItems.append(createLinkItem(i18n("Favorites"),
QL1S("about:favorites"),
QL1S("emblem-favorite"),
KIconLoader::Toolbar));
// Closed Tabs
navItems.append(createLinkItem(i18n("Closed Tabs"),
QL1S("about:closedTabs"),
QL1S("tab-close"),
KIconLoader::Toolbar));
// Bookmarks
navItems.append(createLinkItem(i18n("Bookmarks"),
QL1S("about:bookmarks"),
QL1S("bookmarks"),
KIconLoader::Toolbar));
// History
navItems.append(createLinkItem(i18n("History"),
QL1S("about:history"),
QL1S("view-history"),
KIconLoader::Toolbar));
// Downloads
navItems.append(createLinkItem(i18n("Downloads"),
QL1S("about:downloads"),
QL1S("download"),
KIconLoader::Toolbar));
// Tabs
navItems.append(createLinkItem(i18n("Tabs"),
QL1S("about:tabs"),
QL1S("tab-duplicate"),
KIconLoader::Toolbar));
Q_FOREACH(QWebElement it, navItems)
{
const QString aTagString(QL1C('a'));
const QString hrefAttributeString(QL1S("href"));
if (it.findFirst(aTagString).attribute(hrefAttributeString) == currentUrl.toMimeDataString())
it.addClass(QL1S("current"));
else if (currentUrl == QL1S("about:home") && it.findFirst(aTagString).attribute(hrefAttributeString) == QL1S("about:favorites"))
it.addClass(QL1S("current"));
m_root.document().findFirst(QL1S("#navigation")).appendInside(it);
}
}
void NewTabPage::favoritesPage()
{
m_root.addClass(QL1S("favorites"));
const QWebElement add = createLinkItem(i18n("Add Favorite"),
QL1S("about:preview/add"),
QL1S("list-add"),
KIconLoader::Toolbar);
m_root.document().findFirst("#actions").appendInside(add);
QStringList names = ReKonfig::previewNames();
QStringList urls = ReKonfig::previewUrls();
if (urls.isEmpty())
{
m_root.addClass(QL1S("empty"));
m_root.setPlainText(i18n("You can add a favorite by clicking the \"Add Favorite\" button in the top-right corner of this page"));
return;
}
for (int i = 0; i < urls.count() ; ++i)
{
KUrl url = KUrl(urls.at(i));
QWebElement prev;
if (url.isEmpty())
prev = emptyPreview(i);
else if (!WebSnap::existsImage(url))
prev = loadingPreview(i, url);
else
prev = validPreview(i, url, QString::number(i + 1) + " - " + names.at(i));
m_root.appendInside(prev);
}
}
void NewTabPage::historyPage()
{
m_root.addClass(QL1S("history"));
const QWebElement clearHistory = createLinkItem(i18n("Clear History"),
QL1S("about:history/clear"),
QL1S("edit-clear"),
KIconLoader::Toolbar);
m_root.document().findFirst(QL1S("#actions")).appendInside(clearHistory);
HistoryTreeModel *model = rApp->historyManager()->historyTreeModel();
if (model->rowCount() == 0)
{
m_root.addClass(QL1S("empty"));
m_root.setPlainText(i18n("Your browsing history is empty"));
return;
}
int i = 0;
const int maxTextSize = 103;
const int truncateSize = 100;
do
{
QModelIndex index = model->index(i, 0, QModelIndex());
if (model->hasChildren(index))
{
m_root.appendInside(markup(QL1S("h3")));
m_root.lastChild().setPlainText(index.data().toString());
m_root.appendInside(markup(QL1S(".folder")));
QWebElement little = m_root.lastChild();
for (int j = 0; j < model->rowCount(index); ++j)
{
QModelIndex son = model->index(j, 0, index);
KUrl u = son.data(HistoryModel::UrlStringRole).toUrl();
little.appendInside(son.data(HistoryModel::DateTimeRole).toDateTime().toString("hh:mm"));
little.appendInside(QL1S(" "));
little.appendInside(markup(QL1S("img")));
little.lastChild().setAttribute(QL1S("src"), rApp->iconManager()->iconPathForUrl(u));
little.lastChild().setAttribute(QL1S("width"), QL1S("16"));
little.lastChild().setAttribute(QL1S("height"), QL1S("16"));
little.appendInside(QL1S(" "));
little.appendInside(markup(QL1S("a")));
little.lastChild().setAttribute(QL1S("href") , u.url());
QString shownUrl = son.data().toString();
if (shownUrl.length() > maxTextSize)
{
shownUrl.truncate(truncateSize);
shownUrl += QL1S("...");
}
little.lastChild().appendInside(shownUrl);
little.appendInside(QL1S("<br />"));
}
}
i++;
}
while (model->hasIndex(i , 0 , QModelIndex()));
}
void NewTabPage::bookmarksPage()
{
m_root.addClass(QL1S("bookmarks"));
const QWebElement editBookmarks = createLinkItem(i18n("Edit Bookmarks"),
QL1S("about:bookmarks/edit"),
QL1S("bookmarks-organize"),
KIconLoader::Toolbar);
m_root.document().findFirst(QL1S("#actions")).appendInside(editBookmarks);
KBookmarkGroup bookGroup = rApp->bookmarkManager()->rootGroup();
if (bookGroup.isNull())
{
m_root.addClass(QL1S("empty"));
m_root.setPlainText(i18n("You have no bookmarks"));
return;
}
KBookmark bookmark = bookGroup.first();
while (!bookmark.isNull())
{
createBookItem(bookmark, m_root);
bookmark = bookGroup.next(bookmark);
}
}
void NewTabPage::closedTabsPage()
{
m_root.addClass(QL1S("closedTabs"));
QList<TabHistory> links = rApp->mainWindow()->mainView()->recentlyClosedTabs();
if (links.isEmpty())
{
m_root.addClass(QL1S("empty"));
m_root.setPlainText(i18n("There are no recently closed tabs"));
return;
}
for (int i = 0; i < links.count(); ++i)
{
TabHistory item = links.at(i);
QWebElement prev;
if (item.url.isEmpty())
continue;
prev = WebSnap::existsImage(KUrl(item.url))
? validPreview(i, item.url, item.title)
: loadingPreview(i, item.url);
prev.setAttribute(QL1S("id"), QL1S("preview") + QVariant(i).toString());
hideControls(prev);
m_root.appendInside(prev);
}
}
void NewTabPage::downloadsPage()
{
m_root.addClass(QL1S("downloads"));
const QWebElement clearDownloads = createLinkItem(i18n("Clear Downloads"),
QL1S("about:downloads/clear"),
QL1S("edit-clear"),
KIconLoader::Toolbar);
m_root.document().findFirst(QL1S("#actions")).appendInside(clearDownloads);
DownloadList list = rApp->downloadManager()->downloads();
if (list.isEmpty())
{
m_root.addClass(QL1S("empty"));
m_root.setPlainText(i18n("There are no recently downloaded files to show"));
return;
}
Q_FOREACH(DownloadItem * item, list)
{
m_root.prependInside(markup(QL1S("div")));
QWebElement div = m_root.firstChild();
div.addClass(QL1S("download"));
KUrl u = KUrl(item->destinationUrl());
QString fName = u.fileName();
QString dir = QL1S("file://") + u.directory();
QString file = dir + fName;
KIconLoader *loader = KIconLoader::global();
QString iconPath = QL1S("file://") + loader->iconPath(KMimeType::iconNameForUrl(u), KIconLoader::Desktop);
div.appendInside(markup(QL1S("img")));
div.lastChild().setAttribute(QL1S("src"), iconPath);
div.appendInside(QL1S("<strong>") + fName + QL1S("</strong>"));
div.appendInside(QL1S(" - "));
QString date = KGlobal::locale()->formatDateTime(item->dateTime(), KLocale::FancyLongDate);
div.appendInside(QL1S("<em>") + date + QL1S("</em>"));
div.appendInside(QL1S("<br />"));
div.appendInside(QL1S("<a href=") + item->originUrl() + QL1C('>') + item->originUrl() + QL1S("</a>"));
div.appendInside(QL1S("<br />"));
if (QFile::exists(file))
{
div.appendInside(markup(QL1S("a")));
div.lastChild().setAttribute(QL1S("href"), dir);
div.lastChild().setPlainText(i18n("Open directory"));
div.appendInside(QL1S(" - "));
div.appendInside(markup(QL1S("a")));
div.lastChild().setAttribute(QL1S("href"), file);
div.lastChild().setPlainText(i18n("Open file"));
}
else
{
div.appendInside(QL1S("<em>") + QL1S("Removed") + QL1S("</em>"));
}
}
}
void NewTabPage::tabsPage()
{
m_root.addClass(QL1S("tabs"));
int wins = 0;
Q_FOREACH(const QWeakPointer<MainWindow> &wPointer, rApp->mainWindowList())
{
m_root.appendInside(markup(QL1S("h3")));
m_root.lastChild().setPlainText("Window");
MainWindow *w = wPointer.data();
const int tabCount = w->mainView()->count();
for (int i = 0; i < tabCount; ++i)
{
KUrl url = w->mainView()->webTab(i)->url();
if (url.protocol() == QL1S("about"))
continue;
QString name = w->mainView()->webTab(i)->view()->title();
QWebElement prev;
prev = tabPreview(wins, i, url, name);
m_root.appendInside(prev);
}
wins++;
}
}
// ----------------------------------------------------------------------------
// LOW-LEVEL FUNCTIONS
QWebElement NewTabPage::emptyPreview(int index)
{
QWebElement prev = markup(QL1S(".thumbnail"));
prev.findFirst(QL1S(".preview img")).setAttribute(QL1S("src") ,
QL1S("file:///") + KIconLoader::global()->iconPath("insert-image", KIconLoader::Desktop));
prev.findFirst(QL1S("span a")).setPlainText(i18n("Set a Preview..."));
prev.findFirst(QL1S("a")).setAttribute(QL1S("href"),
QL1S("about:preview/modify/") + QVariant(index).toString());
setupPreview(prev, index);
//hideControls(prev);
return prev;
}
QWebElement NewTabPage::loadingPreview(int index, const KUrl &url)
{
QWebElement prev = markup(QL1S(".thumbnail"));
prev.findFirst(QL1S(".preview img")).setAttribute(QL1S("src"),
QL1S("file:///") + KStandardDirs::locate("appdata", "pics/busywidget.gif"));
prev.findFirst(QL1S("span a")).setPlainText(i18n("Loading Preview..."));
prev.findFirst(QL1S("a")).setAttribute(QL1S("href"), url.toMimeDataString());
setupPreview(prev, index);
showControls(prev);
// NOTE: we need the page frame for two reasons
// 1) to link to the WebPage calling the snapFinished slot
// 2) to "auto-destroy" snaps on tab closing :)
QWebFrame *frame = qobject_cast<QWebFrame *>(parent());
WebSnap *snap = new WebSnap(url, frame);
connect(snap, SIGNAL(snapDone(bool)), frame->page(), SLOT(updateImage(bool)), Qt::UniqueConnection);
return prev;
}
QWebElement NewTabPage::validPreview(int index, const KUrl &url, const QString &title)
{
QWebElement prev = markup(QL1S(".thumbnail"));
QString previewPath = QL1S("file://") + WebSnap::imagePathFromUrl(url);
prev.findFirst(QL1S(".preview img")).setAttribute(QL1S("src") , previewPath);
prev.findFirst(QL1S("a")).setAttribute(QL1S("href"), url.toMimeDataString());
prev.findFirst(QL1S("span a")).setAttribute(QL1S("href"), url.toMimeDataString());
prev.findFirst(QL1S("span a")).setPlainText(checkTitle(title));
setupPreview(prev, index);
showControls(prev);
return prev;
}
QWebElement NewTabPage::tabPreview(int winIndex, int tabIndex, const KUrl &url, const QString &title)
{
QWebElement prev = markup(QL1S(".thumbnail"));
QString previewPath = QL1S("file://") + WebSnap::imagePathFromUrl(url);
QString href = QL1S("about:tabs/show?win=") + QString::number(winIndex) + QL1S("&tab=") + QString::number(tabIndex);
prev.findFirst(QL1S(".preview img")).setAttribute(QL1S("src") , previewPath);
prev.findFirst(QL1S("a")).setAttribute(QL1S("href"), href);
prev.findFirst(QL1S("span a")).setAttribute(QL1S("href"), href);
prev.findFirst(QL1S("span a")).setPlainText(checkTitle(title));
setupTabPreview(prev, winIndex, tabIndex);
prev.findFirst(QL1S(".remove")).setStyleProperty(QL1S("visibility"), QL1S("visible"));
prev.findFirst(QL1S(".modify")).setStyleProperty(QL1S("visibility"), QL1S("hidden"));
return prev;
}
void NewTabPage::hideControls(QWebElement e)
{
e.findFirst(QL1S(".remove")).setStyleProperty(QL1S("visibility"), QL1S("hidden"));
e.findFirst(QL1S(".modify")).setStyleProperty(QL1S("visibility"), QL1S("hidden"));
}
void NewTabPage::showControls(QWebElement e)
{
e.findFirst(QL1S(".remove")).setStyleProperty(QL1S("visibility"), QL1S("visible"));
e.findFirst(QL1S(".modify")).setStyleProperty(QL1S("visibility"), QL1S("visible"));
}
void NewTabPage::setupPreview(QWebElement e, int index)
{
e.findFirst(QL1S(".remove img")).setAttribute(QL1S("src"), QL1S("file:///") + KIconLoader::global()->iconPath("edit-delete", KIconLoader::DefaultState));
e.findFirst(QL1S(".remove")).setAttribute(QL1S("title"), QL1S("Remove favorite"));
e.findFirst(QL1S(".modify img")).setAttribute(QL1S("src"), QL1S("file:///") + KIconLoader::global()->iconPath("insert-image", KIconLoader::DefaultState));
e.findFirst(QL1S(".modify")).setAttribute(QL1S("title"), QL1S("Set new favorite"));
e.findFirst(QL1S(".modify")).setAttribute(QL1S("href"), QL1S("about:preview/modify/") + QVariant(index).toString());
e.findFirst(QL1S(".remove")).setAttribute(QL1S("href"), QL1S("about:preview/remove/") + QVariant(index).toString());
e.setAttribute(QL1S("id"), QL1S("preview") + QVariant(index).toString());
}
void NewTabPage::setupTabPreview(QWebElement e, int winIndex, int tabIndex)
{
e.findFirst(QL1S(".remove img")).setAttribute(QL1S("src"), QL1S("file:///") + KIconLoader::global()->iconPath("edit-delete", KIconLoader::DefaultState));
e.findFirst(QL1S(".remove")).setAttribute(QL1S("title"), QL1S("Close Tab"));
QString href = QL1S("about:tabs/remove?win=") + QString::number(winIndex) + QL1S("&tab=") + QString::number(tabIndex);
e.findFirst(QL1S(".remove")).setAttribute(QL1S("href"), href);
e.setAttribute(QL1S("id"), QL1S("win") + QString::number(winIndex) + QL1S("tab") + QString::number(tabIndex));
}
void NewTabPage::updateThumbs()
{
// Update page, but only if open
if (m_root.document().findAll(QL1S("#rekonq-newtabpage")).count() == 0)
return;
if (m_root.findAll(QL1S(".favorites")).count() == 0 && m_root.findAll(QL1S(".closedTabs")).count() == 0)
return;
QStringList urls = ReKonfig::previewUrls();
QStringList names = ReKonfig::previewNames();
for (int i = 0; i < urls.count(); i++)
{
KUrl url = KUrl(urls.at(i));
QString title = names.at(i);
if (WebSnap::existsImage(url))
{
QWebElement prev = m_root.findFirst(QL1S("#preview") + QVariant(i).toString());
if (KUrl(prev.findFirst("a").attribute(QL1S("href"))) == url)
{
QWebElement newPrev = validPreview(i, url, title);
if (m_root.findAll(QL1S(".closedTabs")).count() != 0)
hideControls(newPrev);
prev.replace(newPrev);
}
}
}
}
void NewTabPage::removePreview(int index)
{
QStringList names = ReKonfig::previewNames();
QStringList urls = ReKonfig::previewUrls();
urls.removeAt(index);
names.removeAt(index);
ReKonfig::setPreviewNames(names);
ReKonfig::setPreviewUrls(urls);
generate(KUrl("about:favorites"));
ReKonfig::self()->writeConfig();
}
void NewTabPage::createBookItem(const KBookmark &bookmark, QWebElement parent)
{
QString cacheDir = QL1S("file://") + KStandardDirs::locateLocal("cache" , "" , true);
QString icon = QL1S("file://") + KGlobal::dirs()->findResource("icon", "oxygen/16x16/mimetypes/text-html.png");
if (bookmark.isGroup())
{
KBookmarkGroup group = bookmark.toGroup();
KBookmark bm = group.first();
parent.appendInside(markup(QL1S("h3")));
parent.lastChild().setPlainText(group.fullText());
parent.appendInside(markup(QL1S(".folder")));
while (!bm.isNull())
{
createBookItem(bm, parent.lastChild()); // it is .folder
bm = group.next(bm);
}
}
else if (bookmark.isSeparator())
{
parent.appendInside(QL1S("<hr />"));
}
else
{
QString b = bookmark.icon();
if (b.contains(QL1S("favicons")))
icon = cacheDir + bookmark.icon() + QL1S(".png");
parent.appendInside(markup(QL1S("img")));
parent.lastChild().setAttribute(QL1S("src") , icon);
parent.lastChild().setAttribute(QL1S("width") , QL1S("16"));
parent.lastChild().setAttribute(QL1S("height") , QL1S("16"));
parent.appendInside(QL1S(" "));
parent.appendInside(markup(QL1S("a")));
parent.lastChild().setAttribute(QL1S("href") , bookmark.url().prettyUrl());
parent.lastChild().setPlainText(bookmark.fullText());
parent.appendInside(QL1S("<br />"));
}
}
QString NewTabPage::checkTitle(const QString &title)
{
QString t(title);
if (t.length() > 23)
{
t.truncate(20);
t += QL1S("...");
}
return t;
}
QWebElement NewTabPage::createLinkItem(const QString &title, const QString &urlString, const QString &iconPath, int groupOrSize) const
{
const KIconLoader * const loader = KIconLoader::global();
QWebElement nav = markup(QL1S(".link"));
nav.findFirst(QL1S("a")).setAttribute(QL1S("href"), urlString);
nav.findFirst(QL1S("img")).setAttribute(QL1S("src"), QL1S("file://") + loader->iconPath(iconPath, groupOrSize));
nav.findFirst(QL1S("span")).appendInside(title);
return nav;
}
void NewTabPage::updateWindowIcon()
{
int currentIndex = rApp->mainWindow()->mainView()->currentIndex();
rApp->mainWindow()->changeWindowIcon(currentIndex);
}
|