private void ModelToUrlShortcuts(BookmarkBucketObjectModel obj, string sDir)
        {
            // make sure filenames don't exist
            if (obj == null || obj.list.Count == 0)
            {
                return;
            }
            if (sDir == null || !Directory.Exists(sDir))
            {
                throw new BookmarkBucketException("Output directory does not exist");
            }

            // first, make sure we won't overwrite files.
            foreach (BookmarkBucketItem item in obj.list)
            {
                if (item.sFile.Length == 0 || item.sUrl.Length == 0)
                {
                    throw new BookmarkBucketException("Missing url or title.");
                }
                if (File.Exists(sDir + "\\" + item.GetNameWithoutExtension() + ".url"))
                {
                    throw new BookmarkBucketException("Url shortcut for '" + item.GetNameWithoutExtension() + "' already exists.");
                }
            }

            // write the url shortcuts (we'll throw if access denied)
            foreach (BookmarkBucketItem item in obj.list)
            {
                ModelItemToUrlShortcut(sDir + "\\" + item.GetNameWithoutExtension() + ".url", item.GetNameWithoutExtension(), item.sUrl);
            }
        }
        private void ModelToXmlFile(BookmarkBucketObjectModel obj, string sFilename)
        {
            XmlSerializer xmlS = new XmlSerializer(typeof(BookmarkBucketObjectModel));

            using (TextWriter writeFileStream = new StreamWriter(sFilename))
            {
                xmlS.Serialize(writeFileStream, obj);
            }
        }
        public void TopRunUnitTests()
        {
            //string sTestDir = @"C:\Users\diamond\Desktop\-frommain\delldev\bookmarkbuckets\bookmarkbuckets_cs\bookmarkbucket\bookmarkbucket_console\bookmarkbucket_ui\test";
            //string s1 = BookmarkBucketCore.GetExifDataSoftware(sTestDir + "\\k.jpeg");
            //string s2 = BookmarkBucketCore.GetExifDataTitle(sTestDir + "\\k.jpeg");
            //string s3 = BookmarkBucketCore.GetExifDataUrl(sTestDir + "\\k.jpeg");
            //MessageBox.Show(String.Format("{0}\n{1}\n{2}\n", s1,s2,s3));

            // convert from old to new.
            //bool b = ThreadPool.SetMaxThreads(4, 4);
            //System.Diagnostics.Debug.Assert(b);

            DialogResult dr = MessageBox.Show("Create new jpgs?", "bb", MessageBoxButtons.YesNo);
            if (dr == DialogResult.No) { ConvertOldToNewWithoutRedownloading(); return; }

            string sDir = InputBoxForm.GetStrInput("All from this directory:", "c:\\example");
            if (sDir == null || !Directory.Exists(sDir)) { MessageBox.Show("Not a valid directory."); return; }

            DirectoryInfo di = new DirectoryInfo(sDir);
            FileInfo[] rgFiles = di.GetFiles("*.jpg");
            foreach (FileInfo fi in rgFiles)
            {
                string sNewname = Path.GetFileNameWithoutExtension(fi.Name.Replace("555 ", "")) + ".png";
                if (File.Exists(sDir + "\\" + sNewname)) throw new BookmarkBucketException("exists png" + fi.Name);
                File.Move(sDir + "\\" + fi.Name, sDir + "\\" + sNewname);
            }

            // make a Model
            BookmarkBucketObjectModel model = new BookmarkBucketObjectModel();
            FileInfo[] rgFilesPng = di.GetFiles("*.png");
            foreach (FileInfo fi in rgFilesPng)
            {
                string sOldUrl = BookmarkBucketCore.GetExifData(fi.FullName, "Comment");
                if (!BookmarkBucketCore.looksLikeAUrl(sOldUrl))
                    throw new BookmarkBucketException("couldnot read url" + fi.Name);

                model.list.Add(new BookmarkBucketItem { sFile = Path.GetFileNameWithoutExtension(fi.Name), sUrl = sOldUrl });
            }

            Form1.g_busy = true;
            Thread oThread = new Thread(delegate()
            {
                CallAndCatchExceptions.Call(delegate(object o)
                {
                    this.ModelToJpegs(model, sDir);
                    Form1.g_busy = false; Form1.g_status = "";
                }, null);
            });
            oThread.Start();
        }
        public void TopUrlShortcutsToJpegs(string sDir)
        {
            if (sDir == null)
            {
                return;
            }
            BookmarkBucketObjectModel obj = UrlShortcutsToModel(sDir);

            ModelToJpegs(obj, sDir);
            if (obj.list.Count == 0)
            {
                MessageBox.Show("None seen.");
            }
            Form1.g_status = "";
        }
        public void TopJpegsToXmlFile()
        {
            BookmarkBucketObjectModel obj = JpegFilesToModelAskDirectory();

            if (obj == null)
            {
                return;
            }
            string sFilename = ImageHelper.GetSaveString("xml", "Save xml file");

            if (sFilename == null)
            {
                return;
            }
            ModelToXmlFile(obj, sFilename);
        }
        private BookmarkBucketObjectModel UrlShortcutsToModel(string sDir)
        {
            if (!Directory.Exists(sDir))
            {
                throw new BookmarkBucketException("Dir does not exist");
            }
            BookmarkBucketObjectModel model = new BookmarkBucketObjectModel();
            DirectoryInfo             di    = new DirectoryInfo(sDir);

            FileInfo[] rgFiles = di.GetFiles("*.URL");
            foreach (FileInfo fi in rgFiles)
            {
                model.list.Add(UrlShortcutToModelItem(sDir + "\\" + fi.Name, fi.Name));
            }
            return(model);
        }
        private BookmarkBucketObjectModel JpegFilesToModelAskDirectory()
        {
            string sInputDir = ImageHelper.GetOpenString("jpeg", "Select BookmarkBucket jpegs in this directory:");

            if (sInputDir == null)
            {
                return(null);
            }
            BookmarkBucketObjectModel obj = JpegFilesToModel(Path.GetDirectoryName(sInputDir));

            if (obj.list.Count == 0)
            {
                MessageBox.Show("None seen."); return(null);
            }
            return(obj);
        }
        public void TopJpegsToUrlShortcuts()
        {
            BookmarkBucketObjectModel obj = JpegFilesToModelAskDirectory();

            if (obj == null)
            {
                return;
            }
            string sFilename = ImageHelper.GetSaveString("url", "Save many URL shortcuts into this location:");

            if (sFilename == null)
            {
                return;
            }
            ModelToUrlShortcuts(obj, Path.GetDirectoryName(sFilename));
        }
        public void TopXmlFileToUrlShortcuts()
        {
            string sFilename = ImageHelper.GetOpenString("xml", "Select xml file:");

            if (sFilename == null)
            {
                return;
            }

            BookmarkBucketObjectModel obj = XmlFileToModel(sFilename);

            if (obj == null)
            {
                return;
            }
            string sOutFilename = ImageHelper.GetSaveString("url", "Save many URL shortcuts into this location:");

            if (sOutFilename == null)
            {
                return;
            }
            ModelToUrlShortcuts(obj, Path.GetDirectoryName(sOutFilename));
        }
        private BookmarkBucketObjectModel JpegFilesToModel(string sDir)
        {
            if (!Directory.Exists(sDir))
            {
                throw new BookmarkBucketException("Dir does not exist");
            }
            BookmarkBucketObjectModel model = new BookmarkBucketObjectModel();
            DirectoryInfo             di    = new DirectoryInfo(sDir);

            FileInfo[] rgFiles = di.GetFiles("*.JPEG");
            foreach (FileInfo fi in rgFiles)
            {
                string sUrl = BookmarkBucketCore.GetExifDataUrl(sDir + "\\" + fi.Name);
                if (sUrl == null || sUrl.Length == 0)
                {
                    throw new BookmarkBucketException("Url is null in " + fi.Name);
                }

                model.list.Add(new BookmarkBucketItem {
                    sFile = Path.GetFileNameWithoutExtension(fi.Name), sUrl = sUrl
                });
            }
            return(model);
        }
 private void ModelToXmlFile(BookmarkBucketObjectModel obj, string sFilename)
 {
     XmlSerializer xmlS = new XmlSerializer(typeof(BookmarkBucketObjectModel));
     using (TextWriter writeFileStream = new StreamWriter(sFilename))
     {
         xmlS.Serialize(writeFileStream, obj);
     }
 }
 private BookmarkBucketObjectModel JpegFilesToModel(string sDir)
 {
     if (!Directory.Exists(sDir)) throw new BookmarkBucketException("Dir does not exist");
     BookmarkBucketObjectModel model = new BookmarkBucketObjectModel();
     DirectoryInfo di = new DirectoryInfo(sDir);
     FileInfo[] rgFiles = di.GetFiles("*.JPEG");
     foreach (FileInfo fi in rgFiles)
     {
         string sUrl = BookmarkBucketCore.GetExifDataUrl(sDir + "\\" + fi.Name);
         if (sUrl == null || sUrl.Length == 0) throw new BookmarkBucketException("Url is null in " + fi.Name);
         
         model.list.Add(new BookmarkBucketItem { sFile = Path.GetFileNameWithoutExtension(fi.Name), sUrl = sUrl });
     }
     return model;
 }
        private void ModelToJpegs(BookmarkBucketObjectModel obj, string sDir)
        {
            if (obj == null || obj.list.Count == 0) return;
            sDir += "\\out";
            if (Directory.Exists(sDir)) throw new BookmarkBucketException("'out' directory already exists.");
            Directory.CreateDirectory(sDir);

            // first, make sure we won't overwrite files.
            foreach (BookmarkBucketItem item in obj.list)
            {
                if (item.sFile.Length == 0 || item.sUrl.Length == 0)
                    throw new BookmarkBucketException("Missing url or title.");
                if (!BookmarkBucketCore.looksLikeAUrl(item.sUrl))
                    throw new BookmarkBucketException("invalid url");
                if (File.Exists(sDir + "\\" + item.GetNameWithoutExtension() + ".jpeg"))
                    throw new BookmarkBucketException("Jpeg for '" + item.GetNameWithoutExtension() + "' already exists.");
                if (File.Exists(sDir + "\\" + item.GetNameWithoutExtension() + ".png"))
                    throw new BookmarkBucketException("Png for '" + item.GetNameWithoutExtension() + "' already exists.");
                if (File.Exists(sDir + "\\" + item.GetNameWithoutExtension() + ".html"))
                    throw new BookmarkBucketException("Html for '" + item.GetNameWithoutExtension() + "' already exists.");
            }
            // test write access.
            try { File.WriteAllText(sDir + "\\!!temp!!.test", "test"); }
            catch (IOException) { throw new BookmarkBucketException("Apparently no write access here..."); }
            finally { if (File.Exists(sDir + "\\!!temp!!.test")) File.Delete(sDir + "\\!!temp!!.test"); }

            // use my own threadpool instead of c#'s because I want to limit number of threads running.
            int nThreads = 4;
            ModelToJpegsParameters param = new ModelToJpegsParameters { iItemLatest = 0, locker = new object(), model = obj, sDir = sDir };
            Thread[] threads = new Thread[nThreads];
            for (int i = 0; i < nThreads; i++)
            {
                threads[i] = new Thread(new ParameterizedThreadStart(this.ModelToJpegsThread));
                threads[i].Start(param);
            }
            // wait for all threads to complete. note that obj.version will be negative if an error occurred.
            for (int i = 0; i < nThreads; i++)
                threads[i].Join();
        }
