Windows Contacts を扱うクラス (ContactHelper)

Windows Vista or lator
(2007.06.06)
#pragma once
 
// WAC.com Class Liblary for Visual C++
// Copyright (C) WAC.com Inc. All rights reserved.
//
// This file is a part of the WAC.com Class Liblary.
// The use and distribution terms for this software are covered by the
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by
// the terms of this license. You must not remove this notice, or
// any other, from this software.
 
#include <vector>
 
#include <objbase.h>                            // for IID_PPV_ARGS
#include <windef.h>                                // for MAX_PATH
//#include <ntdef.h>                            // for ARRAYSIZE
 
#include <icontact.h>
#include <icontactproperties.h>
//#include <smarttypes.h>
 
 
_COM_SMARTPTR_TYPEDEF(IContactManager, __uuidof(IContactManager));
_COM_SMARTPTR_TYPEDEF(IContactCollection, __uuidof(IContactCollection));
_COM_SMARTPTR_TYPEDEF(IContactProperties, __uuidof(IContactProperties));
_COM_SMARTPTR_TYPEDEF(IContact, __uuidof(IContact));
_COM_SMARTPTR_TYPEDEF(IContactPropertyCollection, __uuidof(IContactPropertyCollection));
 
 
/////////////////////////////////////////////////////////////////////////////
// CContactHelper
 
