From 6ebc02eae447102ddef312e2770590b7dddafeef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sun, 12 Feb 2006 17:11:58 +0000 Subject: [PATCH] The MimeTypeListView now listens to changes to the MIME type database as well. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@16363 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/preferences/filetypes/FileTypesWindow.cpp | 12 +- .../filetypes/MimeTypeListView.cpp | 116 ++++++++++++++++-- src/preferences/filetypes/MimeTypeListView.h | 9 +- 3 files changed, 125 insertions(+), 12 deletions(-) diff --git a/src/preferences/filetypes/FileTypesWindow.cpp b/src/preferences/filetypes/FileTypesWindow.cpp index 758b614ebf..5e2e14ee64 100644 --- a/src/preferences/filetypes/FileTypesWindow.cpp +++ b/src/preferences/filetypes/FileTypesWindow.cpp @@ -1025,11 +1025,17 @@ FileTypesWindow::MessageReceived(BMessage* message) case B_META_MIME_CHANGED: { const char* type; - if (message->FindString("be:type", &type) != B_OK) + int32 which; + if (message->FindString("be:type", &type) != B_OK + || message->FindInt32("be:which", &which) != B_OK) break; - if (!strcasecmp(fCurrentType.Type(), type)) - _SetType(&fCurrentType, true); + if (fCurrentType.Type() != NULL && !strcasecmp(fCurrentType.Type(), type)) { + if (which != B_MIME_TYPE_DELETED) + _SetType(&fCurrentType, true); + else + _SetType(NULL); + } break; } diff --git a/src/preferences/filetypes/MimeTypeListView.cpp b/src/preferences/filetypes/MimeTypeListView.cpp index 222f98577b..c2f0733add 100644 --- a/src/preferences/filetypes/MimeTypeListView.cpp +++ b/src/preferences/filetypes/MimeTypeListView.cpp @@ -29,7 +29,7 @@ mimetype_is_application_signature(BMimeType& type) MimeTypeItem::MimeTypeItem(BMimeType& type, bool flat) - : BStringItem(type.Type(), !flat && type.IsSupertypeOnly() ? 1 : 0, false), + : BStringItem(type.Type(), !flat && !type.IsSupertypeOnly() ? 1 : 0, false), fType(type.Type()) { _SetTo(type); @@ -93,6 +93,9 @@ MimeTypeItem::_SetTo(BMimeType& type) void MimeTypeItem::Update() { + if (IsSupertypeOnly()) + return; + BMimeType type(fType.String()); char description[B_MIME_TYPE_LENGTH]; @@ -222,7 +225,7 @@ MimeTypeListView::_CollectTypes() void MimeTypeListView::_MakeTypesUnique(MimeTypeItem* underItem) { - SortItemsUnder(underItem, false, &MimeTypeItem::Compare); + SortItemsUnder(underItem, underItem != NULL, &MimeTypeItem::Compare); bool lastItemSame = false; MimeTypeItem* last = NULL; @@ -273,6 +276,109 @@ MimeTypeListView::_MakeTypesUnique(MimeTypeItem* underItem) } +void +MimeTypeListView::AttachedToWindow() +{ + BOutlineListView::AttachedToWindow(); + + BMimeType::StartWatching(this); +} + + +void +MimeTypeListView::DetachedFromWindow() +{ + BOutlineListView::DetachedFromWindow(); + + BMimeType::StopWatching(this); +} + + +void +MimeTypeListView::MessageReceived(BMessage* message) +{ + switch (message->what) { + case B_META_MIME_CHANGED: + { + const char* type; + int32 which; + if (message->FindString("be:type", &type) != B_OK + || message->FindInt32("be:which", &which) != B_OK) + break; + + switch (which) { + case B_SHORT_DESCRIPTION_CHANGED: + { + // update label + + MimeTypeItem* item = FindItem(type); + if (item != NULL) + UpdateItem(item); + break; + } + case B_MIME_TYPE_CREATED: + { + // create new item + BMimeType created(type); + BMimeType superType; + MimeTypeItem* superItem = NULL; + if (created.GetSupertype(&superType) == B_OK) + superItem = FindItem(superType.Type()); + + MimeTypeItem* item = new MimeTypeItem(created); + + if (superItem != NULL) { + AddUnder(item, superItem); + InvalidateItem(IndexOf(superItem)); + // the super item is not picked up from the class (ie. bug) + } else + AddItem(item); + + UpdateItem(item); + break; + } + case B_MIME_TYPE_DELETED: + { + // delete item + MimeTypeItem* item = FindItem(type); + if (item != NULL) { + RemoveItem(item); + delete item; + } + break; + } + + default: + break; + } + break; + } + + default: + BOutlineListView::MessageReceived(message); + } +} + + +MimeTypeItem* +MimeTypeListView::FindItem(const char* type) +{ + if (type == NULL) + return NULL; + + for (int32 i = FullListCountItems(); i-- > 0;) { + MimeTypeItem* item = dynamic_cast(FullListItemAt(i)); + if (item == NULL) + continue; + + if (!strcasecmp(item->Type(), type)) + return item; + } + + return NULL; +} + + void MimeTypeListView::UpdateItem(MimeTypeItem* item) { @@ -294,9 +400,3 @@ MimeTypeListView::UpdateItem(MimeTypeItem* item) InvalidateItem(IndexOf(item)); } - -void -MimeTypeListView::RemoveItem(MimeTypeItem* item) -{ -} - diff --git a/src/preferences/filetypes/MimeTypeListView.h b/src/preferences/filetypes/MimeTypeListView.h index 8af9a9166b..ff2a7b90f9 100644 --- a/src/preferences/filetypes/MimeTypeListView.h +++ b/src/preferences/filetypes/MimeTypeListView.h @@ -49,8 +49,15 @@ class MimeTypeListView : public BOutlineListView { uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP); virtual ~MimeTypeListView(); + MimeTypeItem* FindItem(const char* type); + void UpdateItem(MimeTypeItem* item); - void RemoveItem(MimeTypeItem* item); + + protected: + virtual void AttachedToWindow(); + virtual void DetachedFromWindow(); + + virtual void MessageReceived(BMessage* message); private: void _CollectTypes();