Пример #1
0
        public bool WithMetadata(int galleryId, Action <string> action)
        {
            string cachedMetadataFilePath;

            if (PathFormatter.IsEnabled)
            {
                cachedMetadataFilePath = PathFormatter.GetMetadata(galleryId);
            }
            else
            {
                cachedMetadataFilePath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", PathFormatter.GetCacheDirectory(), galleryId, ".json");
            }

            if (!File.Exists(cachedMetadataFilePath))
            {
                return(false);
            }

            if (action != null)
            {
                action.Invoke(cachedMetadataFilePath);
            }

            return(true);
        }
Пример #2
0
        public bool WithPagesFolder(int galleryId, Action <string> action)
        {
            string cachedPagesPath;

            if (PathFormatter.IsEnabled)
            {
                string cachedMetadataFilePath = PathFormatter.GetMetadata(galleryId);

                Metadata metadata = SearchResultCache.Find(galleryId) ?? JsonUtility.LoadFromFile <Metadata>(cachedMetadataFilePath);

                if (metadata == null)
                {
                    return(false);
                }

                cachedPagesPath = PathFormatter.GetPages(metadata);
            }
            else
            {
                cachedPagesPath = string.Format(CultureInfo.InvariantCulture, "{0}{1}/", PathFormatter.GetCacheDirectory(), galleryId);
            }

            if (!Directory.Exists(cachedPagesPath))
            {
                return(false);
            }

            if (action != null)
            {
                action.Invoke(cachedPagesPath);
            }

            return(true);
        }
Пример #3
0
        public int[] GetCachedPageIndices(int galleryId)
        {
            List <int> indices = new List <int>();

            string cachedMetadataFilePath;

            if (PathFormatter.IsEnabled)
            {
                cachedMetadataFilePath = PathFormatter.GetMetadata(galleryId);
            }
            else
            {
                cachedMetadataFilePath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", PathFormatter.GetCacheDirectory(), galleryId, ".json");
            }

            Metadata metadata = SearchResultCache.Find(galleryId) ?? JsonUtility.LoadFromFile <Metadata>(cachedMetadataFilePath);

            if (metadata == null)
            {
                return(indices.ToArray());
            }

            string cachedPagesPath;

            if (PathFormatter.IsEnabled)
            {
                cachedPagesPath = PathFormatter.GetPages(metadata);
            }
            else
            {
                cachedPagesPath = string.Format(CultureInfo.InvariantCulture, "{0}{1}/", PathFormatter.GetCacheDirectory(), galleryId);
            }

            if (!Directory.Exists(cachedPagesPath))
            {
                return(indices.ToArray());
            }

            DirectoryInfo dirInfo = new DirectoryInfo(cachedPagesPath);

            foreach (FileInfo fileInfo in dirInfo.EnumerateFiles())
            {
                string fileTitle = Path.GetFileNameWithoutExtension(fileInfo.Name).TrimStart(new char[] { '0' });
                int    num;

                if (int.TryParse(fileTitle, out num))
                {
                    if (num >= 1 && num <= metadata.Images.Pages.Count)
                    {
                        indices.Add(num);
                    }
                }
            }

            return(indices.ToArray());
        }
Пример #4
0
        public bool WithArchive(int galleryId, Action <string> action)
        {
            List <string> cachedArcihveFilePaths = new List <string>();

            if (PathFormatter.IsEnabled)
            {
                string cachedMetadataFilePath = PathFormatter.GetMetadata(galleryId);

                Metadata metadata = SearchResultCache.Find(galleryId) ?? JsonUtility.LoadFromFile <Metadata>(cachedMetadataFilePath);

                if (metadata == null)
                {
                    return(false);
                }

                foreach (IArchiveWriter archiveWriter in ArchiveWriters)
                {
                    string archivePath = PathFormatter.GetArchive(metadata, archiveWriter);

                    cachedArcihveFilePaths.Add(archivePath);
                }
            }
            else
            {
                foreach (IArchiveWriter archiveWriter in ArchiveWriters)
                {
                    string archivePath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", PathFormatter.GetCacheDirectory(), galleryId, archiveWriter.FileExtension);

                    cachedArcihveFilePaths.Add(archivePath);
                }
            }

            foreach (string archivePath in cachedArcihveFilePaths)
            {
                if (!File.Exists(archivePath))
                {
                    continue;
                }

                if (action != null)
                {
                    action.Invoke(archivePath);
                }

                return(true);
            }

            return(false);
        }
