Ejemplo n.º 1
0
        /// <summary>
        /// Fetches/updates all DocumentEntries in a Dictionary and wraps them in a Noc class.
        /// </summary>
        /// <returns>Dictionary of ResourceId's and Noc objects.</returns>
        public static void UpdateAllEntries()
        {
            if (AllDocuments == null)
            {
                AllDocuments = new Dictionary <string, Document>();
            }

            if (AllFolders == null)
            {
                AllFolders = new Dictionary <string, Document>();
            }

            // let's first make sure the user is authenticated
            if (_settings == null)
            {
                throw new Exception("User hasn't been authenticated - internet down?");
            }

            try
            {
                var request = new DocumentsRequest(_settings)
                {
                    Service = { ProtocolMajor = 3 }
                    //BaseUri = DocumentsListQuery.documentsAclUri
                };
                var reqFactory = (GDataRequestFactory)request.Service.RequestFactory;
                reqFactory.Proxy = GetProxy();

                // we'll fetch all entries
                AllEntriesFeed = request.GetEverything();

                // if we've already retrieved items, let's clear the dictionaries before updating them
                if (AllDocuments.Count > 0)
                {
                    AllDocuments.Clear();
                }
                if (AllFolders.Count > 0)
                {
                    AllFolders.Clear();
                }

                foreach (var entry in AllEntriesFeed.Entries)
                {
                    // let's only add documents and folders
                    if (entry.Type == Document.DocumentType.Document)
                    {
                        AllDocuments.Add(entry.ResourceId, entry);
                    }
                    else if (entry.Type == Document.DocumentType.Folder)
                    {
                        AllFolders.Add(entry.ResourceId, entry);
                    }
                }
            }
            catch (GDataNotModifiedException)
            {
                // since doclist updates timestamps on feeds based on access,
                // etags are useless here and we shouldn't get here
                return;
            }
            catch (GDataRequestException exRequest)
            {
                var error = GetErrorMessage(exRequest);
                if (exRequest.ResponseString == null && error.ToLowerInvariant().Contains("execution of request failed"))
                {
                    throw new GDataRequestException("Couldn't fetch all entries - internet down?");
                }

                Trace.WriteLine(string.Format("\n{0} - NocsService: couldn't fetch all entries: {1}\n", DateTime.Now, error));
                throw new GDataRequestException(Tools.TrimErrorMessage(error));
            }
            catch (Exception ex)
            {
                var error = GetErrorMessage(ex);
                Trace.WriteLine(string.Format("\n{0} - NocsService: couldn't fetch all entries: {1}\n", DateTime.Now, error));
                throw new Exception(error);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new document in Google Docs.
        /// </summary>
        /// <param name="folderId">An entry id for any given folder in which the new document is to be saved.</param>
        /// <param name="title">Title for the new document.</param>
        /// <param name="content">HTML content for the new document.</param>
        /// <param name="createDefaultDirectory">
        /// true = create a default directory ('Nocs')
        /// fales = don't create a default directory
        /// </param>
        /// <returns>A newly created Document.</returns>
        public static Document CreateNewDocument(string folderId, string title, string content, bool createDefaultDirectory)
        {
            DocumentEntry newEntry;
            Document      newDocument;

            content = Tools.FormatEditorContentToHtml(title, content);

            try
            {
                var request    = new DocumentsRequest(_settings);
                var reqFactory = (GDataRequestFactory)request.Service.RequestFactory;
                reqFactory.Proxy = GetProxy();

                // we'll first create a default 'Nocs'-folder if one isn't already created
                if (createDefaultDirectory)
                {
                    var defaultFolder = new Document
                    {
                        Type  = Document.DocumentType.Folder,
                        Title = "Nocs"
                    };
                    defaultFolder = request.CreateDocument(defaultFolder);

                    // if we created our default directory, let's add it to our folder dictionary
                    if (defaultFolder != null)
                    {
                        AllFolders.Add(defaultFolder.ResourceId, defaultFolder);
                        folderId = defaultFolder.ResourceId;
                    }
                }

                SetupService(null, null, 3, null, null);
                var textStream = new MemoryStream(Encoding.UTF8.GetBytes(content));

                // we might be creating this document inside a particular folder
                var postUri = !string.IsNullOrEmpty(folderId) ?
                              new Uri(string.Format(DocumentsListQuery.foldersUriTemplate, folderId))
                              : new Uri(DocumentsListQuery.documentsBaseUri);

                newEntry = _documentService.Insert(postUri, textStream, DocumentContentType, title) as DocumentEntry;
            }
            catch (GDataRequestException exRequest)
            {
                var error = !string.IsNullOrEmpty(exRequest.ResponseString) ? exRequest.ResponseString : exRequest.Message;
                if (exRequest.ResponseString == null && error.ToLowerInvariant().Contains("execution of request failed"))
                {
                    throw new GDataRequestException("Couldn't create document - internet down?");
                }


                // we'll also check for InvalidEntryException: Could not convert document
                // - assuming it's a known problem in GData API related to sharing, we will just ignore it
                if (error.ToLowerInvariant().Contains("could not convert document"))
                {
                    Debug.WriteLine(string.Format("Couldn't convert document while creating a document: {0}", title));
                    return(null);
                }

                Trace.WriteLine(string.Format("{0} - Couldn't create a new document: {1} - {2}", DateTime.Now, title, error));
                throw new GDataRequestException(string.Format("Couldn't create a new document: {0} - {1}", title, Tools.TrimErrorMessage(error)));
            }
            catch (Exception ex)
            {
                var error = GetErrorMessage(ex);
                Trace.WriteLine(DateTime.Now + " - NocsService - couldn't create a new document: " + error);
                throw new Exception(string.Format("Couldn't create document: {0} - {1}", title, error));
            }

            // let's create a new Document
            if (newEntry != null)
            {
                newEntry.IsDraft = false;

                newDocument = new Document
                {
                    AtomEntry = newEntry,
                    Title     = title,
                    Content   = Tools.ParseContent(content)
                };

                // let's add the new document to our document dictionary and return it
                AllDocuments.Add(newDocument.ResourceId, newDocument);
                return(newDocument);
            }

            // we should never get here
            throw new Exception((string.Format("Couldn't create document: {0} - internet down?", title)));
        }