Example #1
0
 public FolderType GetSubFolder <FolderType>(string name)
     where FolderType : IFolder
 {
     // Fetching the folder by name throws an exception if not found, loop and find
     // to prevent exceptions in the log.
     // Don't release the items in RawEnum, they are release manually or handed to WrapFolders.
     NSOutlook.Folder sub = null;
     foreach (NSOutlook.Folder folder in _item.Folders.ComEnum(false))
     {
         if (folder.Name == name)
         {
             sub = folder;
             break; // TODO: does this prevent the rest of the objects from getting released?
         }
         else
         {
             ComRelease.Release(folder);
         }
     }
     if (sub == null)
     {
         return(default(FolderType));
     }
     return(sub.Wrap <FolderType>());
 }
Example #2
0
        public IEnumerable <ItemType> Search(int maxResults)
        {
            string filter = MakeFilter();

            int    count = 0;
            object value = _item.Find(filter);

            while (value != null)
            {
                if (count < maxResults)
                {
                    // Wrap and add if it returns an object. If not, WrapOrDefault will release it
                    ItemType wrapped = Mapping.WrapOrDefault <ItemType>(value);
                    if (wrapped != null)
                    {
                        try
                        {
                            yield return(wrapped);
                        }
                        finally
                        {
                            wrapped.Dispose();
                        }
                    }
                }
                else
                {
                    // Release if not returned. Keep looping to release any others
                    ComRelease.Release(value);
                }
                value = _item.FindNext();
                ++count;
            }
        }
Example #3
0
 public IFolder GetFolderFromID(string folderId)
 {
     using (ComRelease com = new ComRelease())
     {
         NSOutlook.NameSpace nmspace = com.Add(_app.Session);
         NSOutlook.Folder    f       = (NSOutlook.Folder)nmspace.GetFolderFromID(folderId);
         return(Mapping.Wrap <IFolder>(f));
     }
 }
Example #4
0
 public ISyncObject GetSyncObject()
 {
     using (ComRelease com = new ComRelease())
     {
         NSOutlook.NameSpace   session     = com.Add(_app.Session);
         NSOutlook.SyncObjects syncObjects = com.Add(session.SyncObjects);
         return(new SyncObjectWrapper(syncObjects.AppFolders));
     }
 }
 override protected void DoRelease()
 {
     if (MustRelease)
     {
         ComRelease.Release(_item);
     }
     else
     {
     }
 }
Example #6
0
 /// <summary>
 /// Creates a new item
 /// </summary>
 public ItemType Create <ItemType>()
     where ItemType : IItem
 {
     using (ComRelease com = new ComRelease())
     {
         NSOutlook.Items items = com.Add(_item.Items);
         object          item  = items.Add(Mapping.OutlookItemType <ItemType>());
         return(Mapping.Wrap <ItemType>(item));
     }
 }
Example #7
0
 protected NSOutlook.MAPIFolder CloneComObject()
 {
     using (ComRelease com = new ComRelease())
     {
         NSOutlook.Application app     = com.Add(_item.Application);
         NSOutlook.NameSpace   session = com.Add(app.Session);
         NSOutlook.MAPIFolder  folder  = session.GetFolderFromID(EntryID);
         return(folder);
     }
 }
 public void RemoveStore(IStore store)
 {
     using (store)
         using (ComRelease com = new ComRelease())
         {
             NSOutlook.NameSpace  session    = com.Add(_item.Session);
             NSOutlook.MAPIFolder rootFolder = com.Add(((FolderWrapper)store.GetRootFolder()).RawItem);
             session.RemoveStore(rootFolder);
         }
 }
Example #9
0
        public IItem GetItemFromID(string id)
        {
            using (ComRelease com = new ComRelease())
            {
                NSOutlook.NameSpace nmspace = com.Add(_item.Session);

                // Get the item; the wrapper manages it
                object o = nmspace.GetItemFromID(id);
                return(Mapping.Wrap <IItem>(o));
            }
        }
 private RegistryKey OpenBaseKey()
 {
     NSOutlook.NameSpace session = _item.Session;
     try
     {
         return(OutlookRegistryUtils.OpenProfileOutlookKey(session.CurrentProfileName));
     }
     finally
     {
         ComRelease.Release(session);
     }
 }
