private static void ProcessSentEncryptedFile(TLDecryptedMessage message, TLSentEncryptedFile result)
        {
            var media = message.Media;

            if (media != null)
            {
                var oldFile = media.File as TLEncryptedFile;

                media.File = result.EncryptedFile;
                if (oldFile != null)
                {
                    var newFile = media.File as TLEncryptedFile;
                    if (newFile != null)
                    {
                        newFile.FileName = oldFile.FileName;
                        newFile.Duration = oldFile.Duration;

                        var mediaPhoto = media as TLDecryptedMessageMediaPhoto;
                        if (mediaPhoto != null)
                        {
                            var sourceFileName = String.Format("{0}_{1}_{2}.jpg",
                                                               oldFile.Id,
                                                               oldFile.DCId,
                                                               oldFile.AccessHash);

                            var destinationFileName = String.Format("{0}_{1}_{2}.jpg",
                                                                    newFile.Id,
                                                                    newFile.DCId,
                                                                    newFile.AccessHash);

                            try
                            {
                                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                                {
                                    if (store.FileExists(sourceFileName))
                                    {
                                        store.CopyFile(sourceFileName, destinationFileName);
                                        store.DeleteFile(sourceFileName);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }

                        var mediaDocument = media as TLDecryptedMessageMediaDocument45;
                        if (mediaDocument != null)
                        {
                            if (message.IsVoice())
                            {
                                var originalFileName = String.Format("audio{0}_{1}.mp3",
                                                                     oldFile.Id,
                                                                     oldFile.AccessHash);

                                var sourceFileName = String.Format("audio{0}_{1}.wav",
                                                                   oldFile.Id,
                                                                   oldFile.AccessHash);

                                var destinationFileName = String.Format("audio{0}_{1}.wav",
                                                                        newFile.Id,
                                                                        newFile.AccessHash);

                                try
                                {
                                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                                    {
                                        if (store.FileExists(sourceFileName))
                                        {
                                            store.CopyFile(sourceFileName, destinationFileName);
                                            store.DeleteFile(sourceFileName);
                                        }

                                        if (store.FileExists(originalFileName))
                                        {
                                            store.DeleteFile(originalFileName);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                }
                            }
                            else
                            {
                                var sourceFileName = String.Format("{0}_{1}_{2}.{3}",
                                                                   oldFile.Id,
                                                                   oldFile.DCId,
                                                                   oldFile.AccessHash,
                                                                   oldFile.FileExt ?? mediaDocument.FileExt);

                                var destinationFileName = String.Format("{0}_{1}_{2}.{3}",
                                                                        newFile.Id,
                                                                        newFile.DCId,
                                                                        newFile.AccessHash,
                                                                        newFile.FileExt ?? mediaDocument.FileExt);

                                try
                                {
                                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                                    {
                                        if (store.FileExists(sourceFileName))
                                        {
                                            store.CopyFile(sourceFileName, destinationFileName);
                                            store.DeleteFile(sourceFileName);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                }
                            }
                        }

                        var mediaAudio = media as TLDecryptedMessageMediaAudio;
                        if (mediaAudio != null)
                        {
                            var originalFileName = String.Format("audio{0}_{1}.mp3",
                                                                 oldFile.Id,
                                                                 oldFile.AccessHash);

                            var sourceFileName = String.Format("audio{0}_{1}.wav",
                                                               oldFile.Id,
                                                               oldFile.AccessHash);

                            var destinationFileName = String.Format("audio{0}_{1}.wav",
                                                                    newFile.Id,
                                                                    newFile.AccessHash);

                            try
                            {
                                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                                {
                                    if (store.FileExists(sourceFileName))
                                    {
                                        store.CopyFile(sourceFileName, destinationFileName);
                                        store.DeleteFile(sourceFileName);
                                    }

                                    if (store.FileExists(originalFileName))
                                    {
                                        store.DeleteFile(originalFileName);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                }
            }
        }
예제 #2
0
        public void OpenMedia(TLDecryptedMessage message)
#endif
        {
            if (message == null)
            {
                return;
            }
            if (message.Status == MessageStatus.Sending)
            {
                return;
            }
            if (message.Media.UploadingProgress > 0.0 && message.Media.UploadingProgress < 1.0)
            {
                return;
            }

            var mediaPhoto = message.Media as TLDecryptedMessageMediaPhoto;

            if (mediaPhoto != null)
            {
                var location = mediaPhoto.File as TLEncryptedFile;
                if (location != null)
                {
                    var fileName = String.Format("{0}_{1}_{2}.jpg",
                                                 location.Id,
                                                 location.DCId,
                                                 location.AccessHash);

                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!store.FileExists(fileName))
                        {
                            message.Media.IsCanceled          = false;
                            message.Media.DownloadingProgress = 0.01;
                            var fileManager = IoC.Get <IEncryptedFileManager>();
                            fileManager.DownloadFile(location, mediaPhoto);

                            return;
                        }
                    }
                }

                if (!message.Out.Value &&
                    message.TTL != null &&
                    message.TTL.Value > 0 &&
                    message.TTL.Value <= 60.0 &&
                    mediaPhoto.TTLParams == null)
                {
                    message.IsTTLStarted = true;
                    message.DeleteDate   = new TLLong(DateTime.Now.Ticks + message.TTL.Value * TimeSpan.TicksPerSecond);
                    mediaPhoto.TTLParams = new TTLParams
                    {
                        StartTime = DateTime.Now,
                        IsStarted = true,
                        Total     = message.TTL.Value
                    };

                    AddToTTLQueue(message, mediaPhoto.TTLParams,
                                  result =>
                    {
                        DeleteMessage(false, message);
                        SplitGroupedMessages(new List <TLLong> {
                            message.RandomId
                        });
                    });

                    CacheService.SyncDecryptedMessage(message, Chat, r =>
                    {
                        var chat = Chat as TLEncryptedChat;
                        if (chat == null)
                        {
                            return;
                        }

                        var action       = new TLDecryptedMessageActionReadMessages();
                        action.RandomIds = new TLVector <TLLong> {
                            message.RandomId
                        };

                        var decryptedTuple = GetDecryptedServiceMessageAndObject(action, chat, MTProtoService.CurrentUserId, CacheService);
#if DEBUG
                        Execute.BeginOnUIThread(() => Items.Insert(0, decryptedTuple.Item1));
#endif
                        SendEncryptedService(chat, decryptedTuple.Item2, MTProtoService, CacheService,
                                             sentEncryptedMessage =>
                        {
                        });
                    });
                }

                message.Unread = new TLBool(false);
                message.Status = MessageStatus.Read;
                CacheService.SyncDecryptedMessage(message, Chat, r => { });

                if (mediaPhoto.IsCanceled)
                {
                    mediaPhoto.IsCanceled = false;
                    mediaPhoto.NotifyOfPropertyChange(() => mediaPhoto.Photo);
                    mediaPhoto.NotifyOfPropertyChange(() => mediaPhoto.Self);

                    return;
                }

                StateService.CurrentDecryptedPhotoMessage  = message;
                StateService.CurrentDecryptedMediaMessages = message.TTL.Value > 0? new List <TLDecryptedMessage>() :
                                                             UngroupEnumerator(Items)
                                                             .OfType <TLDecryptedMessage>()
                                                             .Where(x => x.TTL.Value == 0 && (x.Media is TLDecryptedMessageMediaPhoto || x.IsVideo()))
                                                             .ToList();

                OpenImageViewer();

                return;
            }

            var mediaGeo = message.Media as TLDecryptedMessageMediaGeoPoint;
            if (mediaGeo != null)
            {
                OpenLocation(message);

                return;
            }

            var mediaVideo = message.Media as TLDecryptedMessageMediaVideo;
            if (mediaVideo != null)
            {
                var fileLocation = mediaVideo.File as TLEncryptedFile;
                if (fileLocation == null)
                {
                    return;
                }

                var fileName = String.Format("{0}_{1}_{2}.mp4",
                                             fileLocation.Id,
                                             fileLocation.DCId,
                                             fileLocation.AccessHash);

                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (store.FileExists(fileName))
                    {
                        var mediaVideo17 = mediaVideo as TLDecryptedMessageMediaVideo17;
                        if (mediaVideo17 != null)
                        {
                            if (!message.Out.Value)
                            {
                                if (message.TTL != null && message.TTL.Value > 0 && message.TTL.Value <= 60.0)
                                {
                                    if (mediaVideo17.TTLParams == null)
                                    {
                                        message.IsTTLStarted   = true;
                                        message.DeleteDate     = new TLLong(DateTime.Now.Ticks + Math.Max(mediaVideo17.Duration.Value + 1, message.TTL.Value) * TimeSpan.TicksPerSecond);
                                        mediaVideo17.TTLParams = new TTLParams
                                        {
                                            StartTime = DateTime.Now,
                                            IsStarted = true,
                                            Total     = message.TTL.Value
                                        };
                                        message.Unread = new TLBool(false);
                                        message.Status = MessageStatus.Read;

                                        AddToTTLQueue(message, mediaVideo17.TTLParams,
                                                      result =>
                                        {
                                            DeleteMessage(false, message);
                                            SplitGroupedMessages(new List <TLLong> {
                                                message.RandomId
                                            });
                                        });

                                        CacheService.SyncDecryptedMessage(message, Chat, r =>
                                        {
                                            var chat = Chat as TLEncryptedChat;
                                            if (chat == null)
                                            {
                                                return;
                                            }

                                            var action       = new TLDecryptedMessageActionReadMessages();
                                            action.RandomIds = new TLVector <TLLong> {
                                                message.RandomId
                                            };

                                            var decryptedTuple = GetDecryptedServiceMessageAndObject(action, chat, MTProtoService.CurrentUserId, CacheService);

#if DEBUG
                                            Execute.BeginOnUIThread(() => Items.Insert(0, decryptedTuple.Item1));
#endif
                                            SendEncryptedService(chat, decryptedTuple.Item2, MTProtoService, CacheService,
                                                                 sentEncryptedMessage =>
                                            {
                                            });
                                        });
                                    }
                                }
                            }
                        }

                        var launcher = new MediaPlayerLauncher();
                        launcher.Location = MediaLocationType.Data;
                        launcher.Media    = new Uri(fileName, UriKind.Relative);
                        launcher.Show();
                    }
                    else
                    {
                        mediaVideo.DownloadingProgress = 0.001;
                        var fileManager = IoC.Get <IEncryptedFileManager>();
                        fileManager.DownloadFile(fileLocation, mediaVideo);
                    }
                }

                return;
            }

            var mediaAudio = message.Media as TLDecryptedMessageMediaAudio;
            if (mediaAudio != null)
            {
                var fileLocation = mediaAudio.File as TLEncryptedFile;
                if (fileLocation == null)
                {
                    return;
                }

                var fileName = String.Format("audio{0}_{1}.wav",
                                             fileLocation.Id,
                                             fileLocation.AccessHash);

                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!store.FileExists(fileName))
                    {
                        mediaAudio.DownloadingProgress = 0.001;
                        var fileManager = IoC.Get <IEncryptedFileManager>();
                        fileManager.DownloadFile(fileLocation, mediaAudio);
                    }
                    else
                    {
                        if (mediaAudio.IsCanceled)
                        {
                            mediaAudio.IsCanceled = false;
                            mediaAudio.NotifyOfPropertyChange(() => mediaAudio.ThumbSelf);

                            return;
                        }

                        var mediaAudio17 = mediaAudio as TLDecryptedMessageMediaAudio17;
                        if (mediaAudio17 != null)
                        {
                            if (!message.Out.Value)
                            {
                                if (message.TTL != null && message.TTL.Value > 0 && message.TTL.Value <= 60.0)
                                {
                                    if (mediaAudio17.TTLParams == null)
                                    {
                                        message.IsTTLStarted   = true;
                                        message.DeleteDate     = new TLLong(DateTime.Now.Ticks + Math.Max(mediaAudio17.Duration.Value + 1, message.TTL.Value) * TimeSpan.TicksPerSecond);
                                        mediaAudio17.TTLParams = new TTLParams
                                        {
                                            StartTime = DateTime.Now,
                                            IsStarted = true,
                                            Total     = message.TTL.Value
                                        };
                                        message.Unread = new TLBool(false);
                                        message.Status = MessageStatus.Read;

                                        CacheService.SyncDecryptedMessage(message, Chat, r =>
                                        {
                                            var chat = Chat as TLEncryptedChat;
                                            if (chat == null)
                                            {
                                                return;
                                            }

                                            var action       = new TLDecryptedMessageActionReadMessages();
                                            action.RandomIds = new TLVector <TLLong> {
                                                message.RandomId
                                            };

                                            var decryptedTuple = GetDecryptedServiceMessageAndObject(action, chat, MTProtoService.CurrentUserId, CacheService);

#if DEBUG
                                            Execute.BeginOnUIThread(() => Items.Insert(0, decryptedTuple.Item1));
#endif
                                            SendEncryptedService(chat, decryptedTuple.Item2, MTProtoService, CacheService,
                                                                 sentEncryptedMessage =>
                                            {
                                            });
                                        });
                                    }
                                }
                            }
                        }
                    }
                }

                return;
            }

            var mediaDocument = message.Media as TLDecryptedMessageMediaDocument;
            if (mediaDocument != null)
            {
                if (message.IsVoice())
                {
                    var fileLocation = mediaDocument.File as TLEncryptedFile;
                    if (fileLocation == null)
                    {
                        return;
                    }

                    var fileName = String.Format("audio{0}_{1}.wav",
                                                 fileLocation.Id,
                                                 fileLocation.AccessHash);

                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!store.FileExists(fileName))
                        {
                            mediaDocument.DownloadingProgress = 0.001;
                            var fileManager = IoC.Get <IEncryptedFileManager>();
                            fileManager.DownloadFile(fileLocation, mediaDocument);
                        }
                        else
                        {
                            if (mediaDocument.IsCanceled)
                            {
                                mediaDocument.IsCanceled = false;
                                mediaDocument.NotifyOfPropertyChange(() => mediaDocument.ThumbSelf);

                                return;
                            }

                            var mediaDocument45 = mediaDocument as TLDecryptedMessageMediaDocument45;
                            if (mediaDocument45 != null)
                            {
                                if (!message.Out.Value)
                                {
                                    var message45 = message as TLDecryptedMessage45;
                                    if (message.TTL != null && message.TTL.Value > 0 && message.TTL.Value <= 60.0)
                                    {
                                        if (mediaDocument45.TTLParams == null)
                                        {
                                            message.IsTTLStarted      = true;
                                            message.DeleteDate        = new TLLong(DateTime.Now.Ticks + Math.Max(mediaDocument45.Duration.Value + 1, message.TTL.Value) * TimeSpan.TicksPerSecond);
                                            mediaDocument45.TTLParams = new TTLParams
                                            {
                                                StartTime = DateTime.Now,
                                                IsStarted = true,
                                                Total     = message.TTL.Value
                                            };
                                            message.Unread = new TLBool(false);
                                            message.Status = MessageStatus.Read;

                                            CacheService.SyncDecryptedMessage(message, Chat, r =>
                                            {
                                                var chat = Chat as TLEncryptedChat;
                                                if (chat == null)
                                                {
                                                    return;
                                                }

                                                var action       = new TLDecryptedMessageActionReadMessages();
                                                action.RandomIds = new TLVector <TLLong> {
                                                    message.RandomId
                                                };

                                                var decryptedTuple = GetDecryptedServiceMessageAndObject(action, chat, MTProtoService.CurrentUserId, CacheService);

#if DEBUG
                                                Execute.BeginOnUIThread(() => Items.Insert(0, decryptedTuple.Item1));
#endif
                                                SendEncryptedService(chat, decryptedTuple.Item2, MTProtoService, CacheService,
                                                                     sentEncryptedMessage =>
                                                {
                                                    if (message45 != null)
                                                    {
                                                        message45.SetListened();
                                                        message45.Media.NotListened = false;
                                                        message45.Media.NotifyOfPropertyChange(() => message45.Media.NotListened);

                                                        CacheService.Commit();
                                                    }
                                                });
                                            });
                                        }
                                    }
                                    else
                                    {
                                        ReadMessageContents(message45);
                                    }
                                }
                            }
                        }
                    }

                    return;
                }
                else if (message.IsVideo())
                {
                    var fileLocation = mediaDocument.File as TLEncryptedFile;
                    if (fileLocation == null)
                    {
                        return;
                    }

                    var fileName = String.Format("{0}_{1}_{2}.mp4",
                                                 fileLocation.Id,
                                                 fileLocation.DCId,
                                                 fileLocation.AccessHash);

                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (store.FileExists(fileName))
                        {
                            var mediaVideo17 = mediaDocument as TLDecryptedMessageMediaDocument45;
                            if (mediaVideo17 != null)
                            {
                                if (!message.Out.Value)
                                {
                                    if (message.TTL != null && message.TTL.Value > 0 && message.TTL.Value <= 60.0)
                                    {
                                        if (mediaVideo17.TTLParams == null)
                                        {
                                            message.IsTTLStarted   = true;
                                            message.DeleteDate     = new TLLong(DateTime.Now.Ticks + Math.Max(mediaVideo17.Duration.Value + 1, message.TTL.Value) * TimeSpan.TicksPerSecond);
                                            mediaVideo17.TTLParams = new TTLParams
                                            {
                                                StartTime = DateTime.Now,
                                                IsStarted = true,
                                                Total     = message.TTL.Value
                                            };
                                            message.Unread = new TLBool(false);
                                            message.Status = MessageStatus.Read;

                                            AddToTTLQueue(message, mediaVideo17.TTLParams,
                                                          result =>
                                            {
                                                DeleteMessage(false, message);
                                                SplitGroupedMessages(new List <TLLong> {
                                                    message.RandomId
                                                });
                                            });

                                            CacheService.SyncDecryptedMessage(message, Chat, r =>
                                            {
                                                var chat = Chat as TLEncryptedChat;
                                                if (chat == null)
                                                {
                                                    return;
                                                }

                                                var action       = new TLDecryptedMessageActionReadMessages();
                                                action.RandomIds = new TLVector <TLLong> {
                                                    message.RandomId
                                                };

                                                var decryptedTuple = GetDecryptedServiceMessageAndObject(action, chat, MTProtoService.CurrentUserId, CacheService);

#if DEBUG
                                                Execute.BeginOnUIThread(() => Items.Insert(0, decryptedTuple.Item1));
#endif
                                                SendEncryptedService(chat, decryptedTuple.Item2, MTProtoService, CacheService,
                                                                     sentEncryptedMessage =>
                                                {
                                                });
                                            });
                                        }
                                    }
                                }
                            }

                            var launcher = new MediaPlayerLauncher();
                            launcher.Location = MediaLocationType.Data;
                            launcher.Media    = new Uri(fileName, UriKind.Relative);
                            launcher.Show();
                        }
                        else
                        {
                            mediaDocument.DownloadingProgress = 0.001;
                            var fileManager = IoC.Get <IEncryptedFileManager>();
                            fileManager.DownloadFile(fileLocation, mediaDocument);
                        }
                    }

                    return;
                }
                else
                {
                    var fileLocation = mediaDocument.File as TLEncryptedFile;
                    if (fileLocation == null)
                    {
                        return;
                    }

                    var fileName = String.Format("{0}_{1}_{2}.{3}",
                                                 fileLocation.Id,
                                                 fileLocation.DCId,
                                                 fileLocation.AccessHash,
                                                 fileLocation.FileExt ?? mediaDocument.FileExt);

                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (store.FileExists(fileName))
                        {
#if WP8
                            StorageFile pdfFile = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);

                            Windows.System.Launcher.LaunchFileAsync(pdfFile);
#endif
                        }
                        else
                        {
                            mediaDocument.DownloadingProgress = 0.001;
                            var fileManager = IoC.Get <IEncryptedFileManager>();
                            fileManager.DownloadFile(fileLocation, mediaDocument);
                        }
                    }
                }

                return;
            }
#if DEBUG
            MessageBox.Show("Tap on media");
#endif
        }