qtmobility/examples/qmlcontacts/qmlcontact.cpp
changeset 14 6fbed849b4f4
parent 11 06b8e2af4411
child 15 1f895d8a5b2b
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    12 ** "Redistribution and use in source and binary forms, with or without
       
    13 ** modification, are permitted provided that the following conditions are
       
    14 ** met:
       
    15 **   * Redistributions of source code must retain the above copyright
       
    16 **     notice, this list of conditions and the following disclaimer.
       
    17 **   * Redistributions in binary form must reproduce the above copyright
       
    18 **     notice, this list of conditions and the following disclaimer in
       
    19 **     the documentation and/or other materials provided with the
       
    20 **     distribution.
       
    21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
       
    22 **     the names of its contributors may be used to endorse or promote
       
    23 **     products derived from this software without specific prior written
       
    24 **     permission.
       
    25 **
       
    26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
       
    37 ** $QT_END_LICENSE$
       
    38 **
       
    39 ****************************************************************************/
       
    40 
       
    41 #include "qmlcontact.h"
       
    42 #include <qcontactdetails.h>
       
    43 #include <QtDebug>
       
    44 #include <QStringList>
       
    45 
       
    46 #include <QPixmap>
       
    47 #include <QImage>
       
    48 
       
    49 QT_USE_NAMESPACE
       
    50 QTM_USE_NAMESPACE
       
    51 
       
    52 QmlContact::QmlContact(const QContact& contact, QObject *parent)
       
    53     : QObject(parent), m_contact(contact)
       
    54 {   
       
    55 }
       
    56 
       
    57 QmlContact::QmlContact()
       
    58 {
       
    59 
       
    60 }
       
    61 
       
    62 QmlContact::~QmlContact()
       
    63 {
       
    64 
       
    65 }
       
    66 
       
    67 QContact &QmlContact::contact()
       
    68 {
       
    69     return m_contact;
       
    70 }
       
    71 
       
    72 void QmlContact::setContact(QContact& contact)
       
    73 {
       
    74     m_contact = contact;
       
    75     emit contactChanged(this);
       
    76 }
       
    77 
       
    78 QString QmlContact::name() const
       
    79 {
       
    80     return m_contact.displayLabel();
       
    81 }
       
    82 
       
    83 void QmlContact::setName(QString name)
       
    84 {
       
    85     Q_UNUSED(name);
       
    86     qWarning() << "Not implemented yet";
       
    87     emit nameChanged(this);
       
    88 }
       
    89 
       
    90 QString QmlContact::email() const
       
    91 {
       
    92     QList<QContactDetail> allEmails = m_contact.details(QContactEmailAddress::DefinitionName);
       
    93 
       
    94     QStringList emails;
       
    95     foreach (const QContactDetail& email, allEmails) {
       
    96         emails << email.value(QContactEmailAddress::FieldEmailAddress);
       
    97     }
       
    98     return emails.join(QString::fromLatin1(","));
       
    99 }
       
   100 
       
   101 void QmlContact::setEmail(QString email)
       
   102 {
       
   103     Q_UNUSED(email);
       
   104     qWarning() << "Not implemented yet";
       
   105     emit emailChanged(this);
       
   106 }
       
   107 
       
   108 QString QmlContact::avatar() const
       
   109 {
       
   110     return m_contact.detail<QContactAvatar>().imageUrl().toString();
       
   111 }
       
   112 
       
   113 QPixmap QmlContact::thumbnail() const
       
   114 {
       
   115     return QPixmap::fromImage(m_contact.detail<QContactThumbnail>().thumbnail());
       
   116 }
       
   117 
       
   118 bool QmlContact::hasThumbnail() const
       
   119 {
       
   120     return !thumbnail().isNull();
       
   121 }
       
   122 
       
   123 QString QmlContact::interest() const
       
   124 {
       
   125     // Try a phone number
       
   126     QString det = m_contact.detail(QContactPhoneNumber::DefinitionName).value(QContactPhoneNumber::FieldNumber);
       
   127     if (!det.isEmpty())
       
   128         return det;
       
   129 
       
   130     det = m_contact.detail(QContactEmailAddress::DefinitionName).value(QContactEmailAddress::FieldEmailAddress);
       
   131     if (!det.isEmpty())
       
   132         return det;
       
   133 
       
   134     det = m_contact.detail(QContactOnlineAccount::DefinitionName).value(QContactOnlineAccount::FieldAccountUri);
       
   135     if (!det.isEmpty())
       
   136         return det;
       
   137 
       
   138     // Well, don't know.
       
   139     return QString();
       
   140 }
       
   141 
       
   142 QString QmlContact::interestLabel() const
       
   143 {
       
   144     // Try a phone number
       
   145     QString det = m_contact.detail(QContactPhoneNumber::DefinitionName).value(QContactPhoneNumber::FieldNumber);
       
   146     if (!det.isEmpty())
       
   147         return tr("Phone number:");
       
   148 
       
   149     det = m_contact.detail(QContactEmailAddress::DefinitionName).value(QContactEmailAddress::FieldEmailAddress);
       
   150     if (!det.isEmpty())
       
   151         return tr("Email:");
       
   152 
       
   153     det = m_contact.detail(QContactOnlineAccount::DefinitionName).value(QContactOnlineAccount::FieldAccountUri);
       
   154     if (!det.isEmpty())
       
   155         return QString("%1:").arg(m_contact.detail(QContactOnlineAccount::DefinitionName).value(QContactOnlineAccount::FieldServiceProvider));
       
   156 
       
   157     // Well, don't know.
       
   158     return QString();
       
   159 }
       
   160 
       
   161 QStringList QmlContact::details()
       
   162 {
       
   163     QStringList dets;
       
   164     QList<QContactDetail> ld = m_contact.details();
       
   165     QContactDetail d;
       
   166     foreach(d, ld){
       
   167         dets += d.definitionName();
       
   168     }
       
   169     return dets;
       
   170 }
       
   171 
       
   172 QStringList QmlContact::contexts()
       
   173 {
       
   174     QStringList dets;
       
   175     QList<QContactDetail> ld = m_contact.details();
       
   176     QContactDetail d;
       
   177     foreach(d, ld){
       
   178         dets += d.contexts();
       
   179     }
       
   180     return dets;
       
   181 }
       
   182 
       
   183 QVariantMap QmlContact::values(QString definitionId)
       
   184 {
       
   185     QStringList strlist;
       
   186     QContactDetail detail = m_contact.detail(definitionId);
       
   187 
       
   188     QVariantMap map = detail.variantValues();
       
   189     return map;
       
   190 }
       
   191 
       
   192 #include "moc_qmlcontact.cpp"