/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
namespace Tizen.Pims.Contacts
{
using ContactsViews;
///
/// Delegate for getting a record parsed from a vCard file
///
/// The contacts record
///
public delegate bool ParseCallback(ContactsRecord record);
///
/// A class for parsing and making vCards.
///
///
/// It's based on the vCard v3.0 specification
///
public static class ContactsVcard
{
///
/// Retrieves the vCard stream from a contacts record.
///
/// The contacts record
///
/// The vCard stream.
///
/// http://tizen.org/privilege/contact.read
/// Thrown when one of the arguments provided to a method is not valid
/// Thrown when failed due to out of memory
public static string Compose(ContactsRecord record)
{
int error = 0;
string stream = null;
if (record.Uri.Equals(Person.Uri))
{
error = Interop.Vcard.ContactsVcardMakeFromPerson(record._recordHandle, out stream);
}
else if (record.Uri.Equals(Contact.Uri))
{
error = Interop.Vcard.ContactsVcardMakeFromContact(record._recordHandle, out stream);
}
else if (record.Uri.Equals(MyProfile.Uri))
{
error = Interop.Vcard.ContactsVcardMakeFromMyProfile(record._recordHandle, out stream);
}
else
{
throw new ArgumentException("Invalid Parameters Provided");
}
if ((int)ContactsError.None != error)
{
Log.Error(Globals.LogTag, "Compose Failed with error " + error);
throw ContactsErrorFactory.CheckAndCreateException(error);
}
return stream;
}
///
/// Retrieves all contacts with a contacts list from a vCard stream
///
/// The vCard stream
///
/// The contacts list
///
/// Thrown when one of the arguments provided to a method is not valid
/// Thrown when failed due to out of memory
public static ContactsList Parse(string stream)
{
IntPtr listHandle;
int error = Interop.Vcard.ContactsVcardParseToContacts(stream, out listHandle);
if ((int)ContactsError.None != error)
{
Log.Error(Globals.LogTag, "Parse Failed with error " + error);
throw ContactsErrorFactory.CheckAndCreateException(error);
}
return new ContactsList(listHandle);
}
///
/// Retrieves all contacts with a record from a vCard file.
///
/// The file path of vCard stream file
/// The callback function to invoke
/// Thrown when method failed due to invalid operation
/// Thrown when one of the arguments provided to a method is not valid
/// Thrown when failed due to out of memory
public static void ParseForEach(string path, ParseCallback callback)
{
Interop.Vcard.ContactsVcardParseCallback cb = (IntPtr handle, IntPtr data) =>
{
return callback(new ContactsRecord(handle, true));
};
int error = Interop.Vcard.ContactsVcardParseToContactForeach(path, cb, IntPtr.Zero);
if ((int)ContactsError.None != error)
{
Log.Error(Globals.LogTag, "ParseForEach Failed with error " + error);
throw ContactsErrorFactory.CheckAndCreateException(error);
}
}
}
}