From 66836b1ca6d8d5dfcdf4d48dabc51b7709d6e456 Mon Sep 17 00:00:00 2001 From: matgic78 Date: Wed, 9 Dec 2009 17:12:38 +0100 Subject: Rewrite NewTabPage to use QWebElement for page generation --- src/rekonqpage/newtabpage.cpp | 217 +++++++++++++++++++++--------------------- src/rekonqpage/newtabpage.h | 36 ++++--- 2 files changed, 131 insertions(+), 122 deletions(-) (limited to 'src/rekonqpage') diff --git a/src/rekonqpage/newtabpage.cpp b/src/rekonqpage/newtabpage.cpp index 4a7a72e0..a1b20ac0 100644 --- a/src/rekonqpage/newtabpage.cpp +++ b/src/rekonqpage/newtabpage.cpp @@ -48,9 +48,20 @@ #include -NewTabPage::NewTabPage() +NewTabPage::NewTabPage(WebPage *page) + : m_root(page->mainFrame()->documentElement()) { - m_htmlFilePath = KStandardDirs::locate("data", "rekonq/htmls/home.html"); + QString htmlFilePath = KStandardDirs::locate("data", "rekonq/htmls/home.html"); + + QFile file(htmlFilePath); + bool isOpened = file.open(QIODevice::ReadOnly); + if (!isOpened) + kWarning() << "Couldn't open the home.html file"; + + QString imagesPath = QString("file://") + KGlobal::dirs()->findResourceDir("data", "rekonq/pics/bg.png") + QString("rekonq/pics"); + + m_html = file.readAll(); + m_html.replace(QString("%2"), imagesPath); } @@ -59,76 +70,62 @@ NewTabPage::~NewTabPage() } -QString NewTabPage::newTabPageCode(const KUrl &url) -{ - QFile file(m_htmlFilePath); - bool isOpened = file.open(QIODevice::ReadOnly); - if (!isOpened) - { - kWarning() << "Couldn't open the home.html file"; - return QString(""); - } - QString imagesPath = QString("file://") + KGlobal::dirs()->findResourceDir("data", "rekonq/pics/bg.png") + QString("rekonq/pics"); - QString menu = browsingMenu(url); +void NewTabPage::generate(const KUrl &url) +{ + QWebPage *page = m_root.webFrame()->page(); + page->mainFrame()->setHtml(m_html); + + m_root = page->mainFrame()->documentElement().findFirst("#content"); + + browsingMenu(url); - QString speed; QString title; if(url == KUrl("about:closedTabs")) { - speed = closedTabsPage(); + closedTabsPage(); title = i18n("Closed Tabs"); } if(url == KUrl("about:history")) { - speed = historyPage(); + historyPage(); title = i18n("History"); } if(url == KUrl("about:bookmarks")) { - speed = bookmarksPage(); + bookmarksPage(); title = i18n("Bookmarks"); } if(url == KUrl("about:home") || url == KUrl("about:favorites")) { - speed = favoritesPage(); + favoritesPage(); title = i18n("Favorites"); } - QString html = QString(QLatin1String(file.readAll())) - .arg(title) - .arg(imagesPath) - .arg(menu) - .arg(speed) - ; - - return html; + m_root.document().findFirst("title").setPlainText(title); } -QString NewTabPage::favoritesPage() +void NewTabPage::favoritesPage() { QStringList names = ReKonfig::previewNames(); QStringList urls = ReKonfig::previewUrls(); - QString speed = "
"; + m_root.addClass("favorites"); + for(int i=0; i<8; ++i) { - speed += "
"; - speed += ""; - speed += ""; - speed += ""; - speed += ""; - speed += ""; - speed += "
"; + QWebElement speed = markup(".thumbnail"); + speed.findFirst("object").setAttribute("data" , urls.at(i)); + speed.findFirst("param[name=title]").setAttribute("value", names.at(i)); + speed.findFirst("param[name=index]").setAttribute("value", QString::number(i)); + speed.findFirst("param[name=isFavorite]").setAttribute("value", "true"); + m_root.appendInside(speed); } - - speed += "
"; - return speed; } // FIXME : port to new PreviewImage API to use... -QString NewTabPage::lastVisitedPage() +/*QString NewTabPage::lastVisitedPage() { QString last; QList history = Application::historyManager()->history(); @@ -144,142 +141,140 @@ QString NewTabPage::lastVisitedPage() return last; -} +}*/ -QString NewTabPage::browsingMenu(const KUrl ¤tUrl) +void NewTabPage::browsingMenu(const KUrl ¤tUrl) { - QString menu; + QList navItems; KIconLoader *loader = KIconLoader::global(); - menu += ""; + QWebElement nav = markup(".link"); // Favorites + nav.findFirst("a").setAttribute("href", "about:favorites"); + nav.findFirst("img").setAttribute("src" , QString("file:///" + + loader->iconPath("emblem-favorite", KIconLoader::Desktop ||KIconLoader::SizeSmall))); + nav.findFirst("a").appendInside(i18n("Favorites")); + navItems.append(nav); - menu += ""; + nav = markup(".link"); // Closed Tabs + nav.findFirst("a").setAttribute("href", "about:closedTabs"); + nav.findFirst("img").setAttribute("src" , QString("file:///" + + loader->iconPath("tab-close", KIconLoader::Desktop ||KIconLoader::SizeSmall))); + nav.findFirst("a").appendInside(i18n("Closed Tabs")); + navItems.append(nav); - menu += ""; + nav = markup(".link"); // Bookmarks + nav.findFirst("a").setAttribute("href", "about:bookmarks"); + nav.findFirst("img").setAttribute("src" , QString("file:///" + + loader->iconPath("bookmarks", KIconLoader::Desktop ||KIconLoader::SizeSmall))); + nav.findFirst("a").appendInside(i18n("Bookmarks")); + navItems.append(nav); - menu += ""; + nav = markup(".link"); // History + nav.findFirst("a").setAttribute("href", "about:history"); + nav.findFirst("img").setAttribute("src" , QString("file:///" + + loader->iconPath("view-history", KIconLoader::Desktop ||KIconLoader::SizeSmall))); + nav.findFirst("a").appendInside(i18n("History")); + navItems.append(nav); - return menu; + QWebElement it; + foreach(it, navItems) + { + if(it.findFirst("a").attribute("href") == currentUrl.toMimeDataString()) + it.addClass("current"); + else if(currentUrl == "about:home" && it.findFirst("a").attribute("href") == "about:favorites") + it.addClass("current"); + m_root.document().findFirst("#navigation").appendInside(it); + } } -QString NewTabPage::historyPage() +void NewTabPage::historyPage() { HistoryTreeModel *model = Application::historyManager()->historyTreeModel(); - QString history; int i = 0; do { QModelIndex index = model->index(i, 0, QModelIndex() ); if(model->hasChildren(index)) { - history += "

