-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAttachmentStripView.cpp
More file actions
1583 lines (1315 loc) · 40.3 KB
/
Copy pathAttachmentStripView.cpp
File metadata and controls
1583 lines (1315 loc) · 40.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* AttachmentStripView - Horizontal chip-based attachment display
* Distributed under the terms of the MIT License.
*/
#include "AttachmentStripView.h"
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <Application.h>
#include <Bitmap.h>
#include <Catalog.h>
#include <ControlLook.h>
#include <Cursor.h>
#include <Directory.h>
#include <Entry.h>
#include <FindDirectory.h>
#include <IconUtils.h>
#include <MenuItem.h>
#include <MimeType.h>
#include <Node.h>
#include <NodeInfo.h>
#include <Path.h>
#include <PopUpMenu.h>
#include <Roster.h>
#include <Size.h>
#include <Window.h>
#include <MailAttachment.h>
#include <MailContainer.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "AttachmentStripView"
// Static constants
const float AttachmentStripView::kMaxChipWidth = 200.0f;
const float AttachmentStripView::kChipPadding = 4.0f;
// Static helper function
BString
AttachmentStripView::_BytesToString(off_t bytes)
{
BString result;
if (bytes < 1024) {
result.SetToFormat("%lld B", bytes);
} else if (bytes < 1024 * 1024) {
result.SetToFormat("%.1f KB", bytes / 1024.0);
} else if (bytes < 1024 * 1024 * 1024) {
result.SetToFormat("%.1f MB", bytes / (1024.0 * 1024.0));
} else {
result.SetToFormat("%.1f GB", bytes / (1024.0 * 1024.0 * 1024.0));
}
return result;
}
AttachmentStripView::AttachmentStripView(bool composeMode)
:
BView("attachmentStrip", B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE | B_FRAME_EVENTS),
fAttachments(10),
fEmail(NULL),
fHtmlAlternative(NULL),
fHtmlAlternativeSize(0),
fChipHeight(28.0f),
fChipSpacing(6.0f),
fSelectedAttachment(-1),
fSavePanel(NULL),
fHoveredAttachment(-1),
fComposeMode(composeMode),
fDragStarted(false),
fMouseDown(false),
fMouseDownPoint(0, 0),
fMouseDownIndex(-1),
fDraggingAttachment(-1)
{
SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
}
AttachmentStripView::~AttachmentStripView()
{
free(fHtmlAlternative);
delete fSavePanel;
delete fEmail;
}
void
AttachmentStripView::GetPreferredSize(float* width, float* height)
{
if (fAttachments.CountItems() == 0) {
*width = 0;
*height = 0;
} else {
*width = B_SIZE_UNSET;
*height = _CalculateTotalHeight();
}
}
BSize
AttachmentStripView::MinSize()
{
if (fAttachments.CountItems() == 0)
return BSize(0, 0);
return BSize(100, _CalculateTotalHeight());
}
BSize
AttachmentStripView::MaxSize()
{
if (fAttachments.CountItems() == 0)
return BSize(B_SIZE_UNLIMITED, 0);
return BSize(B_SIZE_UNLIMITED, _CalculateTotalHeight());
}
void
AttachmentStripView::FrameResized(float newWidth, float newHeight)
{
BView::FrameResized(newWidth, newHeight);
// If the correct height differs from the current height, the layout was
// calculated with incorrect metrics (e.g. Bounds().Width() was -1 at
// layout time). Trigger a layout recalculation to correct it.
float correctHeight = _CalculateTotalHeight();
if (fAttachments.CountItems() > 0 && correctHeight != newHeight)
InvalidateLayout();
else
Invalidate();
}
float
AttachmentStripView::_CalculateChipWidth(int32 index) const
{
if (index < 0 || index >= fAttachments.CountItems())
return 0;
AttachmentInfo* info = fAttachments.ItemAt(index);
BFont font;
GetFont(&font);
// Calculate width needed for filename only (no size)
float textWidth = font.StringWidth(info->name.String());
float chipWidth = textWidth + 36; // 20px icon + 16px padding
// Cap at max width
return std::min(chipWidth, kMaxChipWidth);
}
float
AttachmentStripView::_CalculateTotalHeight() const
{
if (fAttachments.CountItems() == 0)
return 0;
float viewWidth = Bounds().Width();
if (viewWidth <= 0)
viewWidth = 400; // Default width for initial layout
float x = kChipPadding;
int32 rows = 1;
for (int32 i = 0; i < fAttachments.CountItems(); i++) {
float chipWidth = _CalculateChipWidth(i);
// Check if chip fits on current row
if (x + chipWidth + kChipPadding > viewWidth && x > kChipPadding) {
// Wrap to next row
rows++;
x = kChipPadding;
}
x += chipWidth + fChipSpacing;
}
// Height = top padding + rows * chip height + spacing between rows + bottom padding
return kChipPadding + (rows * fChipHeight) + ((rows - 1) * fChipSpacing) + kChipPadding;
}
BString
AttachmentStripView::_TruncateFilename(const char* filename, float maxWidth) const
{
BFont font;
GetFont(&font);
BString result(filename);
float width = font.StringWidth(result.String());
if (width <= maxWidth)
return result;
// Find the right length with ellipsis
const char* ellipsis = B_UTF8_ELLIPSIS;
float ellipsisWidth = font.StringWidth(ellipsis);
maxWidth -= ellipsisWidth;
int32 len = result.Length();
while (len > 0 && font.StringWidth(result.String(), len) > maxWidth) {
len--;
}
result.Truncate(len);
result.Append(ellipsis);
return result;
}
void
AttachmentStripView::SetAttachments(BEmailMessage* email)
{
// Clear previous state
fAttachments.MakeEmpty();
free(fHtmlAlternative);
fHtmlAlternative = NULL;
fHtmlAlternativeSize = 0;
delete fEmail;
fEmail = email; // Take ownership
if (fEmail == NULL) {
InvalidateLayout();
Invalidate();
return;
}
// Collect attachments using Mail Kit
BMailComponent* body = fEmail->Body();
for (int32 i = 0; i < fEmail->CountComponents(); i++) {
BMailComponent* component = fEmail->GetComponent(i);
if (component == body)
continue;
_CollectAttachments(component, body);
}
InvalidateLayout();
Invalidate();
}
void
AttachmentStripView::_CollectAttachments(BMailComponent* component,
BMailComponent* body)
{
// Skip the body component — it's the email text itself, not an attachment.
// Walk the MIME tree recursively: multipart containers are expanded,
// leaf components are checked for attachment-ness.
if (component == NULL || component == body)
return;
// If it's a multipart container, recurse into it
if (component->ComponentType() == B_MAIL_MULTIPART_CONTAINER) {
BMIMEMultipartMailContainer* container =
dynamic_cast<BMIMEMultipartMailContainer*>(component);
if (container != NULL) {
for (int32 i = 0; i < container->CountComponents(); i++) {
_CollectAttachments(container->GetComponent(i), body);
}
}
return;
}
// Check MIME type
BMimeType type;
BString mimeType;
if (component->MIMEType(&type) == B_OK) {
mimeType = type.Type();
}
// Get filename (if any)
BString filename;
BMailAttachment* attachment = dynamic_cast<BMailAttachment*>(component);
if (attachment != NULL) {
char name[B_FILE_NAME_LENGTH * 2];
if (attachment->FileName(name) == B_OK) {
filename = name;
}
}
// Check if this is an HTML alternative (text/html with no real filename)
// These are alternative body representations, not real attachments
if (mimeType.IFindFirst("text/html") >= 0 &&
(filename.Length() == 0 || filename.ICompare("unnamed") == 0)) {
// Store the HTML content for "View HTML version" feature
if (fHtmlAlternativeSize == 0 && fEmailPath.Length() > 0) {
// Try to extract raw HTML from email file to preserve original encoding
// This bypasses Mail Kit's UTF-8 conversion
if (ExtractRawHtmlFromEmail(fEmailPath.String(), &fHtmlAlternative, &fHtmlAlternativeSize)) {
// Success - raw bytes extracted
} else {
// Fallback to GetDecodedData (will be UTF-8 converted)
BMallocIO buffer;
if (component->GetDecodedData(&buffer) == B_OK && buffer.BufferLength() > 0) {
fHtmlAlternativeSize = buffer.BufferLength();
fHtmlAlternative = malloc(fHtmlAlternativeSize);
if (fHtmlAlternative != NULL) {
memcpy(fHtmlAlternative, buffer.Buffer(), fHtmlAlternativeSize);
} else {
fHtmlAlternativeSize = 0;
}
}
}
}
return; // Don't add to attachments list
}
// It's a real attachment - extract info
AttachmentInfo* info = new AttachmentInfo();
info->component = component;
info->componentIndex = fAttachments.CountItems();
info->mimeType = mimeType;
// Use filename if we got one
if (filename.Length() > 0) {
info->name = filename;
} else {
info->name = "unnamed";
}
// Get decoded size by decoding to a BMallocIO buffer
BMallocIO buffer;
if (component->GetDecodedData(&buffer) == B_OK) {
info->decodedSize = buffer.BufferLength();
} else {
info->decodedSize = 0;
}
fAttachments.AddItem(info);
}
void
AttachmentStripView::ClearAttachments()
{
fAttachments.MakeEmpty();
free(fHtmlAlternative);
fHtmlAlternative = NULL;
fHtmlAlternativeSize = 0;
delete fEmail;
fEmail = NULL;
fEmailPath = "";
InvalidateLayout();
Invalidate();
}
void
AttachmentStripView::AddAttachment(const entry_ref* ref)
{
if (ref == NULL || !fComposeMode)
return;
// Check if already added (by comparing refs)
for (int32 i = 0; i < fAttachments.CountItems(); i++) {
AttachmentInfo* existing = fAttachments.ItemAt(i);
if (existing->isFileRef && existing->ref == *ref)
return; // Already in list
}
BEntry entry(ref);
if (!entry.Exists() || !entry.IsFile())
return;
AttachmentInfo* info = new AttachmentInfo();
info->ref = *ref;
info->isFileRef = true;
info->name = ref->name;
// Get file size
BFile file(ref, B_READ_ONLY);
if (file.InitCheck() == B_OK) {
file.GetSize(&info->decodedSize);
}
// Get MIME type
BNode node(ref);
BNodeInfo nodeInfo(&node);
char mimeType[B_MIME_TYPE_LENGTH];
if (nodeInfo.GetType(mimeType) == B_OK) {
info->mimeType = mimeType;
} else {
info->mimeType = "application/octet-stream";
}
fAttachments.AddItem(info);
InvalidateLayout();
Invalidate();
// Notify window that attachments changed (for marking as changed)
if (Window())
Window()->PostMessage('atch'); // Custom message for attachment change
}
void
AttachmentStripView::RemoveAttachment(int32 index)
{
if (index < 0 || index >= fAttachments.CountItems() || !fComposeMode)
return;
fAttachments.RemoveItemAt(index);
InvalidateLayout();
Invalidate();
// Notify window that attachments changed
if (Window())
Window()->PostMessage('atch');
}
const entry_ref*
AttachmentStripView::AttachmentAt(int32 index) const
{
if (index < 0 || index >= fAttachments.CountItems())
return NULL;
AttachmentInfo* info = fAttachments.ItemAt(index);
if (info->isFileRef)
return &info->ref;
return NULL;
}
BMailComponent*
AttachmentStripView::ComponentAt(int32 index) const
{
if (index < 0 || index >= fAttachments.CountItems())
return NULL;
AttachmentInfo* info = fAttachments.ItemAt(index);
if (!info->isFileRef)
return info->component;
return NULL;
}
void
AttachmentStripView::AddEnclosuresFromMail(BEmailMessage* mail)
{
if (mail == NULL)
return;
BMailComponent* body = mail->Body();
for (int32 i = 0; i < mail->CountComponents(); i++) {
BMailComponent* component = mail->GetComponent(i);
if (component == body)
continue;
// Handle multipart containers recursively
if (component->ComponentType() == B_MAIL_MULTIPART_CONTAINER) {
BMIMEMultipartMailContainer* container =
dynamic_cast<BMIMEMultipartMailContainer*>(component);
if (container != NULL) {
for (int32 j = 0; j < container->CountComponents(); j++) {
BMailComponent* subComponent = container->GetComponent(j);
if (subComponent != body)
_AddComponentAsAttachment(subComponent);
}
}
continue;
}
_AddComponentAsAttachment(component);
}
// Show the strip if we added attachments
if (HasAttachments()) {
InvalidateLayout();
Invalidate();
}
}
void
AttachmentStripView::_AddComponentAsAttachment(BMailComponent* component)
{
if (component == NULL)
return;
AttachmentInfo* info = new AttachmentInfo();
info->component = component;
info->isFileRef = false;
info->componentIndex = fAttachments.CountItems();
// Get filename
BMailAttachment* attachment = dynamic_cast<BMailAttachment*>(component);
if (attachment != NULL) {
char name[B_FILE_NAME_LENGTH * 2];
if (attachment->FileName(name) == B_OK) {
info->name = name;
}
}
// If no filename, use "unnamed"
if (info->name.Length() == 0) {
info->name = "unnamed";
}
// Get MIME type
BMimeType type;
if (component->MIMEType(&type) == B_OK) {
info->mimeType = type.Type();
}
// Get decoded size
BMallocIO buffer;
if (component->GetDecodedData(&buffer) == B_OK) {
info->decodedSize = buffer.BufferLength();
} else {
info->decodedSize = 0;
}
fAttachments.AddItem(info);
}
void
AttachmentStripView::SetEmailPath(const char* path)
{
fEmailPath = path;
}
BRect
AttachmentStripView::_AttachmentRect(int32 index) const
{
if (index < 0 || index >= fAttachments.CountItems())
return BRect();
float viewWidth = Bounds().Width();
if (viewWidth <= 0)
viewWidth = 400; // Default for initial layout
float x = kChipPadding;
float y = kChipPadding;
// Calculate position by iterating through all chips up to this one
for (int32 i = 0; i <= index; i++) {
float chipWidth = _CalculateChipWidth(i);
// Check if chip fits on current row
if (x + chipWidth + kChipPadding > viewWidth && x > kChipPadding) {
// Wrap to next row
x = kChipPadding;
y += fChipHeight + fChipSpacing;
}
if (i == index) {
// This is our target chip
return BRect(x, y, x + chipWidth, y + fChipHeight);
}
x += chipWidth + fChipSpacing;
}
return BRect();
}
void
AttachmentStripView::Draw(BRect updateRect)
{
if (fAttachments.CountItems() == 0)
return;
BFont font;
GetFont(&font);
font_height fh;
font.GetHeight(&fh);
rgb_color bgColor = ui_color(B_PANEL_BACKGROUND_COLOR);
// Tint chip color based on whether background is dark or light
rgb_color chipColor;
if (bgColor.IsLight())
chipColor = tint_color(bgColor, B_DARKEN_1_TINT);
else
chipColor = tint_color(bgColor, B_LIGHTEN_1_TINT);
rgb_color textColor = ui_color(B_PANEL_TEXT_COLOR);
for (int32 i = 0; i < fAttachments.CountItems(); i++) {
AttachmentInfo* info = fAttachments.ItemAt(i);
BRect chipRect = _AttachmentRect(i);
// Skip if chip is outside update rect
if (!chipRect.Intersects(updateRect))
continue;
// Draw chip background
SetHighColor(chipColor);
FillRoundRect(chipRect, 4, 4);
// Draw MIME type icon (20x20 using vector icon for quality)
float iconX = chipRect.left + 4;
float iconY = chipRect.top + (chipRect.Height() - 20) / 2;
BMimeType mimeType(info->mimeType.String());
BBitmap* icon = new BBitmap(BRect(0, 0, 19, 19), B_RGBA32);
// Try to get vector icon and render at exact size
uint8* vectorData = NULL;
size_t vectorSize = 0;
bool gotIcon = false;
if (mimeType.GetIcon(&vectorData, &vectorSize) == B_OK && vectorData != NULL) {
if (BIconUtils::GetVectorIcon(vectorData, vectorSize, icon) == B_OK) {
gotIcon = true;
}
free(vectorData);
}
// Fallback: try supertype
if (!gotIcon) {
BMimeType superType;
if (mimeType.GetSupertype(&superType) == B_OK) {
if (superType.GetIcon(&vectorData, &vectorSize) == B_OK
&& vectorData != NULL) {
if (BIconUtils::GetVectorIcon(vectorData, vectorSize, icon) == B_OK) {
gotIcon = true;
}
free(vectorData);
}
}
}
// Final fallback: generic file icon (application/octet-stream)
if (!gotIcon) {
BMimeType genericType("application/octet-stream");
if (genericType.GetIcon(&vectorData, &vectorSize) == B_OK
&& vectorData != NULL) {
if (BIconUtils::GetVectorIcon(vectorData, vectorSize, icon) == B_OK) {
gotIcon = true;
}
free(vectorData);
}
}
if (gotIcon) {
SetDrawingMode(B_OP_ALPHA);
DrawBitmap(icon, BPoint(iconX, iconY));
SetDrawingMode(B_OP_COPY);
}
delete icon;
// Draw filename only (truncated if necessary, no size)
SetHighColor(textColor);
float textY = chipRect.top + (chipRect.Height() + fh.ascent - fh.descent) / 2;
float maxTextWidth = chipRect.Width() - 32; // Account for icon and padding
BString label = _TruncateFilename(info->name.String(), maxTextWidth);
DrawString(label.String(), BPoint(chipRect.left + 28, textY));
}
}
void
AttachmentStripView::MouseDown(BPoint where)
{
// Find which attachment was clicked
for (int32 i = 0; i < fAttachments.CountItems(); i++) {
BRect chipRect = _AttachmentRect(i);
if (chipRect.Contains(where)) {
// Check which button was pressed
int32 buttons;
Window()->CurrentMessage()->FindInt32("buttons", &buttons);
if (buttons & B_SECONDARY_MOUSE_BUTTON) {
// Right-click - show context menu
fSelectedAttachment = i;
BPopUpMenu* menu = new BPopUpMenu("attachmentMenu", false, false);
menu->AddItem(new BMenuItem(B_TRANSLATE("Open"),
new BMessage(MSG_ATTACHMENT_OPEN)));
if (fComposeMode) {
// Compose mode: show Remove option
menu->AddItem(new BMenuItem(B_TRANSLATE("Remove"),
new BMessage(MSG_ATTACHMENT_REMOVE)));
} else {
// Read mode: show Save as option
menu->AddItem(new BMenuItem(B_TRANSLATE("Save as" B_UTF8_ELLIPSIS),
new BMessage(MSG_ATTACHMENT_SAVE)));
}
menu->SetTargetForItems(this);
ConvertToScreen(&where);
menu->Go(where, true, true, true);
return;
}
// Left button - track for potential drag or double-click
int32 clicks;
if (Window()->CurrentMessage()->FindInt32("clicks", &clicks) == B_OK
&& clicks >= 2) {
_OpenAttachment(i);
return;
}
// Start tracking for drag
fMouseDown = true;
fDragStarted = false;
fMouseDownPoint = where;
fMouseDownIndex = i;
SetMouseEventMask(B_POINTER_EVENTS, B_NO_POINTER_HISTORY);
return;
}
}
}
void
AttachmentStripView::MouseMoved(BPoint where, uint32 transit,
const BMessage* dragMessage)
{
if (fMouseDown && !fDragStarted && fMouseDownIndex >= 0) {
// Check if mouse moved enough to start drag (5 pixels threshold)
float dx = where.x - fMouseDownPoint.x;
float dy = where.y - fMouseDownPoint.y;
if (dx * dx + dy * dy > 25) {
fDragStarted = true;
_InitiateDrag(fMouseDownIndex, where);
}
}
// Handle drag-and-drop feedback in compose mode
if (fComposeMode && dragMessage != NULL && dragMessage->HasRef("refs")) {
if (transit == B_ENTERED_VIEW || transit == B_INSIDE_VIEW) {
// Show copy cursor when dragging files over
BCursor cursor(B_CURSOR_ID_COPY);
SetViewCursor(&cursor);
} else if (transit == B_EXITED_VIEW) {
// Restore default cursor
SetViewCursor(B_CURSOR_SYSTEM_DEFAULT);
}
}
// Handle tooltip on hover
if (transit == B_EXITED_VIEW) {
// Mouse left the view
if (fHoveredAttachment >= 0) {
fHoveredAttachment = -1;
SetToolTip((const char*)NULL);
}
return;
}
// Find which attachment the mouse is over
int32 hoveredIndex = -1;
for (int32 i = 0; i < fAttachments.CountItems(); i++) {
BRect chipRect = _AttachmentRect(i);
if (chipRect.Contains(where)) {
hoveredIndex = i;
break;
}
}
// Update tooltip if hovered attachment changed
if (hoveredIndex != fHoveredAttachment) {
fHoveredAttachment = hoveredIndex;
if (fHoveredAttachment >= 0) {
AttachmentInfo* info = fAttachments.ItemAt(fHoveredAttachment);
BString tooltip;
tooltip << info->name << "\n" << _BytesToString(info->decodedSize);
SetToolTip(tooltip.String());
} else {
SetToolTip((const char*)NULL);
}
}
}
void
AttachmentStripView::MouseUp(BPoint where)
{
fMouseDown = false;
fDragStarted = false;
fMouseDownIndex = -1;
}
void
AttachmentStripView::MessageReceived(BMessage* message)
{
switch (message->what) {
case MSG_ATTACHMENT_OPEN:
if (fSelectedAttachment >= 0
&& fSelectedAttachment < fAttachments.CountItems()) {
_OpenAttachment(fSelectedAttachment);
}
break;
case MSG_ATTACHMENT_SAVE:
if (fSelectedAttachment >= 0
&& fSelectedAttachment < fAttachments.CountItems()) {
AttachmentInfo* info = fAttachments.ItemAt(fSelectedAttachment);
// Create file panel if needed
if (!fSavePanel) {
BMessenger messenger(this);
fSavePanel = new BFilePanel(B_SAVE_PANEL, &messenger, NULL,
B_FILE_NODE, false, new BMessage(MSG_ATTACHMENT_SAVE_PANEL));
}
// Set default filename
fSavePanel->SetSaveText(info->name.String());
fSavePanel->Show();
}
break;
case MSG_ATTACHMENT_SAVE_PANEL:
{
// User selected save location
entry_ref dirRef;
BString name;
if (message->FindRef("directory", &dirRef) == B_OK &&
message->FindString("name", &name) == B_OK) {
BPath path(&dirRef);
path.Append(name.String());
if (_ExtractAttachment(fSelectedAttachment, path.Path())) {
// Set MIME type on saved file
AttachmentInfo* info = fAttachments.ItemAt(fSelectedAttachment);
BNode node(path.Path());
BNodeInfo nodeInfo(&node);
nodeInfo.SetType(info->mimeType.String());
}
}
break;
}
case MSG_ATTACHMENT_REMOVE:
if (fComposeMode && fSelectedAttachment >= 0
&& fSelectedAttachment < fAttachments.CountItems()) {
RemoveAttachment(fSelectedAttachment);
fSelectedAttachment = -1;
}
break;
case B_SIMPLE_DATA:
case B_REFS_RECEIVED:
{
// Handle drag-and-drop of files (compose mode only)
if (!fComposeMode)
break;
entry_ref ref;
int32 index = 0;
while (message->FindRef("refs", index++, &ref) == B_OK) {
// Follow symlinks
BEntry entry(&ref, true);
entry.GetRef(&ref);
// Only accept files
if (entry.IsFile()) {
AddAttachment(&ref);
}
}
break;
}
case B_COPY_TARGET:
{
// Tracker is asking us to write the file to a specific location
entry_ref dirRef;
BString name;
if (message->FindRef("directory", &dirRef) == B_OK &&
message->FindString("name", &name) == B_OK &&
fDraggingAttachment >= 0
&& fDraggingAttachment < fAttachments.CountItems()) {
BPath path(&dirRef);
path.Append(name.String());
if (_ExtractAttachment(fDraggingAttachment, path.Path())) {
// Set MIME type on saved file - but not for emails
AttachmentInfo* info = fAttachments.ItemAt(fDraggingAttachment);
if (info->mimeType.IFindFirst("message/rfc822") < 0 &&
info->mimeType.IFindFirst("text/x-email") < 0) {
BNode node(path.Path());
BNodeInfo nodeInfo(&node);
nodeInfo.SetType(info->mimeType.String());
}
}
fDraggingAttachment = -1;
}
break;
}
default:
BView::MessageReceived(message);
break;
}
}
bool
AttachmentStripView::_ExtractAttachment(int32 index, const char* destPath)
{
if (index < 0 || index >= fAttachments.CountItems())
return false;
AttachmentInfo* info = fAttachments.ItemAt(index);
if (info->component == NULL)
return false;
// Create output file and write decoded data
{
BFile outFile(destPath, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
if (outFile.InitCheck() != B_OK)
return false;
// Use Mail Kit to decode and write the data
status_t result = info->component->GetDecodedData(&outFile);
if (result != B_OK)
return false;
outFile.Sync();
}
// File is now closed
// If this is an email attachment, parse it and add Haiku mail attributes
if (info->mimeType.IFindFirst("message/rfc822") >= 0 ||
info->mimeType.IFindFirst("text/x-email") >= 0) {
// Set the MIME type
BNode node(destPath);
if (node.InitCheck() != B_OK)
return true; // File extracted OK, just can't add attributes
BNodeInfo nodeInfo(&node);
nodeInfo.SetType("text/x-email");
// Parse the extracted email to get header info
entry_ref ref;
BEntry entry(destPath);
if (entry.GetRef(&ref) == B_OK) {
BEmailMessage extractedMail(&ref);
// Write standard mail attributes using BNode
// NOTE: We intentionally skip MAIL:subject so the temp file
// won't appear in email queries (which match on MAIL:subject=*)
BString value;
// MAIL:from
value = extractedMail.From();
if (value.Length() > 0)
node.WriteAttrString("MAIL:from", &value);
// MAIL:to
value = extractedMail.To();
if (value.Length() > 0)
node.WriteAttrString("MAIL:to", &value);
// MAIL:cc
value = extractedMail.CC();
if (value.Length() > 0)
node.WriteAttrString("MAIL:cc", &value);
// MAIL:when
time_t when = extractedMail.Date();
if (when > 0)
node.WriteAttr("MAIL:when", B_TIME_TYPE, 0, &when, sizeof(when));
// MAIL:status - mark as read
value = "Read";
node.WriteAttrString("MAIL:status", &value);
// MAIL:reply - use from address
value = extractedMail.From();
if (value.Length() > 0)
node.WriteAttrString("MAIL:reply", &value);
// MAIL:name - use subject for display
value = extractedMail.Subject();
if (value.Length() > 0)
node.WriteAttrString("MAIL:name", &value);
// MAIL:account_id and MAIL:account - copy from parent email if available
if (fEmailPath.Length() > 0) {
BNode parentNode(fEmailPath.String());
if (parentNode.InitCheck() == B_OK) {
int32 accountId;
if (parentNode.ReadAttr("MAIL:account_id", B_INT32_TYPE, 0,
&accountId, sizeof(accountId)) == sizeof(accountId)) {
node.WriteAttr("MAIL:account_id", B_INT32_TYPE, 0,
&accountId, sizeof(accountId));
}