private void LibInit()
        {
            List <string> folders = new List <string>();
            Dictionary <string, string> fileHashes = new Dictionary <string, string>();
            var progressWindow = new ProgressWindow();

            Task.Factory.StartNew(() =>
            {
                folders = MiddleDude.GetLibFolders();

                if (folders.Count < 1)
                {
                    using (System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog())
                    {
                        dialog.Description = "Select Folder...";
                        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            string initFolder = dialog.SelectedPath;
                            MiddleDude.AddLibraryFolder(initFolder);
                        }
                    }
                }
            }).ContinueWith(x =>
            {
                progressWindow.StopProgress();
                progressWindow.ChangeText("Locating library contents...");

                string[] allFiles = FileTools.Walker(folders);

                progressWindow.SetProgressBarMax(allFiles.Length);
                progressWindow.StartProgress();
                progressWindow.ChangeText("Processing library files...");

                foreach (string file in allFiles)
                {
                    string ext = Path.GetExtension(file);
                    if (Constants.AllowedExt.Contains(ext.Remove(0, 1)))
                    {
                        string workHash      = WorkIdent.Hash(file);
                        fileHashes[workHash] = file;
                    }
                    progressWindow.UpdateProgressBar();
                }
                progressWindow.StopProgress();
            }).ContinueWith(x =>
            {
                MiddleDude.StoreLibHashes(fileHashes);
            });

            progressWindow.Close();

            Console.WriteLine("LibInit complete!");
            //using (var writer = new FileStream(Constants.LibHashList, FileMode.Create))
            //{
            //    XmlSerializer ser = new XmlSerializer(typeof(SerializableDictionary<string, string>), new XmlRootAttribute("root"));
            //    ser.Serialize(writer, fileHashes);
            //}

            //XmlOperator.Serialize(fileHashes, Constants.LibHashList);
        }
        private void LoadFicsToCache()
        {
            Dictionary <string, Fic> ficListInternal;

            try
            {
                ficListInternal = MiddleDude.GetFics();
            }
            catch (Exception)
            {
                ficListInternal = new Dictionary <string, Fic>();
            }

            ficList = ficListInternal;
        }