Example #11
0
 public string GetDefaultFolderId(DefaultFolder folder)
 {
     NSOutlook.MAPIFolder mapiFolder = GetDefaultFolderObj(folder);
     try
     {
         return(mapiFolder?.EntryID);
     }
     finally
     {
         ComRelease.Release(mapiFolder);
     }
 }
Example #12
0
        unsafe public void SetAccountProp(PropTag propTag, object value)
        {
            // Use IOlkAccount to notify while we're running
            // IOlkAccount can only be accessed on main thread
            Logger.Instance.Trace(this, "SetAccountProp1: {0}: {1}", propTag, value);
            ThisAddIn.Instance.InUI(() =>
            {
                Logger.Instance.Trace(this, "SetAccountProp2: {0}: {1}", propTag, value);
                using (ComRelease com = new ComRelease())
                {
                    Logger.Instance.Trace(this, "SetAccountProp3: {0}: {1}", propTag, value);
                    NSOutlook.Account account = com.Add(FindAccountObject());
                    IOlkAccount olk           = com.Add(account.IOlkAccount);
                    Logger.Instance.Trace(this, "SetAccountProp4: {0}: {1}", propTag, value);

                    switch (propTag.type)
                    {
                    case PropType.UNICODE:
                        Logger.Instance.Trace(this, "SetAccountProp5: {0}: {1}", propTag, value);
                        using (MapiAlloc mem = MapiAlloc.FromString((string)value))
                        {
                            ACCT_VARIANT val = new ACCT_VARIANT()
                            {
                                dwType = (uint)PropType.UNICODE,
                                lpszW  = (char *)mem.Ptr
                            };
                            Logger.Instance.Trace(this, "SetAccountProp5A: {0}: {1}", propTag, value);
                            olk.SetProp(propTag, &val);
                            Logger.Instance.Trace(this, "SetAccountProp6: {0}: {1}", propTag, value);
                            olk.SaveChanges(0);
                            Logger.Instance.Trace(this, "SetAccountProp7: {0}: {1}", propTag, value);
                        }
                        break;

                    case PropType.LONG:
                        {
                            Logger.Instance.Trace(this, "SetAccountProp8: {0}: {1}", propTag, value);
                            ACCT_VARIANT val = new ACCT_VARIANT()
                            {
                                dwType = (uint)PropType.LONG,
                                dw     = (uint)value
                            };
                            olk.SetProp(propTag, &val);
                            Logger.Instance.Trace(this, "SetAccountProp9: {0}: {1}", propTag, value);
                            olk.SaveChanges(0);
                            Logger.Instance.Trace(this, "SetAccountPropA: {0}: {1}", propTag, value);
                            break;
                        }
                    }
                    Logger.Instance.Trace(this, "SetAccountPropDone: {0}: {1}", propTag, value);
                }
            }, true);
        }
 public void SetProperty(string property, object value)
 {
     NSOutlook.PropertyAccessor props = GetPropertyAccessor();
     try
     {
         props.SetProperty(property, value);
     }
     finally
     {
         ComRelease.Release(props);
     }
 }
 public void SetProperties(string[] properties, object[] values)
 {
     NSOutlook.PropertyAccessor props = GetPropertyAccessor();
     try
     {
         props.SetProperties(properties, values);
     }
     finally
     {
         ComRelease.Release(props);
     }
 }
Example #15
0
 public void SendReceive(IAccount account)
 {
     // TODO: send/receive specific account
     NSOutlook.NameSpace session = _app.Session;
     try
     {
         session.SendAndReceive(false);
     }
     finally
     {
         ComRelease.Release(session);
     }
 }
        public IStore AddFileStore(string path)
        {
            using (ComRelease com = new ComRelease())
            {
                NSOutlook.NameSpace session = com.Add(_item.Session);

                // Add the store
                session.AddStore(path);

                // And fetch it and wrap
                return(Mapping.Wrap(_item[_item.Count]));
            }
        }
