Beispiel #1
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;
            }
        }
Beispiel #2
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>());
 }
 override protected void DoRelease()
 {
     if (MustRelease)
     {
         ComRelease.Release(_item);
     }
     else
     {
     }
 }
Beispiel #4
0
 public string GetDefaultFolderId(DefaultFolder folder)
 {
     NSOutlook.MAPIFolder mapiFolder = GetDefaultFolderObj(folder);
     try
     {
         return(mapiFolder?.EntryID);
     }
     finally
     {
         ComRelease.Release(mapiFolder);
     }
 }
 public void SetProperty(string property, object value)
 {
     NSOutlook.PropertyAccessor props = GetPropertyAccessor();
     try
     {
         props.SetProperty(property, value);
     }
     finally
     {
         ComRelease.Release(props);
     }
 }
 private RegistryKey OpenBaseKey()
 {
     NSOutlook.NameSpace session = _item.Session;
     try
     {
         return(OutlookRegistryUtils.OpenProfileOutlookKey(session.CurrentProfileName));
     }
     finally
     {
         ComRelease.Release(session);
     }
 }
 public void SetProperties(string[] properties, object[] values)
 {
     NSOutlook.PropertyAccessor props = GetPropertyAccessor();
     try
     {
         props.SetProperties(properties, values);
     }
     finally
     {
         ComRelease.Release(props);
     }
 }
Beispiel #8
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 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);
                }
            }
Beispiel #10
0
        public void Save()
        {
            IMAPIFolder imapi = _item.MAPIOBJECT as IMAPIFolder;

            try
            {
                imapi.SaveChanges(SaveChangesFlags.FORCE_SAVE);
            }
            finally
            {
                ComRelease.Release(imapi);
            }
        }
Beispiel #11
0
 protected override void DoRelease()
 {
     CleanLast();
     if (_enum != null)
     {
         if (_enum is IDisposable)
         {
             ((IDisposable)_enum).Dispose();
         }
         ComRelease.Release(_enum);
         _enum = null;
     }
     base.DoRelease();
 }
 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);
     }
 }
Beispiel #15
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);
            }
        }
        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);
        }
 /// <summary>
 /// Event handler for Stores.StoreAdded event.
 /// </summary>
 private void Event_StoreAdded(NSOutlook.Store _)
 {
     try
     {
         // Accessing the store object causes random crashes, simply iterate to find new stores
         Logger.Instance.Trace(this, "StoreAdded");
         foreach (NSOutlook.Store rawStore in IteratorStoresSafe())
         {
             if (!_accountsByStoreId.ContainsKey(rawStore.StoreID))
             {
                 StoreAdded(rawStore);
             }
             else
             {
                 ComRelease.Release(rawStore);
             }
         }
     }
     catch (System.Exception e)
     {
         Logger.Instance.Error(this, "Event_StoreAdded Exception: {0}", e);
     }
 }
Beispiel #18
0
        public AddInWrapper(ThisAddIn thisAddIn)
        {
            this._thisAddIn = thisAddIn;
            this._app       = thisAddIn.Application;

            NSOutlook.NameSpace session = _app.Session;
            try
            {
                this._stores = new StoresWrapper(session.Stores);
            }
            finally
            {
                ComRelease.Release(session);
            }

            // The synchronization context is needed to allow background tasks to jump back to the UI thread.
            // It's null in older versions of .Net, this fixes that
            if (SynchronizationContext.Current == null)
            {
                SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext());
            }
            _sync = SynchronizationContext.Current;
        }