Пример #5
0
        public void ShowToolTip(Control control)
        {
            if (!associatedControls.ContainsKey(control))
            {
                return;
            }

            int    associatedGalleryId = associatedControls[control];
            string cachedMetadataFileName;

            if (PathFormatter.IsEnabled)
            {
                cachedMetadataFileName = PathFormatter.GetMetadata(associatedGalleryId);
            }
            else
            {
                cachedMetadataFileName = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", PathFormatter.GetCacheDirectory(), associatedGalleryId, ".json");
            }

            Metadata metadata = JsonUtility.LoadFromFile <Metadata>(cachedMetadataFileName);

            if (metadata == null)
            {
                return;
            }

            string html = GalleryTooltipTemplate.GetFormattedText(metadata);

            if (string.IsNullOrEmpty(html))
            {
                return;
            }

            Point toolTipLocation = control.Location;

            toolTipLocation.Offset(0, control.Height);

            Location = toolTipLocation;

            webBrowser.DocumentText = html;
        }
Пример #6
0
        // NOTE: at the moment both details and download share the same webbrowser.
        private bool ShowDetailsOrDownload(int galleryId, Action <Metadata> action)
        {
            DetailsModel.AddSearch(galleryId);

            string cachedMetadataFilePath;

            if (PathFormatter.IsEnabled)
            {
                cachedMetadataFilePath = PathFormatter.GetMetadata(galleryId);
            }
            else
            {
                cachedMetadataFilePath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", PathFormatter.GetCacheDirectory(), galleryId, ".json");
            }

            Metadata metadata = SearchResultCache.Find(galleryId) ?? JsonUtility.LoadFromFile <Metadata>(cachedMetadataFilePath);

            if (metadata == null)
            {
                // let it download in the background, DetailsBrowserView will respond to its completion event.
                GalleryDownloader.Download(galleryId);

                return(false);
            }

            if (metadata == null)
            {
                MessageBox.Show("can't find metadata");

                return(false);
            }

            if (action != null)
            {
                action.Invoke(metadata);
            }

            return(true);
        }
Пример #7
0
        public bool DoesCoverExists(int galleryId)
        {
            string cachedMetadataFilePath;

            if (PathFormatter.IsEnabled)
            {
                cachedMetadataFilePath = PathFormatter.GetMetadata(galleryId);
            }
            else
            {
                cachedMetadataFilePath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", PathFormatter.GetCacheDirectory(), galleryId, ".json");
            }

            Metadata metadata = SearchResultCache.Find(galleryId) ?? JsonUtility.LoadFromFile <Metadata>(cachedMetadataFilePath);

            if (metadata == null)
            {
                return(false);
            }

            return(WithCover(metadata, null));
        }
Пример #8
0
        public bool DoesPageExists(int galleryId, int pageIndex)
        {
            string cachedMetadataFilePath;

            if (PathFormatter.IsEnabled)
            {
                cachedMetadataFilePath = PathFormatter.GetMetadata(galleryId);
            }
            else
            {
                cachedMetadataFilePath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", PathFormatter.GetCacheDirectory(), galleryId, ".json");
            }

            Metadata metadata = SearchResultCache.Find(galleryId) ?? JsonUtility.LoadFromFile <Metadata>(cachedMetadataFilePath);

            if (metadata == null ||
                pageIndex < 0 ||
                pageIndex > metadata.Images.Pages.Count - 1)
            {
                return(false);
            }

            return(WithPage(metadata, pageIndex, null));
        }
