private void Refresh_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            TargetFolderBrowserData data = this.DataContext as TargetFolderBrowserData;
            if (data != null)
            {
                using (WaitCursor waitCursor = new WaitCursor(this))
                {
                    e.Handled = true;

                    data.Reload();
                }
            }
        }
        private void GoUp_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            TargetOpenSaveFileData data = this.DataContext as TargetOpenSaveFileData;
            if (data != null)
            {
                using (WaitCursor waitCursor = new WaitCursor(this))
                {
                    e.Handled = true;

                    data.GoUp();
                }
            }
        }
        private void MonitorUnselectAllCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            if (e != null)
            {
                e.Handled = true;
            }

            using (WaitCursor waitCursor = new WaitCursor(this))
            {
                if ((this.kstudioService != null) && this.kstudioService.IsTargetConnected && !this.kstudioService.HasMonitor)
                {
                    foreach (KStudioEventStream eventStream in this.kstudioService.TargetMonitorableStreams)
                    {
                        EventStreamState eventStreamState = eventStream.UserState as EventStreamState;
                        if (eventStreamState != null)
                        {
                            eventStreamState.IsSelectedForTargetMonitorNoCheck = false;
                            eventStreamState.IsEnabledForTargetMonitor = true;
                        }
                    }

                    this.kstudioService.DoTargetMonitorDance(null, false);
                }
            }
        }
        private void MonitorStopCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            if (e != null)
            {
                e.Handled = true;
            }

            using (WaitCursor waitCursor = new WaitCursor(this))
            {
                if (this.kstudioService != null)
                {
                    this.kstudioService.StopMonitor();
                }
            }
        }
        private void PlaybackRemoveAllPausePointsCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            if (e != null)
            {
                e.Handled = true;
            }

            MessageBoxResult result = MessageBoxResult.Yes;
            if (this.notificationService != null)
            {
                result = this.notificationService.ShowMessageBox(Strings.ConfirmPausePointRemoveAll_Text, 
                    MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
            }

            if (result == MessageBoxResult.Yes)
            {
                using (WaitCursor waitCursor = new WaitCursor(this))
                {
                    if (this.kstudioService != null)
                    {
                        TimelinePausePointsCollection pausePoints = this.kstudioService.PlaybackPausePoints;
                        if (pausePoints != null)
                        {
                            pausePoints.RemoveAll();
                        }
                    }
                }
            }
        }
        private void PlaybackEnableAllPausePointsCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            if (e != null)
            {
                e.Handled = true;
            }

            using (WaitCursor waitCursor = new WaitCursor(this))
            {
                if (this.kstudioService != null)
                {
                    TimelinePausePointsCollection pausePoints = this.kstudioService.PlaybackPausePoints;
                    if (pausePoints != null)
                    {
                        pausePoints.EnableAll();
                    }
                }
            }
        }
        private void PlaybackUnselectAllCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            if (e != null)
            {
                e.Handled = true;
            }

            using (WaitCursor waitCursor = new WaitCursor(this))
            {
                if ((this.kstudioService != null) && (this.kstudioService.PlaybackableFileStreams != null))
                {
                    foreach (KStudioEventStream eventStream in this.kstudioService.PlaybackableFileStreams)
                    {
                        EventStreamState eventStreamState = eventStream.UserState as EventStreamState;
                        if ((eventStreamState != null) && (eventStreamState.SelectedLivePlaybackStream != null))
                        {
                            EventStreamState liveEventStreamState = eventStreamState.SelectedLivePlaybackStream.UserState as EventStreamState;
                            liveEventStreamState.IsSelectedForTargetPlaybackNoCheck = false;
                            liveEventStreamState.IsEnabledForTargetPlayback = true;
                        }
                    }

                    this.kstudioService.DoTargetPlaybackDance(null, false);
                }
            }
        }
        private void PlaybackToggleCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            if (e != null)
            {
                e.Handled = true;
            }

            using (WaitCursor waitCursor = new WaitCursor(this))
            {
                if (this.kstudioService != null)
                {
                    if (this.kstudioService.HasPlayback || this.kstudioService.CanPlayback)
                    {
                        if (this.kstudioService.PlaybackState == KStudioPlaybackState.Playing)
                        {
                            this.kstudioService.PausePlayback();
                        }
                        else
                        {
                            this.kstudioService.PlayPlayback();
                        }
                    }
                }
            }
        }
        private void PlaybackCloseCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            this.IsFileTabOpen = false;

            if (e != null)
            {
                e.Handled = true;
            }

            using (WaitCursor waitCursor = new WaitCursor(this))
            {
                if (this.kstudioService != null)
                {
                    this.kstudioService.ClosePlayback();
                }
            }
        }
        private void TargetDisconnectCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            if (e != null)
            {
                e.Handled = true;
            }

            using (WaitCursor waitCursor = new WaitCursor(this))
            {
                if (this.kstudioService != null)
                {
                    string currentFileName = this.kstudioService.PlaybackFilePath;
                    bool readOnly = this.kstudioService.IsPlaybackFileReadOnly;

                    this.kstudioService.DisconnectFromTarget();

                    if (!String.IsNullOrWhiteSpace(currentFileName))
                    {
                        this.suppressAutoSwitch = true;
                        this.kstudioService.OpenLocalPlayback(currentFileName, readOnly);
                    }
                }
            }
        }
        public MainWindow(string[] args)
        {
            InitializeWindow();
            InitializeComponent();

            IServiceProvider serviceProvider = ToolsUIApplication.Instance.RootServiceProvider;
            if (serviceProvider != null)
            {
                this.loggingService = serviceProvider.GetService(typeof(ILoggingService)) as ILoggingService;
                this.notificationService = serviceProvider.GetService(typeof(IUserNotificationService)) as IUserNotificationService;
                this.kstudioService = serviceProvider.GetService(typeof(IKStudioService)) as IKStudioService;
                this.metadataViewService = serviceProvider.GetService(typeof(IMetadataViewService)) as IMetadataViewService;
                this.pluginService = serviceProvider.GetService(typeof(IPluginService)) as IPluginService;
            }

            if (this.kstudioService != null)
            {
                MultiBinding titleBinding = new MultiBinding
                    {
                        Converter = new TitleConverter
                            {
                                NoFileString = Strings.WindowTitle_NoFile,
                                ReadOnlyFileFormat = Strings.WindowTitle_ReadOnlyFileFormat,
                                WritableFileFormat = Strings.WindowTitle_WritableFileFormat,
                            },
                        FallbackValue = ToolsUIApplication.Instance.AppTitle,
                    };

                titleBinding.Bindings.Add(new Binding
                    {
                        Source = this.kstudioService,
                        Path = new PropertyPath("RecordingFilePath"),
                    });
                titleBinding.Bindings.Add(new Binding 
                    { 
                        Source = this.kstudioService,
                        Path = new PropertyPath("PlaybackFilePath"),
                    });
                titleBinding.Bindings.Add(new Binding
                    {
                        Source = this.kstudioService,
                        Path = new PropertyPath("IsPlaybackFileReadOnly"),
                    });

                this.SetBinding(Window.TitleProperty, titleBinding);

                this.kstudioService.PlaybackOpened += (s, e) =>
                    {
                        DebugHelper.AssertUIThread();

                        if (!this.suppressAutoSwitch)
                        {
                            this.SwitchToView("PlaybackableStreamsView");
                        }
                    };

                this.kstudioService.Busy += (s, e) =>
                    {
                        DebugHelper.AssertUIThread();
                        if (e != null)
                        {
                            this.IsEnabled = !e.IsBusy;
                        }
                    };
            }

            if ((args != null) && (this.kstudioService != null))
            {
                if (args.Length > 1)
                {
                    bool connect = false;
                    bool readOnly = false;
                    string fileName = null;

                    for (int i = 1; i < args.Length; ++i)
                    {
                        string arg = args[i];
                        if ((arg.Length > 1) && (arg[0] == '-'))
                        {
                            char ch = Char.ToUpperInvariant(arg[1]);

                            switch (ch)
                            {
                                case 'R':
                                    readOnly = true;
                                    break;
                            }
                        }
                        else
                        {
                            fileName = args[i];
                        }
                    }

                    string error = null;
                    string targetAlias = null;
                    IPAddress targetAddress = null;
                    
                    targetAlias = Environment.MachineName;
                    targetAddress = IPAddress.Loopback;

                    this.Loaded += (s, e) =>
                        {
                            Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    if (error == null)
                                    {
                                        if (connect)
                                        {
                                            if (targetAddress != null)
                                            {
                                                using (WaitCursor waitCursor = new WaitCursor(this))
                                                {
                                                    if (!this.kstudioService.ConnectToTarget(targetAddress, targetAlias))
                                                    {
                                                        fileName = null;
                                                    }
                                                }
                                            }
                                        }

                                        if (fileName != null)
                                        {
                                            error = String.Format(CultureInfo.CurrentCulture, Strings.Arg_Error_InvalidFileName, fileName);

                                            if (fileName.Length > 2)
                                            {
                                                using (WaitCursor waitCursor = new WaitCursor(this))
                                                {
                                                    this.OpenFile(fileName, readOnly);

                                                    error = null;
                                                }
                                            }
                                        }
                                    }

                                    if (error != null)
                                    {
                                        if (this.notificationService != null)
                                        {
                                            this.notificationService.ShowMessageBox(error, MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);
                                        }
                                        else if (this.loggingService != null)
                                        {
                                            this.loggingService.LogLine(error);
                                        }
                                    }

                                    CommandManager.InvalidateRequerySuggested();
                                }
                            ));
                        };
                }
            }
        }
        private void OpenRecentWritableLocalFileCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            if (e != null)
            {
                e.Handled = true;
            }

            using (WaitCursor waitCursor = new WaitCursor(this))
            {
                string filePath = e.Parameter as string;

                this.OpenFile(filePath, false);
            }
        }
        private void OpenRecentReadOnlyLocalFileCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            if (e != null)
            {
                e.Handled = true;
            }

            using (WaitCursor waitCursor = new WaitCursor(this))
            {
                string filePath = e.Parameter as string;

                bool readOnly = true;
                if (e.Parameter != null)
                {
                    bool temp;
                    if (bool.TryParse(e.Parameter as string, out temp))
                    {
                        readOnly = temp;
                    }
                }

                this.OpenFile(filePath, true);
            }
        }
        private void RecordToggleCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            if (e != null)
            {
                e.Handled = true;
            }

            using (WaitCursor waitCursor = new WaitCursor(this))
            {
                if (this.kstudioService != null)
                {
                    if (this.kstudioService.HasRecording)
                    {
                        if (this.kstudioService.RecordingState == KStudioRecordingState.Recording)
                        {
                            this.kstudioService.StopRecording();
                        }
                        else
                        {
                            this.kstudioService.CloseRecording(true);
                        }
                    }
                    else
                    {
                        this.kstudioService.StartRecording();
                    }
                }
            }
        }
        private void ShowMetadata(MetadataView view, MetadataInfo metadataInfo)
        {
            DebugHelper.AssertUIThread();

            this.IsFileTabOpen = false;

            if ((metadataInfo != null) && (view != null))
            {
                MetadataViewContent viewContent = view.ViewContent as MetadataViewContent;
                if (viewContent != null)
                {
                    using (WaitCursor waitCursor = new WaitCursor(this))
                    {
                        viewContent.MetadataInfo = metadataInfo;
                    }
                }
            }
        }