/* * Copyright (C) 2012 Canonical, Ltd. * * Authors: * Guenter Schwann * * 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; version 3. * * 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 . */ #include "peoplepreviewdata.h" #include #include "lens.h" static void phoneAppend(QQmlListProperty *property, PeoplePhoneData *val) { Q_UNUSED(val); Q_UNUSED(property); } static PeoplePhoneData *phoneAt(QQmlListProperty *property, int index) { PeoplePreviewData *d = static_cast(property->data); return d->phoneNumberList().at(index); } static int phoneCount(QQmlListProperty *property) { PeoplePreviewData *d = static_cast(property->data); return d->phoneNumberList().count(); } static void phoneClear(QQmlListProperty *property) { Q_UNUSED(property); } static void emailAppend(QQmlListProperty *property, PeopleAddressData *val) { Q_UNUSED(val); Q_UNUSED(property); } static PeopleAddressData *emailAt(QQmlListProperty *property, int index) { PeoplePreviewData *d = static_cast(property->data); return d->emailAddressesList().at(index); } static int emailCount(QQmlListProperty *property) { PeoplePreviewData *d = static_cast(property->data); return d->emailAddressesList().count(); } static void emailClear(QQmlListProperty *property) { Q_UNUSED(property); } static void imAppend(QQmlListProperty *property, PeopleIMData *val) { Q_UNUSED(val); Q_UNUSED(property); } static PeopleIMData *imAt(QQmlListProperty *property, int index) { PeoplePreviewData *d = static_cast(property->data); return d->imAccountsList().at(index); } static int imCount(QQmlListProperty *property) { PeoplePreviewData *d = static_cast(property->data); return d->imAccountsList().count(); } static void imClear(QQmlListProperty *property) { Q_UNUSED(property); } static void addressAppend(QQmlListProperty *property, PeopleAddressData *val) { Q_UNUSED(val); Q_UNUSED(property); } static PeopleAddressData *addressAt(QQmlListProperty *property, int index) { PeoplePreviewData *d = static_cast(property->data); return d->addressesList().at(index); } static int addressCount(QQmlListProperty *property) { PeoplePreviewData *d = static_cast(property->data); return d->addressesList().count(); } static void addressClear(QQmlListProperty *property) { Q_UNUSED(property); } using namespace unity; PeoplePreviewData::PeoplePreviewData(QObject *parent) : QObject(parent) , m_lens(0) , m_ready(false) { } PeoplePreviewData::~PeoplePreviewData() { clearData(); } Lens *PeoplePreviewData::lens() const { return m_lens; } void PeoplePreviewData::setLens(Lens *lens) { if (lens == m_lens) return; setReady(false); if (m_lensConnection.connected()) m_lensConnection.disconnect(); m_lens = lens; m_lensConnection = m_lens->unityLens()->preview_ready.connect( sigc::mem_fun(this, &PeoplePreviewData::setUnityPreview)); Q_EMIT lensChanged(); if (!m_uri.isEmpty() && !m_lens) getPeoplePreview(); } QString PeoplePreviewData::uri() const { return m_uri; } void PeoplePreviewData::setUri(const QString &uri) { if (uri == m_uri) return; setReady(false); m_uri = uri; Q_EMIT uriChanged(); if (!m_uri.isEmpty() && m_lens) getPeoplePreview(); } bool PeoplePreviewData::ready() const { return m_ready; } void PeoplePreviewData::setReady(const bool &ready) { if (ready == m_ready) return; m_ready = ready; Q_EMIT readyChanged(); } QString PeoplePreviewData::renderName() const { if (!m_unityPreview) return QLatin1String(""); return QString::fromStdString(m_unityPreview->renderer_name()); } QString PeoplePreviewData::title() const { if (!m_unityPreview) return QLatin1String(""); return QString::fromStdString(m_unityPreview->title()); } QString PeoplePreviewData::subTitle() const { if (!m_unityPreview) return QLatin1String(""); return QString::fromStdString(m_unityPreview->subtitle()); } QString PeoplePreviewData::description() const { if (!m_unityPreview) return QLatin1String(""); return QString::fromStdString(m_unityPreview->description()); } QString PeoplePreviewData::imageSource() const { if (!m_unityPreview) return QLatin1String(""); return QString::fromStdString(m_unityPreview->image_source_uri()); } QQmlListProperty PeoplePreviewData::phoneNumbers() { return QQmlListProperty (this, this, phoneAppend, phoneCount, phoneAt, phoneClear); } QQmlListProperty PeoplePreviewData::emailAddresses() { return QQmlListProperty (this, this, emailAppend, emailCount, emailAt, emailClear); } QQmlListProperty PeoplePreviewData::imAccounts() { return QQmlListProperty (this, this, imAppend, imCount, imAt, imClear); } QString PeoplePreviewData::displayName() const { if (!m_unityPreview) return QLatin1String(""); return QString::fromStdString(m_unityPreview->display_name()); } QString PeoplePreviewData::status() const { if (!m_unityPreview) return QLatin1String(""); return QString::fromStdString(m_unityPreview->status()); } QString PeoplePreviewData::statusTime() const { if (!m_unityPreview) return QLatin1String(""); return QString::fromStdString(m_unityPreview->status_time()); } QString PeoplePreviewData::statusService() const { if (!m_unityPreview) return QLatin1String(""); return QString::fromStdString(m_unityPreview->status_service()); } QString PeoplePreviewData::statusServiceIcon() const { if (!m_unityPreview) return QLatin1String(""); glib::Object icon = m_unityPreview->status_service_icon(); if (!icon) return QLatin1String(""); return QString(g_icon_to_string(icon)); } QString PeoplePreviewData::statusPostUri() const { if (!m_unityPreview) return QLatin1String(""); return QString::fromStdString(m_unityPreview->status_post_uri()); } QQmlListProperty PeoplePreviewData::addresses() { return QQmlListProperty (this, this, addressAppend, addressCount, addressAt, addressClear); } QString PeoplePreviewData::avatar() const { if (!m_unityPreview) return QLatin1String(""); glib::Object avatar = m_unityPreview->avatar(); if (!avatar) return QLatin1String(""); return QString(g_icon_to_string(avatar)); } void PeoplePreviewData::getPeoplePreview() { m_lens->unityLens()->Preview(m_uri.toStdString()); } void PeoplePreviewData::setUnityPreview(std::string const& uri, dash::Preview::Ptr const& preview) { if (uri != m_uri.toStdString()) return; dash::PeoplePreview::Ptr peoplePtr = std::dynamic_pointer_cast(preview); if (peoplePtr == nullptr) { qWarning() << "Backend returned a dash::Preview that isn't a dash::PeoplePreview"; Q_EMIT error(); return; } clearData(); m_unityPreview = peoplePtr; getPhoneData(); getEmailAddresses(); getImAccounts(); getAddresses(); Q_EMIT renderNameChanged(); Q_EMIT titleChanged(); Q_EMIT subTitleChanged(); Q_EMIT descriptionChanged(); Q_EMIT imageSourceChanged(); Q_EMIT phoneNumbersChanged(); Q_EMIT emailAddressesChanged(); Q_EMIT imAccountsChanged(); Q_EMIT displayNameChanged(); Q_EMIT statusChanged(); Q_EMIT statusTimeChanged(); Q_EMIT statusServiceChanged(); Q_EMIT addressesChanged(); Q_EMIT avatarChanged(); setReady(true); } void PeoplePreviewData::clearData() { while (!m_phoneList.empty()) { PeoplePhoneData *phone = m_phoneList.takeLast(); delete phone; } while (!m_emailList.empty()) { PeopleAddressData *email = m_emailList.takeLast(); delete email; } while (!m_imList.empty()) { PeopleIMData *im = m_imList.takeLast(); delete im; } while (!m_addressList.empty()) { PeopleAddressData *address = m_addressList.takeLast(); delete address; } } void PeoplePreviewData::getPhoneData() { dash::PeoplePreview::PhonePtrList uPhoneList; uPhoneList = m_unityPreview->GetPhone(); for (size_t i=0; im_type = QString::fromStdString(uPhoneList[i]->type); phone->m_location = QString::fromStdString(uPhoneList[i]->location); phone->m_number = QString::fromStdString(uPhoneList[i]->number); m_phoneList.append(phone); } } void PeoplePreviewData::getEmailAddresses() { dash::PeoplePreview::EmailPtrList uEmailList; uEmailList = m_unityPreview->GetEmail(); for (size_t i=0; im_type = QString::fromStdString(uEmailList[i]->type); email->m_address = QString::fromStdString(uEmailList[i]->address); m_emailList.append(email); } } void PeoplePreviewData::getImAccounts() { dash::PeoplePreview::IMAccountPtrList uIMList; uIMList = m_unityPreview->GetIMAccount(); for (size_t i=0; im_protocol = QString::fromStdString(uIMList[i]->protocol); im->m_address = QString::fromStdString(uIMList[i]->address); m_imList.append(im); } } void PeoplePreviewData::getAddresses() { dash::PeoplePreview::AddressPtrList uAddressList; uAddressList = m_unityPreview->GetAddress(); for (size_t i=0; im_type = QString::fromStdString(uAddressList[i]->type); address->m_address = QString::fromStdString(uAddressList[i]->address); address->m_poBox = QString::fromStdString(uAddressList[i]->po_box); address->m_extension = QString::fromStdString(uAddressList[i]->extension); address->m_street = QString::fromStdString(uAddressList[i]->street); address->m_locality = QString::fromStdString(uAddressList[i]->locality); address->m_region = QString::fromStdString(uAddressList[i]->region); address->m_postalCode = QString::fromStdString(uAddressList[i]->postal_code); address->m_country = QString::fromStdString(uAddressList[i]->country); address->m_addressFormat = QString::fromStdString(uAddressList[i]->address_format); m_addressList.append(address); } }