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
This commit is contained in:
Stephan Aßmus 2009-04-08 18:23:16 +00:00
parent e7f2fa91bc
commit 54dceb1316
3 changed files with 27 additions and 18 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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;) {