diff options
author | Andrea Diamantini <adjam7@gmail.com> | 2010-02-07 00:01:52 +0100 |
---|---|---|
committer | Andrea Diamantini <adjam7@gmail.com> | 2010-02-07 00:01:52 +0100 |
commit | f4f86382a7dc3f57a62820df19afd8b1b1d9355d (patch) | |
tree | fdb618f2877f61ddfc3f91504a1675420d8e2f78 /src/rekonqpage | |
parent | Merge commit 'refs/merge-requests/86' of git://gitorious.org/rekonq/mainline ... (diff) | |
parent | Fix loading previews : when load finished, result wasn't shown (diff) | |
download | rekonq-f4f86382a7dc3f57a62820df19afd8b1b1d9355d.tar.xz |
Merge commit 'refs/merge-requests/84' of git://gitorious.org/rekonq/mainline into matgic_tab_page
Diffstat (limited to 'src/rekonqpage')
-rw-r--r-- | src/rekonqpage/newtabpage.cpp | 240 | ||||
-rw-r--r-- | src/rekonqpage/newtabpage.h | 35 | ||||
-rw-r--r-- | src/rekonqpage/previewselectorbar.cpp | 160 | ||||
-rw-r--r-- | src/rekonqpage/previewselectorbar.h | 60 |
4 files changed, 450 insertions, 45 deletions
diff --git a/src/rekonqpage/newtabpage.cpp b/src/rekonqpage/newtabpage.cpp index 66f74b86..19f1686f 100644 --- a/src/rekonqpage/newtabpage.cpp +++ b/src/rekonqpage/newtabpage.cpp @@ -26,6 +26,7 @@ // Self Includes #include "newtabpage.h" +#include "newtabpage.moc" // Auto Includes #include "rekonq.h" @@ -36,6 +37,8 @@ #include "application.h" #include "mainwindow.h" #include "mainview.h" +#include "websnap.h" +#include "previewselectorbar.h" // KDE Includes #include <KStandardDirs> @@ -43,6 +46,7 @@ #include <KDebug> #include <KConfig> #include <KConfigGroup> +#include <KDialog> // Qt Includes #include <QFile> @@ -50,6 +54,7 @@ NewTabPage::NewTabPage(QWebFrame *frame) : m_root(frame->documentElement()) + , m_url(KUrl()) { QString htmlFilePath = KStandardDirs::locate("data", "rekonq/htmls/home.html"); @@ -70,8 +75,33 @@ NewTabPage::~NewTabPage() } -void NewTabPage::generate(const KUrl &url) -{ +void NewTabPage::generate(KUrl url) +{ + if(KUrl("about:preview").isParentOf(url)) + { + if(url.directory() == QString("preview/remove")) + { + removePreview(url.fileName().toInt()); + return; + } + if(url.directory() == QString("preview/modify")) + { + int index = url.fileName().toInt(); + Application::instance()->mainWindow()->findChild<PreviewSelectorBar *>() + ->enable(index, qobject_cast< WebPage* >(m_root.webFrame()->page())); + return; + } + } + + if( url != KUrl("about:home") + && url != KUrl("about:favorites") + && url != KUrl("about:closedTabs") + && url != KUrl("about:history") + && url != KUrl("about:bookmarks") + ) + return; + + QWebPage *page = m_root.webFrame()->page(); page->mainFrame()->setHtml(m_html); @@ -80,26 +110,29 @@ void NewTabPage::generate(const KUrl &url) browsingMenu(url); QString title; - if(url == KUrl("about:closedTabs")) + if(url == KUrl("about:home") || url == KUrl("about:favorites")) + { + favoritesPage(); + title = i18n("Favorites"); + url = KUrl("about:favorites"); + } + else if(url == KUrl("about:closedTabs")) { closedTabsPage(); title = i18n("Closed Tabs"); } - if(url == KUrl("about:history")) + else if(url == KUrl("about:history")) { historyPage(); title = i18n("History"); } - if(url == KUrl("about:bookmarks")) + else if(url == KUrl("about:bookmarks")) { bookmarksPage(); title = i18n("Bookmarks"); } - if(url == KUrl("about:home") || url == KUrl("about:favorites")) - { - favoritesPage(); - title = i18n("Favorites"); - } + + m_url = url; m_root.document().findFirst("title").setPlainText(title); } @@ -107,41 +140,150 @@ void NewTabPage::generate(const KUrl &url) void NewTabPage::favoritesPage() { + m_root.addClass("favorites"); + QStringList names = ReKonfig::previewNames(); QStringList urls = ReKonfig::previewUrls(); - - m_root.addClass("favorites"); for(int i=0; i<8; ++i) { - 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); + KUrl url = urls.at(i); + QWebElement prev; + + if(url.isEmpty()) + prev = emptyPreview(i); + else if(!QFile::exists(WebSnap::fileForUrl(url).toLocalFile())) + prev = loadingPreview(i, url); + else + prev = validPreview(i, url, names.at(i)); + + setupPreview(prev, i); + + m_root.appendInside(prev); } } -// FIXME : port to new PreviewImage API to use... -/*QString NewTabPage::lastVisitedPage() +QWebElement NewTabPage::emptyPreview(int index) +{ + QWebElement prev = markup(".thumbnail"); + + prev.findFirst(".preview img").setAttribute("src" , QString("file:///") + + KIconLoader::global()->iconPath("insert-image", KIconLoader::Desktop)); + prev.findFirst("span").appendInside(i18n("Set a Preview...")); + prev.findFirst("a").setAttribute("href", QString("about:preview/modify/" + QVariant(index).toString())); + + setupPreview(prev, index); + hideControls(prev); + + return prev; +} + + +QWebElement NewTabPage::loadingPreview(int index, KUrl url) +{ + QWebElement prev = markup(".thumbnail"); + + prev.findFirst(".preview img").setAttribute("src" , + QString("file:///") + KStandardDirs::locate("appdata", "pics/busywidget.gif")); + prev.findFirst("span").appendInside(i18n("Loading Preview...")); + prev.findFirst("a").setAttribute("href", url.toMimeDataString()); + + setupPreview(prev, index); + showControls(prev); + + new WebSnap(url, m_root.webFrame()->page(), index); + + return prev; +} + +QWebElement NewTabPage::validPreview(int index, KUrl url, QString title) { - QString last; - QList<HistoryItem> history = Application::historyManager()->history(); - for (int i = 0; i < 8 && i < history.size(); ++i) + QWebElement prev = markup(".thumbnail"); + KUrl previewPath = WebSnap::fileForUrl(url); + QString iString = QVariant(index).toString(); + + prev.findFirst(".preview img").setAttribute("src" , previewPath.toMimeDataString()); + prev.findFirst("a").setAttribute("href", url.toMimeDataString()); + prev.findFirst("span a").setAttribute("href", url.toMimeDataString()); + prev.findFirst("span").setPlainText(checkTitle(title)); + + setupPreview(prev, index); + showControls(prev); + + return prev; +} + + +void NewTabPage::hideControls(QWebElement e) +{ + e.findFirst(".remove").setStyleProperty("visibility", "hidden"); + e.findFirst(".modify").setStyleProperty("visibility", "hidden"); +} +void NewTabPage::showControls(QWebElement e) +{ + e.findFirst(".remove").setStyleProperty("visibility", "visible"); + e.findFirst(".modify").setStyleProperty("visibility", "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(".modify").setAttribute("href", QString("about:preview/modify/" + QVariant(index).toString())); + e.findFirst(".remove").setAttribute("href", QString("about:preview/remove/" + QVariant(index).toString())); + + e.setAttribute("id", "preview" + QVariant(index).toString()); +} + + +void NewTabPage::snapFinished(int index, KUrl url, QString title) +{ + // do not try to modify the page if it isn't the newTabPage + if(m_root.document().findAll("#rekonq-newtabpage").count() == 0) + return; + + QWebElement prev = m_root.findFirst("#preview" + QVariant(index).toString()); + QWebElement newPrev = validPreview(index, url, title); + + if(m_root.findAll(".closedTabs").count() != 0) + hideControls(newPrev); + + prev.replace(newPrev); + + // update title + if(m_root.findAll(".favorites").count() != 0) { - HistoryItem it = history.at(i); - last += "<div class=\"thumbnail\">"; - last += "<object type=\"application/image-preview\" data=\"" + it.url + "\" >"; - last += "</object>"; - last += "<br />"; - last += "<a href=\"" + it.url + "\">" + it.title + "</a></div>"; + QStringList names = ReKonfig::previewNames(); + names.replace(index, title); + ReKonfig::setPreviewNames(names); + + ReKonfig::self()->writeConfig(); } +} - return last; -}*/ +void NewTabPage::removePreview(int index) +{ + QStringList names = ReKonfig::previewNames(); + QStringList urls = ReKonfig::previewUrls(); + + urls.replace(index, QString("")); + names.replace(index, QString("")); + + ReKonfig::setPreviewNames(names); + ReKonfig::setPreviewUrls(urls); + + ReKonfig::self()->writeConfig(); + + QWebElement prev = m_root.findFirst("#preview" + QVariant(index).toString()); + prev.replace(emptyPreview(index)); +} void NewTabPage::browsingMenu(const KUrl ¤tUrl) @@ -192,6 +334,8 @@ void NewTabPage::browsingMenu(const KUrl ¤tUrl) void NewTabPage::historyPage() { + m_root.addClass("history"); + HistoryTreeModel *model = Application::historyManager()->historyTreeModel(); int i = 0; @@ -222,6 +366,8 @@ void NewTabPage::historyPage() void NewTabPage::bookmarksPage() { + m_root.addClass("bookmarks"); + KBookmarkGroup bookGroup = Application::bookmarkProvider()->rootGroup(); if (bookGroup.isNull()) { @@ -268,13 +414,35 @@ void NewTabPage::createBookItem(const KBookmark &bookmark, QWebElement parent) void NewTabPage::closedTabsPage() { + m_root.addClass("closedTabs"); + QList<HistoryItem> links = Application::instance()->mainWindow()->mainView()->recentlyClosedTabs(); + + for(int i=0; i < links.count(); ++i) + { + HistoryItem item = links.at(i); + QWebElement prev; + + if(item.url.isEmpty()) + continue; + else if(!QFile::exists(WebSnap::fileForUrl(item.url).toLocalFile())) + prev = loadingPreview(i, item.url); + else + prev = validPreview(i, item.url, item.title); + + prev.setAttribute("id", "preview" + QVariant(i).toString()); + hideControls(prev); + m_root.appendInside(prev); + } +} - foreach(const HistoryItem &item, links) + +QString NewTabPage::checkTitle(QString title) +{ + if(title.length() > 23) { - QWebElement closed = markup(".thumbnail"); - closed.findFirst("object").setAttribute("data" , item.url); - closed.findFirst("param[name=title]").setAttribute("value", item.title); - m_root.appendInside(closed); + title.truncate(20); + title += "..."; } + return title; } diff --git a/src/rekonqpage/newtabpage.h b/src/rekonqpage/newtabpage.h index 003aa84e..9d41946e 100644 --- a/src/rekonqpage/newtabpage.h +++ b/src/rekonqpage/newtabpage.h @@ -28,24 +28,22 @@ #define REKONQ_NEW_TAB_PAGE -// rekonq Includes -#include <webpage.h> - // KDE Includes #include <KUrl> // Qt Includes -#include <QtCore/QObject> -#include <QtCore/QString> +#include <QObject> +#include <QString> #include <QWebElement> // Forward Includes class KBookmark; +class WebPage; -class NewTabPage +class NewTabPage : public QObject { - +Q_OBJECT public: NewTabPage(QWebFrame *frame); ~NewTabPage(); @@ -55,13 +53,28 @@ public: * about: url and loads the corresponding part of the * new tab page */ - void generate(const KUrl &url = KUrl("about:home")); + void generate(KUrl url = KUrl("about:home")); + +public slots: + void snapFinished(int index, KUrl url, QString title); + void removePreview(int index); protected: // these are the function to build the new tab page void browsingMenu(const KUrl ¤tUrl); void favoritesPage(); - //QString lastVisitedPage(); + QWebElement emptyPreview(int index); + QWebElement loadingPreview(int index, KUrl url); + QWebElement validPreview(int index, KUrl url, QString title); + + /** This function takes a QwebElement with the .thumbnail structure. + It hides the "remove" and "modify" buttons-> + */ + void hideControls(QWebElement e); + void showControls(QWebElement e); + void setupPreview(QWebElement e, int index); + + void historyPage(); void bookmarksPage(); void closedTabsPage(); @@ -77,10 +90,14 @@ private: { return m_root.document().findFirst("#models > " + selector).clone(); } + + QString checkTitle(QString title); QString m_html; QWebElement m_root; + + KUrl m_url; }; #endif // REKONQ_NEW_TAB_PAGE diff --git a/src/rekonqpage/previewselectorbar.cpp b/src/rekonqpage/previewselectorbar.cpp new file mode 100644 index 00000000..5f3956d7 --- /dev/null +++ b/src/rekonqpage/previewselectorbar.cpp @@ -0,0 +1,160 @@ +/* + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + 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 3 of the License, or + (at your option) any later version. + + 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/>. + +*/ + + +// Auto Includes +#include "previewselectorbar.h" + +// Local Include +#include "rekonq.h" +#include "websnap.h" + +// KDE Includes +#include <KIcon> +#include <KLocalizedString> + +// Qt Includes +#include <QToolButton> +#include <QHBoxLayout> +#include <QString> + + +PreviewSelectorBar::PreviewSelectorBar(QWidget* parent) + : QWidget(parent) + , m_button(0) + , m_label(0) + , m_page(0) +{ + hide(); +} + + +void PreviewSelectorBar::setup() +{ + if(m_button != 0) + return; + + m_label = new QLabel(i18n("Please open up the webpage you want to add as favorite"), this); + m_label->setWordWrap(true); + + QToolButton *closeButton = new QToolButton(this); + closeButton->setAutoRaise(true); + closeButton->setIcon(KIcon("dialog-close")); + connect(closeButton, SIGNAL(clicked(bool)), SLOT(hide())); + + m_button = new QPushButton(KIcon("insert-image"), i18n("Set to This Page"), this); + m_button->setMaximumWidth(250); + connect(m_button, SIGNAL(clicked(bool)), SLOT(clicked())); + + // layout + QHBoxLayout *layout = new QHBoxLayout(this); + layout->addWidget(closeButton); + layout->addWidget(m_label); + layout->addWidget(m_button); + + layout->setContentsMargins(2, 0, 2, 0); + + setLayout(layout); +} + + +void PreviewSelectorBar::setPage(WebPage* page) +{ + m_page = page; + verifyUrl(); +} + + +void PreviewSelectorBar::verifyUrl() +{ + if(m_page->mainFrame()->url().scheme() != "about") + { + m_button->setEnabled(true); + m_button->setToolTip(""); + } + else + { + m_button->setEnabled(false); + m_button->setToolTip(i18n("You can not add this webpage as favorite")); + } +} + + +void PreviewSelectorBar::enable(int previewIndex, WebPage* page) +{ + if(m_page != 0) + disconnect(m_page, 0, this, 0); + + + setup(); + m_previewIndex = previewIndex; + m_page = page; + + verifyUrl(); + + show(); + + connect(page, SIGNAL(loadStarted()), SLOT(loadProgress())); + connect(page, SIGNAL(loadProgress(int)), SLOT(loadProgress())); + connect(page, SIGNAL(loadFinished(bool)), SLOT(loadFinished())); + connect(page->mainFrame(), SIGNAL(urlChanged(QUrl)), SLOT(verifyUrl())); +} + + +void PreviewSelectorBar::loadProgress() +{ + m_button->setEnabled(false); + m_button->setToolTip(i18n("Page is loading...")); +} + + + +void PreviewSelectorBar::loadFinished() +{ + m_button->setEnabled(true); + m_button->setToolTip(""); + + verifyUrl(); +} + + +void PreviewSelectorBar::clicked() +{ + KUrl url = m_page->mainFrame()->url(); + + WebSnap::savePreview(WebSnap::renderPreview(*m_page), url); + + QStringList names = ReKonfig::previewNames(); + QStringList urls = ReKonfig::previewUrls(); + + urls.replace(m_previewIndex, url.toMimeDataString()); + names.replace(m_previewIndex, m_page->mainFrame()->title()); + + ReKonfig::setPreviewNames(names); + ReKonfig::setPreviewUrls(urls); + + ReKonfig::self()->writeConfig(); + + + m_page->mainFrame()->load(KUrl("about:favorites")); + + hide(); +} + + diff --git a/src/rekonqpage/previewselectorbar.h b/src/rekonqpage/previewselectorbar.h new file mode 100644 index 00000000..aa011fe4 --- /dev/null +++ b/src/rekonqpage/previewselectorbar.h @@ -0,0 +1,60 @@ +/* + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + 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 3 of the License, or + (at your option) any later version. + + 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/>. + +*/ + +#ifndef PREVIEWSELECTORBAR_H +#define PREVIEWSELECTORBAR_H + +// Local Includes +#include "webpage.h" + +// Qt Includes +#include <QWidget> +#include <QPushButton> +#include <QLabel> + +class PreviewSelectorBar : public QWidget +{ + Q_OBJECT + + public: + PreviewSelectorBar(QWidget *parent = 0); + + void setPage(WebPage *page); + + public slots: + void enable(int previewIndex, WebPage *page); + void clicked(); + + void loadProgress(); + void loadFinished(); + + void verifyUrl(); + + private: + void setup(); + + QPushButton *m_button; + QLabel *m_label; + + int m_previewIndex; + WebPage *m_page; + +}; + +#endif // PREVIEWSELECTORBAR_H |