" + index.data().toString() + "

"; + m_root.appendInside(markup("h3")); + m_root.lastChild().setPlainText(index.data().toString()); + for(int j=0; j< model->rowCount(index); ++j) { QModelIndex son = model->index(j, 0, index ); - history += son.data(HistoryModel::DateTimeRole).toDateTime().toString("hh:mm"); - history += ' '; - history += QString("") + - son.data().toString() + QString(""); - history += "
"; + m_root.appendInside(son.data(HistoryModel::DateTimeRole).toDateTime().toString("hh:mm")); + m_root.appendInside(" "); + m_root.appendInside(markup("a")); + m_root.lastChild().setAttribute("href" , son.data(HistoryModel::UrlStringRole).toString()); + m_root.lastChild().appendInside(son.data().toString()); + m_root.appendInside("
"); } } i++; } while( model->hasIndex( i , 0 , QModelIndex() ) ); - - history += ""; - return history; } -QString NewTabPage::bookmarksPage() +void NewTabPage::bookmarksPage() { KBookmarkGroup bookGroup = Application::bookmarkProvider()->rootGroup(); if (bookGroup.isNull()) { - return QString("Error retrieving bookmarks!"); + return; } - QString str; KBookmark bookmark = bookGroup.first(); while (!bookmark.isNull()) { - str += createBookItem(bookmark); + createBookItem(bookmark, m_root); bookmark = bookGroup.next(bookmark); } - return str; } -QString NewTabPage::createBookItem(const KBookmark &bookmark) +void NewTabPage::createBookItem(const KBookmark &bookmark, QWebElement parent) { if (bookmark.isGroup()) { - QString result; KBookmarkGroup group = bookmark.toGroup(); KBookmark bm = group.first(); - result += "

