コード例 #1
0
ファイル: MainFrm.cs プロジェクト: codemonkey85/evimsync
        private string ExtractNotes(string notebook)
        {
            if (cancelled)
            {
                return(null);
            }

            ENScriptWrapper enscript = new ENScriptWrapper();

            enscript.ENScriptPath = enscriptpath;

            string exportFile = Path.GetTempFileName();

#if DEBUG
            exportFile = @"D:\Development\evimsync\" + notebook + ".xml";
#endif
            if (enscript.ExportNotebook(notebook, exportFile))
            {
                return(exportFile);
            }

            // in case the selected notebook is empty, we don't get
            // an exportFile. But just to make sure the notebook
            // exists anyway, we check that here before giving up
            if (enscript.GetNotebooks().Contains(notebook))
            {
                return(string.Empty);
            }

            return(null);
        }
コード例 #2
0
ファイル: MainFrm.cs プロジェクト: codemonkey85/evimsync
        private void DownloadAndImportMailsToEvernote(List <Note> notesIMAP, List <Note> notesEvernote, string notebook)
        {
            int counter          = 0;
            int numNotesToUpload = 0;

            foreach (Note ntu in notesIMAP)
            {
                if (ntu.Action == NoteAction.ImportToEvernote)
                {
                    numNotesToUpload++;
                }
            }

            while (notesIMAP.Count > 0)
            {
                if (cancelled)
                {
                    break;
                }

                Note n = notesIMAP[0];
                if (n.Action == NoteAction.ImportToEvernote)
                {
                    IMessage msg = n.IMAPMessages[0];
                    SetInfo(null, string.Format("getting email ({0} of {1}) : \"{2}\"", counter + 1, numNotesToUpload, msg.Subject), counter++, numNotesToUpload);

                    FullMessageRequest fmr = new FullMessageRequest(client, msg);

                    // fmr.MessageProgress += new FullMessageProgressCallback(fmr_MessageProgress);
                    fmr.SubmitAndWait();
                    if (msg.ContentLoaded)
                    {
                        IMessageContent[] content = msg.MessageContent;
                        foreach (IMessageContent msgcontent in content)
                        {
                            if (!msgcontent.IsAttachment)
                            {
                                if ((msgcontent.TextData != null) && (msgcontent.TextData.Length > 0) && ((n.ContentHash == string.Empty) || (n.Content == null)))
                                {
                                    n.SetTextContent(msgcontent.TextData);
                                }
                                else if ((msgcontent.HTMLData != null) && ((msgcontent.HTMLData.Length > 0) || (n.Content == null)))
                                {
                                    n.SetHtmlContent(msgcontent.HTMLData);
                                }
                                else if ((msgcontent.ContentFilename != null) && (msgcontent.ContentFilename.Length > 0))
                                {
                                    n.AddAttachment(System.Text.Encoding.ASCII.GetBytes(msgcontent.TextData), msgcontent.ContentId, msgcontent.ContentType, msgcontent.ContentFilename);
                                }

                                Debug.Assert(n.ContentHash != string.Empty, "Hash is empty!");
                            }
                            else
                            {
                                n.AddAttachment(msgcontent.BinaryData, msgcontent.ContentId, msgcontent.ContentType, msgcontent.ContentFilename);
                            }
                        }

                        n.Content = "<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" +
                                    "<en-note>" + n.Content + "</en-note>]]>";

                        // remove existing XEveIm flags
                        List <string> fls = new List <string>(msg.GetCustomFlags());
                        foreach (string flag in fls)
                        {
                            if (flag.ToLower().StartsWith("xeveim"))
                            {
                                if (flag.ToLower().Substring(6) != n.ContentHash.ToLower())
                                {
                                    client.MailboxManager.SetMessageFlag(msg, flag, false);
                                }
                            }
                        }

                        // add the date
                        n.Date = msg.DateReceived;

                        // update the XEveImHash tag for this email
                        string customFlag = "xeveim" + n.ContentHash;
                        msg.SetCustomFlag(customFlag, false);
                        if (client.MailboxManager.SetMessageFlag(msg, customFlag, true))
                        {
                            // sometimes it happens that the flag wasn't set, so now that we have
                            // the hash of the email, we check whether that note/email
                            // already exists in Evernote.
                            bool existsInEvernote = notesEvernote.Find(delegate(Note findNote) { return(findNote.ContentHash == n.ContentHash); }) != null;
                            if (!existsInEvernote)
                            {
                                // now, since GMail uses IMAP folders for tags and a message can have multiple tags,
                                // we have to see if the changed flag affected not just this IMAP message but
                                // others in other IMAP folders as well. If it has, those are the same message
                                // and we have to add those folder names to the tag list of this note.
                                List <Note> sameTitleNotes = notesIMAP.FindAll(delegate(Note findNote) { return(findNote.Title == n.Title); });
                                foreach (Note same in sameTitleNotes)
                                {
                                    IMessage m = same.IMAPMessages[0];
                                    client.RequestManager.SubmitAndWait(new MessageFlagRequest(m, null), false);
                                    string        hash  = null;
                                    List <string> flags = m.GetCustomFlags();
                                    foreach (string flag in flags)
                                    {
                                        if (flag.ToLower().StartsWith("xeveim"))
                                        {
                                            hash = flag.Substring(6);
                                            break;
                                        }
                                    }

                                    if ((hash != null) && (hash == n.ContentHash))
                                    {
                                        // yes, this is the same message!
                                        // remove it from the list and add its folder name as a tag
                                        // to this note
                                        if (n != same)
                                        {
                                            string tag = m.Folder.FullPath;
                                            if (tag.IndexOf('/') >= 0)
                                            {
                                                tag = tag.Substring(tag.IndexOf('/') + 1);
                                            }
                                            else
                                            {
                                                tag = string.Empty;
                                            }
                                            n.Tags.Add(tag);
                                            n.IMAPMessages.Add(m);
                                            notesIMAP.Remove(same);
                                        }
                                    }
                                }

                                // generate the Evernote export file
                                string path = Path.GetTempFileName();
#if DEBUG
                                path = @"D:\Development\evimsync\email.xml";
#endif
                                n.SaveEvernoteExportData(path);

                                // import the export file into Evernote
                                ENScriptWrapper enscript = new ENScriptWrapper();
                                enscript.ENScriptPath = enscriptpath;
                                if (enscript.ImportNotes(path, notebook))
                                {
                                    notesEvernote.Add(n);
                                }
                                else
                                {
                                    // failed to import note
                                }

                                File.Delete(path);
                            }
                            else
                            {
                                //Debug.Assert(false);
                            }
                        }
                    }
                }

                notesIMAP.Remove(n);
            }
        }
