bool DeclarativeTabModel::activateTab(int index)
{
    if (index >= 0 && index < m_tabs.count()) {
        Tab newActiveTab = m_tabs.at(index);
#ifdef DEBUG_LOGS
        qDebug() << "activate tab: " << index << &newActiveTab;
#endif
        beginRemoveRows(QModelIndex(), index, index);
        m_tabs.removeAt(index);
        endRemoveRows();

        // Current active tab back to model data.
        if (m_activeTab.isValid()) {
#ifdef DEBUG_LOGS
            qDebug() << "insert to first index: " << &m_activeTab;
#endif
            beginInsertRows(QModelIndex(), 0, 0);
            m_tabs.insert(0, m_activeTab);
            endInsertRows();
        }

        updateActiveTab(newActiveTab);
        return true;
    }
    return false;
}
void DeclarativeTabModel::tabChanged(const Tab &tab)
{
#ifdef DEBUG_LOGS
    qDebug() << &m_activeTab;
    qDebug() << "new tab data:" << &tab;
#endif
    if (m_activeTab.tabId() == tab.tabId()) {
        updateActiveTab(tab);
    } else {
        int i = m_tabs.indexOf(tab); // match based on tab_id
        if (i > -1) {
            QVector<int> roles;
            Tab oldTab = m_tabs[i];
            if (oldTab.url() != tab.url()) {
                roles << UrlRole;
            }
            if (oldTab.title() != tab.title()) {
                roles << TitleRole;
            }
            if (oldTab.thumbnailPath() != tab.thumbnailPath()) {
                roles << ThumbPathRole;
            }
            m_tabs[i] = tab;
            QModelIndex start = index(i, 0);
            QModelIndex end = index(i, 0);
            emit dataChanged(start, end, roles);
        }
    }
}
void DeclarativeTabModel::addTab(const QString& url, const QString &title) {
    if (!LinkValidator::navigable(QUrl(url))) {
        return;
    }
    int tabId = createTab();
    int linkId = createLink(tabId, url, title);

    Tab tab(tabId, Link(linkId, url, "", title), 0, 0);
#if DEBUG_LOGS
    qDebug() << "new tab data:" << &tab;
#endif
    int index = m_tabs.count();
    beginInsertRows(QModelIndex(), index, index);
    m_tabs.append(tab);
    endInsertRows();
    // We should trigger this only when
    // tab is added through new window request. In all other
    // case we should keep the new tab in background.
    updateActiveTab(tab, true);

    emit countChanged();
    emit tabAdded(tabId);

    m_nextTabId = ++tabId;
    emit nextTabIdChanged();

    setWaitingForNewTab(false);
}
void DeclarativeTabModel::tabChanged(Tab tab)
{
    // When a tab was closed do not update anything from database as
    // loading might be on going.
    if (m_activeTabClosed && this->sender() == DBManager::instance()) {
        m_activeTabClosed = false;
        return;
    }
#ifdef DEBUG_LOGS
    qDebug() << "tab: " << tab.tabId() << m_activeTab.tabId() << tab.currentLink().thumbPath() << tab.currentLink().url() << tab.currentLink().title() << m_tabs.indexOf(tab);
#endif
    if (tab.tabId() == m_activeTab.tabId()) {
        updateActiveTab(tab);
    } else {
        int i = m_tabs.indexOf(tab); // match based on tab_id
        if (i > -1) {
            QVector<int> roles;
            Tab oldTab = m_tabs[i];
            if (oldTab.currentLink().url() != tab.currentLink().url()) {
                roles << UrlRole;
            }
            if (oldTab.currentLink().title() != tab.currentLink().title()) {
                roles << TitleRole;
            }
            if (oldTab.currentLink().thumbPath() != tab.currentLink().thumbPath()) {
                roles << ThumbPathRole;
            }
            m_tabs[i] = tab;
            QModelIndex start = index(i, 0);
            QModelIndex end = index(i, 0);
            emit dataChanged(start, end, roles);
        }
    }
}
bool DeclarativeTabModel::activateTab(int index, bool loadActiveTab)
{
    if (index >= 0 && index < m_tabs.count()) {
        const Tab &newActiveTab = m_tabs.at(index);
#if DEBUG_LOGS
        qDebug() << "activate tab: " << index << &newActiveTab;
#endif
        updateActiveTab(newActiveTab, loadActiveTab);
        return true;
    } else {
        return false;
    }
}
// TODO : Remove foreground flag.
void DeclarativeTabModel::addTab(const QString& url, bool foreground) {
    int tabId = DBManager::instance()->createTab();
    int linkId = DBManager::instance()->createLink(tabId, url);
#ifdef DEBUG_LOGS
    qDebug() << "new tab id:" << tabId << "new link id:" << linkId;
#endif
    Tab tab(tabId, Link(linkId, url, "", ""), 0, 0);
    if (m_activeTab.isValid()) {
        beginInsertRows(QModelIndex(), 0, 0);
        m_tabs.insert(0, m_activeTab);
        endInsertRows();
    }

    updateActiveTab(tab, foreground);
    emit countChanged();
}
void DeclarativeTabModel::tabsAvailable(QList<Tab> tabs)
{
    beginResetModel();
    int oldCount = count();
    m_activeTab.setTabId(0);
    m_tabs.clear();
    m_tabs = tabs;
    if (m_currentTab) {
        m_currentTab->invalidate();
    }

    int activeTabId = loadTabOrder();
    if (m_tabs.count() > 0) {
        Tab tab;
        tab.setTabId(activeTabId);

        int index = m_tabs.indexOf(tab);
        if (index == -1) {
            index = 0;
        }
        const Tab &activeTab = m_tabs.at(index);
        m_tabs.removeAt(index);
        updateActiveTab(activeTab, true);
    }

    qSort(m_tabs.begin(), m_tabs.end(), DeclarativeTabModel::tabSort);
    endResetModel();

    // Startup should be synced to this.
    if (!m_loaded) {
        m_loaded = true;
        emit loadedChanged();
    }

    if (count() != oldCount) {
        emit countChanged();
    }
}
void DeclarativeTabModel::addTab(const QString& url, const QString &title) {
    if (!LinkValidator::navigable(QUrl(url))) {
        return;
    }
    int tabId = DBManager::instance()->createTab();
    int linkId = DBManager::instance()->createLink(tabId, url, title);

    Tab tab(tabId, Link(linkId, url, "", title), 0, 0);
#ifdef DEBUG_LOGS
    qDebug() << "new tab data:" << &tab;
#endif
    if (m_activeTab.isValid()) {
        beginInsertRows(QModelIndex(), 0, 0);
        m_tabs.insert(0, m_activeTab);
        endInsertRows();
    }

    updateActiveTab(tab);
    emit countChanged();
    emit tabAdded(tabId);

    m_nextTabId = ++tabId;
    emit nextTabIdChanged();
}