Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports Microsoft.Office.Interop
Imports DevExpress.Persistent.Validation
Imports DevExpress.Xpo
Imports System.IO
Imports DevExpress.Persistent.BaseImpl
Imports Microsoft.Office.Interop.Outlook
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Editors
Imports System.Windows.Forms
Imports DevExpress.ExpressApp.Utils


Public Class OutlookEmailVC
    Inherits DevExpress.ExpressApp.ViewController
    Dim _WaitFormClass As New WaitFormClass
    Protected selection As ArrayList
    Protected noselect As Boolean = False
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
        selection = New ArrayList()
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of OutlookEmailVC).aEmailIn_To_CRMEstateLeads.Active.SetItemValue("", False)

        If View IsNot Nothing Then
            If View.Id = "OutlookEMailIn_ListView" Then
                Dim _OutlookEmailListView As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
                Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
                Dim _CurrentUser As User = _ocur.Session.GetObjectByKey(Of User)(_ocur.Session.GetKeyValue(SecuritySystem.CurrentUser))
                _OutlookEmailListView.CollectionSource.Criteria.Add("Criteria1", CriteriaOperator.Parse("ReceiverEmailAdress=?", _CurrentUser.Email))
            End If
        End If
        Frame.GetController(Of OutlookEmailVC).aAttachFile.Active.SetItemValue("", False)
        Frame.GetController(Of OutlookEmailVC).aViewAttachedFile.Active.SetItemValue("", False)
        If View.Id = "OutlookEMailIn_ListView" Then
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            'Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of ObjectViewController)().Active.SetItemValue("", False)
            AddHandler View.SelectionChanged, AddressOf view_SelectionChanged
        End If
        If View.Id = "OutlookEMailIn_DetailView" Then
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of ObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "OutlookEMail_OutlookEMailAttachments_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If
        If View.Id = "OutlookEMail_DetailView" Then
            Dim _OutlookEMail As OutlookEMail = TryCast(View.SelectedObjects(0), OutlookEMail)
            Dim _OutlookEMailIn As OutlookEMailIn = TryCast(View.SelectedObjects(0), OutlookEMailIn)
            If _OutlookEMail IsNot Nothing Then
                If _OutlookEMail.EntryID <> "" Then
                    Frame.GetController(Of OutlookEmailVC)().aSendOutlookEmail.Active.SetItemValue("", False)
                End If
            Else
                If _OutlookEMailIn.EntryID <> "" Then
                    Frame.GetController(Of OutlookEmailVC)().aSendOutlookEmail.Active.SetItemValue("", False)
                End If
            End If
        End If
        If View.Id = "OutlookEMail_OutlookEMailAttachments_ListView" Or _
            View.Id = "OutlookEMailIn_OutlookEMailAttachments_ListView" Then
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of OutlookEmailVC).aAttachFile.Active.SetItemValue("", True)
            Frame.GetController(Of OutlookEmailVC).aViewAttachedFile.Active.SetItemValue("", True)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "OutlookEMailIn_ListView" Then
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of ObjectViewController)().Active.SetItemValue("", True)
            RemoveHandler View.SelectionChanged, AddressOf view_SelectionChanged
        End If
        If View.Id = "OutlookEMail_OutlookEMailAttachments_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
        If View.Id = "OutlookEMail_DetailView" Then
            Dim _OutlookEMail As OutlookEMail = TryCast(View.SelectedObjects(0), OutlookEMail)
            Dim _OutlookEMailIn As OutlookEMailIn = TryCast(View.SelectedObjects(0), OutlookEMailIn)
            If _OutlookEMail IsNot Nothing Then
                If _OutlookEMail.EntryID <> "" Then
                    Frame.GetController(Of OutlookEmailVC)().aSendOutlookEmail.Active.SetItemValue("", False)
                End If
            Else
                If _OutlookEMailIn.EntryID <> "" Then
                    Frame.GetController(Of OutlookEmailVC)().aSendOutlookEmail.Active.SetItemValue("", False)
                End If
            End If
        End If
        If View.Id = "OutlookEMail_OutlookEMailAttachments_ListView" Or _
            View.Id = "OutlookEMailIn_OutlookEMailAttachments_ListView" Then
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", True)
        End If
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "OutlookEMailIn_DetailView" Then
            Dim _OutlookEMailIn As OutlookEMailIn = View.SelectedObjects(0)
            For Each _OutlookEMailAttachments As OutlookEMailAttachments In _OutlookEMailIn.OutlookEMailAttachments
                If _OutlookEMailIn.EmailBody.Contains(_OutlookEMailAttachments.DocumentFileData.File.FileName) Then
                    Dim _ActCIDText As String = "cid:"
                    Dim _GenString As String = _OutlookEMailIn.EmailBody.Substring(_OutlookEMailIn.EmailBody.IndexOf(_OutlookEMailAttachments.DocumentFileData.File.FileName))
                    _GenString = _GenString.Remove(_GenString.IndexOf(Chr(34)), _GenString.Length - _GenString.IndexOf(Chr(34)))
                    _ActCIDText += _GenString
                    Dim _fs As New FileStream(Path.GetTempPath() + _OutlookEMailAttachments.DocumentFileData.File.FileName, FileMode.Create)
                    _OutlookEMailAttachments.DocumentFileData.File.SaveToStream(_fs)
                    _fs.Close()

                    _OutlookEMailIn.EmailBody = _OutlookEMailIn.EmailBody.Replace(_ActCIDText, Path.GetTempPath() + _OutlookEMailAttachments.DocumentFileData.File.FileName)
                End If
            Next
        End If
    End Sub
    Private Sub aSendOutlookEmail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSendOutlookEmail.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OutlookEMail As OutlookEMail = TryCast(View.SelectedObjects(0), OutlookEMail)
        _OutlookEMail.SendMail(_OutlookEMail, _ocur, True)

        Dim _CRM_Leads As CRM_Leads = _OutlookEMail.CRM_Leads
        Dim _CRM_Opportunities As CRM_Opportunities = _OutlookEMail.CRM_Opportunities
        Dim _CRMEstate_Leads As CRMEstate_Leads = _OutlookEMail.CRMEstate_Leads

        '--CRM_Leads; CRM_Opportunities ==>CRM_Leads_Activity-be
        If _CRM_Leads IsNot Nothing Then
            Dim _CRM_Leads_Activity As CRM_Leads_Activity = _ocur.CreateObject(Of CRM_Leads_Activity)()
            _CRM_Leads_Activity.CRM_Leads = _CRM_Leads
            _CRM_Leads_Activity.Subject = _OutlookEMail.Subject
            _CRM_Leads_Activity.Email_Oid = _OutlookEMail.Oid.ToString()
            _CRM_Leads_Activity.EventType = eEventType.eEmailOut

            If _CRM_Opportunities IsNot Nothing Then
                _CRM_Leads_Activity.CRM_Opportunities = _CRM_Opportunities
            End If
        End If

        If _CRMEstate_Leads IsNot Nothing Then
            Dim _CRMEstate_Leads_Activity As CRMEstate_Leads_Activity = _ocur.CreateObject(Of CRMEstate_Leads_Activity)()
            _CRMEstate_Leads_Activity.CRMEstate_Leads = _CRMEstate_Leads
            _CRMEstate_Leads_Activity.Email_Oid = _OutlookEMail.Oid.ToString()
            _CRMEstate_Leads_Activity.Subject = _OutlookEMail.Subject
            _CRMEstate_Leads_Activity.EventType = eEventType.eEmailOut
            _CRMEstate_Leads_Activity.HRPerson = _CRMEstate_Leads.HRPerson
        End If

        _ocur.CommitChanges()
        View.Close(False)
    End Sub
    Private Sub aRecieveEmails_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aRecieveEmails.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OutlookApp As New Outlook.Application()
        Dim _OutlookNameSpace As Outlook.NameSpace
        Dim _FolderMail As Outlook.MAPIFolder
        Dim _Mail As Outlook.MailItem
        Dim _OutlookEMailIn As OutlookEMailIn
        Dim _GO As Boolean = False
        Dim _CollectionOfEmailIn As XPCollection = _ocur.CreateCollection(GetType(OutlookEMailIn))
        Dim _Mail_New As OutlookEMailIn
        Dim _FilePath As String
        Dim _fs As FileStream
        Dim i As Long = 0
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _FDObject As Object
        Dim _CurrentUser As User = _ocur.Session.GetObjectByKey(Of User)(_ocur.Session.GetKeyValue(SecuritySystem.CurrentUser))
        Dim _HRPerson As HRPerson = _ocur.FindObject(Of HRPerson)(CriteriaOperator.Parse("User=?", _CurrentUser))



        If _HRPerson Is Nothing Then
            Throw New System.Exception("Nincs a felhasználó a személyzeti törzsben !")
            Exit Sub
        End If

        If _CurrentUser.Email <> "" Then
            _OutlookNameSpace = _OutlookApp.GetNamespace("MAPI")
            _OutlookNameSpace.Logon(, , False, True)

            '_FolderMail = _OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
            _FolderMail = _OutlookNameSpace.GetFolderFromID(_HRPerson.DefaultOutlookFolderEntryID)

            If _FolderMail Is Nothing Then
                Throw New System.Exception("Nem találom a megadott outlook könyvtárat ! ")
                Exit Sub
            End If

            _WaitFormClass.Initwork(1, 1, _FolderMail.Items.Count)
            For Each _FDObject In _FolderMail.Items
                _Mail = TryCast(_FDObject, MailItem)

                If _Mail IsNot Nothing Then
                    _GO = False
                    _WaitFormClass.DoWork()
                    i += 1
                    For Each _OutlookEMailIn In _CollectionOfEmailIn
                        If _Mail.EntryID = _OutlookEMailIn.EntryID Then
                            _GO = True
                            Exit For
                        End If
                    Next
                    If _GO = False Then
                        _Mail_New = New OutlookEMailIn(_uow)
                        'If _Mail.SenderEmailType = "EX" Then
                        '    _Mail_New.SenderEmailAddress = GetExchangeSenderAddress(_Mail, _OutlookNameSpace)
                        'Else
                        '    _Mail_New.SenderEmailAddress = _Mail.SenderEmailAddress
                        'End If
                        _Mail_New.SenderEmailAddress = GetSmtpAddress(_Mail)
                        _Mail_New.ReceivedTime = _Mail.ReceivedTime
                        _Mail_New.SenderName = _Mail.SenderName
                        _Mail_New.EmailSubject = _Mail.Subject
                        _Mail_New.EmailBody = _Mail.HTMLBody
                        _Mail_New.EntryID = _Mail.EntryID
                        _Mail_New.ReceiverEmailAdress = _CurrentUser.Email

                        If _Mail.Attachments.Count > 0 Then
                            For Each _Attachment As Outlook.Attachment In _Mail.Attachments
                                _FilePath = Path.GetTempPath & _Attachment.FileName
                                _Attachment.SaveAsFile(_FilePath)

                                Dim _EmailAttachments As OutlookEMailAttachments
                                Dim _DocumentFileData As DocumentFileData = New DocumentFileData(_uow)

                                _fs = New FileStream(_FilePath, FileMode.Open)

                                _DocumentFileData.File = New FileData(_uow)
                                _DocumentFileData.File.LoadFromStream(Path.GetFileName(_Attachment.FileName), _fs)

                                _EmailAttachments = New OutlookEMailAttachments(_uow)
                                _EmailAttachments.DocumentFileData = _DocumentFileData

                                _Mail_New.OutlookEMailAttachments.Add(_EmailAttachments)

                                _fs.Close()
                                If System.IO.File.Exists(_FilePath) Then
                                    System.IO.File.Delete(_FilePath)
                                End If
                            Next
                        End If
                        '_Mail_New.Save()
                        If i Mod 100 Then _uow.CommitChanges()

                    End If
                End If
            Next
            _uow.CommitChanges()
            _WaitFormClass.FinalWork()
            If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            End If
        Else
            _ocur.Rollback()
            Throw New System.Exception("Nincs beállítva e-mail cím a bejelentkezett felhasználónak!")
        End If
    End Sub
    Private Function GetSmtpAddress(ByVal item As Outlook.MailItem) As String
        Dim sAddress As String
        Dim recip As Outlook.Recipient
        Dim exUser As Outlook.ExchangeUser
        Dim oOutlook As Outlook.Application
        Dim oNS As Outlook.NameSpace

        On Error Resume Next

        oOutlook = New Outlook.Application
        oNS = oOutlook.GetNamespace("MAPI")
        If UCase$(item.SenderEmailType) = "EX" Then
            recip = oNS.CreateRecipient(item.SenderEmailAddress)
            exUser = recip.AddressEntry.GetExchangeUser()
            sAddress = exUser.PrimarySmtpAddress
        Else
            sAddress = item.SenderEmailAddress
        End If
        GetSmtpAddress = sAddress
        oNS = Nothing
        oOutlook = Nothing
    End Function
    Private Sub aEmailIn_To_CRMLeads_Cancel(sender As Object, e As System.EventArgs) Handles aEmailIn_To_CRMLeads.Cancel
        noselect = False
    End Sub
    Private Sub aEmailIn_To_CRMLeads_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aEmailIn_To_CRMLeads.CustomizePopupWindowParams
        If View.SelectedObjects.Count > 0 Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            noselect = True
            Dim _OutlookEmailIn_To_CRMLeads As OutlookEmailIn_To_CRMLeads = _onew.CreateObject(Of OutlookEmailIn_To_CRMLeads)()
            Dim _Mail As Outlook.MailItem = TryCast(View.SelectedObjects(0), Outlook.MailItem)
            Dim _OutlookEMailIn As OutlookEMailIn = TryCast(View.SelectedObjects(0), OutlookEMailIn)
            _OutlookEmailIn_To_CRMLeads.EMail = _OutlookEMailIn.SenderEmailAddress

            e.View = Application.CreateDetailView(_onew, "OutlookEmailIn_To_CRMLeads_DetailView", False, _OutlookEmailIn_To_CRMLeads)
            View.Refresh()
        End If
    End Sub
    Private Sub aEmailIn_To_CRMLeads_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aEmailIn_To_CRMLeads.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _OutlookEmailIn_To_CRMLeads As OutlookEmailIn_To_CRMLeads = TryCast(e.PopupWindow.View.SelectedObjects(0), OutlookEmailIn_To_CRMLeads)
            Dim _OutlookEMailIn As OutlookEMailIn
            noselect = True

            Select Case _OutlookEmailIn_To_CRMLeads.EmailTo
                Case eEmailTo.eCRM
                    Dim _CRM_Leads_POPUP As CRM_Leads
                    Dim _CRM_Opportunities_POPUP As CRM_Opportunities
                    Dim _CRM_Leads_Activity As CRM_Leads_Activity

                    If _OutlookEmailIn_To_CRMLeads.CRM_Leads IsNot Nothing Then
                        _CRM_Leads_POPUP = _ocur.GetObjectByKey(Of CRM_Leads)(_OutlookEmailIn_To_CRMLeads.CRM_Leads.Oid)
                    End If
                    If _OutlookEmailIn_To_CRMLeads.CRM_Opportunities IsNot Nothing Then
                        _CRM_Opportunities_POPUP = _ocur.GetObjectByKey(Of CRM_Opportunities)(_OutlookEmailIn_To_CRMLeads.CRM_Opportunities.Oid)
                        If _CRM_Leads_POPUP Is Nothing Then
                            _CRM_Leads_POPUP = _CRM_Opportunities_POPUP.CRM_Leads
                        End If
                    End If
                    _WaitFormClass.Initwork(1, 1, selection.Count)
                    For Each _OutlookEMailIn In selection
                        _WaitFormClass.DoWork()
                        _OutlookEMailIn.IsReaded = True
                        _OutlookEMailIn.Qualified = True
                        If _OutlookEMailIn.SenderEmailAddress = _OutlookEmailIn_To_CRMLeads.EMail Then
                            If _CRM_Leads_POPUP IsNot Nothing Then
                                _CRM_Leads_Activity = _ocur.CreateObject(Of CRM_Leads_Activity)()

                                If _CRM_Opportunities_POPUP IsNot Nothing Then
                                    _CRM_Leads_Activity.CRM_Leads = _CRM_Leads_POPUP
                                    _CRM_Leads_Activity.CRM_Opportunities = _CRM_Opportunities_POPUP
                                    _CRM_Leads_Activity.Email_Oid = _OutlookEMailIn.Oid.ToString()
                                    _CRM_Leads_Activity.Subject = _OutlookEMailIn.EmailSubject
                                    _CRM_Leads_Activity.EventType = eEventType.eEMailIn
                                Else

                                    _CRM_Leads_Activity.CRM_Leads = _CRM_Leads_POPUP
                                    _CRM_Leads_Activity.CRM_Opportunities = Nothing
                                    _CRM_Leads_Activity.Email_Oid = _OutlookEMailIn.Oid.ToString()
                                    _CRM_Leads_Activity.Subject = _OutlookEMailIn.EmailSubject
                                    _CRM_Leads_Activity.EventType = eEventType.eEMailIn
                                End If

                            End If

                        End If
                    Next
                Case eEmailTo.eRegistry
                    _WaitFormClass.Initwork(1, 1, selection.Count)
                    Dim _OutlookApp As New Outlook.Application()
                    Dim _OutlookNameSpace As Outlook.NameSpace
                    _OutlookNameSpace = _OutlookApp.GetNamespace("MAPI")
                    _OutlookNameSpace.Logon(, , False, True)

                    For Each _OutlookEMailIn In selection
                        _WaitFormClass.DoWork()
                        _OutlookEMailIn.IsReaded = True
                        _OutlookEMailIn.Qualified = True
                        Dim _RegistryHeader As RegistryHeader = _ocur.CreateObject(Of RegistryHeader)()
                        _RegistryHeader.RegistryType = _ocur.GetObjectByKey(Of RegistryType)(_OutlookEmailIn_To_CRMLeads.RegistryType.Oid)
                        _RegistryHeader.DeliveryType = eDeliveryType.eMail
                        _RegistryHeader.CustomersFrom = _ocur.GetObjectByKey(Of CustomersFrom)(_OutlookEmailIn_To_CRMLeads.CustomersFrom.Oid)


                        Dim _Mail As Outlook.MailItem = TryCast(_OutlookNameSpace.GetItemFromID(_OutlookEMailIn.EntryID), Outlook.MailItem)
                        _Mail.SaveAs(Path.GetTempPath() + "EmailMessage.msg")

                        Dim _fs As FileStream
                        _fs = New FileStream(Path.GetTempPath() + "EmailMessage.msg", FileMode.Open)
                        _RegistryHeader.DocumentFile = New FileData(_ocur.Session)
                        _RegistryHeader.DocumentFile.LoadFromStream("EmailMessage.msg", _fs)

                        _fs.Close()
                        If System.IO.File.Exists(Path.GetTempPath() + "EmailMessage.msg") Then
                            System.IO.File.Delete(Path.GetTempPath() + "EmailMessage.msg")
                        End If
                    Next
                Case eEmailTo.eCRMEstate
                    _WaitFormClass.Initwork(1, 1, selection.Count)

                    For Each _OutlookEMailIn In selection
                        _WaitFormClass.DoWork()
                        _OutlookEMailIn.IsReaded = True
                        _OutlookEMailIn.Qualified = True

                        Dim _CRMEstate_Leads As CRMEstate_Leads = _ocur.GetObjectByKey(Of CRMEstate_Leads)(_OutlookEmailIn_To_CRMLeads.CRMEstate_Leads.Oid)
                        If _CRMEstate_Leads Is Nothing Then Throw New System.Exception("Nem lett kiválasztva a felugró ablakban egyetlen ingatlan ügy sem!" + vbNewLine + "A folyamat megszakadt!")

                        Dim _CRMEstate_Leads_Activity As CRMEstate_Leads_Activity = _ocur.CreateObject(Of CRMEstate_Leads_Activity)()

                        _CRMEstate_Leads_Activity.EventType = eEventType.eEMailIn
                        _CRMEstate_Leads_Activity.CRMEstate_Leads = _CRMEstate_Leads
                        _CRMEstate_Leads_Activity.Email_Oid = _OutlookEMailIn.Oid.ToString
                        _CRMEstate_Leads_Activity.HRPerson = _CRMEstate_Leads.HRPerson
                        _CRMEstate_Leads_Activity.Subject = _OutlookEMailIn.EmailSubject

                    Next
            End Select
            noselect = False
            _ocur.CommitChanges()
        Catch ex As System.Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        Finally
            Me._WaitFormClass.FinalWork()
        End Try
    End Sub
    Private Sub view_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim _OutlookEmailIn As OutlookEMailIn
        If View Is Nothing Then Exit Sub
        If noselect = False Then
            selection.Clear()

            For Each _OutlookEmailIn In View.SelectedObjects
                selection.Add(_OutlookEmailIn)
            Next
        End If
    End Sub
    Private Sub aViewAttachedFile_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewAttachedFile.Execute
        Try
            Dim _ListView As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            Dim _OutlookEMailAttachments As OutlookEMailAttachments = TryCast(View.SelectedObjects(0), OutlookEMailAttachments)
            Dim _FilePath As String = Path.Combine(Path.GetTempPath(), _OutlookEMailAttachments.DocumentFileData.File.FileName)
            Dim _Stream As Stream = New FileStream(_FilePath, FileMode.Create)
            _OutlookEMailAttachments.DocumentFileData.File.SaveToStream(_Stream)
            _Stream.Close()
            Process.Start(_FilePath)
        Catch
            Throw New UserFriendlyException("Nem tudom megnyitni a file-t!")
        End Try
    End Sub
    Private Sub aAttachFile_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aAttachFile.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ListView As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
        Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)
        Dim _OutlookEMail As OutlookEMail = TryCast(_PropertyCollectionSource.MasterObject, OutlookEMail)
        Dim _MyStream As Stream = Nothing
        Dim _OpenFileDialog As New OpenFileDialog()

        _OpenFileDialog.InitialDirectory = "c:\"
        _OpenFileDialog.RestoreDirectory = True

        If _OpenFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Try
                _MyStream = _OpenFileDialog.OpenFile()
                If (_MyStream IsNot Nothing) Then

                    Dim _EmailAttachments As OutlookEMailAttachments
                    Dim _DocumentFileData As DocumentFileData = _ocur.CreateObject(Of DocumentFileData)()

                    _DocumentFileData.File = _ocur.CreateObject(Of FileData)()
                    _DocumentFileData.File.LoadFromStream(Path.GetFileName(_OpenFileDialog.FileName), _MyStream)

                    _EmailAttachments = _ocur.CreateObject(Of OutlookEMailAttachments)()
                    _EmailAttachments.DocumentFileData = _DocumentFileData

                    _OutlookEMail.OutlookEMailAttachments.Add(_ocur.GetObject(_EmailAttachments))
                    _OutlookEMail.Save()
                    _ocur.CommitChanges()
                    View.Refresh()
                End If
            Catch
                '--- TESZT MIATT!!! -----
                MessageBox.Show("Nem tudtam csatolni a file-t!")
            Finally

                If (_MyStream IsNot Nothing) Then
                    _MyStream.Close()
                End If
            End Try
        End If
    End Sub

    Private Sub aEmailIn_To_CRMEstateLeads_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aEmailIn_To_CRMEstateLeads.CustomizePopupWindowParams
        Try

        Catch ex As System.Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aEmailIn_To_CRMEstateLeads_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aEmailIn_To_CRMEstateLeads.Execute
        Try

        Catch ex As System.Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
End Class