" + bookmark.text() + "

"; - result += "

"; + parent.appendInside(markup("h3")); + parent.lastChild().setPlainText(group.text()); + parent.appendInside(markup(".bookfolder")); while (!bm.isNull()) { - result += createBookItem(bm); + createBookItem(bm, parent.lastChild()); // it is .bookfolder bm = group.next(bm); } - result += "

"; - return result; } - - if(bookmark.isSeparator()) + else if(bookmark.isSeparator()) { - return QString("
"); + parent.appendInside("
"); + } + else + { + parent.appendInside(markup("a")); + parent.lastChild().setAttribute("href" , bookmark.url().prettyUrl()); + parent.lastChild().setPlainText(bookmark.text()); + parent.appendInside("
"); } - - QString books = "" + bookmark.text() + "
"; - return books; } -QString NewTabPage::closedTabsPage() +void NewTabPage::closedTabsPage() { QList links = Application::instance()->mainWindow()->mainView()->recentlyClosedTabs(); - QString closed; - Q_FOREACH( const HistoryItem &item, links) + foreach(const HistoryItem &item, links) { - closed += "
"; - closed += ""; - closed += ""; - closed += ""; - closed += "
"; - } - - return closed; + QWebElement closed = markup(".thumbnail"); + closed.findFirst("object").setAttribute("data" , item.url); + closed.findFirst("param[name=title]").setAttribute("value", item.title); + m_root.appendInside(closed); + } } diff --git a/src/rekonqpage/newtabpage.h b/src/rekonqpage/newtabpage.h index 2d8d50bd..62baf79e 100644 --- a/src/rekonqpage/newtabpage.h +++ b/src/rekonqpage/newtabpage.h @@ -28,12 +28,16 @@ #define REKONQ_NEW_TAB_PAGE +// rekonq Includes +#include + // KDE Includes #include // Qt Includes #include #include +#include // Forward Includes class KBookmark; @@ -43,7 +47,7 @@ class NewTabPage { public: - NewTabPage(); + NewTabPage(WebPage *page); ~NewTabPage(); /** @@ -51,22 +55,32 @@ public: * about: url and loads the corresponding part of the * new tab page */ - QString newTabPageCode(const KUrl &url = KUrl("about:home")); + void generate(const KUrl &url = KUrl("about:home")); protected: // these are the function to build the new tab page - - QString browsingMenu(const KUrl ¤tUrl); + void browsingMenu(const KUrl ¤tUrl); - QString favoritesPage(); - QString lastVisitedPage(); - QString historyPage(); - QString bookmarksPage(); - QString closedTabsPage(); + void favoritesPage(); + //QString lastVisitedPage(); + void historyPage(); + void bookmarksPage(); + void closedTabsPage(); private: - QString createBookItem(const KBookmark &bookmark); + void createBookItem(const KBookmark &bookmark, QWebElement parent); + + /** This function helps to get faster a new markup of one type,it isn't easy to create one with QWebElement. + It gets it in the #models div of home.html. + It works for all elements defined here. + */ + inline QWebElement markup(QString selector) + { + return m_root.document().findFirst("#models > " + selector).clone(); + } - QString m_htmlFilePath; + QString m_html; + + QWebElement m_root; }; #endif // REKONQ_NEW_TAB_PAGE -- cgit v1.2.1