コード例 #3
0
ファイル: MainFrm.cs プロジェクト: gavioto/evimsync
        private string ExtractNotes(string notebook)
        {
            if (cancelled)
            {
                return null;
            }

            ENScriptWrapper enscript = new ENScriptWrapper();
            enscript.ENScriptPath = enscriptpath;

            string exportFile = Path.GetTempFileName();
            #if DEBUG
            exportFile = @"D:\Development\evimsync\" + notebook + ".xml";
            #endif
            if (enscript.ExportNotebook(notebook, exportFile))
            {
                return exportFile;
            }

            // in case the selected notebook is empty, we don't get
            // an exportFile. But just to make sure the notebook
            // exists anyway, we check that here before giving up
            if (enscript.GetNotebooks().Contains(notebook))
                return string.Empty;

            return null;
        }
コード例 #4
0
ファイル: MainFrm.cs プロジェクト: gavioto/evimsync
        private void DownloadAndImportMailsToEvernote(List<Note> notesIMAP, List<Note> notesEvernote, string notebook)
        {
            int counter = 0;
            int numNotesToUpload = 0;
            foreach (Note ntu in notesIMAP)
            {
                if (ntu.Action == NoteAction.ImportToEvernote)
                    numNotesToUpload++;
            }

            while (notesIMAP.Count > 0)
            {
                if (cancelled)
                {
                    break;
                }

                Note n = notesIMAP[0];
                if (n.Action == NoteAction.ImportToEvernote)
                {
                    IMessage msg = n.IMAPMessages[0];
                    SetInfo(null, string.Format("getting email ({0} of {1}) : \"{2}\"", counter + 1, numNotesToUpload, msg.Subject), counter++, numNotesToUpload);

                    FullMessageRequest fmr = new FullMessageRequest(client, msg);

                    // fmr.MessageProgress += new FullMessageProgressCallback(fmr_MessageProgress);
                    fmr.SubmitAndWait();
                    if (msg.ContentLoaded)
                    {
                        IMessageContent[] content = msg.MessageContent;
                        foreach (IMessageContent msgcontent in content)
                        {
                            if (!msgcontent.IsAttachment)
                            {
                                if ((msgcontent.TextData != null) && (msgcontent.TextData.Length > 0) && ((n.ContentHash == string.Empty) || (n.Content == null)))
                                {
                                    n.SetTextContent(msgcontent.TextData);
                                }
                                else if ((msgcontent.HTMLData != null) && ((msgcontent.HTMLData.Length > 0) || (n.Content == null)))
                                {
                                    n.SetHtmlContent(msgcontent.HTMLData);
                                }
                                else if ((msgcontent.ContentFilename != null) && (msgcontent.ContentFilename.Length > 0))
                                {
                                    n.AddAttachment(System.Text.Encoding.ASCII.GetBytes(msgcontent.TextData), msgcontent.ContentId, msgcontent.ContentType, msgcontent.ContentFilename);
                                }

                                Debug.Assert(n.ContentHash != string.Empty, "Hash is empty!");
                            }
                            else
                            {
                                n.AddAttachment(msgcontent.BinaryData, msgcontent.ContentId, msgcontent.ContentType, msgcontent.ContentFilename);
                            }
                        }

                        n.Content = "<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" +
                                        "<en-note>" + n.Content + "</en-note>]]>";

                        // remove existing XEveIm flags
                        List<string> fls = new List<string>(msg.GetCustomFlags());
                        foreach (string flag in fls)
                        {
                            if (flag.ToLower().StartsWith("xeveim"))
                            {
                                if (flag.ToLower().Substring(6) != n.ContentHash.ToLower())
                                    client.MailboxManager.SetMessageFlag(msg, flag, false);
                            }
                        }

                        // add the date
                        n.Date = msg.DateReceived;

                        // update the XEveImHash tag for this email
                        string customFlag = "xeveim" + n.ContentHash;
                        msg.SetCustomFlag(customFlag, false);
                        if (client.MailboxManager.SetMessageFlag(msg, customFlag, true))
                        {
                            // sometimes it happens that the flag wasn't set, so now that we have
                            // the hash of the email, we check whether that note/email
                            // already exists in Evernote.
                            bool existsInEvernote = notesEvernote.Find(delegate(Note findNote) { return findNote.ContentHash == n.ContentHash; }) != null;
                            if (!existsInEvernote)
                            {
                                // now, since GMail uses IMAP folders for tags and a message can have multiple tags,
                                // we have to see if the changed flag affected not just this IMAP message but
                                // others in other IMAP folders as well. If it has, those are the same message
                                // and we have to add those folder names to the tag list of this note.
                                List<Note> sameTitleNotes = notesIMAP.FindAll(delegate(Note findNote) { return findNote.Title == n.Title; });
                                foreach (Note same in sameTitleNotes)
                                {
                                    IMessage m = same.IMAPMessages[0];
                                    client.RequestManager.SubmitAndWait(new MessageFlagRequest(m, null), false);
                                    string hash = null;
                                    List<string> flags = m.GetCustomFlags();
                                    foreach (string flag in flags)
                                    {
                                        if (flag.ToLower().StartsWith("xeveim"))
                                        {
                                            hash = flag.Substring(6);
                                            break;
                                        }
                                    }

                                    if ((hash != null) && (hash == n.ContentHash))
                                    {
                                        // yes, this is the same message!
                                        // remove it from the list and add its folder name as a tag
                                        // to this note
                                        if (n != same)
                                        {
                                            string tag = m.Folder.FullPath;
                                            if (tag.IndexOf('/') >= 0)
                                            {
                                                tag = tag.Substring(tag.IndexOf('/') + 1);
                                            }
                                            else
                                            {
                                                tag = string.Empty;
                                            }
                                            n.Tags.Add(tag);
                                            n.IMAPMessages.Add(m);
                                            notesIMAP.Remove(same);
                                        }
                                    }
                                }

                                // generate the Evernote export file
                                string path = Path.GetTempFileName();
            #if DEBUG
                                path = @"D:\Development\evimsync\email.xml";
            #endif
                                n.SaveEvernoteExportData(path);

                                // import the export file into Evernote
                                ENScriptWrapper enscript = new ENScriptWrapper();
                                enscript.ENScriptPath = enscriptpath;
                                if (enscript.ImportNotes(path, notebook))
                                {
                                    notesEvernote.Add(n);
                                }
                                else
                                {
                                    // failed to import note
                                }

                                File.Delete(path);
                            }
                            else
                            {
                                //Debug.Assert(false);
                            }
                        }
                    }
                }

                notesIMAP.Remove(n);
            }
        }