<commit_before>/*
 * Copyright (c) 2010, Intel Corporation. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are 
 * met:
 * 
 *     * Redistributions of source code must retain the above copyright 
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above 
 * copyright notice, this list of conditions and the following disclaimer 
 * in the documentation and/or other materials provided with the 
 * distribution.
 *     * Neither the name of Intel Corporation nor the names of its 
 * contributors may be used to endorse or promote products derived from 
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "chrome/browser/ui/meegotouch/back_forward_button_qt.h"
#include "chrome/browser/ui/meegotouch/browser_window_qt.h"
#include "chrome/browser/ui/meegotouch/browser_toolbar_qt.h"
#include "base/utf_string_conversions.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "base/string_number_conversions.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "content/browser/tab_contents/navigation_controller.h"
#include "content/browser/tab_contents/navigation_entry.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "chrome/common/url_constants.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "net/base/registry_controlled_domain.h"
#include "chrome/browser/history/history.h"
#include "chrome/browser/history/recent_and_bookmark_thumbnails_qt.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"

#include <QGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <QVector>
#include <QList>
#include <QTimer>
#include <algorithm>
#include <QGraphicsSceneResizeEvent>
#include <QGraphicsWidget>
#include <QEvent>
#include <QDebug>
//#include <QTapAndHoldGesture>

#include <QDeclarativeEngine>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDeclarativeItem>
#include <QDeclarativeImageProvider>
#include <QAbstractListModel>

// Image provider, managing to provide image to QML
// for history view
// Due to qml limitation, the id of image is suffixed with
// a static increaser, which forces using new image source
// each time
class HistoryImageProvider : public QDeclarativeImageProvider 
{
public:
    HistoryImageProvider()
        : QDeclarativeImageProvider(QDeclarativeImageProvider::Image)
    {}

    // clear all images in image hashmap
    void clear()
    {
        imageList_.clear();
    }

    // overrided function, inherited from QDeclarativeImageProvider
    virtual QImage requestImage(const QString& id,
                                QSize* size,
                                const QSize& requestedSize)
    {
        DLOG(INFO) << "requesting image id: " << id.toStdString();
        int finded = id.indexOf("_");
        if (finded != -1) {
            QImage& image = imageList_[id.left(finded)];
            if (!image.isNull()) {
                //QImage scaled = image.scaled(requestedSize);
                *size = image.size();
                return image;
            }
        }
        *size = QSize(0, 0);
        return QImage();
    }

    // add a new image
    void addImage(const QString& id, const QImage &image)
    {
        imageList_.insert(id, image);
    }

private:
    QMap<QString, QImage> imageList_;
};

class HistoryStackModel;

// history entry to represent one history data
class HistoryEntry 
{
public:
  
    // constructor
    // @param index the index in history stack
    // @param hiProvider image provider for history stack
    // @param entry representation of navigation entry in browser
    // @param controller current navigation controller in browser
    // @param model history stack model
    HistoryEntry(const int index, 
                 HistoryImageProvider& hiProvider,
                 NavigationEntry* entry,
                 NavigationController* controller,
                 HistoryStackModel* model)
        : index_(index), hiProvider_(hiProvider), entry_(entry), model_(model)
    {
        // get image
        getThumbnailData(controller);
        std::wstring title_str = UTF16ToWide(entry->title());
        title_ = QString::fromStdWString(title_str);
    }

    void imgURLGen() <commit_msg>HistoryStack: avoid qml warnings<commit_after>/*
 * Copyright (c) 2010, Intel Corporation. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are 
 * met:
 * 
 *     * Redistributions of source code must retain the above copyright 
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above 
 * copyright notice, this list of conditions and the following disclaimer 
 * in the documentation and/or other materials provided with the 
 * distribution.
 *     * Neither the name of Intel Corporation nor the names of its 
 * contributors may be used to endorse or promote products derived from 
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "chrome/browser/ui/meegotouch/back_forward_button_qt.h"
#include "chrome/browser/ui/meegotouch/browser_window_qt.h"
#include "chrome/browser/ui/meegotouch/browser_toolbar_qt.h"
#include "base/utf_string_conversions.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "base/string_number_conversions.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "content/browser/tab_contents/navigation_controller.h"
#include "content/browser/tab_contents/navigation_entry.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "chrome/common/url_constants.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "net/base/registry_controlled_domain.h"
#include "chrome/browser/history/history.h"
#include "chrome/browser/history/recent_and_bookmark_thumbnails_qt.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"

#include <QGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <QVector>
#include <QList>
#include <QTimer>
#include <algorithm>
#include <QGraphicsSceneResizeEvent>
#include <QGraphicsWidget>
#include <QEvent>
#include <QDebug>
//#include <QTapAndHoldGesture>

#include <QDeclarativeEngine>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDeclarativeItem>
#include <QDeclarativeImageProvider>
#include <QAbstractListModel>

// Image provider, managing to provide image to QML
// for history view
// Due to qml limitation, the id of image is suffixed with
// a static increaser, which forces using new image source
// each time
class HistoryImageProvider : public QDeclarativeImageProvider 
{
public:
    HistoryImageProvider()
        : QDeclarativeImageProvider(QDeclarativeImageProvider::Image)
    {}

    // clear all images in image hashmap
    void clear()
    {
        imageList_.clear();
    }

    // overrided function, inherited from QDeclarativeImageProvider
    virtual QImage requestImage(const QString& id,
                                QSize* size,
                                const QSize& requestedSize)
    {
        DLOG(INFO) << "requesting image id: " << id.toStdString();
        int finded = id.indexOf("_");
        if (finded != -1) {
            QImage& image = imageList_[id.left(finded)];
            if (!image.isNull()) {
                //QImage scaled = image.scaled(requestedSize);
                if (size) {
                  *size = image.size();
                }
                return image;
            }
        }
        QImage ret(kThumbnailWidth,kThumbnailHeight, QImage::Format_RGB32);
        ret.fill(0xFFFFFF);
        if (size) {
          *size = ret.size();
        }
        return ret;
    }

    // add a new image
    void addImage(const QString& id, const QImage &image)
    {
        imageList_.insert(id, image);
    }

private:
    static const int kThumbnailWidth = 212;
    static const int kThumbnailHeight = 132;
    QMap<QString, QImage> imageList_;
};

class HistoryStackModel;

// history entry to represent one history data
class HistoryEntry 
{
public:
  
    // constructor
    // @param index the index in history stack
    // @param hiProvider image provider for history stack
    // @param entry representation of navigation entry in browser
    // @param controller current navigation controller in browser
    // @param model history stack model
    HistoryEntry(const int index, 
                 HistoryImageProvider& hiProvider,
                 NavigationEntry* entry,
                 NavigationController* controller,
                 HistoryStackModel* model)
        : index_(index), hiProvider_(hiProvider), entry_(entry), model_(model)
    {
        // get image
        getThumbnailData(controller);
        std::wstring title_str = UTF16ToWide(entry->title());
        title_ = QString::fromStdWString(title_str);
    }

    void imgURLGen() 
{'fraction_non_alphanumeric': 0.08745874587458746, 'fraction_numerical': 0.003025302530253025, 'mean_word_length': 3.4005647438483257, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7055135', 'n_tokens_mistral': 3140, 'n_tokens_neox': 2938, 'n_words': 972}