[haiku-commits] haiku: hrev44485 - src/kits/translation headers/os/translation

  • From: leavengood@xxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 7 Aug 2012 06:45:22 +0200 (CEST)

hrev44485 adds 1 changeset to branch 'master'
old head: 0671704e3a8b534a6219ae009c78a8ec2f032d5c
new head: e77304562da050fbc739b99474f8b5bfad0cf5b2

----------------------------------------------------------------------------

e773045: Actually sort the translator menu used in ShowImage.
  
  There were two problems with the last commit:
  
  * the list needed to be outside of the top-level loop.
  * BList was just broken for sorting translation_format pointers.
  
  I fixed this by moving the loop outside and converting the translation_formats
  to translator_info, which has the translator_id, since that is needed to 
create
  the menu item, and would otherwise be unavailable outside the loop.
  
  I tried to get this working with BList, but the sorting was completely broken,
  and converting to BObjectList made the code much, much better and worked 
great.
  Screw BList and casting, hurray templated BObjectList.
  
  Really fixes #6782.

                                  [ Ryan Leavengood <leavengood@xxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev44485
Commit:      e77304562da050fbc739b99474f8b5bfad0cf5b2
URL:         http://cgit.haiku-os.org/haiku/commit/?id=e773045
Author:      Ryan Leavengood <leavengood@xxxxxxxxx>
Date:        Tue Aug  7 04:29:32 2012 UTC

Ticket:      https://dev.haiku-os.org/ticket/6782

----------------------------------------------------------------------------

2 files changed, 48 insertions(+), 26 deletions(-)
headers/os/translation/TranslationUtils.h |    6 ++-
src/kits/translation/TranslationUtils.cpp |   68 ++++++++++++++++---------

----------------------------------------------------------------------------

diff --git a/headers/os/translation/TranslationUtils.h 
b/headers/os/translation/TranslationUtils.h
index 66c927a..c501bbc 100644
--- a/headers/os/translation/TranslationUtils.h
+++ b/headers/os/translation/TranslationUtils.h
@@ -77,8 +77,10 @@ public:
                                                                        
BTranslatorRoster* roster = NULL);
 
 private:
-       static  int                                     
CompareTranslationFormatByName(const void* format1,
-                                                                       const 
void* format2);
+       static  translator_info*        _BuildTranslatorInfo(const 
translator_id id,
+                                                                       const 
translation_format* format);
+       static  int                                     
_CompareTranslatorInfoByName(const translator_info* info1,
+                                                                       const 
translator_info* info2);
 
        static  color_space                     sBitmapSpace;
 };
diff --git a/src/kits/translation/TranslationUtils.cpp 
b/src/kits/translation/TranslationUtils.cpp
index 1d40796..129a28d 100644
--- a/src/kits/translation/TranslationUtils.cpp
+++ b/src/kits/translation/TranslationUtils.cpp
@@ -17,9 +17,9 @@
 #include <CharacterSetRoster.h>
 #include <Entry.h>
 #include <File.h>
-#include <List.h>
 #include <MenuItem.h>
 #include <NodeInfo.h>
+#include <ObjectList.h>
 #include <Path.h>
 #include <Resources.h>
 #include <Roster.h>
@@ -894,6 +894,8 @@ BTranslationUtils::AddTranslationItems(BMenu *intoMenu, 
uint32 fromType,
        if (err < B_OK)
                return err;
 
+       BObjectList<translator_info> infoList;
+
        for (int tix = 0; tix < count; tix++) {
                const translation_format *formats = NULL;
                int32 numFormats = 0;
@@ -911,34 +913,35 @@ BTranslationUtils::AddTranslationItems(BMenu *intoMenu, 
uint32 fromType,
                        continue;
 
                // Get supported output formats
-               BList formatList;
                err = roster->GetOutputFormats(ids[tix], &formats, 
&numFormats); 
                if (err == B_OK) {
                        for (int oix = 0; oix < numFormats; oix++) {
                                if (formats[oix].type != fromType) {
-                                       
formatList.AddItem(const_cast<translation_format*>(
-                                               &formats[oix]));
+                                       
infoList.AddItem(_BuildTranslatorInfo(ids[tix],
+                                               
const_cast<translation_format*>(&formats[oix])));
                                }
                        }
                }
+       }
 
-               // Sort alphabetically by name
-               formatList.SortItems(&CompareTranslationFormatByName);
-
-               // Now add the menu items
-               for (int i = 0; i < formatList.CountItems(); i++) {
-                       translation_format* format = 
static_cast<translation_format*>(
-                               formatList.ItemAt(i));
-
-                       BMessage *itemmsg;
-                       if (kModel)
-                               itemmsg = new BMessage(*kModel);
-                       else
-                               itemmsg = new BMessage(B_TRANSLATION_MENU);
-                       itemmsg->AddInt32(kTranslatorIdName, ids[tix]);
-                       itemmsg->AddInt32(kTranslatorTypeName, format->type);
-                       intoMenu->AddItem(new BMenuItem(format->name, itemmsg));
-               }
+       // Sort alphabetically by name
+       infoList.SortItems(&_CompareTranslatorInfoByName);
+
+       // Now add the menu items
+       for (int i = 0; i < infoList.CountItems(); i++) {
+               translator_info* info = infoList.ItemAt(i);
+
+               BMessage *itemmsg;
+               if (kModel)
+                       itemmsg = new BMessage(*kModel);
+               else
+                       itemmsg = new BMessage(B_TRANSLATION_MENU);
+               itemmsg->AddInt32(kTranslatorIdName, info->translator);
+               itemmsg->AddInt32(kTranslatorTypeName, info->type);
+               intoMenu->AddItem(new BMenuItem(info->name, itemmsg));
+
+               // Delete object created in _BuildTranslatorInfo
+               delete info;
        }
 
        delete[] ids;
@@ -946,9 +949,26 @@ BTranslationUtils::AddTranslationItems(BMenu *intoMenu, 
uint32 fromType,
 }
 
 
+translator_info*
+BTranslationUtils::_BuildTranslatorInfo(const translator_id id, const 
translation_format* format)
+{
+       // Caller must delete
+       translator_info* info = new translator_info;
+
+       info->translator = id;
+       info->type = format->type;
+       info->group = format->group;
+       info->quality = format->quality;
+       info->capability = format->capability;
+       strlcpy(info->name, format->name, sizeof(info->name));
+       strlcpy(info->MIME, format->MIME, sizeof(info->MIME));
+
+       return info;
+}
+
+
 int
-BTranslationUtils::CompareTranslationFormatByName(const void* format1, const 
void* format2)
+BTranslationUtils::_CompareTranslatorInfoByName(const translator_info* info1, 
const translator_info* info2)
 {
-       return strcasecmp(static_cast<const translation_format*>(format1)->name,
-               static_cast<const translation_format*>(format2)->name);
+       return strcasecmp(info1->name, info2->name);
 }


Other related posts:

  • » [haiku-commits] haiku: hrev44485 - src/kits/translation headers/os/translation - leavengood