// ---------------------------------------------------------------------- private void BgWorkerCreateFolder_DoWork(object sender, DoWorkEventArgs e) { // rename an item with the title from the event argument var folderNameToCreate = ((object[])e.Argument)[0].ToString(); NocsService.CreateNewFolder(folderNameToCreate); }
// ---------------------------------------------------------------------- private void BgWorkerMoveEntry_DoWork(object sender, DoWorkEventArgs e) { // rename an item with the title from the event argument var entryToMove = ((object[])e.Argument)[0] as Document; var folderToMoveIn = ((object[])e.Argument)[1] as Document; NocsService.MoveEntry(folderToMoveIn, entryToMove); }
private static void BgWorkerLoadDocumentContent_DoWork(object sender, DoWorkEventArgs e) { var document = e.Argument as Document; // if an error will occur, we'll keep the document as result so we can retry e.Result = document; e.Result = NocsService.GetDocumentContent(document); }
private void BgWorkerRenameEntry_DoWork(object sender, DoWorkEventArgs e) { // rename an item with the title from the event argument var entryId = ((object[])e.Argument)[0].ToString(); var newTitle = ((object[])e.Argument)[1].ToString(); var entryType = (Document.DocumentType)((object[])e.Argument)[2]; NocsService.RenameEntry(entryId, newTitle, entryType); e.Result = entryId; }
private void BgWorkerValidateDoWork(object sender, DoWorkEventArgs e) { if (Tools.IsConnected()) { // try to start the NocsServiceervice with given credentials NocsService.AuthenticateUser(txtGUser.Text, txtGPassword.Text, true); } else { throw new Exception("No internet connection"); } }
// ---------------------------------------------------------------------- private void BgWorkerRemoveEntryFromAllFolders_DoWork(object sender, DoWorkEventArgs e) { // rename an item with the title from the event argument var entryToRemove = e.Argument as Document; if (entryToRemove != null && entryToRemove.ParentFolders.Count == 0) { e.Cancel = true; } else { NocsService.RemoveEntryFromAllFolders(entryToRemove); } }
/// <summary> /// Occurs whenever the auto-save Timer elapses. /// Will save the document if all of the following conditions are met: /// 1. the text inside the editor has changed /// 2. AutoSave is enabled /// 3. service is idle /// 4. we're properly authenticated /// 5. current file is not new (a draft) /// 6. bgWorker for Saving this document isn't busy /// </summary> private void AutoSaveTimerElapsed(object sender, ElapsedEventArgs e) { // let's make sure all the necessary conditions are met if (ContentHasChanged && Settings.Default.AutoSave && !NocsService.Working && NocsService.UserIsAuthenticated() && !Document.IsDraft && !_bgWorkerSaveNoc.IsBusy) { _autoSaveTimer.Stop(); Save(); } }
private void BgWorkerStartService_DoWork(object sender, DoWorkEventArgs e) { // first check that user is connected to Internet if (Tools.IsConnected()) { // start the DocumentService with user's credentials // (e.Argument is a boolean determining whether to force authToken retrieval) NocsService.AuthenticateUser(NocsService.Username, NocsService.Password, (bool)e.Argument); } else { Trace.WriteLine(DateTime.Now + " - Main: couldn't connect to internet"); throw new Exception("Could not connect to internet!"); } }
private void MenuPreferencesClick(object sender, EventArgs e) { // make sure we are authenticated if (!NocsService.UserIsAuthenticated()) { // user's not authenticated, let's ask for credentials to retrieve items var login = new Login(); if (login.ShowDialog() == DialogResult.OK) { RetrieveDocuments(); } return; } var nocsPreferences = new Preferences(); if (nocsPreferences.ShowDialog() == DialogResult.OK) { // let's notify listeners if (SettingsChanged != null) { SettingsChanged(); } } // if proxy is enabled after preferences were saved, and for some reason the Browsing isn't enabled and // no worker is busy, let's try to re-retrieve all items if (Settings.Default.UseProxy && !menuBrowse.Enabled && !BgWorkerStartService.IsBusy && !BgWorkerGetAllItems.IsBusy) { // make sure user was validated if (!string.IsNullOrEmpty(NocsService.Username) && !string.IsNullOrEmpty(NocsService.Password)) { // inform user that we are retrieving items (documents) Status(StatusType.Retrieve, "Retrieving the list of documents.."); // disable menu options for changing the Google Account and for browsing Google Docs while items are being retrieved menuGoogleAccount.Enabled = false; menuBrowse.Enabled = false; menuSave.Enabled = false; // reset help variable NocsService.AccountChanged = false; // run the backgroundworker for retrieving items BgWorkerGetAllItems.RunWorkerAsync(); } } }
// ---------------------------------------------------------------------- private void BgWorkerDeleteEntry_DoWork(object sender, DoWorkEventArgs e) { // delete an item with the given documentId var entryId = ((object[])e.Argument)[0].ToString(); var entryType = (Document.DocumentType)((object[])e.Argument)[1]; var entryIsFolderWithEntries = (bool)((object[])e.Argument)[2]; NocsService.DeleteEntry(entryId, entryType); // if we removed a folder, let's update all entries also if (entryIsFolderWithEntries) { NocsService.UpdateAllEntries(); } e.Result = entryId; }
private void MenuSaveClick(object sender, EventArgs e) { // let's first make sure the document content has changed var currentTab = tabs.SelectedTab as Noc; if (currentTab == null || (!currentTab.Document.IsDraft && !currentTab.ContentHasChanged)) { return; } // let's stop here if we're already saving the current tab if (currentTab.SaveWorkerIsBusy()) { return; } // make sure we are authenticated if (!NocsService.UserIsAuthenticated()) { // user's not authenticated, let's ask for credentials to retrieve items var login = new Login(); if (login.ShowDialog() == DialogResult.OK) { RetrieveDocuments(); } return; } // if current file is not new, let's just save it if (!currentTab.Document.IsDraft) { Status(StatusType.Save, "Saving..."); currentTab.Save(); } else { // ask for a name var saveResponse = SaveDialog.SaveDialogBox("Enter a name for your file:", "Save", "Untitled"); if (!string.IsNullOrEmpty(saveResponse.DocumentName)) { Status(StatusType.Save, "Saving..."); currentTab.SaveAs(saveResponse); } } }
private void MenuBrowseClick(object sender, EventArgs e) { // make sure we are authenticated if (!NocsService.UserIsAuthenticated()) { // user's not authenticated, let's ask for credentials to retrieve items var login = new Login(); if (login.ShowDialog() == DialogResult.OK) { Status(StatusType.Retrieve, "Retrieving items..."); menuGoogleAccount.Enabled = false; menuBrowse.Enabled = false; menuSave.Enabled = false; BgWorkerGetAllItems.RunWorkerAsync(); } return; } var currentTab = tabs.SelectedTab as Noc; Browse nocsBrowse; // we will give the Browse-form the current selected documentId (from selected tab), // so the Browse-form can select it from its listBox on load // we'll also give Browse a reference to Synchronizer because AutoFetchAllEventFinished might fire if (currentTab != null && !currentTab.Document.IsDraft) { nocsBrowse = new Browse(ref _synchronizer, currentTab.Document.ResourceId); } else { nocsBrowse = new Browse(ref _synchronizer); } // Browse-form can tell us to add+load a document, or notify us of document renames/deletions nocsBrowse.AddDocumentToMainForm += BrowseAddDocumentToMainForm; nocsBrowse.DocumentRenamed += BrowseDocumentRenamed; nocsBrowse.DocumentDeleted += BrowseDocumentDeleted; nocsBrowse.ShowDialog(); }
private void MenuGoogleAccountClick(object sender, EventArgs e) { // let's first stop all processing _synchronizer.Stop(); _autoFetchAllEntriesTimer.Stop(); // let's then find out if any of the open tabs are unsaved foreach (Noc tab in tabs.TabPages) { if (tab.ContentHasChanged && NocsService.UserIsAuthenticated()) { // display a msg asking the user to save changes or abort var result = MessageBox.Show("Do you want to save the changes to " + tab.Text + "?", "Nocs", MessageBoxButtons.YesNoCancel); if (result == DialogResult.Yes) { // let's stop here if we're already saving the current tab if (tab.SaveWorkerIsBusy()) { // re-enable all processing _synchronizer.Start(); _autoFetchAllEntriesTimer.Start(); return; } // if current file is not new, let's just save it if (!tab.Document.IsDraft) { Status(StatusType.Save, "Saving..."); tab.Save(); } else { // ask for a name var saveResponse = SaveDialog.SaveDialogBox("Enter a name for your file:", "Save", "Untitled"); if (!string.IsNullOrEmpty(saveResponse.DocumentName)) { Status(StatusType.Save, "Saving..."); tab.SaveAs(saveResponse); } } // re-enable all processing _synchronizer.Start(); _autoFetchAllEntriesTimer.Start(); return; } if (result == DialogResult.Cancel) { // re-enable all processing _synchronizer.Start(); _autoFetchAllEntriesTimer.Start(); return; } } } // we're past saving questions, let's show the Login dialog for changing Google account var login = new Login(); login.ShowDialog(); // let's check if user account was changed once the login window is closed if (NocsService.AccountChanged) { // inform the new user that we are retrieving items Status(StatusType.Retrieve, "Synchronizing with Google Docs..."); // disable menu options for changing google account and opening documents while items are being retrieved menuGoogleAccount.Enabled = false; menuBrowse.Enabled = false; menuSave.Enabled = false; // reset helping variable NocsService.AccountChanged = false; // let's reset titlebar, clear tabs, etc. SetMainTitle(string.Empty); tabs.TabPages.Clear(); // let's clear synchronizer queues _synchronizer.JobQueue.Clear(); _synchronizer.ErrorQueue.Clear(); // let's then create a new tab (Noc) AddNoc(); // and finally let's run the backgroundworker to retrieve new items for this account BgWorkerGetAllItems.RunWorkerAsync(false); } // either way, let's re-enable timers _synchronizer.Start(); _autoFetchAllEntriesTimer.Start(); }
/// <summary> /// Closes a tab with a given index. /// </summary> /// <param name="tabIndex">Index for the tab to be closed</param> private void CloseTab(int tabIndex) { // let's makes sure the index is valid if (tabIndex < 0 || tabIndex > (tabs.TabCount - 1)) { return; } var tabToBeClosed = tabs.TabPages[tabIndex] as Noc; if (tabToBeClosed != null && tabToBeClosed.ContentHasChanged && NocsService.UserIsAuthenticated()) { // let's first stop all processing _synchronizer.Stop(); _autoFetchAllEntriesTimer.Stop(); var result = MessageBox.Show("Do you want to save the changes to " + tabToBeClosed.Text + "?", "Nocs", MessageBoxButtons.YesNoCancel); // display a msg asking the user to save changes or abort if (result == DialogResult.Yes) { // let's stop here if we're already saving the current tab if (tabToBeClosed.SaveWorkerIsBusy()) { // re-enable all processing _synchronizer.Start(); _autoFetchAllEntriesTimer.Start(); return; } // if current file is not new, let's just save it if (!tabToBeClosed.Document.IsDraft) { Status(StatusType.Save, "Saving..."); tabToBeClosed.Save(); } else { // ask for a name var saveResponse = SaveDialog.SaveDialogBox("Enter a name for your file:", "Save", "Untitled"); if (!string.IsNullOrEmpty(saveResponse.DocumentName)) { Status(StatusType.Save, "Saving..."); tabToBeClosed.SaveAs(saveResponse); } } // re-enable all processing _synchronizer.Start(); _autoFetchAllEntriesTimer.Start(); return; } if (result == DialogResult.Cancel) { // re-enable all processing _synchronizer.Start(); _autoFetchAllEntriesTimer.Start(); return; } } // if selected tab will be removed, let's jump to the one on its left var selectedRemoved = false; var newSelectedIndex = 0; if (tabIndex == tabs.SelectedIndex) { selectedRemoved = true; newSelectedIndex = tabIndex - 1; if (newSelectedIndex < 0) { newSelectedIndex = 0; } } // let's handle the selection changes if (selectedRemoved) { tabs.SelectedIndex = newSelectedIndex; } // let's get the TabPage for dispose/deactivation purposes var tab = tabs.TabPages[tabIndex] as Noc; // if get here, we can remove the tab tabs.TabPages.RemoveAt(tabIndex); // let's handle disposing if (tab != null) { tab.Deactivate(); SettingsChanged -= tab.SettingsChanged; tab.Dispose(); } // if no more tabs left, let's reset the title to "Nocs" and disable saving to file if (tabs.TabPages.Count == 0) { SetMainTitle(string.Empty); lblCaretPosition.Text = string.Empty; menuSaveFileAs.Enabled = false; } }
private void MainFormClosing(object sender, FormClosingEventArgs e) { // for pinned documents var pinnedDocuments = new List <string>(); // let's first find out if any of the open tabs are unsaved foreach (Noc tab in tabs.TabPages) { // let's also collect potential pinned documents if (tab.Pinned) { pinnedDocuments.Add(tab.Document.ResourceId); } if (tab.ContentHasChanged && NocsService.UserIsAuthenticated()) { // let's first stop all processing if (!_synchronizer.SyncStopped || _autoFetchAllEntriesTimer.Enabled) { _synchronizer.Stop(); _autoFetchAllEntriesTimer.Stop(); } // ..and display a msg asking the user to save changes or abort var result = MessageBox.Show("Do you want to save the changes to " + tab.Text + "?", "Nocs", MessageBoxButtons.YesNoCancel); if (result == DialogResult.Yes) { // let's cancel the FormClosing-event e.Cancel = true; // let's stop here if we're already saving the current tab if (tab.SaveWorkerIsBusy()) { // re-enable all processing and exit _synchronizer.Start(); _autoFetchAllEntriesTimer.Start(); return; } // if current file is not new, let's just save it if (!tab.Document.IsDraft) { Status(StatusType.Save, "Saving..."); tab.Save(); } else { // ask for a name var saveResponse = SaveDialog.SaveDialogBox("Enter a name for your file:", "Save", "Untitled"); if (!string.IsNullOrEmpty(saveResponse.DocumentName)) { Status(StatusType.Save, "Saving..."); tab.SaveAs(saveResponse); } } // re-enable all processing and exit _synchronizer.Start(); _autoFetchAllEntriesTimer.Start(); return; } if (result == DialogResult.Cancel) { e.Cancel = true; // re-enable all processing _synchronizer.Start(); _autoFetchAllEntriesTimer.Start(); return; } } } // save all current settings just before closing application Settings.Default.WindowLocation = Location; Settings.Default.WindowSize = WindowState == FormWindowState.Normal ? Size : RestoreBounds.Size; // let's also save pinned documents if (pinnedDocuments.Any()) { var pinnedDocumentsJoined = String.Join(";", pinnedDocuments.Where(s => !string.IsNullOrEmpty(s))); Settings.Default.PinnedDocuments = pinnedDocumentsJoined; } else { Settings.Default.PinnedDocuments = string.Empty; } Settings.Default.Save(); // let's flush the trace content to the file Trace.Flush(); }
private void BgWorkerGetAllItems_DoWork(object sender, DoWorkEventArgs e) { NocsService.UpdateAllEntries(); }