kopano-ol-extension/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/Mapping.cs

163 lines
6.1 KiB
C#
Raw Normal View History

2016-12-21 12:53:16 +01:00
/// Copyright 2016 Kopano b.v.
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License, version 3,
/// as published by the Free Software Foundation.
///
/// 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 Affero General Public License for more details.
///
/// You should have received a copy of the GNU Affero General Public License
/// along with this program.If not, see<http://www.gnu.org/licenses/>.
///
/// Consult LICENSE file for details
using Acacia.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2017-02-08 15:40:48 +01:00
using NSOutlook = Microsoft.Office.Interop.Outlook;
2016-12-21 12:53:16 +01:00
namespace Acacia.Stubs.OutlookWrappers
{
// TODO: a clean up is needed, move as much as possible to Wrappers.cs
2016-12-21 12:53:16 +01:00
public static class Mapping
{
/// <summary>
/// Wraps the specified Outlook object in an IItem;
/// </summary>
/// <param name="o">The Outlook object.</param>
/// <returns>The IItem wrapper, or null if the object could not be wrapped</returns>
2017-02-08 15:40:48 +01:00
private static IBase Wrap(object o, bool mustRelease = true)
2016-12-21 12:53:16 +01:00
{
if (o == null)
return null;
IBase wrapper = CreateWrapper(o, mustRelease);
2016-12-21 12:53:16 +01:00
if (wrapper != null)
wrapper.MustRelease = mustRelease;
return wrapper;
}
private static IBase CreateWrapper(object o, bool mustRelease)
2019-01-10 09:33:32 +01:00
{
try
{
switch ((NSOutlook.OlObjectClass)((dynamic)o).Class)
{
case NSOutlook.OlObjectClass.olAppointment:
return new AppointmentItemWrapper((NSOutlook.AppointmentItem)o);
case NSOutlook.OlObjectClass.olMail:
return new MailItemWrapper((NSOutlook.MailItem)o);
case NSOutlook.OlObjectClass.olFolder:
return new FolderWrapper((NSOutlook.Folder)o);
case NSOutlook.OlObjectClass.olContact:
return new ContactItemWrapper((NSOutlook.ContactItem)o);
case NSOutlook.OlObjectClass.olDistributionList:
return new DistributionListWrapper((NSOutlook.DistListItem)o);
case NSOutlook.OlObjectClass.olNote:
return new NoteItemWrapper((NSOutlook.NoteItem)o);
case NSOutlook.OlObjectClass.olTask:
return new TaskItemWrapper((NSOutlook.TaskItem)o);
}
// TODO: switch on o.Class
if (o is NSOutlook.MeetingItem)
return new MeetingItemWrapper((NSOutlook.MeetingItem)o);
2016-12-21 12:53:16 +01:00
2019-01-10 09:33:32 +01:00
// TODO: support others?
if (mustRelease)
{
// The caller assumes a wrapper will be returned, so any lingering object here will never be released.
ComRelease.Release(o);
}
return null;
}
catch(Exception e)
{
2019-01-10 09:33:32 +01:00
throw e;
}
2016-12-21 12:53:16 +01:00
}
public static Type Wrap<Type>(object o, bool mustRelease = true)
where Type : IBase
{
return (Type)Wrap(o, mustRelease);
}
public static IRecipient Wrap(NSOutlook.Recipient r, bool mustRelease = true)
{
if (r == null)
return null;
RecipientWrapper wrapped = new RecipientWrapper(r);
wrapped.MustRelease = mustRelease;
return wrapped;
}
public static IPicture Wrap(stdole.IPictureDisp obj, bool mustRelease = true)
{
if (obj == null)
return null;
PictureWrapper wrapped = new PictureWrapper(obj);
wrapped.MustRelease = mustRelease;
return wrapped;
}
// TODO: extension methods for this
public static IStore Wrap(NSOutlook.Store obj, bool mustRelease = true)
{
if (obj == null)
return null;
StoreWrapper wrapped = new StoreWrapper(obj);
wrapped.MustRelease = mustRelease;
return wrapped;
}
2017-02-08 15:40:48 +01:00
// TODO: are these not the same now? Differ only on wrong type?
2016-12-21 12:53:16 +01:00
public static Type WrapOrDefault<Type>(object o, bool mustRelease = true)
where Type : IBase
{
IBase wrapped = Wrap(o, mustRelease);
if (wrapped is Type)
return (Type)wrapped;
2017-02-08 15:40:48 +01:00
2017-02-15 11:32:37 +01:00
// Release if required
2016-12-21 12:53:16 +01:00
if (wrapped != null)
wrapped.Dispose();
return default(Type);
}
2017-02-08 15:40:48 +01:00
public static NSOutlook.OlItemType OutlookItemType<ItemType>()
2016-12-21 12:53:16 +01:00
where ItemType: IItem
{
Type type = typeof(ItemType);
if (type == typeof(IContactItem))
2017-02-08 15:40:48 +01:00
return NSOutlook.OlItemType.olContactItem;
2016-12-21 12:53:16 +01:00
if (type == typeof(IDistributionList))
2017-02-08 15:40:48 +01:00
return NSOutlook.OlItemType.olDistributionListItem;
2016-12-21 12:53:16 +01:00
throw new NotImplementedException(); // TODO
}
2017-02-08 15:40:48 +01:00
public static NSOutlook.OlUserPropertyType OutlookPropertyType<PropType>()
2016-12-21 12:53:16 +01:00
{
Type type = typeof(PropType);
if (type == typeof(string))
2017-02-08 15:40:48 +01:00
return NSOutlook.OlUserPropertyType.olText;
2016-12-21 12:53:16 +01:00
if (type == typeof(DateTime))
2017-02-08 15:40:48 +01:00
return NSOutlook.OlUserPropertyType.olDateTime;
2016-12-21 12:53:16 +01:00
if (type == typeof(int))
2017-02-08 15:40:48 +01:00
return NSOutlook.OlUserPropertyType.olInteger;
2016-12-21 12:53:16 +01:00
if (type.IsEnum)
2017-02-08 15:40:48 +01:00
return NSOutlook.OlUserPropertyType.olInteger;
2016-12-21 12:53:16 +01:00
if (type == typeof(string[]))
2017-02-08 15:40:48 +01:00
return NSOutlook.OlUserPropertyType.olKeywords;
2016-12-21 12:53:16 +01:00
throw new NotImplementedException(); // TODO
}
}
}