public static bool ArchiveRootFolderExists(Outlook.NameSpace sessionNamespace, BrugerInfo brugerInfo)
        {
            using (var _dc = new iorunEntities())
            {
                var userSettings = _dc.UserSettings.Where(us => us.US_UserGUID == brugerInfo.ID).ToList().Single();
                var rootArchiveFolderStoreID = userSettings.US_ExchangeStore;
                var rootArchiveFolderEntryID = userSettings.US_ExchangeEntry;

                try
                {
                    var rootArchiveFolder = sessionNamespace.GetFolderFromID(rootArchiveFolderEntryID, rootArchiveFolderStoreID);
                    if (rootArchiveFolder == null)
                    {
                        return false;
                    }

                    return true;
                }
                catch (COMException ce)
                {
                    log.Info(string.Format("ArchiveRootFolderExists: Findes ikke. COM fejl: {0}", ce.Message), ce);

                    return false;
                }
            }
        }
        public static bool ArchiveSelectedItem(bool treatAtSentMail, BrugerInfo currentBruger, Outlook.MailItem mailItem, MailItemInfo mailItemInfo, KundeInfo companyInfo, KontaktInfo contactInfo, TaskInfo taskInfo, Outlook.NameSpace sessionNamespace)
        {
            var fromBruger = treatAtSentMail ? currentBruger : null;
            var toBruger = treatAtSentMail ? currentBruger : null;
            var documentType = treatAtSentMail ? DocumentType.SentMail : DocumentType.ReceivedMail;
            var documentGuid = Guid.NewGuid();

            var companyFolder = TryGetCompanyOutlookFolder(companyInfo.ID, currentBruger, sessionNamespace);

            // Vi har muligvis ikke rettigheder til at tilgå eller oprette mappen
            // Brugeren har fået besked så vi kan returnere
            if (companyFolder == null)
            {
                return false;
            }

            SetIOfficeProperties(documentGuid, mailItem);

            dynamic archivedItemDynamic;

            var itemCopy = (Outlook.MailItem)mailItem.Copy();

            archivedItemDynamic = itemCopy.Move(companyFolder);

            var archivedItem = (Outlook.MailItem)archivedItemDynamic;

            // Efter move får item nyt entryID og storeID
            mailItemInfo = new MailItemInfo(archivedItem);

            var mailItemDocument = new IOfficeOutlookAddIn.Document()
            {
                DOC_Body = mailItemInfo.BodyPlainText,
                DOC_CompanyGUID = companyInfo.ID,
                DOC_ContactGUID = contactInfo != null ? contactInfo.ID : new Nullable<Guid>(),
                DOC_CreatedByGUID = currentBruger.ID,
                DOC_CreatedDate = DateTime.Now,
                DOC_DocumentType = (int)documentType,
                DOC_ExchangeEntryID = mailItemInfo.CurrentMailItemEntryID,
                DOC_ExchangeFolderEntryID = null, // BRUGES IKKE
                DOC_ExchangeFolderStoreID = null, // BRUGES IKKE
                DOC_ExchangeStoreID = mailItemInfo.CurrentMailItemFolderStoreID,
                DOC_FromUserGUID = fromBruger != null ? fromBruger.ID : new Nullable<Guid>(),
                DOC_GUID = documentGuid,
                DOC_IsArchived = 1,
                DOC_IsInstantiated = 1,
                DOC_IsPrivate = 0,
                DOC_MailCreatedDatetime = mailItemInfo.Created,
                DOC_MailReceivedDatetime = mailItemInfo.Received,
                DOC_MailSentDatetime = mailItemInfo.Sent,
                DOC_MailUpdatedDatetime = DateTime.Now, // Hvad er det?
                DOC_MasterDate = DateTime.Now, // Hvad er det?
                DOC_OriginalFileName = string.Empty,
                DOC_ProjectGUID = null,
                DOC_SMTP_From = mailItemInfo.FromEmailAddress,
                DOC_SMTP_FromDisplayName = string.IsNullOrEmpty(mailItemInfo.FromDisplayName) == false ? mailItemInfo.FromDisplayName : treatAtSentMail == true ? fromBruger.Navn : "(Unknown)",
                DOC_SMTP_To = mailItemInfo.RecipientsEmailAddress,
                DOC_SMTP_ToDisplayName = mailItemInfo.RecipientsDisplayName,
                DOC_Subject = mailItemInfo.Subject,
                DOC_TaskGUID = taskInfo != null ? taskInfo.ID : new Nullable<Guid>(),
                DOC_TemplateGUID = null,
                DOC_Title = null, // Bruges vist ikke hvis mail
                DOC_ToUserGUID = toBruger != null ? toBruger.ID : new Nullable<Guid>(),
                DOC_TypeGUID = new Nullable<Guid>(), // BRUGES IKKE
                DOC_UpdatedByGUID = currentBruger.ID,
                DOC_UpdatedDate = DateTime.Now,
                DOC_WebVisible = 0,
                IsThrash = 0
            };

            using (var _dc = new iorunEntities())
            {
                _dc.Document.Add(mailItemDocument);

                _dc.SaveChanges();
            }

            return true;
        }
        public static Outlook.MAPIFolder TryGetCompanyOutlookFolder(Guid companyID, BrugerInfo brugerInfo, Outlook.NameSpace sessionNamespace)
        {
            using (var _dc = new iorunEntities())
            {
                var kunde = _dc.Kunder.Where(k => k.Kunde_GUID == companyID).ToList().Single();
                var userSettings = _dc.UserSettings.Where(us => us.US_UserGUID == brugerInfo.ID).ToList().Single();

                var companyArchiveFolderEntryID = kunde.Kunde_EntryID;
                var companyArchiveFolderStoreID = kunde.Kunde_StoreID;

                var rootArchiveFolderStoreID = userSettings.US_ExchangeStore;
                var rootArchiveFolderEntryID = userSettings.US_ExchangeEntry;

                var companyFolderName = kunde.Kunde_Navn_1.Trim();
                var folderLetter = companyFolderName[0];

                var rootArchiveFolder = sessionNamespace.GetFolderFromID(rootArchiveFolderEntryID, rootArchiveFolderStoreID);
                if (rootArchiveFolder == null)
                {
                    throw new ApplicationException("Failed to get root folder for archive");
                }

                var letterFolder = GetSubFolderByName(rootArchiveFolder, folderLetter.ToString());
                if (letterFolder == null)
                {
                    letterFolder = rootArchiveFolder.Folders.Add(folderLetter.ToString());
                }

                var companyFolder = GetSubFolderByName(letterFolder, companyFolderName);
                if (companyFolder == null)
                {
                    try
                    {
                        companyFolder = letterFolder.Folders.Add(companyFolderName);
                    }
                    catch (COMException comException)
                    {
                        log.Error(comException);

                        MessageBox.Show(string.Format("Du har ikke rettighed til at tilgå eller oprette mappen\n\nFejlen var: {0}", comException.Message), "Adgang nægtet", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }

                return companyFolder;
            }
        }
        public static BrugerInfo GetCurrentBruger()
        {
            if (_brugerInfo == null)
            {
                using (var dc = new iorunEntities())
                {
                    log.InfoFormat("GetCurrentBruger(): Finder bruger med username = {0}. Connection string = {1}", Environment.UserName, dc.Database.Connection.ConnectionString);

                    // Grunden til der bruges WHere og ikke SingleOrDefault() er pga. SQL server compatibility
                    var brugere = dc.Bruger.Where(br => br.User_LoginID == Environment.UserName).ToList();

                    if (brugere.Count == 1)
                    {
                        _brugerInfo = new BrugerInfo(brugere.Single());
                    }
                    else
                    {
                        log.InfoFormat("GetCurrentBruger(): {0} brugere matchede", brugere.Count);
                        // NOP
                    }
                }
            }

            return _brugerInfo;
        }
        public static void EnsureCache()
        {
            if (IOfficeConnectGlobals.KunderCache == null)
            {
                using (var _dc = new iorunEntities())
                {
                    log.Info("Creating DC");

                    log.Info("Ensuring cache - kunder");

                    var kunder = _dc.Kunder.Where(k => k.IsThrash == 0).Select(k => new KundeInfo()
                    {
                        ID = k.Kunde_GUID,
                        Navn = k.Kunde_Navn_1,
                        DomainName = k.Kunde_DomainName,
                        EmailAddress = k.Kunde_EMail
                    }).ToList();

                    log.Info("Ensuring cache - kontakter");

                    var kontakter = _dc.Contact.Where(k => k.IsThrash == 0 && k.Contact_CompanyGUID != null).Select(k => new KontaktInfo
                    {
                        ID = k.Contact_GUID,
                        Navn = k.Contact_Name,
                        KundeID = k.Contact_CompanyGUID.Value,
                        WorkEmail1 = k.Contact_Work_EMail,
                        WorkEmail2 = k.Contact_Work_EMail1,
                        WorkEmail3 = k.Contact_Work_EMail2,
                        PrivateEmail = k.Contact_Private_EMail
                    }).ToList();

                    log.Info("Ensuring cache - bygger");

                    var kunderCache = new Dictionary<Guid, KundeInfo>();
                    var kontakterLookup = new Dictionary<Guid, KontaktInfo>();

                    kunder.ForEach(k => kunderCache.Add(k.ID, k));
                    kontakter.ForEach(k => kontakterLookup.Add(k.ID, k));

                    foreach (var thisKunde in kunder)
                    {
                        thisKunde.NavnTerms = thisKunde.Navn.Split(' ').Where(s => string.IsNullOrEmpty(s) == false).ToList();
                    }

                    foreach (var thisKontakt in kontakter)
                    {
                        thisKontakt.NavnTerms = thisKontakt.Navn.Split(' ').Where(s => string.IsNullOrEmpty(s) == false).ToList();

                        if (kunderCache.ContainsKey(thisKontakt.KundeID))
                        {
                            kunderCache[thisKontakt.KundeID].Kontakter.Add(new KontaktSearchHit() { Kontakt = thisKontakt });
                        }
                    }

                    IOfficeConnectGlobals.KunderCache = kunderCache;
                    IOfficeConnectGlobals.KontakterLookup = kontakterLookup;

                    log.Info("Ensuring cache - done");
                }
            }
        }
        private void NavigateToCompanyFolderForSelectedMail()
        {
            var currentMailItem = GetCurrentMailItem();
            var docGuid = GetDocumentID(currentMailItem);
            var currentBruger = IOfficeConnectGlobals.GetCurrentBruger();

            using (var dc = new iorunEntities())
            {
                var doc = dc.Document.Where(d => d.DOC_GUID == docGuid).ToList().Single();

                var companyOutlookFolder = IOfficeConnectGlobals.TryGetCompanyOutlookFolder(doc.DOC_CompanyGUID.Value, currentBruger, this.OutlookFormRegion.Application.Session);
                if (companyOutlookFolder != null)
                {
                    companyOutlookFolder.Display();
                }
            }
        }
        private void DisplayPanelForArchivedMail(Outlook.MailItem currentMailItem)
        {
            try
            {
                archivePanel.Visible = false;
                archivedInfoPanel.Visible = true;
                archivedInfoPanel.Dock = DockStyle.Fill;

                _showingPanel = MailFormPanel.ArhivedMail;

                var docGuid = GetDocumentID(currentMailItem);

                using (var dc = new iorunEntities())
                {
                    var doc = dc.Document.SingleOrDefault(d => d.DOC_GUID == docGuid);

                    if (doc != null)
                    {
                        var company = dc.Kunder.Where(c => c.Kunde_GUID == doc.DOC_CompanyGUID).ToList().Single();
                        var contact = doc.DOC_ContactGUID != null ? dc.Contact.Where(c => c.Contact_GUID == doc.DOC_ContactGUID).ToList().Single() : null;
                        var task = doc.DOC_TaskGUID != null ? dc.Tasks.Where(t => t.Task_GUID == doc.DOC_TaskGUID).ToList().Single() : null;
                        var archivedBy = doc.DOC_CreatedByGUID != null ? dc.Bruger.Where(b => b.Bruger_GUID == doc.DOC_CreatedByGUID).ToList().Single() : null;

                        archivedCompanyTexttBox.Text = company.Kunde_Navn_1;
                        archivedContactTextBox.Text = contact != null ? contact.Contact_Name : "(None selected)";
                        archivedTaskTextBox.Text = task != null ? task.Task_Caption : "(None selected)";
                        archivedByTextBox.Text = archivedBy != null ? archivedBy.Bruger_Navn : "(Unknown)";
                        archivedWhenTextBox.Text = doc.DOC_CreatedDate.ToString("g");
                    }
                    else
                    {
                        archivedCompanyTexttBox.Text = "(Dokument ikke fundet i database)";
                        archivedContactTextBox.Text = "(Dokument ikke fundet i database)";
                        archivedTaskTextBox.Text = "(Dokument ikke fundet i database)";
                        archivedByTextBox.Text = "(Dokument ikke fundet i database)";
                        archivedWhenTextBox.Text = "(Dokument ikke fundet i database)";
                    }
                }
            }
            catch (Exception archiveExzception)
            {
                var m = string.Format("Fejl ved forsøg på vising af arkivering info: {0}", archiveExzception.Message);

                log.Error(m, archiveExzception);

                MessageBox.Show(m, "Ukendt fejl ved vis arkivering info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        // Af en eller anden årsag fyres dette event to gange. Første gang er Item ok, anden gang kastes der COM exceptions
        void sentItems_ItemAdd(object Item)
        {
            log.Info("sentItems_ItemAdd");

            if (IOfficeConnectGlobals.AutoPopupMails == false)
            {
                log.Info("sentItems_ItemAdd::autopopup slået fra");
                return;
            }

            try
            {
                // Et item blev tilføjet til sent items. Se om det er en mail der ikke er præpareret med arkiveringsinfo.
                var outlookMailItem = Item as Outlook.MailItem;

                if (outlookMailItem != null)
                {
                    // Omgå issue at der fyres to gange
                    var erOk = false;

                    try
                    {
                        var test = outlookMailItem.UserProperties[Constants.MailItemProperty_Archive_CompanyID];

                        erOk = true;
                    }
                    catch
                    {
                        // Åbentbart anden gang den fyrer
                    }

                    if (erOk)
                    {
                        var companyUserProperty = outlookMailItem.UserProperties[Constants.MailItemProperty_Archive_CompanyID];

                        if (companyUserProperty != null)
                        {
                            log.InfoFormat("sentItems_ItemAdd::Item sent: {0}", outlookMailItem.Subject);

                            var contactUserProperty = outlookMailItem.UserProperties[Constants.MailItemProperty_Archive_ContactID];
                            var taskUserProperty = outlookMailItem.UserProperties[Constants.MailItemProperty_Archive_TaskID];
                            var isArchivedProperty = outlookMailItem.UserProperties[Constants.MailItemProperty_IsArchived];

                            var isArchived = isArchivedProperty != null ? bool.Parse(isArchivedProperty.Value) : false;

                            if (isArchived == false)
                            {
                                if (companyUserProperty != null)
                                {
                                    log.InfoFormat("sentItems_ItemAdd::Item {0} skal arkiveres", outlookMailItem.Subject);

                                    var currentBruger = IOfficeConnectGlobals.GetCurrentBruger();
                                    var mailItemInfo = new MailItemInfo(outlookMailItem);

                                    var companyInfo = (KundeInfo)IOfficeConnectGlobals.KunderCache[Guid.Parse(companyUserProperty.Value)];
                                    var contactInfo = contactUserProperty != null ? companyInfo.Kontakter.Single(k => k.Kontakt.ID == Guid.Parse(contactUserProperty.Value)).Kontakt : null;
                                    TaskInfo taskInfo = null;

                                    var sessionNamespace = outlookMailItem.Application.Session;

                                    if (taskUserProperty != null)
                                    {
                                        using (var dc = new iorunEntities())
                                        {
                                            var taskID = (Guid)Guid.Parse(taskUserProperty.Value); // Funky???

                                            taskInfo = new TaskInfo(dc.Tasks.Where(t => t.Task_GUID == taskID).ToList().Single());
                                        }
                                    }

                                    IOfficeConnectGlobals.ArchiveSelectedItem(true, currentBruger, outlookMailItem, mailItemInfo, companyInfo, contactInfo, taskInfo, sessionNamespace);
                                }
                                else
                                {
                                    // Bliver fanget af sent items -> item add event og bruger bliver bedt om at arkivere
                                }
                            }
                            else
                            {
                                // Åbentbart har et arkiveret item fyret
                                log.Warn("Et arkiveret item har fyret i sent items");
                            }
                        }
                        else
                        {
                            // Har sendt mail uden at prækvalificere. Vis mailen.
                            outlookMailItem.Display();
                        }
                    }
                    else
                    {
                        // Var anden gang der blev fyret
                    }
                }
                else
                {
                    // Må være sendt noget andet end en mail (Måske kalender aftale, contact card etc.)
                }
            }
            catch (Exception sentItemsException)
            {
                var m = string.Format("Generel fejl under behandling at sendt post: {0}", sentItemsException.Message);

                log.Error(m, sentItemsException);

                MessageBox.Show(m, "Ukendt fejl sendt post", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private void UpdateTasks(KundeInfo kundeInfo)
        {
            using (var dc = new iorunEntities())
            {
                var tasks = dc.Tasks.Where(t =>
                    t.Task_CompanyGUID == kundeInfo.ID &&
                    t.IsThrash == 0 &&
                    t.Task_IsClosed == 0).
                ToList().
                OrderBy(t => t.Task_Caption).ToList();

                var lvs = new List<ListViewItem>();

                foreach (var thisTask in tasks)
                {
                    var taskInfo = new TaskInfo(thisTask);

                    var lvi = new ListViewItem(taskInfo.Caption);
                    lvi.SubItems.Add(taskInfo.Description);
                    lvi.Tag = taskInfo;

                    lvs.Add(lvi);
                }

                if (tasks.Count > 0)
                {
                    tasksListView.Enabled = true;
                }
                else
                {
                    tasksListView.Enabled = false;

                    tasksListView.Items.Add(new ListViewItem("(No tasks)"));
                }

                tasksListView.Items.AddRange(lvs.ToArray());
            }
        }