Exemplo n.º 1
0
        private void SynchronizerContentUpdated(SyncResult result)
        {
            // no need to continue checking if this is an Untitled-document
            if (!Document.IsDraft && result.Job.SyncDocument.ResourceId == Document.ResourceId && result.ContentUpdated)
            {
                // in case for some reason the content gets "updated" on google's side
                // and the content hasn't actually changed, let's just stop here
                if (result.Document.Content == TxtNocContentNow && result.Document.Title == Document.Title)
                {
                    Debug.WriteLine("SynchronizerContentUpdated and incoming document is identical");
                    Document.ETag = result.Document.ETag;
                    return;
                }

                // also, if we're currently writing something (Content has changed),
                // let's not merge changes yet to avoid deletion of our own work
                if (ContentHasChanged)
                {
                    return;
                }

                // let's update the fields for this document
                Document = result.Document;

                // let's get the merged text based on text at last sync
                //Debug.WriteLine("SyncContentUpdated > From: TxtNocContentAtLastSync:\t" + TxtNocContentAtLastSync);
                Debug.WriteLine("SyncContentUpdated > From: TxtNocContentAtLastSuccessfulSave:\t" + TxtNocContentAtLastSuccessfulSave);
                Debug.WriteLine("SyncContentUpdated >   To: Incoming:\t\t\t\t\t" + Document.Content);
                Debug.WriteLine("SyncContentUpdated > TxtNocContentAtLastSave:\t" + TxtNocContentAtLastSuccessfulSave);
                Debug.WriteLine("SyncContentUpdated > TxtNocContentNow:\t\t\t" + TxtNocContentNow);
                Document.Content = Tools.MergeText(TxtNocContentAtLastSuccessfulSave, TxtNocContentNow, Document.Content);
                Debug.WriteLine("SyncContentUpdated > Merged:\t\t\t\t\t" + Document.Content);

                // let's find out if we've changed the editor text since last sync
                var editorContentChangedSinceLastSync = TxtNocContentAtLastSync != TxtNocContentNow;

                // let's reset the sync situation
                Debug.WriteLine("SyncContentUpdated > TxtNocContentAtLastSync/Save = " + Document.Content);
                TxtNocContentAtLastSync = Document.Content;
                TxtNocContentAtLastSuccessfulSave = Document.Content;
                ContentHasChanged = false;
                _tabTitleModifiedBecauseContentHasChanged = false;

                // this event will always be called from a separate thread so we'll have to use Invoke to modify Noc's properties
                // (can only make changes to WinForm controls from the master thread)
                if (IsHandleCreated)
                {
                    // let's update the tab's title and the RichTextBox content
                    NocEventHandler setTabTitle = SetTabTitleText;
                    Invoke(setTabTitle, result.Document.Title);

                    NocEventHandler setEditorContent = SetEditorContent;
                    Invoke(setEditorContent, Document.Content);
                }
                else
                {
                    Trace.WriteLine(DateTime.Now + " - Noc: Handle not created while trying to invoke content changes for: " + result.Document.Title);
                    return;
                }

                // in case we've change the text since last sync (while syncing for example),
                // let's save this document again
                if (editorContentChangedSinceLastSync && !NocsService.Working && !_bgWorkerSaveNoc.IsBusy)
                {
                    Debug.WriteLine("ContentUpdated: editor content changed since last sync, saving current content");
                    
                    // let's run the backgroundWorker for saving this document
                    var folderId = string.Empty;
                    if (Document.ParentFolders.Count > 0)
                    {
                        folderId = Document.ParentFolders[0];
                    }
                    var args = new object[] { folderId, Document.Title, false, false };
                    _bgWorkerSaveNoc.RunWorkerAsync(args);
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Will be called whenever an autofetch for all documents finishes.
 /// </summary>
 private void AutoFetchAllEntriesFinished(SyncResult result)
 {
     // we'll have to check if any of the open tabs were deleted
     RemoveTabsNotFoundInAllDocuments();
 }
Exemplo n.º 3
0
 /// <summary>
 /// Will fire if when the Synchronizer finishes executing a autoFetchAll-job.
 /// When event fires, we'll have to refresh the listbox of documents incase some document was deleted/renamed.
 /// </summary>
 /// <param name="result"></param>
 private void SynchronizerAutoFetchAllEntriesFinished(SyncResult result)
 {
     // refresh the listbox with current item titles
     BrowseRefreshEntriesEventHandler refreshListBoxDelegate = RefreshListBoxItemsFromSelectedFolder;
     Invoke(refreshListBoxDelegate, true);
 }
Exemplo n.º 4
0
        private void SynchronizerErrorWhileSyncing(SyncResult result)
        {
            if (!string.IsNullOrEmpty(result.Error))
            {
                Trace.WriteLine(string.Format("{0} - Main: error while syncing - type: {1} - message: {2}", DateTime.Now,
                                              result.Job.Type, result.Error));

                var error = result.Error.ToLowerInvariant();

                if (error.Contains("authorization") || error.Contains("token expired"))
                {
                    TokenExpiredWhileSaving();
                }
                else if (error.Contains("internet down") ||
                         error.Contains("connection timed out") ||
                         error.Contains("remote name could not be resolved") ||
                         error.Contains("unable to connect to the remote server"))
                {
                    MessageBox.Show(new Form { TopMost = true },
                                    "Can't connect to internet. Make sure you're online and try again.",
                                    "Can't connect to internet",
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else if (error.Contains("resource not found"))
                {
                    MessageBox.Show(new Form { TopMost = true },
                                    "A resource couldn't be found while attempting an update. Please investigate nocs.log and report any errors at http://nocs.googlecode.com/. Thanks!",
                                    "Resource not found",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show(new Form { TopMost = true },
                                    "Error occurred while syncing, please close Nocs, inspect nocs.log and report found errors to http://nocs.googlecode.com/",
                                    "Error while syncing",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            Trace.Flush();
        }
Exemplo n.º 5
0
        public Document SaveDocument(Document document)
        {
            try
            {
                return NocsService.SaveDocument(document);
            }
            catch (Exception ex)
            {
                var result = new SyncResult
                {
                    Job = new SyncJob { Title = document.Title, Type = SyncJobType.Save },
                    ContentUpdated = false,
                    Error = ex.Message,
                    Document = document
                };

                // let's notify listeners
                ErrorWhileSyncing(result);
                return null;
            }
        }
Exemplo n.º 6
0
        public Document CreateNewDocument(string folderId, string title, string content, bool createDefaultDirectory)
        {
            try
            {
                return NocsService.CreateNewDocument(folderId, title, content, createDefaultDirectory);
            }
            catch (Exception ex)
            {
                var result = new SyncResult
                {
                    Job = new SyncJob { Title = title, Type = SyncJobType.Save },
                    ContentUpdated = false,
                    Error = ex.Message,
                    Document = new Document { Content = content, Title = title }
                };

                // let's notify listeners
                ErrorWhileSyncing(result);
                return null;
            }
        }
Exemplo n.º 7
0
        private static void BgWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            if (e.Argument == null)
            {
                Debug.WriteLine("Synchronizer: BgWorkerDoWork - given syncjob (e.Argument) is null");
                return;
            }

            SyncResult result = null;
            var job = (SyncJob)e.Argument;

            try
            {
                switch (job.Type)
                {
                    case SyncJobType.CheckForChanges:
                    {
                        var updatedDocument = NocsService.GetUpdatedDocument(job.SyncDocument);
                    
                        // GetUpdatedDocument won't return null if the document has been updated since last check
                        if (updatedDocument != null)
                        {
                            // the document has been updated, let's fetch the updated content
                            result = new SyncResult
                            {
                                ContentUpdated = true,
                                TimeUpdated = DateTime.Now,
                                Job = job,
                                Document = NocsService.GetDocumentContent(updatedDocument)
                            };
                        }
                        else
                        {
                            // the document hasn't been updated, let's generate a null result
                            result = new SyncResult
                            {
                                ContentUpdated = false,
                                Job = job
                            };
                        }
                        break;
                    }

                    case SyncJobType.UpdateAllEntries:
                    {
                        NocsService.UpdateAllEntries();
                        result = new SyncResult
                        {
                            ContentUpdated = false,
                            // this is only to be used with single documents
                            Job = job,
                        };
                        break;
                    }
                }

                e.Result = result;
            }
            catch (Exception ex)
            {
                var errorMessage = ex.Message;

                if (job.Type == SyncJobType.CheckForChanges)
                {
                    Trace.WriteLine(string.Format("{0} - Synchronizer: error while checking if {1} is different in Google Docs: {2}", DateTime.Now, job.SyncDocument.Title, ex.Message));
                }
                else if (job.Type == SyncJobType.UpdateAllEntries)
                {
                    Trace.WriteLine(string.Format("{0} - Synchronizer: error while fetching all documents: {1}", DateTime.Now, ex.Message));
                }

                result = new SyncResult
                {
                    ContentUpdated = false,
                    Error = errorMessage,
                    Job = job
                };

                e.Result = result;
            }
        }
Exemplo n.º 8
0
        private void SynchronizerContentUpdated(SyncResult result)
        {
            // no need to continue checking if this is an Untitled-document
            if (!Document.IsDraft && result.Job.SyncDocument.ResourceId == Document.ResourceId && result.ContentUpdated)
            {
                // in case for some reason the content gets "updated" on google's side
                // and the content hasn't actually changed, let's just stop here
                if (result.Document.Content == TxtNocContentNow && result.Document.Title == Document.Title)
                {
                    Debug.WriteLine("SynchronizerContentUpdated and incoming document is identical");
                    Document.ETag = result.Document.ETag;
                    return;
                }

                // also, if we're currently writing something (Content has changed),
                // let's not merge changes yet to avoid deletion of our own work
                if (ContentHasChanged)
                {
                    return;
                }

                // let's update the fields for this document
                Document = result.Document;

                // let's get the merged text based on text at last sync
                //Debug.WriteLine("SyncContentUpdated > From: TxtNocContentAtLastSync:\t" + TxtNocContentAtLastSync);
                Debug.WriteLine("SyncContentUpdated > From: TxtNocContentAtLastSuccessfulSave:\t" + TxtNocContentAtLastSuccessfulSave);
                Debug.WriteLine("SyncContentUpdated >   To: Incoming:\t\t\t\t\t" + Document.Content);
                Debug.WriteLine("SyncContentUpdated > TxtNocContentAtLastSave:\t" + TxtNocContentAtLastSuccessfulSave);
                Debug.WriteLine("SyncContentUpdated > TxtNocContentNow:\t\t\t" + TxtNocContentNow);
                Document.Content = Tools.MergeText(TxtNocContentAtLastSuccessfulSave, TxtNocContentNow, Document.Content);
                Debug.WriteLine("SyncContentUpdated > Merged:\t\t\t\t\t" + Document.Content);

                // let's find out if we've changed the editor text since last sync
                var editorContentChangedSinceLastSync = TxtNocContentAtLastSync != TxtNocContentNow;

                // let's reset the sync situation
                Debug.WriteLine("SyncContentUpdated > TxtNocContentAtLastSync/Save = " + Document.Content);
                TxtNocContentAtLastSync           = Document.Content;
                TxtNocContentAtLastSuccessfulSave = Document.Content;
                ContentHasChanged = false;
                _tabTitleModifiedBecauseContentHasChanged = false;

                // this event will always be called from a separate thread so we'll have to use Invoke to modify Noc's properties
                // (can only make changes to WinForm controls from the master thread)
                if (IsHandleCreated)
                {
                    // let's update the tab's title and the RichTextBox content
                    NocEventHandler setTabTitle = SetTabTitleText;
                    Invoke(setTabTitle, result.Document.Title);

                    NocEventHandler setEditorContent = SetEditorContent;
                    Invoke(setEditorContent, Document.Content);
                }
                else
                {
                    Trace.WriteLine(DateTime.Now + " - Noc: Handle not created while trying to invoke content changes for: " + result.Document.Title);
                    return;
                }

                // in case we've change the text since last sync (while syncing for example),
                // let's save this document again
                if (editorContentChangedSinceLastSync && !NocsService.Working && !_bgWorkerSaveNoc.IsBusy)
                {
                    Debug.WriteLine("ContentUpdated: editor content changed since last sync, saving current content");

                    // let's run the backgroundWorker for saving this document
                    var folderId = string.Empty;
                    if (Document.ParentFolders.Count > 0)
                    {
                        folderId = Document.ParentFolders[0];
                    }
                    var args = new object[] { folderId, Document.Title, false, false };
                    _bgWorkerSaveNoc.RunWorkerAsync(args);
                }
            }
        }