Exemplo n.º 1
0
        private void FillSubFoldersRecursive(ref PGFolder folder, PGFolderComparer defaultSortingComparer, out List <string> filenames_cache)
        {
            filenames_cache = new List <string>();

            DirectoryInfo directory = new DirectoryInfo(folder.PhysicalPath);

            if (!directory.Exists)
            {
                throw new DirectoryNotFoundException(string.Format("Directory {0} not found", directory));
            }
            else
            {
                //1. READ INFO FILE
                folder.ReadFolder();

                Images.AddRange(folder.Images);     //cache images for easy access
                Comments.AddRange(folder.Comments); //cache comments for easy access.

                //2. LOAD SUB FOLDERS from disk (RECURSIVE)
                folder.SubFolders = new List <PGFolder>();

                defaultSortingComparer = PGFolderComparer.GetComparerBySortAction(folder.SortAction, defaultSortingComparer);

                //3. subfolder recursive
                foreach (DirectoryInfo subDirectory in directory.GetDirectories())
                {
                    if (!IsDirectoryIgnored(subDirectory.FullName))
                    {
                        PGFolder subfolder = new PGFolder()
                        {
                            Name = subDirectory.Name, PhysicalPath = subDirectory.FullName
                        };
                        subfolder.ParentFolder = folder;

                        List <string> filename_cache_subfolder = new List <string>();
                        FillSubFoldersRecursive(ref subfolder, defaultSortingComparer, out filename_cache_subfolder);
                        filenames_cache.AddRange(filename_cache_subfolder);
                        folder.SubFolders.Add(subfolder);
                        folder.NumberNestedImages += subfolder.NumberNestedImages; //todo refactor, inside pgfolder.
                    }
                }

                //4. Sorting?
                folder.SortSubFolders(folder.SortAction, ref defaultSortingComparer);

                AllFolders.Add(folder); //index for faster access

                filenames_cache.Add(folder.FolderInfoFileLocation());
            }

            //5. WRITE if necessary
            if (!folder.ExistFolderInfoFile())
            {
                folder.WriteFolderFileInfo();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new folder in Google Docs.
        /// </summary>
        /// <param name="folderName">Name for the new folder.</param>
        public static void CreateNewFolder(string folderName)
        {
            try
            {
                var request    = new DocumentsRequest(_settings);
                var reqFactory = (GDataRequestFactory)request.Service.RequestFactory;
                reqFactory.Proxy = GetProxy();

                var newFolder = new Document
                {
                    Type  = Document.DocumentType.Folder,
                    Title = folderName
                };
                newFolder = request.CreateDocument(newFolder);

                // let's add the new directory to our folder dictionary
                if (newFolder != null)
                {
                    AllFolders.Add(newFolder.ResourceId, newFolder);
                }
            }
            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 folder - internet down?");
                }

                Trace.WriteLine(DateTime.Now + " - NocsService - couldn't create folder: " + error);
                throw new GDataRequestException(string.Format("Couldn't create folder: {0} - {1}", folderName, Tools.TrimErrorMessage(error)));
            }
            catch (Exception ex)
            {
                var error = GetErrorMessage(ex);
                Trace.WriteLine(DateTime.Now + " - NocsService - couldn't create folder: " + error);
                throw new Exception(string.Format("Couldn't create folder: {0} - {1}", folderName, error));
            }
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
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)));
        }