From 7df4b41328242918b7201d12d80a7fbc7f1e025e Mon Sep 17 00:00:00 2001 From: Aleksas Pantechovskis Date: Sun, 15 Jan 2012 20:48:06 +0100 Subject: [PATCH] Improve the auto-signature insertion algorithm * Append the signature only after the e-mail Reader thread will finish it's work. This prevent from inserting the signature in wrong place; * Added extra lines between original e-mail quote and the signature for more comfortable editing of replies. * This fix was made during GCI2011; * This fixes #4590. Signed-off-by: Siarzhuk Zharski --- src/apps/mail/Content.cpp | 39 ++++++++++++++++++++++++++++-------- src/apps/mail/Content.h | 1 + src/apps/mail/MailWindow.cpp | 17 +++++----------- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/apps/mail/Content.cpp b/src/apps/mail/Content.cpp index b0c90c6027..28a3687580 100644 --- a/src/apps/mail/Content.cpp +++ b/src/apps/mail/Content.cpp @@ -702,6 +702,13 @@ TContentView::MessageReceived(BMessage *msg) case M_SIGNATURE: { + if (fTextView->IsReaderThreadRunning()) { + // Do not add the signature until the reader thread + // is finished. Resubmit the message for later processing + Window()->PostMessage(msg); + break; + } + entry_ref ref; msg->FindRef("ref", &ref); @@ -727,21 +734,23 @@ TContentView::MessageReceived(BMessage *msg) const char *text = fTextView->Text(); int32 length = fTextView->TextLength(); - if (length && text[length - 1] != '\n') { - fTextView->Select(length, length); + // reserve some empty lines before the signature + const char* newLines = "\n\n\n\n"; + if (length && text[length - 1] == '\n') + newLines++; - char newLine = '\n'; - fTextView->Insert(&newLine, 1); - - length++; - } + fTextView->Select(length, length); + fTextView->Insert(newLines, strlen(newLines)); + length += strlen(newLines); + // append the signature fTextView->Select(length, length); fTextView->Insert(signature, bytesRead); fTextView->Select(length, length + bytesRead); fTextView->ScrollToSelection(); - fTextView->Select(start, finish); + // set the editing cursor position + fTextView->Select(length - 2 , length - 2); fTextView->ScrollToSelection(); free (signature); } else { @@ -2086,6 +2095,20 @@ TTextView::StopLoad() } +bool +TTextView::IsReaderThreadRunning() +{ + if (fThread == 0) + return false; + + thread_info info; + for (int i = 5; i > 0; i--, usleep(100000)) + if (get_thread_info(fThread, &info) != B_OK) + return false; + return true; +} + + void TTextView::AddAsContent(BEmailMessage *mail, bool wrap, uint32 charset, mail_encoding encoding) { diff --git a/src/apps/mail/Content.h b/src/apps/mail/Content.h index 7317b4308f..703e986e88 100644 --- a/src/apps/mail/Content.h +++ b/src/apps/mail/Content.h @@ -168,6 +168,7 @@ class TTextView : public BTextView { void Open(hyper_text*); status_t Save(BMessage *, bool makeNewFile = true); void StopLoad(); + bool IsReaderThreadRunning(); void AddAsContent(BEmailMessage *mail, bool wrap, uint32 charset, mail_encoding encoding); void CheckSpelling(int32 start, int32 end, int32 flags = S_CLEAR_ERRORS | S_SHOW_ERRORS); diff --git a/src/apps/mail/MailWindow.cpp b/src/apps/mail/MailWindow.cpp index 43a996bf16..399a1d3b0e 100644 --- a/src/apps/mail/MailWindow.cpp +++ b/src/apps/mail/MailWindow.cpp @@ -534,22 +534,15 @@ TMailWindow::TMailWindow(BRect rect, const char* title, TMailApp* app, // If we find the named query, add it to the text. BEntry entry; if (query.GetNextEntry(&entry) == B_NO_ERROR) { - off_t size; BFile file; file.SetTo(&entry, O_RDWR); if (file.InitCheck() == B_NO_ERROR) { - file.GetSize(&size); - char *str = (char *)malloc(size); - size = file.Read(str, size); + entry_ref ref; + entry.GetRef(&ref); - fContentView->fTextView->Insert(str, size); - fContentView->fTextView->GoToLine(0); - fContentView->fTextView->ScrollToSelection(); - - fStartingText = (char *)malloc(size - = strlen(fContentView->fTextView->Text()) + 1); - if (fStartingText != NULL) - strcpy(fStartingText, fContentView->fTextView->Text()); + BMessage msg(M_SIGNATURE); + msg.AddRef("ref", &ref); + PostMessage(&msg); } } else { char tempString [2048];