예제 #1
0
        /// <summary>
        /// reads all the User accounts from the accounts XML file, including their comic libraries
        /// </summary>
        /// <returns>
        /// array of User objects
        /// </returns>
        public new User[] ReadAllUserAccounts()
        {
            if (PathIsValid())
            {
                XDocument xDocument = XDocument.Load(filePath);
                userList = new User[UserCount];
                int index = 0;

                foreach (XElement account in xDocument.Descendants("Account"))
                {
                    //get required elements
                    XElement id_element          = account.Element("ID");
                    XElement username_element    = account.Element("Username");
                    XElement password_element    = account.Element("Password");
                    XElement libraryHead_element = account.Element("MyComicLibrary");

                    //check if id does not have 1000 as its first 4 digits
                    //the 1000 indicates an admin, any other will be a user
                    if (id_element.Value.Substring(0, 4) != "1000")
                    {
                        userList[index] = new User(Convert.ToInt32(id_element.Value));
                        {
                            userList[index].Username = username_element.Value;
                            userList[index].Password = password_element.Value;

                            ComicLibrary cbLib = new ComicLibrary();
                            //check foreach book imported in library
                            if (libraryHead_element != null &&
                                libraryHead_element.Descendants("MySavedComicBook") != null)
                            {
                                foreach (var savedCb_element in libraryHead_element.Descendants("MySavedComicBook"))
                                {
                                    //get all required elements for this saved comic book
                                    XElement archivePath_element = savedCb_element.Element("ArchivePath");
                                    XElement bookMark_element    = savedCb_element.Element("Bookmark");
                                    XElement lastViewed_element  = savedCb_element.Element("Last_Viewed");
                                    XElement rating_element      = savedCb_element.Element("MyRating");

                                    //set all required data
                                    //set all required data : use ComicAccess to get ComicBook instance of archivePath
                                    ComicAccess ca        = new ComicAccess();
                                    ComicBook   public_cb = ca.GetComicBook(archivePath_element.Value);
                                    //obtained ComicBook is common to all, get the ComicBook for this user
                                    UserComicBook private_cb = UserComicBook.MorphToUserComicBook(public_cb);
                                    private_cb.LastViewed = DateTime.Parse(lastViewed_element.Value);
                                    private_cb.RateComicBook(float.Parse(rating_element.Value));

                                    //get all required elements : get bookmark for this saved comic book
                                    XElement bookMark_maxPages_element = bookMark_element.Element("MaxPages");
                                    XElement bookmark_pageNum_element  = bookMark_element.Element("PageNum");
                                    Bookmark bm = new Bookmark(int.Parse(bookMark_maxPages_element.Value));
                                    //check if there are any bookmarked pages
                                    if (bookmark_pageNum_element.Value != "")
                                    {
                                        foreach (var pageNum in bookmark_pageNum_element.Value.Split(','))
                                        {
                                            bm.AddPageNum(int.Parse(pageNum));
                                        }
                                    }
                                    //set bookmark to user's saved comic book
                                    private_cb.BookMark = bm;

                                    //add user's saved comic book to user's comic library
                                    cbLib.AddComicBook(private_cb);
                                }
                                userList[index].MyComicLibrary = cbLib;
                            }
                            index++;
                        }
                    }
                }
                return(userList);
            }
            else
            {
                Trace.WriteLine("Set Path = " + "<" + filePath + ">" + " is invalid, cannot proceed with account access.");
                FileNotFoundException exc = new FileNotFoundException
                                            (
                    "Set Path = " + "<" + filePath + ">" + " is invalid, cannot proceed with account access."
                                            );
                throw exc;
            }
        }
예제 #2
0
        /// <summary>
        /// Open the comic based on archivePath, this closes the current one
        /// </summary>
        public void OpenComicBook(string archivePath)
        {
            //get the comic book from the archivePath using ComicAccess
            ComicAccess comAcc = new ComicAccess();

            comicBook = comAcc.GetComicBook(archivePath);

            //set extract folder in temporary folder pertaining to comic, and make it the new tempPath
            //this will be the cache for the comic book
            tempPath = Path.Combine(tempPath,
                                    Path.GetFileNameWithoutExtension(archivePath));

            //check if comic book has already been cached to temp folder
            if (Directory.Exists(tempPath) && Directory.GetFiles(tempPath).Length != 0)
            {
                //don't extract anymore, load the comic book from cache
                comicBook.SetComicPath(tempPath);
                //set current page as cover page(first page)
                currentPage = 1;
                //nothing to report
                ReportProgress?.Invoke(this, new ProgressArgs
                {
                    TotalProcessed   = 100,
                    TotalRecords     = 100,
                    Description      = "Done!",
                    IsDoneProcessing = true
                });
            }
            //comic book has not been cached yet, perform archive extraction
            else
            {
                //create extract folder in temporary folder pertaining to comic
                Directory.CreateDirectory(tempPath);

                //direct the program to the dependency: 7z.dll (7z64.dll for 64-bit)
                SevenZipBase.SetLibraryPath(Path.Combine(Environment.CurrentDirectory, "7z.dll"));

                //extract archive to temporary folder
                using (var extractor = new SevenZipExtractor(archivePath))
                {
                    //extract all images in temporary folder
                    for (var i = 0; i < extractor.ArchiveFileData.Count; i++)
                    {
                        //check if file to be extracted is an image
                        string fileToBeExtractedExtension = Path.GetExtension(extractor.ArchiveFileData[i].FileName);
                        if (ImageChecker.IsImageExtension(fileToBeExtractedExtension))
                        {
                            //extract the file
                            extractor.ExtractFiles(tempPath, extractor.ArchiveFileData[i].Index);
                        }

                        //report progress that a file has been processed
                        ReportProgress?.Invoke(this, new ProgressArgs
                        {
                            TotalProcessed   = i + 1,
                            TotalRecords     = extractor.ArchiveFileData.Count,
                            Description      = "Extracting comic book to cache.",
                            IsDoneProcessing = false
                        });
                    }
                }

                //move images outside the directories, if there are any
                string[] comicDirs = Directory.GetDirectories(tempPath);
                if (comicDirs.Length != 0)
                {
                    string[] fileList;
                    foreach (var dir in comicDirs)
                    {
                        //get files in directory
                        fileList = Directory.GetFiles(dir);
                        //start a new report
                        //report progress that a file has been processed
                        ReportProgress?.Invoke(this, new ProgressArgs
                        {
                            TotalProcessed   = 0,
                            TotalRecords     = fileList.Length,
                            Description      = "Rearranging comic book.",
                            IsDoneProcessing = false
                        });

                        int filesProcessed = 0;
                        //bring them to tempPath
                        foreach (var file in fileList)
                        {
                            string newFilePath = Path.Combine(tempPath, Path.GetFileName(file));
                            File.Move(file, newFilePath);
                            ReportProgress?.Invoke(this, new ProgressArgs
                            {
                                TotalProcessed   = filesProcessed,
                                TotalRecords     = fileList.Length,
                                Description      = "Rearranging comic book.",
                                IsDoneProcessing = false
                            });
                            filesProcessed++;
                        }
                        //delete the directory
                        Directory.Delete(dir);
                    }
                }

                //set the comic book path
                comicBook.SetComicPath(tempPath);

                //set current page as cover page(first page)
                currentPage = 1;

                //end reporting
                ReportProgress?.Invoke(this, new ProgressArgs
                {
                    TotalProcessed   = 100,
                    TotalRecords     = 100,
                    Description      = "Done!",
                    IsDoneProcessing = true
                });
                //go back to temp directory
                tempPath = Path.Combine(tempPath, @"..\");
            }
        }