class CContactHelper
{
protected:
    typedef CContactHelper                        thisClass;
 
protected:
    CContactHelper();
    virtual ~CContactHelper();
 
public:
    static IContactManagerPtr GetContactManager(LPCWSTR szAppName, LPCWSTR szAppVersion)
    {
        HRESULT hr = S_OK;
 
        IContactManagerPtr spContactManager;
        hr = spContactManager.CreateInstance(CLSID_ContactManager);
        if (FAILED(hr))
        {
            return NULL;
        }
 
        hr = spContactManager->Initialize(szAppName, szAppVersion);
        if (FAILED(hr))
        {
            return NULL;
        }
 
        return spContactManager;
    }
    static HRESULT EnumContacts(IContactManagerPtr spContactManager, std::vector<IContactPtr>& spContacts)
    {
        if (spContactManager == NULL)
        {
            return E_FAIL;
        }
 
        HRESULT hr = S_OK;
 
        spContacts.clear();
 
        IContactCollectionPtr spContactCollection;
        hr = spContactManager->GetContactCollection(&spContactCollection);
        if (FAILED(hr))
        {
            return hr;
        }
 
        while (S_FALSE != spContactCollection->Next())
        {
            IContactPtr spContact;
            hr = spContactCollection->GetCurrent(&spContact);
            if (FAILED(hr))
            {
                ATLASSERT(0);
                continue;
            }
 
            spContacts.push_back(spContact);
        }
 
        return S_OK;
    }
    static HRESULT EnumContactIDs(IContactManagerPtr spContactManager, std::vector<CStringW>& strContactIDs)
    {
        if (spContactManager == NULL)
        {
            ATLASSERT(0);
            return E_FAIL;
        }
 
        HRESULT hr = S_OK;
 
        strContactIDs.clear();
 
        IContactCollectionPtr spContactCollection;
        hr = spContactManager->GetContactCollection(&spContactCollection);
        if (FAILED(hr))
        {
            ATLASSERT(0);
            return hr;
        }
 
        while (S_FALSE != spContactCollection->Next())
        {
            IContactPtr spContact;
            hr = spContactCollection->GetCurrent(&spContact);
            if (FAILED(hr))
            {
                ATLASSERT(0);
                continue;
            }
 
            // ID
            WCHAR szContactID[1024] = { 0 };
            hr = spContact->GetContactID(szContactID, ARRAYSIZE(szContactID), NULL);
            if (FAILED(hr))
            {
                ATLASSERT(0);
                continue;
            }
 
            strContactIDs.push_back(szContactID);
        }
 
        return S_OK;
    }
    static HRESULT EnumContactPaths(IContactManagerPtr spContactManager, std::vector<CStringW>& strPaths)
    {
        if (spContactManager == NULL)
        {
            return E_FAIL;
        }
 
        HRESULT hr = S_OK;
 
        strPaths.clear();
 
        IContactCollectionPtr spContactCollection;
        hr = spContactManager->GetContactCollection(&spContactCollection);
        if (FAILED(hr))
        {
            return hr;
        }
 
        while (S_FALSE != spContactCollection->Next())
        {
            IContactPtr spContact;
            hr = spContactCollection->GetCurrent(&spContact);
            if (FAILED(hr))
            {
                ATLASSERT(0);
                continue;
            }
 
            // パス
            WCHAR szPath[_MAX_PATH] = { 0 };
            hr = spContact->GetContactID(szPath, ARRAYSIZE(szPath), NULL);
            if (FAILED(hr))
            {
                ATLASSERT(0);
                continue;
            }
 
            strPaths.push_back(szPath);
        }
 
        return S_OK;
    }
};
    HRESULT TestContacts()
    {
        HRESULT hr = S_OK;
 
        WCHAR szAppName[] = L"WAC.com TestContacts";
        WCHAR szAppVersion[] = L"1.0";
        IContactManagerPtr spContactManager = CContactHelper::GetContactManager(szAppName, szAppVersion);
        if (spContactManager == NULL)
        {
            return E_FAIL;
        }
 
#if 0
        std::vector<IContactPtr> spContacts;
        hr = CContactHelper::EnumContacts(spContactManager, spContacts);
        if (FAILED(hr))
        {
            return hr;
        }
#else
        std::vector<CStringW> strContactIDs;
        hr = CContactHelper::EnumContactIDs(spContactManager,  strContactIDs);
        if (FAILED(hr))
        {
            return hr;
        }
 
        std::vector<IContactPtr> spContacts;
        for (int i = 0; i < (int)strContactIDs.size(); i++)
        {
            const CStringW& strContactID = strContactIDs[i];
 
            IContactPtr spContact;
            hr = spContactManager->Load(strContactID, &spContact);
            if (FAILED(hr))
            {
                ATLASSERT(0);
                continue;
            }
 
            spContacts.push_back(spContact);
        }
#endif
 
        for (int i = 0; i < (int)spContacts.size(); i++)
        {
            IContactPtr spContact = spContacts[i];
            if (spContact == NULL)
            {
                ATLASSERT(0);
                continue;
            }
 
            IContactPropertiesPtr spContactProperties = spContact;
            if (spContactProperties == NULL)
            {
                ATLASSERT(0);
                continue;
            }
 
            // 表示名
            WCHAR szFormattedName[MAX_PATH] = { 0 };
            hr = spContactProperties->GetString(
                CONTACTPROP_PUB_L1_NAMECOLLECTION CONTACTPROP_PUB_L2_NAME L"[1]" CONTACTPROP_PUB_L3_FORMATTEDNAME,
                CGD_DEFAULT, szFormattedName, ARRAYSIZE(szFormattedName), NULL);
            if (FAILED(hr))
            {
//                ATLASSERT(0);
            }
 
            // 誕生日
            FILETIME ftDateTime = { 0 };
            hr = spContactProperties->GetDate(
                CONTACTPROP_PUB_L1_DATECOLLECTION CONTACTPROP_PUB_L2_DATE L"[1]" CONTACTPROP_PUB_L3_VALUE,
                CGD_DEFAULT, &ftDateTime);
            if (FAILED(hr))
            {
//                ATLASSERT(0);
            }
 
            // タイトル (例: Dr.)
            WCHAR szTitle[MAX_PATH] = { 0 };
            hr = spContactProperties->GetString(
                CONTACTPROP_PUB_L1_NAMECOLLECTION CONTACTPROP_PUB_L2_NAME L"[1]" CONTACTPROP_PUB_L3_TITLE,
                CGD_DEFAULT, szTitle, ARRAYSIZE(szTitle), NULL);
            if (FAILED(hr))
            {
//                ATLASSERT(0);
            }
 
            // ニックネーム
            WCHAR szNickName[MAX_PATH] = { 0 };
            hr = spContactProperties->GetString(
                CONTACTPROP_PUB_L1_NAMECOLLECTION CONTACTPROP_PUB_L2_NAME L"[1]" CONTACTPROP_PUB_L3_NICKNAME,
                CGD_DEFAULT, szNickName, ARRAYSIZE(szNickName), NULL);
            if (FAILED(hr))
            {
//                ATLASSERT(0);
            }
        }
 
        return S_OK;
    }
一覧に戻る
© 2003 WAC.com All Right Reserved.