Exemple #3
0
        public static Dictionary <string, string> GetKindleList()
        {
            /* If Kindle is not connected, it will use the list of IDs stored from the last time it was.
             * Returns a dictionary in the format:
             *      Dictionary
             *      {
             *          MD5 hash: Filepath
             *      }
             */
            Dictionary <string, string> fileHashes = new Dictionary <string, string>();
            var progressWindow = new ProgressWindow();

            progressWindow.Show();
            progressWindow.StopProgress();

            Task.Factory.StartNew(() =>
            {
                foreach (DriveInfo drive in DriveInfo.GetDrives())
                {
                    string driveName = drive.IsReady ? drive.VolumeLabel : null; // Makes sure we only try to query mounted drives
                    if (driveName != null && driveName.Contains("Kindle"))       // TODO more robust Kindle detection
                    {
                        // Kindle is connected, get current library


                        progressWindow.ChangeText("Locating Kindle contents...");


                        string[] kindleFiles = FileTools.Walker(new List <string> {
                            drive.Name
                        });
                        progressWindow.SetProgressBarMax(kindleFiles.Length);
                        progressWindow.StartProgress();
                        progressWindow.ChangeText("Processing Kindle files...");

                        foreach (string file in kindleFiles)
                        {
                            string ext = Path.GetExtension(file);
                            if (ext.StartsWith("."))
                            {
                                ext = ext.Substring(1);
                            }
                            if (Constants.AllowedExt.Contains(ext))
                            {
                                string workHash      = WorkIdent.Hash(file);
                                fileHashes[workHash] = file;
                            }

                            progressWindow.UpdateProgressBar();
                        }

                        MiddleDude.StoreKindle(fileHashes);
                    }
                }
            });


            progressWindow.Close();
            Console.WriteLine("Kindle fetch complete!");
            try
            {
                fileHashes = MiddleDude.LoadKindle();
                return(fileHashes);
            }
            catch (InvalidOperationException)
            {
                return(fileHashes);
            }
        }
        private void Start_Click(object sender, RoutedEventArgs e)
        {
            if (pasteBox.Text == Constants.PasteBoxDefText)
            {
                Log("There are no URLs to download!");
                return;
            }

            List <String> urlEntries = pasteBox.Text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();

            urlEntries = urlEntries.Distinct().ToList();


            this.pendingDownloads = new List <WebClient>();
            this.activeDownloads  = true;
            UpdateControls(true);

            var fics    = new SerializableDictionary <string, Fic>();
            var dlLinks = new List <String>();

            // NOTE: These are also set when using the browse dialog
            userSettings.DownloadLocation = saveLocation.Text;
            userSettings.DevicePath       = deviceLocation.Text;



            Task.Factory.StartNew(() =>
            {
                foreach (string urlEntry in urlEntries)
                {
                    Fic fic = GetFic(urlEntry);
                    if (fic != null)
                    {
                        fics[fic.ID] = fic;
                        IncrementProgress(labelProgressedFetched);

                        BeGentle(linksTotal: urlEntries.Count);
                    }
                }
            }).ContinueWith(x =>
            {
                // TODO create flag
                //foreach (Fic fic in fics)
                //{
                //    foreach (KeyValuePair<string, string> entry in fic.Downloads)
                //    {
                //        dlLinks.Add(entry.Value);
                //    }

                //}
                //Task[] tasks = new Task[dlLinks.Count];
                //for (int i = 0; i < dlLinks.Count; i++)
                //{
                //    string link = dlLinks[i];
                //    tasks[i] = Task.Factory.StartNew(() => DownloadEbook(link));  // THIS IS IMPORTANT
                //}
                //Task.WaitAll(tasks);  // SO IS THIS
                //}).ContinueWith(x =>
                //{
                // Both download methods are kept for now, for performance testing purposes

                // New method, to hopefully make connecting metadata and file easier
                List <string> formats = getSelectedFormats();
                int totalDl           = fics.Count * formats.Count;
                int currentDl         = 0;
                Task[] tasks          = new Task[totalDl];
                foreach (KeyValuePair <string, Fic> pair in fics)
                {
                    var fic          = pair.Value;
                    tasks[currentDl] = Task.Factory.StartNew(() =>
                    {
                        foreach (KeyValuePair <string, string> entry in fic.Downloads)
                        {
                            if (formats.Contains(entry.Key))
                            {
                                string success = DownloadEbook(entry.Value);
                                if (!String.IsNullOrEmpty(success))
                                {
                                    fic.LocalFiles[entry.Key] = success;
                                    IncrementProgress(labelProgressDownloaded);
                                    Log($"Downloaded {success}...");
                                }
                                BeGentle(linksTotal: totalDl);
                            }
                        }
                    });
                    currentDl++;
                }
                Task.WaitAll(tasks);
            }).ContinueWith(x =>
            {
                if (this.userCancel)
                {
                    Log("Cancelled by user.");
                }
            }).ContinueWith(x =>
            {
                this.activeDownloads             = false;
                Dictionary <string, Fic> allFics = new Dictionary <string, Fic>();
                try
                {
                    allFics = MiddleDude.GetFics();
                    //allFics = XmlOperator.DeserializeFile<SerializableDictionary<string, Fic>>(Constants.WorksRef);
                }
                catch (System.Xml.XmlException)
                {
                }
                if (allFics != null)
                {
                    foreach (KeyValuePair <string, Fic> pair in fics)
                    {
                        if (!allFics.ContainsKey(pair.Key))
                        {
                            allFics[pair.Key] = pair.Value;
                        }
                        else
                        {
                            Fic mergedFic     = Fic.Merge(allFics[pair.Key], pair.Value);
                            allFics[pair.Key] = mergedFic;
                        }
                    }
                }
                else
                {
                    allFics = fics;
                }
                MiddleDude.StoreFics(allFics);
                UpdateControls(false);
                Log("Downloads complete!");
            });
        }