Пример #9
0
        private void ListBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            SearchErrorArg errorArg = e.UserState as SearchErrorArg;

            if (errorArg != null)
            {
                if (!string.IsNullOrEmpty(WebBrowser.DocumentText))
                {
                    WebBrowser.Document?.InvokeScript("__onSearchError", errorArg.ToObjectArray());
                }

                return;
            }

            SearchProgressArg searchProgressArg = e.UserState as SearchProgressArg;

            if (searchProgressArg != null)
            {
                SearchResult searchResult = searchProgressArg.SearchResult;

                if (searchResult == null)
                {
                    MessageBox.Show("Couldn't get search results for page " + (searchProgressArg.PageIndex + 1) + "!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // TODO: should this be done in DoWork instead?

                for (int i = 0; i < searchResult.Result.Count; ++i)
                {
                    Metadata metadata = searchResult.Result[i];
                    string   cachedMetadataPath;

                    if (PathFormatter.IsEnabled)
                    {
                        cachedMetadataPath = PathFormatter.GetMetadata(metadata.Id);
                    }
                    else
                    {
                        cachedMetadataPath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", PathFormatter.GetCacheDirectory(), metadata.Id.ToString(CultureInfo.InvariantCulture), ".json");
                    }

                    if (!File.Exists(cachedMetadataPath))
                    {
                        try
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(cachedMetadataPath));
                            File.WriteAllText(cachedMetadataPath, JsonConvert.SerializeObject(metadata, Formatting.Indented));
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }

                    // TODO: the progress should be multiple searchresult, not individual metadata in the first searchresult.

                    if (!string.IsNullOrEmpty(WebBrowser.DocumentText))
                    {
                        // TODO: invoke an event instead of directly invoking a script function.

                        WebBrowser.Document?.InvokeScript("__onSearchResultLoaded", new object[]
                        {
                            searchProgressArg.SearchArg.PageIndex,
                            searchProgressArg.SearchArg.TagId,
                            searchProgressArg.SearchArg.Query ?? "",
                            searchProgressArg.SearchArg.Target.ToString().ToLowerInvariant(),
                            e.ProgressPercentage,
                            i + 1,
                            searchResult.Result.Count,
                            JsonConvert.SerializeObject(metadata)
                        });
                    }
                }

                if (searchProgressArg.PageIndex == searchProgressArg.SearchArg.PageIndex)
                {
                    if (searchResult.Result != null &&
                        searchProgressArg.SortType != GallerySortType.None)
                    {
                        SearchResult customSearchResult = new SearchResult();

                        customSearchResult.NumPages = searchProgressArg.SearchResult.NumPages;
                        customSearchResult.PerPage  = searchProgressArg.SearchResult.PerPage;

                        /*
                         * IEnumerable<Metadata> customResult;
                         *
                         * customResult = searchProgressArg.SearchResult.Result.GetFilteredSearchResult(searchProgressArg.MetadataKeywordLists);
                         * customResult = customResult.GetSortedSearchResult(searchProgressArg.SortType, searchProgressArg.SortOrder);
                         */
                        IEnumerable <Metadata> customResult = searchProgressArg.SearchResult.Result.GetSortedSearchResult(searchProgressArg.SortType, searchProgressArg.SortOrder);

                        customSearchResult.Result = customResult.ToList();

                        SearchProgressArg customSearchProgressArg = new SearchProgressArg(searchProgressArg.SearchArg, customSearchResult, searchProgressArg.PageIndex, searchProgressArg.MetadataKeywordLists, searchProgressArg.SortType, searchProgressArg.SortOrder);

                        GalleryModel.SearchProgressArg = customSearchProgressArg;
                    }
                    else
                    {
                        GalleryModel.SearchProgressArg = searchProgressArg;
                    }
                }

                //
                // TODO notify javascript to add one page of items
                //
                //galleryBrowserView.WebBrowser.Document.InvokeScript("__onProgressChanged", new object[] { searchProgressArg.PageIndex, JsonConvert.SerializeObject(searchProgressArg.SearchResult) });
            }
        }