예제 #14
0
        public void TopRunUnitTests()
        {
            //string sTestDir = @"C:\Users\diamond\Desktop\-frommain\delldev\bookmarkbuckets\bookmarkbuckets_cs\bookmarkbucket\bookmarkbucket_console\bookmarkbucket_ui\test";
            //string s1 = BookmarkBucketCore.GetExifDataSoftware(sTestDir + "\\k.jpeg");
            //string s2 = BookmarkBucketCore.GetExifDataTitle(sTestDir + "\\k.jpeg");
            //string s3 = BookmarkBucketCore.GetExifDataUrl(sTestDir + "\\k.jpeg");
            //MessageBox.Show(String.Format("{0}\n{1}\n{2}\n", s1,s2,s3));

            // convert from old to new.
            //bool b = ThreadPool.SetMaxThreads(4, 4);
            //System.Diagnostics.Debug.Assert(b);

            DialogResult dr = MessageBox.Show("Create new jpgs?", "bb", MessageBoxButtons.YesNo);

            if (dr == DialogResult.No)
            {
                ConvertOldToNewWithoutRedownloading(); return;
            }

            string sDir = InputBoxForm.GetStrInput("All from this directory:", "c:\\example");

            if (sDir == null || !Directory.Exists(sDir))
            {
                MessageBox.Show("Not a valid directory."); return;
            }

            DirectoryInfo di = new DirectoryInfo(sDir);

            FileInfo[] rgFiles = di.GetFiles("*.jpg");
            foreach (FileInfo fi in rgFiles)
            {
                string sNewname = Path.GetFileNameWithoutExtension(fi.Name.Replace("555 ", "")) + ".png";
                if (File.Exists(sDir + "\\" + sNewname))
                {
                    throw new BookmarkBucketException("exists png" + fi.Name);
                }
                File.Move(sDir + "\\" + fi.Name, sDir + "\\" + sNewname);
            }

            // make a Model
            BookmarkBucketObjectModel model = new BookmarkBucketObjectModel();

            FileInfo[] rgFilesPng = di.GetFiles("*.png");
            foreach (FileInfo fi in rgFilesPng)
            {
                string sOldUrl = BookmarkBucketCore.GetExifData(fi.FullName, "Comment");
                if (!BookmarkBucketCore.looksLikeAUrl(sOldUrl))
                {
                    throw new BookmarkBucketException("couldnot read url" + fi.Name);
                }

                model.list.Add(new BookmarkBucketItem {
                    sFile = Path.GetFileNameWithoutExtension(fi.Name), sUrl = sOldUrl
                });
            }

            Form1.g_busy = true;
            Thread oThread = new Thread(delegate()
            {
                CallAndCatchExceptions.Call(delegate(object o)
                {
                    this.ModelToJpegs(model, sDir);
                    Form1.g_busy = false; Form1.g_status = "";
                }, null);
            });

            oThread.Start();
        }
        private void ModelToUrlShortcuts(BookmarkBucketObjectModel obj, string sDir)
        {
            // make sure filenames don't exist
            if (obj == null || obj.list.Count == 0) return;
            if (sDir==null||!Directory.Exists(sDir)) throw new BookmarkBucketException("Output directory does not exist"); 

            // first, make sure we won't overwrite files.
            foreach (BookmarkBucketItem item in obj.list)
            {
                if (item.sFile.Length == 0 || item.sUrl.Length == 0)
                    throw new BookmarkBucketException("Missing url or title.");
                if (File.Exists(sDir + "\\" + item.GetNameWithoutExtension() + ".url"))
                    throw new BookmarkBucketException("Url shortcut for '" + item.GetNameWithoutExtension() + "' already exists.");
            }

            // write the url shortcuts (we'll throw if access denied)
            foreach (BookmarkBucketItem item in obj.list)
            {
                ModelItemToUrlShortcut(sDir + "\\" + item.GetNameWithoutExtension() + ".url", item.GetNameWithoutExtension(), item.sUrl);
            }
        }
 private BookmarkBucketObjectModel UrlShortcutsToModel(string sDir)
 {
     if (!Directory.Exists(sDir)) throw new BookmarkBucketException("Dir does not exist");
     BookmarkBucketObjectModel model = new BookmarkBucketObjectModel();
     DirectoryInfo di = new DirectoryInfo(sDir);
     FileInfo[] rgFiles = di.GetFiles("*.URL");
     foreach (FileInfo fi in rgFiles)
     {
         model.list.Add(UrlShortcutToModelItem(sDir + "\\" + fi.Name, fi.Name));
     }
     return model;
 }
        private void ModelToJpegs(BookmarkBucketObjectModel obj, string sDir)
        {
            if (obj == null || obj.list.Count == 0)
            {
                return;
            }
            sDir += "\\out";
            if (Directory.Exists(sDir))
            {
                throw new BookmarkBucketException("'out' directory already exists.");
            }
            Directory.CreateDirectory(sDir);

            // first, make sure we won't overwrite files.
            foreach (BookmarkBucketItem item in obj.list)
            {
                if (item.sFile.Length == 0 || item.sUrl.Length == 0)
                {
                    throw new BookmarkBucketException("Missing url or title.");
                }
                if (!BookmarkBucketCore.looksLikeAUrl(item.sUrl))
                {
                    throw new BookmarkBucketException("invalid url");
                }
                if (File.Exists(sDir + "\\" + item.GetNameWithoutExtension() + ".jpeg"))
                {
                    throw new BookmarkBucketException("Jpeg for '" + item.GetNameWithoutExtension() + "' already exists.");
                }
                if (File.Exists(sDir + "\\" + item.GetNameWithoutExtension() + ".png"))
                {
                    throw new BookmarkBucketException("Png for '" + item.GetNameWithoutExtension() + "' already exists.");
                }
                if (File.Exists(sDir + "\\" + item.GetNameWithoutExtension() + ".html"))
                {
                    throw new BookmarkBucketException("Html for '" + item.GetNameWithoutExtension() + "' already exists.");
                }
            }
            // test write access.
            try { File.WriteAllText(sDir + "\\!!temp!!.test", "test"); }
            catch (IOException) { throw new BookmarkBucketException("Apparently no write access here..."); }
            finally { if (File.Exists(sDir + "\\!!temp!!.test"))
                      {
                          File.Delete(sDir + "\\!!temp!!.test");
                      }
            }

            // use my own threadpool instead of c#'s because I want to limit number of threads running.
            int nThreads = 4;
            ModelToJpegsParameters param = new ModelToJpegsParameters {
                iItemLatest = 0, locker = new object(), model = obj, sDir = sDir
            };

            Thread[] threads = new Thread[nThreads];
            for (int i = 0; i < nThreads; i++)
            {
                threads[i] = new Thread(new ParameterizedThreadStart(this.ModelToJpegsThread));
                threads[i].Start(param);
            }
            // wait for all threads to complete. note that obj.version will be negative if an error occurred.
            for (int i = 0; i < nThreads; i++)
            {
                threads[i].Join();
            }
        }