public AttachmentDataHelper(Document document)
 {
     DocumentId = document.DocumentId;
     Filename = document.Filename;
     Streamname = ClientState.Current.Storage.ResolvePhysicalFilename(".", document.StreamName);
     fileInfo = new FileInfo(Streamname);
 }
        internal static void DocumentReceived(Document source)
        {
            try
            {
                var document = source;

                document.Crc32 = document.ContentStream.CalculateCrc32();
                document.Size = document.ContentStream.Length;

                var message = document.Message;
                var root = ClientState.Current.DataService.SelectBy<Document>(new { Crc32 = document.Crc32 });

                if (root == null)
                {
                    document.StreamName = Guid.NewGuid().GetHash(12) + "_" + Path.GetExtension(document.Filename);

                    // Save document to storage engine
                    ClientState.Current.Storage.Write(
                        ".",
                        document.StreamName,
                        document.ContentStream);

                    // New document, save stream and add to search/mailbox
                    ClientState.Current.DataService.Save(document);

                    ClientState.Current.Search.Store(document);
                }
                else
                {
                    // Replace document reference with root to create new version for
                    document = root;
                }

                // Create new version
                var version = new DocumentVersion
                {
                    DocumentId = document.DocumentId.Value,
                    SourceChannelId = document.SourceChannelId,
                    TargetChannelId = document.TargetChannelId,
                    StreamName = document.StreamName,
                    Filename = document.Filename,
                    DateReceived = document.DateReceived,
                    DateSent = document.DateSent
                };

                if (message != null)
                {
                    version.From = message.From;
                    version.To = message.To;
                    version.MessageId = message.MessageId;
                }

                ClientState.Current.DataService.Save(version);
            }
            finally
            {
                source.ContentStream.Dispose();
                source.ContentStream = null;
            }
        }
        public static void Save(Document document)
        {
            var dialog = new System.Windows.Forms.SaveFileDialog();
            dialog.FileName = document.Filename;
            dialog.Filter = String.Format("{0} files (*.{0})|*.{0}|All files (*.*)|*.*",
                Path.GetExtension(document.Filename));

            var result = dialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                string filename = ClientState.Current.Storage.ResolvePhysicalFilename(".", document.StreamName);
                File.Copy(filename, dialog.FileName);
            }
        }
        public static void UploadFile(string filename)
        {
            var document = new Document
            {
                Filename = Path.GetFileName(filename),
                ContentType = ContentType.Attachment,
                DateCreated = DateTime.Now,
                DateSent = DateTime.Now,
                DocumentFolder = Folders.SentItems
            };

            document.StreamName = Guid.NewGuid().GetHash(12) + "_" + Path.GetExtension(document.Filename);
            document.ContentStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);

            EventBroker.Publish(AppEvents.DocumentReceived, document);
        }
 public bool IsSearchVisible(Document document)
 {
     return searchResults.Contains(document.DocumentId ?? 0);
 }
        void UploadDocment_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            ClientStats.LogEvent("Upload document");

            var dialog = new OpenFileDialog();

            dialog.Multiselect = true;

            var result = dialog.ShowDialog();

            if (result == true)
            {
                if (dialog.FileNames != null && dialog.FileNames.Length > 0)
                {
                    foreach (var filename in dialog.FileNames)
                    {
                        var fi = new FileInfo(filename);

                        if (fi.Exists)
                        {
                            var document = new Document
                            {
                                Filename = fi.Name,
                                ContentType = ContentType.Attachment,
                                DateCreated = DateTime.Now,
                                DateSent = DateTime.Now,
                                DocumentFolder = Folders.SentItems
                            };

                            document.StreamName = Guid.NewGuid().GetHash(12) + "_" + Path.GetExtension(document.Filename);
                            document.ContentStream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);

                            EventBroker.Publish(AppEvents.DocumentReceived, document);
                        }
                    }
                }
            }
        }
        public static void Open(Document document)
        {
            string filename = ClientState.Current.Storage.ResolvePhysicalFilename(".", document.StreamName);

            new Process { StartInfo = new ProcessStartInfo(filename) }.Start();
        }
        protected override void ExecuteCore()
        {
            try
            {
                Logger.Debug("Retreiving message {0} from {1}", LogSource.Receive, header, config.DisplayName);

                foreach (var channelMessage in channel.GetMessage(header))
                {
                    var message = new Message
                        {
                            MessageNumber = header.MessageNumber,
                            MessageIdentifier = channelMessage.MessageIdentifier,
                            From = channelMessage.From,
                            ReturnTo = channelMessage.ReturnTo,
                            To = channelMessage.To,
                            CC = channelMessage.CC,
                            BCC = channelMessage.BCC,
                            InReplyTo = channelMessage.InReplyTo,
                            Size = header.Size,
                            Context = channelMessage.Context,
                            ConversationIdentifier = channelMessage.ConversationId,
                            SourceFolder = channelMessage.SourceFolder,
                            Metadata = channelMessage.Metadata,
                            IsRead = channelMessage.IsRead,
                            IsStarred = channelMessage.IsStarred,
                            DateReceived = channelMessage.DateReceived,
                            DateSent = channelMessage.DateSent
                        };

                    message.Context = message.Context != null ? message.Context.Trim() : String.Empty;

                    string bodyText = channelMessage.BodyText.ReadString();
                    string bodyHtml = channelMessage.BodyHtml.ReadString();

                    var access = new ClientMessageAccess(message, bodyText, bodyHtml);

                    if (folder.ToStorageFolder() == Folders.SentItems)
                    {
                        // For sent items we sent the TargetChannelId
                        message.SourceChannelId = 0;
                        message.TargetChannelId = config.ChannelId;
                    }
                    else
                    {
                        // For all other items we sent the SourceChannelId
                        message.SourceChannelId = config.ChannelId;
                    }

                    // Create BodyPreview field from reader
                    message.BodyPreview = access.GetBodyPreview();
                    message.BodyHtmlStreamName = access.WriteBodyHtml();
                    message.BodyTextStreamName = access.WriteBodyText();
                    message.MessageFolder = folder.ToStorageFolder();
                    message.Metadata = header.Metadata;

                    // Fix for messages which have a timestamp in the future
                    if (message.DateReceived > DateTime.Now)
                        message.DateReceived = DateTime.Now;

                    // Set IsNew state for message
                    if (!message.IsRead)
                        message.IsNew = true;

                    // Save message
                    ClientState.Current.DataService.Save(message);

                    // Message received, process attachments
                    foreach (var attachment in channelMessage.Attachments)
                    {
                        var document = new Document
                            {
                                Filename = attachment.Filename,
                                TargetChannelId = attachment.TargetChannelId,
                                DocumentFolder = folder.ToStorageFolder(),
                                ContentType = attachment.ContentType,
                                ContentId = attachment.ContentId,
                                ContentStream = attachment.ContentStream,
                                SourceChannelId = config.ChannelId,
                                DateReceived = message.DateReceived,
                                DateSent = message.DateSent,
                                Message = message
                            };

                        EventBroker.Publish(AppEvents.DocumentReceived, document);

                        if (attachment.ContentStream != null)
                        {
                            attachment.ContentStream.Dispose();
                            attachment.ContentStream = null;
                        }
                    }

                    if (channelMessage.BodyText != null)
                        channelMessage.BodyText.Dispose();

                    if (channelMessage.BodyHtml != null)
                        channelMessage.BodyHtml.Dispose();

                    EventBroker.Publish(AppEvents.MessageStored, message);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("An error occured when trying to download header {0}. Exception = {1}", LogSource.BackgroundTask, header, ex);

                throw;
            }
        }
        void SaveDocuments(Message message, bool isDraft)
        {
            if (isDraft)
            {
                // Delete old attachments from message, will be re-added again below
                SourceMessage.Documents.ToList()
                    .ForEach(d => d.DeleteFrom(SourceMessage));
            }

            // Add attachments to message
            foreach (var file in attachedFiles)
            {
                var document = new Document
                {
                    Message = message,
                    Filename = file.Filename,
                    ContentType = ContentType.Attachment,
                    SourceChannelId = message.SourceChannelId,
                    DateCreated = DateTime.Now,
                    DateSent = DateTime.Now,
                    DocumentFolder = Folders.SentItems
                };

                document.StreamName = Guid.NewGuid().GetHash(12) + "_" + Path.GetExtension(document.Filename);
                document.ContentStream = new FileStream(file.Streamname, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                EventBroker.Publish(AppEvents.DocumentReceived, document);

                // Might needs this when deleting an attachment from a draft message
                file.DocumentId = document.DocumentId;
            }
        }
Exemple #10
0
        void LoadDocuments()
        {
            using (new CodeTimer("VirtualMailBox/Load/Documents"))
            {
                // Load documents
                const string query1 = "select DocumentId, Filename, SourceChannelId, TargetChannelId, ContentType, ContentId, DocumentKey, StreamName, Crc32, Size, IsRead, DocumentFolder, Labels, DateReceived, DateSent, DateCreated, DateModified from Documents";
                const string query2 = "select VersionId, DocumentId, MessageId, Filename, SourceChannelId, TargetChannelId, [From], [To], StreamName, Crc32, Size, DateReceived, DateSent, DateCreated, DateModified from DocumentVersions";

                using (var reader = ClientState.Current.DataService.ExecuteReader(query1))
                {
                    while (reader.Read())
                    {
                        var document = new Document(reader);

                        documents.Add(document);
                        keyedDocuments.Add(document.DocumentId.Value, document);
                    }
                }

                using (var reader = ClientState.Current.DataService.ExecuteReader(query2))
                    while (reader.Read())
                        versions.Add(new DocumentVersion(reader));

                #region Add versions to documents

                foreach (var version in versions)
                {
                    if (keyedDocuments.ContainsKey(version.DocumentId))
                    {
                        keyedDocuments[version.DocumentId].Versions.Add(version);

                        // Add document to message if there is an association
                        if (version.MessageId.HasValue)
                        {
                            if (keyedMessages.ContainsKey(version.MessageId.Value))
                                keyedMessages[version.MessageId.Value].Documents.Add(keyedDocuments[version.DocumentId]);
                        }
                    }
                }

                foreach (var document in documents)
                {
                    if (!String.IsNullOrEmpty(document.Labels))
                    {
                        foreach (var labelstring in document.Labels
                            .Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries)
                            .Where(s => !String.IsNullOrEmpty(s))
                            .ToList())
                        {
                            var label = new Label(labelstring);
                            var key = label.Labelname.ToLower();

                            if (!labels.ContainsKey(key))
                                labels.Add(key, new List<Label>());

                            label.Documents.Add(document);

                            labels[key].Add(label);

                            document.LabelsList.Add(label);
                        }
                    }
                }

                #endregion
            }
        }
Exemple #11
0
        void SelectDocument(Document document)
        {
            State.SelectedDocuments.Replace(new[] { document });

            OnSelectedDocumentChanged();
        }
 public DocumentContentMapper(Document document)
 {
     this.document = document;
 }
Exemple #13
0
        public void Add(Document document)
        {
            Documents.Add(document);

            document.Message = this;
        }
        public static void DocumentReceived(Document source)
        {
            try
            {
                var mailbox = VirtualMailBox.Current;
                var document = source;

                document.Crc32 = document.ContentStream.CalculateCrc32();
                document.Size = document.ContentStream.Length;

                var message = document.Message;

                bool isNewVersion;

                using (mailbox.Documents.ReaderLock)
                {
                    Document document1 = document;

                    isNewVersion = mailbox.Documents.Any(d => d.Crc32 == document1.Crc32);
                }

                if (isNewVersion == false)
                {
                    document.StreamName = Guid.NewGuid().GetHash(12) + "_" + Path.GetExtension(document.Filename);

                    // Save document to storage engine
                    ClientState.Current.Storage.Write(
                        ".",
                        document.StreamName,
                        document.ContentStream);

                    // New document, save stream and add to search/mailbox
                    ClientState.Current.DataService.Save(document);

                    ClientState.Current.Search.Store(document);

                    mailbox.Documents.Add(document);
                }
                else
                {
                    // Replace document reference with instance from mailbox
                    Document document1 = document;

                    document = mailbox.Documents.First(d => d.Crc32 == document1.Crc32);
                }

                // Create new version
                var version = new DocumentVersion
                {
                    DocumentId = document.DocumentId.Value,
                    SourceChannelId = document.SourceChannelId,
                    TargetChannelId = document.TargetChannelId,
                    StreamName = document.StreamName,
                    Filename = document.Filename,
                    DateReceived = document.DateReceived,
                    DateSent = document.DateSent
                };

                mailbox.DocumentVersions.Add(version);

                document.Versions.Add(version);

                ClientState.Current.DataService.Save(version);

                if (message != null)
                {
                    version.From = message.From;
                    version.To = message.To;
                    version.MessageId = message.MessageId;

                    Thread.CurrentThread.ExecuteOnUIThread(() => message.Add(document));
                }
            }
            finally
            {
                source.ContentStream.Dispose();
                source.ContentStream = null;
            }
        }