internal override void PublishWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            #region Check for cancellation

            if (CheckForCancelledWorker())
            {
                PublishWorkerResult = PublishWorkerResults.Cancelled;
                facebookClient = null;
                return;
            }

            if (activationState == Protection.ActivationState.Activated)
                AnalyticsHelper.FireEvent("Each share - activation state - activated");
            else if (activationState == Protection.ActivationState.Trial)
                AnalyticsHelper.FireEvent("Each share - activation state - trial");
            else if (activationState == Protection.ActivationState.TrialExpired)
                AnalyticsHelper.FireEvent("Each share - activation state - trial expired");
            else if (activationState == Protection.ActivationState.Unlicensed)
                AnalyticsHelper.FireEvent("Each share - activation state - unlicensed");

            #endregion Check for cancellation

            #region Prepare video. We always do this, even if the user already saved it, because Facebook has formats it prefers

            using (var saveWorker = new SaveWorker(HighlightObject))
            {
                saveWorker.ProgressChanged += saveWorker_ProgressChanged;

            #if RELEASE
                saveWorker.HideOutputFile = true;
            #endif

                saveWorker.OutputFormat = SaveWorker.OutputFormats.Facebook;

                try
                {
                    string tempOutputFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + HighlightObject.InputFileObject.SourceFileInfo.Extension);
                    saveWorker.RunWorkerAsync(tempOutputFilePath);

                    // try connecting while we start the saving
                    if (InitializeFacebookClient() == false)
                    {
                        PublishWorkerResult = PublishWorkerResults.UnableToAuthenticate;
                        saveWorker.CancelAsync();
                        return;
                    }

                    while (saveWorker.IsBusy)
                    { // wait for the scanworker to finish
                        if (CheckForCancelledWorker())
                        {
                            saveWorker.CancelAsync();
                            while (saveWorker.IsBusy) { System.Windows.Forms.Application.DoEvents(); } // DoEvents is required: http://social.msdn.microsoft.com/Forums/en-US/clr/thread/ad9a9a02-8a11-4bb8-b50a-613bcaa46886
                            PublishWorkerResult = PublishWorkerResults.Cancelled;
                            facebookClient = null;
                            return;
                        }

                        System.Threading.Thread.Sleep(500);
                    }
                }
                catch (InvalidOperationException ex)
                {
                    Logger.Error("Error starting SaveWorker! " + ex.ToString());
                }

                if (saveWorker.PublishWorkerResult != PublishWorkerResults.Success ||
                    saveWorker.OutputFileInfo == null)
                {
                    ErrorMessage = saveWorker.ErrorMessage;
                    PublishWorkerResult = PublishWorkerResults.UnableToShare;
                    facebookClient = null;
                    return;
                }

            #endregion Prepare video. We always do this, even if the user already saved it, because Facebook has formats it prefers

                #region Upload to Facebook

                var parameters = new Dictionary<string, object>();
                var bytes = File.ReadAllBytes(saveWorker.OutputFileInfo.FullName);

                parameters["source"] = new FacebookMediaObject { ContentType = "video/mpeg", FileName = "video.mp4" }.SetValue(bytes);
                parameters["title"] = HighlightObject.Title;

                // let's be more subtle and not spam their video.
                // it looks like Facebook is using the description field in the newsfeed instead of the title. lame.
                //parameters["description"] = "Found with Highlight Hunter. Download free for Mac and PC at www.HighlightHunter.com.";
                parameters["description"] = HighlightObject.Title;

                facebookClient.PostAsync("/me/videos", parameters);

                Logger.Info("Posted in background");

                trackFirstShare();

                while (!uploadCompleted)
                {
                    if (CheckForCancelledWorker())
                    {
                        facebookClient.CancelAsync();
                        PublishWorkerResult = PublishWorkerResults.Cancelled;
                        break;
                    }

                    Thread.Sleep(500); // this is on a background thread
                }

                #endregion Upload to Facebook

                #region Cleanup

                try
                {
                    saveWorker.OutputFileInfo.Delete();
                }
                catch (Exception ex)
                {
                    Logger.Error("Unable to delete " + ex);
                }

                #endregion Cleanup
            }

            facebookClient = null;

            // result is determined by facebookClient_PostCompleted
            AnalyticsHelper.FireEvent("Each share - Facebook - result - " + PublishWorkerResult);
        }
Example #2
0
        private void TrimButton_Click(object sender, EventArgs e)
        {
            MainModel.Initialize();

            OpenFileDialog.CheckFileExists = true;
            OpenFileDialog.FileName = "";

            string supportedExtensionsAsSingleString = "";
            foreach (string supportedExtension in MainModel.GetVideoExtensions())
            {
                supportedExtensionsAsSingleString += "*" + supportedExtension + ";";
            }
            if (supportedExtensionsAsSingleString.Length > 0)
                supportedExtensionsAsSingleString = supportedExtensionsAsSingleString.Substring(0, supportedExtensionsAsSingleString.Length - 1);

            OpenFileDialog.Multiselect = false;
            OpenFileDialog.Title = "Tell me which video you'd like to trim.";
            OpenFileDialog.Filter = "Supported Files (" + supportedExtensionsAsSingleString + ")|" + supportedExtensionsAsSingleString + "|All Files (*.*)|*.*";
            OpenFileDialog.ShowDialog();

            if (File.Exists(OpenFileDialog.FileName))
            {
                var inputFileObject = new InputFileObject(new FileInfo(OpenFileDialog.FileName));

                string ss = "0:00:00", end = "0:00:00";
                if (InputBox("Start time", "What is the starting time that you'd like to trim from? (e.g. 0:00:20)", ref ss) == DialogResult.OK)
                {
                    if (InputBox("End time", "What is the ending time that you'd like to trim to? (e.g. 0:00:35)", ref end) == DialogResult.OK)
                    {

                        var saveFileDialog = new SaveFileDialog();
                        saveFileDialog.InitialDirectory = inputFileObject.SourceFileInfo.DirectoryName;
                        saveFileDialog.FileName = inputFileObject.SourceFileInfo.Name + " - " + ss + " to " + end + inputFileObject.SourceFileInfo.Extension;
                        saveFileDialog.Title = "Where should we save the new file?";

                        if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {

                            using (var scanWorker = new ScanWorker(inputFileObject))
                            {

                                scanWorker.SetFramesPerSecond();
                                scanWorker.SetBitrate();

                                var highlightObject = new HighlightObject();
                                highlightObject.InputFileObject = inputFileObject;
                                highlightObject.StartTime = TimeSpan.Parse(ss); // ex. 0:06
                                highlightObject.EndTime = TimeSpan.Parse(end);

                                var saveWorker = new SaveWorker(highlightObject);
                                saveWorker.RunWorkerAsync(saveFileDialog.FileName);

                                while (saveWorker.PublishWorkerResult == PublishWorker.PublishWorkerResults.NotFinished)
                                    Application.DoEvents();

                                if (saveWorker.PublishWorkerResult == PublishWorker.PublishWorkerResults.Success)
                                    while (saveWorker.OutputFileInfo == null)
                                        Application.DoEvents();

                                var files = new List<FileInfo> { saveWorker.OutputFileInfo };
                                MainModel.LaunchExplorerWithFilesSelected(files);

                            }

                        }

                    }

                }
            }

            MainModel.Dispose();
        }