Пример #10
0
        public bool WithCachedPage(int galleryId, Func <int, int, int, bool> predicate, Action <string> action)
        {
            string cachedMetadataFilePath;

            if (PathFormatter.IsEnabled)
            {
                cachedMetadataFilePath = PathFormatter.GetMetadata(galleryId);
            }
            else
            {
                cachedMetadataFilePath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", PathFormatter.GetCacheDirectory(), galleryId, ".json");
            }

            Metadata metadata = SearchResultCache.Find(galleryId) ?? JsonUtility.LoadFromFile <Metadata>(cachedMetadataFilePath);

            if (metadata == null)
            {
                return(false);
            }

            string cachedPagesPath;

            if (PathFormatter.IsEnabled)
            {
                cachedPagesPath = PathFormatter.GetPages(metadata);
            }
            else
            {
                cachedPagesPath = string.Format(CultureInfo.InvariantCulture, "{0}{1}/", PathFormatter.GetCacheDirectory(), galleryId);
            }

            if (!Directory.Exists(cachedPagesPath))
            {
                return(false);
            }

            DirectoryInfo dirInfo           = new DirectoryInfo(cachedPagesPath);
            string        firstPageFileName = null;
            int           numPages          = metadata.Images.Pages.Count;

            foreach (FileInfo fileInfo in dirInfo.EnumerateFiles())
            {
                string fileTitle = Path.GetFileNameWithoutExtension(fileInfo.Name).TrimStart(new char[] { '0' });
                int    num;

                if (int.TryParse(fileTitle, out num))
                {
                    if (predicate.Invoke(num, 1, numPages))
                    //if (num >= 1 && num <= metadata.Images.Pages.Count)
                    {
                        firstPageFileName = fileInfo.FullName;
                        break;
                    }
                }
            }

            if (string.IsNullOrEmpty(firstPageFileName))
            {
                return(false);
            }

            if (action != null)
            {
                action.Invoke(firstPageFileName);
            }

            return(true);
        }
Пример #11
0
        public void ShowToolTip(TreeNode control)
        {
            if (showing)
            {
                return;
            }

            if (!associatedControls.ContainsKey(control))
            {
                if (control != null)
                {
                    //MessageBox.Show(control.Name + " has not been associated.");
                }

                HideToolTip();

                return;
            }

            showing = true;

            //Hide();

            Screen screen          = Screen.FromControl(this);
            Point  toolTipLocation = new Point(control.TreeView.Bounds.Left, control.Bounds.Location.Y);

            toolTipLocation.Offset(control.TreeView.Bounds.Width, control.Bounds.Height * 2);
            toolTipLocation.Offset(control.TreeView.Margin.Horizontal, 0);

            if (toolTipLocation.X + Size.Width > screen.WorkingArea.Width)
            {
                toolTipLocation.X -= Size.Width;
            }

            if (toolTipLocation.Y + Size.Height > screen.WorkingArea.Height)
            {
                toolTipLocation.Y -= Size.Height;
            }

            Location = toolTipLocation;

            int    associatedGalleryId = associatedControls[control];
            string cachedMetadataFileName;

            if (PathFormatter.IsEnabled)
            {
                cachedMetadataFileName = PathFormatter.GetMetadata(associatedGalleryId);
            }
            else
            {
                cachedMetadataFileName = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", PathFormatter.GetCacheDirectory(), associatedGalleryId, ".json");
            }

            Metadata metadata = JsonUtility.LoadFromFile <Metadata>(cachedMetadataFileName);

            if (metadata == null)
            {
                //Logger.LogLineFormat("{0} has no associated metadata.", associatedGalleryId);
                showing = false;
                return;
            }

            string html = GalleryTooltipTemplate.GetFormattedText(metadata);

            if (string.IsNullOrEmpty(html))
            {
                //Logger.LogLine("Failed to get template html.");
                showing = false;
                return;
            }

            webBrowser.DocumentText = html;
        }