From 54dceb13166e29a94a5af184fe1ab15ef4023ad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Wed, 8 Apr 2009 18:23:16 +0000 Subject: [PATCH] axeld+stippi, inspired on a patch by Maxime Simon: * Renamed add_extensions() to merge_extensions(), since that is what it does. * Improved some other variable names in that function for clarity. * Fixed the duplicate extension detection code, also extended it to the list of extensions which are supposed to be added, just in case it already contains duplicates. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30040 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/preferences/filetypes/ExtensionWindow.cpp | 41 +++++++++++-------- src/preferences/filetypes/ExtensionWindow.h | 2 +- src/preferences/filetypes/FileTypesWindow.cpp | 2 +- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/preferences/filetypes/ExtensionWindow.cpp b/src/preferences/filetypes/ExtensionWindow.cpp index f5d9173ec3..7ed7c8ad09 100644 --- a/src/preferences/filetypes/ExtensionWindow.cpp +++ b/src/preferences/filetypes/ExtensionWindow.cpp @@ -38,8 +38,10 @@ compare_extensions(const void* _a, const void* _b) } +//! newExtensionsList contains all the entries (char*) which are to be added. status_t -add_extensions(BMimeType& type, BList& list, const char* removeExtension) +merge_extensions(BMimeType& type, const BList& newExtensionsList, + const char* removeExtension) { BMessage extensions; status_t status = type.GetFileExtensions(&extensions); @@ -47,32 +49,38 @@ add_extensions(BMimeType& type, BList& list, const char* removeExtension) return status; // replace the entry, and remove any equivalent entries - BList newList; - newList.AddList(&list); + BList mergedList; + mergedList.AddList(&newExtensionsList); + int32 originalCount = mergedList.CountItems(); const char* extension; for (int32 i = 0; extensions.FindString("extensions", i, &extension) == B_OK; i++) { - bool add = true; - for (int32 j = list.CountItems(); j-- > 0;) { - if ((removeExtension && !strcmp(removeExtension, extension)) - || !strcmp((const char*)list.ItemAt(j), extension)) { - // remove this item - continue; + + for (int32 j = originalCount; j-- > 0;) { + if (!strcmp((const char*)mergedList.ItemAt(j), extension)) { + // Do not add this old item again, since it's already + // there. + mergedList.RemoveItem(j); + originalCount--; } } - if (add) - newList.AddItem((void *)extension); + // The item will be added behind "originalCount", so we cannot + // remove it accidentally in the next iterations, it's is added + // for good. + if (removeExtension == NULL || strcmp(removeExtension, extension)) + mergedList.AddItem((void *)extension); } - newList.SortItems(compare_extensions); + mergedList.SortItems(compare_extensions); // Copy them to a new message (their memory is still part of the // original BMessage) BMessage newExtensions; - for (int32 i = 0; i < newList.CountItems(); i++) { - newExtensions.AddString("extensions", (const char*)newList.ItemAt(i)); + for (int32 i = 0; i < mergedList.CountItems(); i++) { + newExtensions.AddString("extensions", + (const char*)mergedList.ItemAt(i)); } return type.SetFileExtensions(&newExtensions); @@ -80,12 +88,13 @@ add_extensions(BMimeType& type, BList& list, const char* removeExtension) status_t -replace_extension(BMimeType& type, const char* newExtension, const char* oldExtension) +replace_extension(BMimeType& type, const char* newExtension, + const char* oldExtension) { BList list; list.AddItem((void *)newExtension); - return add_extensions(type, list, oldExtension); + return merge_extensions(type, list, oldExtension); } diff --git a/src/preferences/filetypes/ExtensionWindow.h b/src/preferences/filetypes/ExtensionWindow.h index fa9804c3c4..a98f7a5c02 100644 --- a/src/preferences/filetypes/ExtensionWindow.h +++ b/src/preferences/filetypes/ExtensionWindow.h @@ -33,7 +33,7 @@ class ExtensionWindow : public BWindow { BButton* fAcceptButton; }; -extern status_t add_extensions(BMimeType& type, BList& list, +extern status_t merge_extensions(BMimeType& type, const BList& newExtensions, const char* removeExtension = NULL); extern status_t replace_extension(BMimeType& type, const char* newExtension, const char* oldExtension); diff --git a/src/preferences/filetypes/FileTypesWindow.cpp b/src/preferences/filetypes/FileTypesWindow.cpp index 03b3a1d293..635abb21f7 100644 --- a/src/preferences/filetypes/FileTypesWindow.cpp +++ b/src/preferences/filetypes/FileTypesWindow.cpp @@ -227,7 +227,7 @@ ExtensionListView::MessageReceived(BMessage* message) list.AddItem(strdup(++point)); } - add_extensions(fType, list); + merge_extensions(fType, list); // delete extension list for (int32 index = list.CountItems(); index-- > 0;) {