Example #17
0
        public void Save()
        {
            IMAPIFolder imapi = _item.MAPIOBJECT as IMAPIFolder;

            try
            {
                imapi.SaveChanges(SaveChangesFlags.FORCE_SAVE);
            }
            finally
            {
                ComRelease.Release(imapi);
            }
        }
            public Bitmap GetImage(Size imageSize)
            {
                IPictureDisp pict = _commands._item.GetImageMso(_id, imageSize.Width, imageSize.Height);

                try
                {
                    return(ImageUtils.GetBitmapFromHBitmap(new IntPtr(pict.Handle)));
                }
                finally
                {
                    ComRelease.Release(pict);
                }
            }
 public ComStreamWrapper(IStream baseStream, FileAccess access, ComRelease release)
 {
     if (baseStream == null)
     {
         throw new ArgumentNullException("baseStream");
     }
     if (!Enum.IsDefined(typeof(FileAccess), access))
     {
         throw new ArgumentException("access");
     }
     this.FBaseStream = baseStream;
     this.FAccess = access;
     this.FStreamRelease = release;
 }
Example #20
0
 protected override void DoRelease()
 {
     CleanLast();
     if (_enum != null)
     {
         if (_enum is IDisposable)
         {
             ((IDisposable)_enum).Dispose();
         }
         ComRelease.Release(_enum);
         _enum = null;
     }
     base.DoRelease();
 }
Example #21
0
 public ComStreamWrapper(IStream baseStream, FileAccess access, ComRelease release)
 {
     if (baseStream == null)
     {
         throw new ArgumentNullException("baseStream");
     }
     if (!Enum.IsDefined(typeof(FileAccess), access))
     {
         throw new ArgumentException("access");
     }
     this.FBaseStream    = baseStream;
     this.FAccess        = access;
     this.FStreamRelease = release;
 }
Example #22
0
 public IRecipient ResolveRecipient(string name)
 {
     using (ComRelease com = new ComRelease())
     {
         NSOutlook.NameSpace session = com.Add(_app.Session);
         // Add recipient, unlock after Resolve (which might throw) to wrap
         NSOutlook.Recipient recipient = com.Add(session.CreateRecipient(name));
         if (recipient == null)
         {
             return(null);
         }
         IRecipient wrapped = Mapping.Wrap(com.Remove(recipient));
         wrapped.Resolve();
         return(wrapped);
     }
 }
Example #23
0
 public static FolderType Wrap <FolderType>(this NSOutlook.MAPIFolder folder)
     where FolderType : IFolder
 {
     if (typeof(FolderType) == typeof(IFolder))
     {
         return((FolderType)(IFolder) new FolderWrapper(folder));
     }
     else if (typeof(FolderType) == typeof(IAddressBook))
     {
         return((FolderType)(IFolder) new AddressBookWrapper(folder));
     }
     else
     {
         ComRelease.Release(folder);
         throw new NotSupportedException();
     }
 }
        private void CheckAccountsRemoved()
        {
            try
            {
                // Collect all the store ids
                HashSet <string> stores = new HashSet <string>();
                foreach (NSOutlook.Store store in IteratorStoresSafe())
                {
                    try
                    {
                        stores.Add(store.StoreID);
                    }
                    finally
                    {
                        ComRelease.Release(store);
                    }
                }

                // Check if any relevant ones are removed
                List <KeyValuePair <string, AccountWrapper> > removed = new List <KeyValuePair <string, AccountWrapper> >();
                foreach (KeyValuePair <string, AccountWrapper> account in _accountsByStoreId)
                {
                    if (!stores.Contains(account.Key))
                    {
                        Logger.Instance.Trace(this, "Store not found: {0} - {1}", account.Value, account.Key);
                        removed.Add(account);
                    }
                }

                // Process any removed stores
                foreach (KeyValuePair <string, AccountWrapper> remove in removed)
                {
                    Logger.Instance.Debug(this, "Account removed: {0} - {1}", remove.Value, remove.Key);
                    _accountsByStoreId.Remove(remove.Key);
                    if (remove.Value != null)
                    {
                        _accountsBySmtp.Remove(remove.Value.SmtpAddress);
                        OnAccountRemoved(remove.Value);
                    }
                }
            }
            catch (System.Exception e)
            {
                Logger.Instance.Error(this, "Exception in CheckAccountsRemoved: {0}", e);
            }
        }
 public object GetProperty(string property)
 {
     NSOutlook.PropertyAccessor props = GetPropertyAccessor();
     try
     {
         object val = props.GetProperty(property);
         if (val is DBNull)
         {
             return(null);
         }
         return(val);
     }
     catch (System.Exception) { return(null); }
     finally
     {
         ComRelease.Release(props);
     }
 }
        public Type GetUserProperty <Type>(string name)
        {
            using (ComRelease com = new ComRelease())
            {
                NSOutlook.UserProperties userProperties = com.Add(GetUserProperties());
                NSOutlook.UserProperty   prop           = com.Add(userProperties.Find(name, true));
                if (prop == null)
                {
                    return(default(Type));
                }

                if (typeof(Type).IsEnum)
                {
                    return(typeof(Type).GetEnumValues().GetValue(prop.Value));
                }
                return(prop.Value);
            }
        }
