/*private ProgressStatusClass ProgressStatus;*/ public frmMain() { InitializeComponent(); setWorkingMode(true); lblInfo.Text = "Loading..."; this.Show(); this.Cursor = Cursors.WaitCursor; Application.DoEvents(); //Registre = Registry.CurrentUser.CreateSubKey(@"Software\Calexo\CaMuMa"); Registre = Registry.CurrentUser.OpenSubKey(@"Software\Calexo\CaMuMa", true); if (Registre == null) Registre = Registry.CurrentUser.CreateSubKey(@"Software\Calexo\CaMuMa"); myParams = new Parameters(); loadParams(); //ProgressStatus = new ProgressStatusClass(); userStoreUrl = new Uri(edamBaseUrl + "/edam/user"); userStoreTransport = new THttpClient(userStoreUrl); userStoreProtocol = new TBinaryProtocol(userStoreTransport); userStore = new UserStore.Client(userStoreProtocol); sFolderList = new String[65535]; Application.DoEvents(); bool versionOK = userStore.checkVersion("CaMuMa", Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MAJOR, Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MINOR); //Console.WriteLine("Is my EDAM protocol version up to date? " + versionOK); Application.DoEvents(); if (!versionOK) { MessageBox.Show("Version KO"); } /*else { //lblInfo.Text = "Ready..."; }*/ userStoreProt = new TBinaryProtocol(new THttpClient(userStoreUrl)); userStore = new UserStore.Client(userStoreProt, userStoreProt); try { notestoreUrl = new Uri(userStore.getNoteStoreUrl(myParams.DevKey)); // Set up the NoteStore client noteStoreTrans = new THttpClient(notestoreUrl); //noteStoreTrans.setCustomHeader("User-Agent", "Calexo CaMuMa"); //noteStoreTrans.CustomHeaders.Add( noteStoreProt = new TBinaryProtocol(noteStoreTrans); noteStore = new NoteStore.Client(noteStoreProt, noteStoreProt); authToken = myParams.DevKey; notebooks = noteStore.listNotebooks(authToken); Console.WriteLine("Found " + notebooks.Count + " notebooks"); //musicNotebook = notebooks[0]; cmbNotebook.Items.Clear(); foreach (Notebook notebook in notebooks) { // Console.WriteLine(" * " + notebook.Name); cmbNotebook.Items.Add(notebook.Name); //if (notebook.DefaultNotebook) if (notebook.Name == myParams.Notebook) { musicNotebook = notebook; cmbNotebook.Text = notebook.Name; } } Application.DoEvents(); tags = noteStore.listTags(authToken); Console.WriteLine("Found " + tags.Count + " tags"); lblInfo.Text = "Ready !"; setWorkingMode(false); } catch (Evernote.EDAM.Error.EDAMUserException e) { //mes e.ErrorCode; lblInfo.Text = "ERREUR : " + e.ErrorCode; setWorkingMode(false); btnGo.Enabled = false; } this.Cursor = DefaultCursor; }
private void launch(BackgroundWorker worker, DoWorkEventArgs e) { //this.pbProgress.Value = 0; //SetProgress(0); //frmMain frmMainE = (frmMain)e.Argument; myParams = (Parameters)e.Argument; ProgressStatusClass myProgress = new ProgressStatusClass(); // List of listed extensions when Only Music Files is Checked List<string> sExtOK = new List<string>(); sExtOK.Add(".flac"); sExtOK.Add(".mp3"); sExtOK.Add(".ogg"); sExtOK.Add(".wma"); sExtOK.Add(".alac"); foreach (Notebook notebook in notebooks) { if (notebook.Name == myParams.Notebook) { musicNotebook = notebook; } } // Liste des M-0Top if (chkRegenLists.Checked) { //TextWriter tw_lst = new StreamWriter(myParams.Folder + "\\camuma-top.lst", false, Encoding.Default); TextWriter tw_lst = new StreamWriter(myParams.Folder + "\\camuma-top.lst", false, Encoding.GetEncoding(850)); NoteFilter filter = new NoteFilter(); filter.NotebookGuid = musicNotebook.Guid; //filter.Words = "intitle:\"" + dir.Name + "\""; //any: "Camuma ID: 153266" intitle:"Muse - 1999 - Showbiz" //filter.Words = "\"CaMuMa ID: " + sId + "\""; // Recherche par CaMuMa ID ou Titre //filter.Words = "any: \"CaMuMa ID: " + sId + "\" intitle:\"" + dir.Name + "\""; filter.Words = "tag:M-0Top"; NotesMetadataResultSpec spec = new NotesMetadataResultSpec(); spec.IncludeTitle = true; int pageSize = 10; int offset = 0; NotesMetadataList notes = noteStore.findNotesMetadata(authToken, filter, 0, pageSize, spec); do { notes = noteStore.findNotesMetadata(authToken, filter, offset, pageSize, spec); foreach (NoteMetadata note in notes.Notes) { Console.WriteLine("M-0Top : " + note.Title); tw_lst.WriteLine(note.Title); } offset = offset + notes.Notes.Count; } while (notes.TotalNotes > offset); tw_lst.Close(); } //Liste des M-0Top /*if (chkPdf.Checked) { PdfDocument pdf = new PdfDocument(); pdf.PageLayout = PdfPageLayout.TwoColumnLeft; //pdf.PageMode = PdfPageMode. pdf.Info.Title = "CaMuMa List"; //pdf.Info.Producer = "Calexo CaMuMa"; pdf.Info.Creator = "Calexo CaMuMa"; }*/ // Listing all directories DirectoryInfo dir = new DirectoryInfo(myParams.Folder); DirectoryInfo dir2; //String sMsg=""; Int32 i = 0; foreach (DirectoryInfo f in dir.GetDirectories()) { this.sFolderList[i] = f.FullName; dir2 = new DirectoryInfo(this.sFolderList[i]); foreach (DirectoryInfo f2 in dir2.GetDirectories()) { i++; this.sFolderList[i] = f2.FullName; } i++; myProgress.Info = i + " directories..."; worker.ReportProgress(0, myProgress); } //TextWriter tw = new StreamWriter(myParams.Folder + "\\camuma.lst", false, System.Text.Encoding.Unicode); //TextWriter tw = new StreamWriter(myParams.Folder + "\\camuma.lst", false, Encoding.GetEncoding(65001)); //TextWriter tw = new StreamWriter(myParams.Folder + "\\camuma.lst", false, Encoding.BigEndianUnicode); TextWriter tw = new StreamWriter(myParams.Folder + "\\camuma.lst", false, Encoding.Default); // Checking each directory // Checking if MP3 or Flac is here // Adding note bool isFlac, isMP3, isAlac, isWma; bool isFolderJPG; bool isSpotify; String sFileList; String sContent = ""; String sFolderJPG = ""; Int32 j = 0; String sId = ""; List<string> lTagNames; String sArtist, sAlbum, sYear; //, sGenre; Boolean isParsed; Regex oRegex; MatchCollection oMatchCollection; String regArtist = @"^(?<ARTIST>([\w\'\(\)\&\, -])*([\w\'\(\)\&\,]))"; String regAlbum = @"(?<ALBUM>([\w\'\(\)\-\&\,\. ])+)$"; String regYear = @"(?<YEAR>([0-9]{4}))"; String regDash = "[ ]+-[ ]+"; foreach (String sDirName in sFolderList) { if (worker.CancellationPending == true) { e.Cancel = true; break; } if (sDirName != null) { dir = new DirectoryInfo(sDirName); isFlac = false; isMP3 = false; isWma = false; isAlac = false; isSpotify = false; isFolderJPG = false; sFolderJPG = ""; sFileList = "<div ><ul>";// "<div><b>" + WebUtility.HtmlEncode("Liste des fichiers :") + "</b></div>"; sContent = ""; sArtist = ""; sAlbum = ""; sYear = ""; //sGenre = ""; isParsed = false; if (chkAddIdTags.Checked) { if (File.Exists(dir.FullName + "\\camuma.id")) { TextReader tr = new StreamReader(dir.FullName + "\\camuma.id"); sId = tr.ReadLine().Trim(); } else { Random rRnd = new Random(); int iRnd = rRnd.Next(1001, 999999); sId = iRnd.ToString().PadLeft(6,'0'); System.IO.File.WriteAllText(@dir.FullName + "\\camuma.id", sId); } // Ajout au camuma.lst tw.WriteLine(sId + ":" + dir.FullName.Replace(myParams.Folder+"\\","")); //tw.Flush(); if (!File.Exists(dir.FullName + "\\camuma.png")) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = new QrCode(); //QrCode qrCode = qrEncoder.Encode(sId, out qrCode); qrEncoder.TryEncode("camuma://" + sId, out qrCode); //GraphicsRenderer renderer = new GraphicsRenderer(5, Brushes.Black, Brushes.White); GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Zero), Brushes.Black, Brushes.White); //int pixelSize = renderer.Measure( qrCode.Matrix.Width).Width; //WriteableBitmap wBitmap = new WriteableBitmap(pixelSize, pixelSize, 96, 96, PixelFormats.Gray8, null); //renderer.CreateImageFile(qrCode.Matrix, @dir.FullName + "\\camuma.png", ImageFormat.Png); using (FileStream stream = new FileStream(@dir.FullName + "\\camuma.png", FileMode.Create)) { renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream); } SizeF size; Font f = new Font(FontFamily.GenericSansSerif,20); // creates 1Kx1K image buffer and uses it to find out how bit the image needs to be to fit the text using (Image imageg = (Image)new Bitmap(1000, 1000)) size = Graphics.FromImage(imageg).MeasureString(sId, f); using (Bitmap image = new Bitmap((int)size.Width, (int)size.Height)) //,, PixelFormat.Format32bppArgb)) { Graphics g = Graphics.FromImage((Image)image); //g.TranslateTransform(image.Width, image.Height); //g.RotateTransform(180.0F); //note that we need the rotation as the default is down //Color colBG = new Color(); //colBG. g.Clear(ColorTranslator.FromHtml("#ABDA4C")); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; // draw text g.DrawString(sId, f, Brushes.Black, 0f, 0f); //note that this image has to be a PNG, as GDI+'s gif handling renders any transparency as black. //context.Response.AddHeader("ContentType", "image/png"); using (MemoryStream memStream = new MemoryStream()) { //note that context.Response.OutputStream doesn't support the Save, but does support WriteTo image.Save(memStream, ImageFormat.Png); //memStream.WriteTo(context.Response.OutputStream); System.IO.File.WriteAllBytes(@dir.FullName + "\\camumaid.png", memStream.ToArray()); } } } } foreach (FileInfo f in dir.GetFiles()) { //if (f.Extension.Equals("flac", StringComparison.CurrentCultureIgnoreCase) if (f.Extension.Equals(".flac", StringComparison.CurrentCultureIgnoreCase)) isFlac = true; else if (f.Extension.Equals(".mp3", StringComparison.CurrentCultureIgnoreCase)) isMP3 = true; else if (f.Extension.Equals(".wma", StringComparison.CurrentCultureIgnoreCase)) isWma = true; else if (f.Extension.Equals(".alac", StringComparison.CurrentCultureIgnoreCase)) isAlac = true; else if (f.Name.Equals("folder.jpg", StringComparison.CurrentCultureIgnoreCase)) { isFolderJPG = true; sFolderJPG = f.FullName; } if (!chkMusicFiles.Checked || sExtOK.Contains(f.Extension.ToLower())) { sFileList += "<li>" + WebUtility.HtmlEncode(f.Name) + "</li>"; } } sContent = "<h1>" + WebUtility.HtmlEncode(dir.Name) + "</h1>"; sContent += sFileList + "</ul></div>"; if (isMP3 || isFlac || isWma || isAlac) { j++; myProgress.ProgressValue = j; myProgress.Info = j + "/" + i +" \nAlbum..."; worker.ReportProgress(j, myProgress); //Info("Creating Note... " + j + "/" + i); lTagNames = new List<string>(); lTagNames.Add("M-Support:Cowon"); lTagNames.Add("M-Support:NAS2"); lTagNames.Add("M-Support:WD"); lTagNames.Add("M-Album"); if (isMP3) lTagNames.Add("M-MP3"); if (isFlac) lTagNames.Add("M-FLAC"); if (isAlac) lTagNames.Add("M-ALAC"); if (isWma) lTagNames.Add("M-WMA"); if (isFolderJPG) lTagNames.Add("M-Cover"); else lTagNames.Add("M-NoCover"); Console.WriteLine(dir.Name + "..."); // ARTIST - YEAR - ALBUM oRegex = new Regex(regArtist + regDash + regYear + regDash + regAlbum); oMatchCollection = oRegex.Matches(dir.Name); foreach (Match oMatch in oMatchCollection) { Console.WriteLine(" * " + oMatch.Groups["ARTIST"] + ":" + oMatch.Groups["ALBUM"] + "(" + oMatch.Groups["YEAR"] + ")"); sArtist = oMatch.Groups["ARTIST"].ToString(); sYear = oMatch.Groups["YEAR"].ToString(); sAlbum = oMatch.Groups["ALBUM"].ToString(); isParsed = true; } // ARTIST - ALBUM - YEAR if (!isParsed) { oRegex = new Regex(regArtist + regDash + regAlbum + regDash + regYear); oMatchCollection = oRegex.Matches(dir.Name); foreach (Match oMatch in oMatchCollection) { Console.WriteLine(" * " + oMatch.Groups["ARTIST"] + ":" + oMatch.Groups["ALBUM"] + "(" + oMatch.Groups["YEAR"] + ")"); sArtist = oMatch.Groups["ARTIST"].ToString(); sYear = oMatch.Groups["YEAR"].ToString(); sAlbum = oMatch.Groups["ALBUM"].ToString(); isParsed = true; } } // ARTIST - ALBUM if (!isParsed) { oRegex = new Regex(regArtist + regDash + regAlbum); oMatchCollection = oRegex.Matches(dir.Name); foreach (Match oMatch in oMatchCollection) { //lTagNames.Add (oMatch.Groups["ARTIST"].ToString()); Console.WriteLine(" * " + oMatch.Groups["ARTIST"] + ":" + oMatch.Groups["ALBUM"]); sArtist = oMatch.Groups["ARTIST"].ToString(); sAlbum = oMatch.Groups["ALBUM"].ToString(); isParsed = true; } } if (isParsed) { sArtist = sArtist.Trim(); sYear = sYear.Trim(); sAlbum = sAlbum.Trim(); if (!String.IsNullOrWhiteSpace(sArtist)) lTagNames.Add("M-Artist:" + sArtist); if (!String.IsNullOrWhiteSpace(sYear)) lTagNames.Add("M-Year:" + sYear); else lTagNames.Add("M-Year:None"); //if (!String.IsNullOrWhiteSpace(sAlbum)) lTagNames.Add("M-Album:" + sAlbum); myProgress.Info = j + "/" + i + "\n" + "Album : " + sArtist + ":" + sAlbum + "... "; worker.ReportProgress(j, myProgress); } else lTagNames.Add("M-NotParsed"); // Spotify if (chkSpotify.Checked) { try { SearchResults<Album> spoRes = Search.SearchAlbums(dir.Name); if (spoRes.SearchResultsPage.Length > 0) { Album album = spoRes.SearchResultsPage.First(); Console.WriteLine(" * Spotify : " + album.Artist + " - " + album.Name + " (" + album.Url + ")"); sContent += "<div><br/></div>"; sContent += "<h2>Liens</h2>"; sContent += "<div><strong>Spotify</strong> : "; sContent += "<a href=\"" + album.Url + "\">"; sContent += WebUtility.HtmlEncode(album.Artist + " - " + album.Name + " : " + album.Url); sContent += "</a>"; sContent += "</div>"; lTagNames.Add("M-Spotify"); isSpotify = true; } } catch (WebException we) { Console.WriteLine(" * Spotify : " + we.Message); } catch (System.FormatException we) { Console.WriteLine(" * Spotify : " + we.Message); } } // chkSpotify true /*foreach (Album album in spoRes.SearchResultsPage) { Console.WriteLine(" * Spotify : " + album.Artist + " - " + album.Name + " (" + album.Url + ")"); }*/ // Add CaMuMa Id sContent += "<div><p><i>CaMuMa ID:</i> <a href=\"camuma://" + sId + "\">" + sId + "</a></p>"; byte[] image = ReadFully(File.OpenRead(@dir.FullName + "\\camuma.png")); byte[] hashQR = new MD5CryptoServiceProvider().ComputeHash(image); String hashHexQR = BitConverter.ToString(hashQR).Replace("-", "").ToLower(); //sContent += "<a href=\"camuma://" + sId + "\">"; sContent += "<en-media type=\"image/png\" hash=\"" + hashHexQR + "\"/>"; sContent += "</div>"; Data data = new Data(); data.Size = image.Length; data.BodyHash = hashQR; data.Body = image; Resource imgQR = new Resource(); imgQR.Mime = "image/png"; imgQR.Data = data; Console.WriteLine(@dir.FullName + "\\camuma.png -> " + hashHexQR); // On ajoute si on n'est pas dans le Action AddSpotify // ou si il y a effectivement du Spotify if (chkEvernote.Checked) { if (!rbActSpotify.Checked || isSpotify) { // On recherche la note avec le même titre NoteFilter filter = new NoteFilter(); filter.NotebookGuid = musicNotebook.Guid; //filter.Words = "intitle:\"" + dir.Name + "\""; //any: "Camuma ID: 153266" intitle:"Muse - 1999 - Showbiz" //filter.Words = "\"CaMuMa ID: " + sId + "\""; // Recherche par CaMuMa ID ou Titre //filter.Words = "any: \"CaMuMa ID: " + sId + "\" intitle:\"" + dir.Name + "\""; filter.Words = "\"CaMuMa ID: " + sId; NotesMetadataResultSpec spec = new NotesMetadataResultSpec(); spec.IncludeTitle = true; int pageSize = 10; NotesMetadataList notes = noteStore.findNotesMetadata(authToken, filter, 0, pageSize, spec); // Non existing Note if (notes.TotalNotes == 0) { // If Adding ou Replacing if (rbAdd.Checked || rbReplace.Checked) { // Creating new note createNote(sFolderJPG, sContent, dir.Name, lTagNames, imgQR); Console.WriteLine(" * Creation"); } } else if (notes.TotalNotes == 1) { NoteMetadata noteToMod = notes.Notes.First(); Console.WriteLine(" * Existing..."); if (rbReplace.Checked) { Note note = noteStore.getNote(authToken, noteToMod.Guid, true, true, false, false); String hashHex = ""; // Suppr tags négatifs (NotParsed, ...) note.TagGuids.Remove(getTagGuid("M-NotParsed")); note.TagGuids.Remove(getTagGuid("M-NoCover")); // Adding new tags note.TagNames = lTagNames; byte[] hash = null; if (sFolderJPG != "") { Console.WriteLine(" * folder.jpg exists"); image = ReadFully(File.OpenRead(sFolderJPG)); hash = new MD5CryptoServiceProvider().ComputeHash(image); hashHex = BitConverter.ToString(hash).Replace("-", "").ToLower(); } if (note.Content != nodeContentEnrich(sContent, hashHex)) { Console.WriteLine(" * Different contents"); note.Content = nodeContentEnrich(sContent, hashHex); note.Resources = new List<Resource>(); if (hash != null) { Console.WriteLine(" * Adding folder image"); Data dataF = new Data(); dataF.Size = image.Length; dataF.BodyHash = hash; dataF.Body = image; Resource resource = new Resource(); resource.Mime = "image/jpg"; resource.Data = dataF; note.Resources.Add(resource); } note.Resources.Add(imgQR); Console.WriteLine("Res : " + note.Resources.Count); note.UpdateSequenceNum = 0; note.Updated = 0; Note newNote = noteStore.updateNote(authToken, note); if (newNote != null) { Console.WriteLine(" * Modified"); Console.WriteLine("Res : " + newNote.Resources.Count); //Console.WriteLine(newNote.Title); //Console.WriteLine(newNote.Updated); //Console.WriteLine(note.UpdateSequenceNum + " -> " + newNote.UpdateSequenceNum); } else { Console.WriteLine(" * Modification failed"); } } } } // TotalNotes=1 else // More than 1 matches { createNote(sFolderJPG, sContent, dir.Name, lTagNames, imgQR); Console.WriteLine(" * ERROR - Multiple found"); } } // ActSpotify } // chkEvernote } else { i--; } } //this.pbProgress.Maximum = i; //pbProgress.Value = j; myProgress.ProgressMax = i; myProgress.ProgressValue = j; worker.ReportProgress(j, myProgress); } //Info("OK"); tw.Close(); }