From a00d4a2847cd8797d3029568611ed5e3fe4cfa51 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Sat, 23 Oct 2010 23:04:26 +0200 Subject: NewTabPage improvements 1) icons in the history & bookmarks pages 2) minor performance improvement, using QL1S macro --- src/newtabpage.cpp | 214 +++++++++++++++++++++++++++++------------------------ 1 file changed, 119 insertions(+), 95 deletions(-) (limited to 'src') diff --git a/src/newtabpage.cpp b/src/newtabpage.cpp index 36462f8c..7dcc9581 100644 --- a/src/newtabpage.cpp +++ b/src/newtabpage.cpp @@ -60,7 +60,7 @@ NewTabPage::NewTabPage(QWebFrame *frame) , m_root(frame->documentElement()) { QString htmlFilePath = KStandardDirs::locate("data", "rekonq/htmls/home.html"); - QString imagesPath = QString("file://") + KGlobal::dirs()->findResourceDir("data", "rekonq/pics/bg.png") + QString("rekonq/pics"); + QString imagesPath = QL1S("file://") + KGlobal::dirs()->findResourceDir("data", "rekonq/pics/bg.png") + QL1S("rekonq/pics"); QFile file(htmlFilePath); bool isOpened = file.open(QIODevice::ReadOnly); @@ -71,7 +71,7 @@ NewTabPage::NewTabPage(QWebFrame *frame) else { m_html = file.readAll(); - m_html.replace(QString("%2"), imagesPath); + m_html.replace(QL1S("%2"), imagesPath); } } @@ -85,7 +85,7 @@ void NewTabPage::generate(const KUrl &url) { if (KUrl("about:preview").isParentOf(url)) { - if (url.fileName() == QString("add")) + if (url.fileName() == QL1S("add")) { QStringList names = ReKonfig::previewNames(); QStringList urls = ReKonfig::previewUrls(); @@ -102,22 +102,22 @@ void NewTabPage::generate(const KUrl &url) generate(KUrl("about:favorites")); return; } - if (url.directory() == QString("preview/remove")) + if (url.directory() == QL1S("preview/remove")) { removePreview(url.fileName().toInt()); return; } - if (url.directory() == QString("preview/modify")) + if (url.directory() == QL1S("preview/modify")) { int index = url.fileName().toInt(); Application::instance()->mainWindow()->currentTab()->createPreviewSelectorBar(index); return; } } - if (url.fileName() == QString("clear")) + if (url.fileName() == QL1S("clear")) { Application::instance()->mainWindow()->actionByName("clear_private_data")->trigger(); - generate(QString("about:" + url.directory())); + generate( QL1S("about:") + url.directory() ); return; } if (url == KUrl("about:bookmarks/edit")) @@ -130,7 +130,7 @@ void NewTabPage::generate(const KUrl &url) page->mainFrame()->setHtml(m_html); page->setIsOnRekonqPage(true); - m_root = page->mainFrame()->documentElement().findFirst("#content"); + m_root = page->mainFrame()->documentElement().findFirst( QL1S("#content") ); browsingMenu(url); @@ -162,13 +162,13 @@ void NewTabPage::generate(const KUrl &url) title = i18n("Downloads"); } - m_root.document().findFirst("title").setPlainText(title); + m_root.document().findFirst( QL1S("title") ).setPlainText(title); } void NewTabPage::favoritesPage() { - m_root.addClass("favorites"); + m_root.addClass( QL1S("favorites") ); const QWebElement add = createLinkItem(i18n("Add Favorite"), QL1S("about:preview/add"), @@ -181,7 +181,7 @@ void NewTabPage::favoritesPage() if (urls.isEmpty()) { - m_root.addClass("empty"); + 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; } @@ -207,12 +207,13 @@ void NewTabPage::favoritesPage() QWebElement NewTabPage::emptyPreview(int index) { - QWebElement prev = markup(".thumbnail"); + QWebElement prev = markup( QL1S(".thumbnail") ); - prev.findFirst(".preview img").setAttribute("src" , QString("file:///") + - KIconLoader::global()->iconPath("insert-image", KIconLoader::Desktop)); - prev.findFirst("span a").setPlainText(i18n("Set a Preview...")); - prev.findFirst("a").setAttribute("href", QString("about:preview/modify/" + QVariant(index).toString())); + 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); @@ -223,12 +224,12 @@ QWebElement NewTabPage::emptyPreview(int index) QWebElement NewTabPage::loadingPreview(int index, const KUrl &url) { - QWebElement prev = markup(".thumbnail"); + QWebElement prev = markup( QL1S(".thumbnail") ); - prev.findFirst(".preview img").setAttribute("src" , - QString("file:///") + KStandardDirs::locate("appdata", "pics/busywidget.gif")); - prev.findFirst("span a").setPlainText(i18n("Loading Preview...")); - prev.findFirst("a").setAttribute("href", url.toMimeDataString()); + 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); @@ -245,13 +246,13 @@ QWebElement NewTabPage::loadingPreview(int index, const KUrl &url) QWebElement NewTabPage::validPreview(int index, const KUrl &url, const QString &title) { - QWebElement prev = markup(".thumbnail"); + QWebElement prev = markup( QL1S(".thumbnail") ); QString previewPath = QL1S("file://") + WebSnap::imagePathFromUrl(url); - prev.findFirst(".preview img").setAttribute("src" , previewPath); - prev.findFirst("a").setAttribute("href", url.toMimeDataString()); // NOTE ? - prev.findFirst("span a").setAttribute("href", url.toMimeDataString()); // NOTE ? - prev.findFirst("span a").setPlainText(checkTitle(title)); + 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); @@ -261,40 +262,38 @@ QWebElement NewTabPage::validPreview(int index, const KUrl &url, const QString & void NewTabPage::hideControls(QWebElement e) { - e.findFirst(".remove").setStyleProperty("visibility", "hidden"); - e.findFirst(".modify").setStyleProperty("visibility", "hidden"); + 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(".remove").setStyleProperty("visibility", "visible"); - e.findFirst(".modify").setStyleProperty("visibility", "visible"); + 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(".remove img").setAttribute("src", QString("file:///") + - KIconLoader::global()->iconPath("edit-delete", KIconLoader::DefaultState)); - e.findFirst(".remove").setAttribute("title", "Remove favorite"); - e.findFirst(".modify img").setAttribute("src", QString("file:///") + - KIconLoader::global()->iconPath("insert-image", KIconLoader::DefaultState)); - e.findFirst(".modify").setAttribute("title", "Set new favorite"); + 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(".modify").setAttribute("href", QString("about:preview/modify/" + QVariant(index).toString())); - e.findFirst(".remove").setAttribute("href", QString("about:preview/remove/" + QVariant(index).toString())); + 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("id", "preview" + QVariant(index).toString()); + e.setAttribute( QL1S("id"), QL1S("preview") + QVariant(index).toString()); } void NewTabPage::snapFinished() { // Update page, but only if open - if (m_root.document().findAll("#rekonq-newtabpage").count() == 0) + if (m_root.document().findAll( QL1S("#rekonq-newtabpage") ).count() == 0) return; - if (m_root.findAll(".favorites").count() == 0 && m_root.findAll(".closedTabs").count() == 0) + if (m_root.findAll( QL1S(".favorites") ).count() == 0 && m_root.findAll( QL1S(".closedTabs") ).count() == 0) return; QStringList urls = ReKonfig::previewUrls(); @@ -307,12 +306,12 @@ void NewTabPage::snapFinished() if (WebSnap::existsImage(url)) { - QWebElement prev = m_root.findFirst("#preview" + QVariant(i).toString()); - if (KUrl(prev.findFirst("a").attribute("href")) == 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(".closedTabs").count() != 0) + if (m_root.findAll( QL1S(".closedTabs") ).count() != 0) hideControls(newPrev); prev.replace(newPrev); @@ -338,6 +337,7 @@ void NewTabPage::removePreview(int index) ReKonfig::self()->writeConfig(); } + void NewTabPage::browsingMenu(const KUrl ¤tUrl) { QList navItems; @@ -374,55 +374,68 @@ void NewTabPage::browsingMenu(const KUrl ¤tUrl) foreach(QWebElement it, navItems) { - const QString aTagString('a'); + const QString aTagString( QL1C('a') ); const QString hrefAttributeString(QL1S("href")); if (it.findFirst(aTagString).attribute(hrefAttributeString) == currentUrl.toMimeDataString()) - it.addClass(QL1S("current")); + 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); + it.addClass( QL1S("current") ); + m_root.document().findFirst( QL1S("#navigation") ).appendInside(it); } } void NewTabPage::historyPage() { - m_root.addClass("history"); + m_root.addClass( QL1S("history") ); const QWebElement clearData = createLinkItem(i18n("Clear Private Data"), QL1S("about:history/clear"), QL1S("edit-clear"), KIconLoader::Toolbar); - m_root.document().findFirst("#actions").appendInside(clearData); + m_root.document().findFirst( QL1S("#actions") ).appendInside(clearData); HistoryTreeModel *model = Application::historyManager()->historyTreeModel(); if (model->rowCount() == 0) { - m_root.addClass("empty"); + m_root.addClass( QL1S("empty") ); m_root.setPlainText(i18n("Your browsing history is empty")); return; } int i = 0; + QString faviconsDir = KStandardDirs::locateLocal("cache" , "favicons/" , true); + QString icon = QL1S("file://") + KGlobal::dirs()->findResource("icon", "oxygen/16x16/mimetypes/text-html.png"); do { QModelIndex index = model->index(i, 0, QModelIndex()); if (model->hasChildren(index)) { - m_root.appendInside(markup("h3")); + m_root.appendInside(markup( QL1S("h3") )); m_root.lastChild().setPlainText(index.data().toString()); for (int j = 0; j < model->rowCount(index); ++j) - { + { QModelIndex son = model->index(j, 0, index); + KUrl u = son.data(HistoryModel::UrlStringRole).toUrl(); + + QString b = faviconsDir + u.host() + QL1S(".png"); + if( QFile::exists(b) ) + icon = QL1S("file://") + b; + 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.appendInside( QL1S(" ") ); + m_root.appendInside(markup( QL1S("img") )); + m_root.lastChild().setAttribute( QL1S("src"), icon); + m_root.lastChild().setAttribute( QL1S("width"), QL1S("16")); + m_root.lastChild().setAttribute( QL1S("height"), QL1S("16")); + m_root.appendInside( QL1S(" ") ); + m_root.appendInside(markup( QL1S("a") )); + m_root.lastChild().setAttribute( QL1S("href") , u.url()); m_root.lastChild().appendInside(son.data().toString()); - m_root.appendInside("
"); + m_root.appendInside( QL1S("
") ); } } i++; @@ -433,18 +446,18 @@ void NewTabPage::historyPage() void NewTabPage::bookmarksPage() { - m_root.addClass("bookmarks"); + 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("#actions").appendInside(editBookmarks); + m_root.document().findFirst( QL1S("#actions") ).appendInside(editBookmarks); KBookmarkGroup bookGroup = Application::bookmarkProvider()->rootGroup(); if (bookGroup.isNull()) { - m_root.addClass("empty"); + m_root.addClass( QL1S("empty") ); m_root.setPlainText(i18n("You have no bookmarks")); return; } @@ -460,13 +473,15 @@ void NewTabPage::bookmarksPage() 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("h3")); + parent.appendInside(markup( QL1S("h3") )); parent.lastChild().setPlainText(group.fullText()); - parent.appendInside(markup(".bookfolder")); + parent.appendInside(markup( QL1S(".bookfolder") )); while (!bm.isNull()) { createBookItem(bm, parent.lastChild()); // it is .bookfolder @@ -475,27 +490,36 @@ void NewTabPage::createBookItem(const KBookmark &bookmark, QWebElement parent) } else if (bookmark.isSeparator()) { - parent.appendInside("
"); + parent.appendInside( QL1S("
") ); } else { - parent.appendInside(markup("a")); - parent.lastChild().setAttribute("href" , bookmark.url().prettyUrl()); + 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("
"); + parent.appendInside( QL1S("
") ); } } void NewTabPage::closedTabsPage() { - m_root.addClass("closedTabs"); + m_root.addClass( QL1S("closedTabs") ); QList links = Application::instance()->mainWindow()->mainView()->recentlyClosedTabs(); if (links.isEmpty()) { - m_root.addClass("empty"); + m_root.addClass( QL1S("empty") ); m_root.setPlainText(i18n("There are no recently closed tabs")); return; } @@ -512,7 +536,7 @@ void NewTabPage::closedTabsPage() ? validPreview(i, item.url, item.title) : loadingPreview(i, item.url); - prev.setAttribute("id", "preview" + QVariant(i).toString()); + prev.setAttribute( QL1S("id"), QL1S("preview") + QVariant(i).toString()); hideControls(prev); m_root.appendInside(prev); } @@ -525,7 +549,7 @@ QString NewTabPage::checkTitle(const QString &title) if (t.length() > 23) { t.truncate(20); - t += "..."; + t += QL1S("..."); } return t; } @@ -533,69 +557,69 @@ QString NewTabPage::checkTitle(const QString &title) void NewTabPage::downloadsPage() { - m_root.addClass("downloads"); + m_root.addClass( QL1S("downloads") ); const QWebElement clearData = createLinkItem(i18n("Clear Private Data"), QL1S("about:downloads/clear"), QL1S("edit-clear"), KIconLoader::Toolbar); - m_root.document().findFirst("#actions").appendInside(clearData); + m_root.document().findFirst( QL1S("#actions") ).appendInside(clearData); DownloadList list = Application::instance()->downloads(); if (list.isEmpty()) { - m_root.addClass("empty"); + m_root.addClass( QL1S("empty") ); m_root.setPlainText(i18n("There are no recently downloaded files to show")); return; } foreach(const DownloadItem &item, list) { - m_root.prependInside(markup("div")); + m_root.prependInside(markup( QL1S("div") )); QWebElement div = m_root.firstChild(); - div.addClass("download"); + div.addClass( QL1S("download") ); KUrl u = KUrl(item.destUrlString); QString fName = u.fileName(); QString dir = QL1S("file://") + u.directory(); - QString file = dir + '/' + fName; + QString file = dir + QL1C('/') + fName; KIconLoader *loader = KIconLoader::global(); - QString iconPath = "file://" + loader->iconPath(KMimeType::iconNameForUrl(u), KIconLoader::Desktop); + QString iconPath = QL1S("file://") + loader->iconPath(KMimeType::iconNameForUrl(u), KIconLoader::Desktop); - div.appendInside(markup("img")); - div.lastChild().setAttribute("src", iconPath); + div.appendInside(markup( QL1S("img") )); + div.lastChild().setAttribute( QL1S("src"), iconPath); - div.appendInside("" + fName + ""); - div.appendInside(" - "); + div.appendInside( QL1S("") + fName + QL1S("") ); + div.appendInside( QL1S(" - ") ); QString date = KGlobal::locale()->formatDateTime(item.dateTime, KLocale::FancyLongDate); - div.appendInside("" + date + ""); - div.appendInside("
"); + div.appendInside( QL1S("") + date + QL1S("") ); + div.appendInside( QL1S("
") ); - div.appendInside(""); - div.appendInside("
"); + div.appendInside( QL1S("
") ); + div.appendInside( QL1S("
") ); - div.appendInside(markup("a")); - div.lastChild().setAttribute("href", dir); + div.appendInside(markup( QL1S("a") )); + div.lastChild().setAttribute( QL1S("href"), dir); div.lastChild().setPlainText(i18n("Open directory")); - div.appendInside(" - "); - div.appendInside(markup("a")); - div.lastChild().setAttribute("href", file); + div.appendInside( QL1S(" - ") ); + div.appendInside(markup( QL1S("a") )); + div.lastChild().setAttribute( QL1S("href"), file); div.lastChild().setPlainText(i18n("Open file")); } } + 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(QString('a')).setAttribute(QL1S("href"), urlString); - nav.findFirst(QL1S("img")).setAttribute(QL1S("src"), - QString::fromLatin1("file://") + loader->iconPath(iconPath, groupOrSize)); - nav.findFirst(QL1S("span")).appendInside(title); + 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; } -- cgit v1.2.1