Example #27
0
        public void SendReceive(IAccount account, AcaciaTask after)
        {
            if (after != null)
            {
                Watcher.Sync.AddEndTaskOnce(after);
            }

            // TODO: send/receive specific account
            NSOutlook.NameSpace session = _app.Session;
            try
            {
                session.SendAndReceive(false);
            }
            finally
            {
                ComRelease.Release(session);
            }
        }
Example #28
0
        private static IBase CreateWrapper(object o, bool mustRelease)
        {
            // TODO: switch on o.Class
            if (o is NSOutlook.MailItem)
            {
                return(new MailItemWrapper((NSOutlook.MailItem)o));
            }
            if (o is NSOutlook.AppointmentItem)
            {
                return(new AppointmentItemWrapper((NSOutlook.AppointmentItem)o));
            }
            if (o is NSOutlook.Folder)
            {
                return(new FolderWrapper((NSOutlook.Folder)o));
            }
            if (o is NSOutlook.ContactItem)
            {
                return(new ContactItemWrapper((NSOutlook.ContactItem)o));
            }
            if (o is NSOutlook.DistListItem)
            {
                return(new DistributionListWrapper((NSOutlook.DistListItem)o));
            }
            if (o is NSOutlook.NoteItem)
            {
                return(new NoteItemWrapper((NSOutlook.NoteItem)o));
            }
            if (o is NSOutlook.TaskItem)
            {
                return(new TaskItemWrapper((NSOutlook.TaskItem)o));
            }
            if (o is NSOutlook.MeetingItem)
            {
                return(new MeetingItemWrapper((NSOutlook.MeetingItem)o));
            }

            // 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);
        }
Example #29
0
 private NSOutlook.Account FindAccountObject()
 {
     using (ComRelease com = new ComRelease())
     {
         NSOutlook.NameSpace session = com.Add(_item.Session);
         foreach (NSOutlook.Account account in session.Accounts.ComEnum(false))
         {
             if (account.SmtpAddress == this.SmtpAddress)
             {
                 return(account);
             }
             else
             {
                 com.Add(account);
             }
         }
     }
     return(null);
 }
Example #30
0
 public FolderType CreateFolder <FolderType>(string name)
     where FolderType : IFolder
 {
     using (ComRelease com = new ComRelease())
     {
         NSOutlook.Folders folders = com.Add(_item.Folders);
         if (typeof(FolderType) == typeof(IFolder))
         {
             return(folders.Add(name).Wrap <FolderType>());
         }
         else if (typeof(FolderType) == typeof(IAddressBook))
         {
             NSOutlook.MAPIFolder newFolder = folders.Add(name, NSOutlook.OlDefaultFolders.olFolderContacts);
             newFolder.ShowAsOutlookAB = true;
             return(newFolder.Wrap <FolderType>());
         }
         else
         {
             throw new NotSupportedException();
         }
     }
 }
        public void SetUserProperty <Type>(string name, Type value)
        {
            using (ComRelease com = new ComRelease())
            {
                NSOutlook.UserProperties userProperties = com.Add(GetUserProperties());
                NSOutlook.UserProperty   prop           = com.Add(userProperties.Find(name, true));
                if (prop == null)
                {
                    prop = userProperties.Add(name, Mapping.OutlookPropertyType <Type>());
                }

                if (typeof(Type).IsEnum)
                {
                    int i = Array.FindIndex(typeof(Type).GetEnumNames(), n => n.Equals(value.ToString()));
                    prop.Value = typeof(Type).GetEnumValues().GetValue(i);
                }
                else
                {
                    prop.Value = value;
                }
            }
        }