Exemple #1
0
        private void messageListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int uid = Int32.Parse(messageListBox.SelectedItem.ToString());
            UpdateLabel("Getting header for message id "+uid);
            IMessage msg = client.MailboxManager.GetMessageByUID(uid, folder.ID);

            FullMessageRequest fmr = new FullMessageRequest(client, msg);
            fmr.MessageComplete += new FullMessageCompleteCallback(fmr_MessageComplete);
            fmr.MessageProgress += new FullMessageProgressCallback(fmr_MessageProgress);
            fmr.Start();
            //IMAPRequestManager.SubmitRequest(new MessageHeaderRequest(msg, MessageHeaderComplete), false);
            //IMAPRequestManager.SubmitRequest(new MessageStructureRequest(msg, MessageStructureComplete), false);
            //client.RequestManager.SubmitBatchRequest(
            //    new SimpleBatchRequest(new IRequest[]
            //        {
            //            new MessageHeaderRequest(msg, MessageHeaderComplete),
            //            new MessageStructureRequest(msg, MessageStructureComplete)
            //        }), false);
        }
        /// <summary>
        /// Downloads every message in every folder in the system. Used for building the local cache
        /// </summary>
        /// <param name="progCallback"></param>
        /// <param name="completedCallback"></param>
        public void DownloadEntireAccount(AccountDownloadProgressCallback progCallback, AccountDownloadCompleteCallback completedCallback)
        {
            int messagesCompleted = 0;
            IFolder[] folderList = GetAllFolders();
            TimeSpan ts = new TimeSpan();
            int totalMessages = GetMessageCount();
            ManualResetEvent mre = new ManualResetEvent(false);
            foreach (IFolder folder in folderList)
            {
                foreach (IMessage msg in folder.Messages)
                {
                    FullMessageRequest fmr = new FullMessageRequest(_client, msg);
                    fmr.MessageComplete += delegate(IMessage m, TimeSpan totalTime)
                                               {
                                                   progCallback(++messagesCompleted, totalMessages, m.Folder);
                                                   StoreMessage(m);
                                                   ts.Add(totalTime);
                                                   if (messagesCompleted >= totalMessages)
                                                       mre.Set();
                                               };
                    fmr.MessageProgress += delegate(IMessage m, long receieved, long total)
                                               {
                                                   Console.CursorLeft = 0;
                                                   Console.Write("{0}/{1}k downloaded", receieved / 1024, total / 1024);
                                               };
                    fmr.MessageProgress += delegate(IMessage m, long bytesReceived, long totalBytes) { };
                    fmr.Start();
                }
            }

            mre.WaitOne();

            completedCallback(folderList.Length, messagesCompleted, ts.Ticks);


        }
Exemple #3
0
        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);
            }
        }