From ee6a2e5589a8ff169bb64870650b8f194aa9da14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sundstr=C3=B6m?= Date: Sun, 27 Mar 2011 18:38:05 +0000 Subject: [PATCH] * Extend the Model class with a method bool HasLocalizedName(). * Disallow renaming of entries with localized names for now - this is meant to be temporary - and so far only in Tracker's Info window. Renames do not result in a change, visually, as the localized name hides the real name, and results in a bad user experience. One could possibly allow renames of the localized name, writing it back to the catalog. I've experimented with using BCatalogAddOn::SetString() but haven't been able to make it stick yet. * Disallow renaming Trash in Tracker's Info window via Command-E. * Adjust the argument order of BLocaleRoster::GetLocalizedFileName(). * Add a BLocaleRoster::GetLocalizedFileName() variant to look up another app's name given its signature and unlocalized, canonical name. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41126 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/os/locale/LocaleRoster.h | 12 +++- src/apps/deskbar/BarApp.cpp | 2 +- src/bin/query.cpp | 2 +- src/kits/locale/LocaleRoster.cpp | 101 +++++++++++++++++++---------- src/kits/tracker/FilePanelPriv.cpp | 2 +- src/kits/tracker/InfoWindow.cpp | 13 ++-- src/kits/tracker/Model.cpp | 2 +- src/kits/tracker/Model.h | 10 +++ 8 files changed, 98 insertions(+), 46 deletions(-) diff --git a/headers/os/locale/LocaleRoster.h b/headers/os/locale/LocaleRoster.h index c4e1a0acf7..cb7766e753 100644 --- a/headers/os/locale/LocaleRoster.h +++ b/headers/os/locale/LocaleRoster.h @@ -68,10 +68,14 @@ public: bool IsFilesystemTranslationPreferred() const; - status_t GetLocalizedFileName(const entry_ref& ref, - BString& localizedFileName, + status_t GetLocalizedFileName(BString& localizedFileName, + const entry_ref& ref, bool traverse = false); + status_t GetLocalizedFileName(BString& localizedFileName, + const char* signature, const char* context, + const char* string); + static const char* kCatLangAttr; static const char* kCatSigAttr; static const char* kCatFingerprintAttr; @@ -82,6 +86,10 @@ public: private: static BCatalog* _GetCatalog(BCatalog* catalog, vint32* catalogInitStatus); + + status_t _PrepareCatalogEntry(const entry_ref& ref, + BString& signature, BString& context, + BString& string, bool traverse); }; diff --git a/src/apps/deskbar/BarApp.cpp b/src/apps/deskbar/BarApp.cpp index 43c5c9d925..3a5716a490 100644 --- a/src/apps/deskbar/BarApp.cpp +++ b/src/apps/deskbar/BarApp.cpp @@ -620,7 +620,7 @@ TBarApp::AddTeam(team_id team, uint32 flags, const char* sig, entry_ref* ref) BString name; if (!gLocalizedNamePreferred - || BLocaleRoster::Default()->GetLocalizedFileName(*ref, name) != B_OK) + || BLocaleRoster::Default()->GetLocalizedFileName(name, *ref) != B_OK) name = ref->name; BarTeamInfo* barInfo = new BarTeamInfo(new BList(), flags, strdup(sig), diff --git a/src/bin/query.cpp b/src/bin/query.cpp index 2018746706..7191014c6b 100644 --- a/src/bin/query.cpp +++ b/src/bin/query.cpp @@ -94,7 +94,7 @@ perform_query(BVolume &volume, const char *predicate) entry_ref ref; if (entry.GetRef(&ref) != B_OK || BLocaleRoster::Default() - ->GetLocalizedFileName(ref, string) != B_OK) + ->GetLocalizedFileName(string, ref) != B_OK) continue; if (string.IFindFirst(predicate) < 0) diff --git a/src/kits/locale/LocaleRoster.cpp b/src/kits/locale/LocaleRoster.cpp index 23bc303830..e9b7460631 100644 --- a/src/kits/locale/LocaleRoster.cpp +++ b/src/kits/locale/LocaleRoster.cpp @@ -381,11 +381,10 @@ BLocaleRoster::IsFilesystemTranslationPreferred() const } -/*! \brief Looks up a localized filename in a catalog, using attribute data - on the entry. - \param ref An entry_ref with an attribute holding data for catalog lookup. +/*! \brief Looks up a localized filename from a catalog. \param localizedFileName A pre-allocated BString object for the result of the lookup. + \param ref An entry_ref with an attribute holding data for catalog lookup. \param traverse A boolean to decide if symlinks are to be traversed. \return - \c B_OK: success @@ -400,8 +399,56 @@ BLocaleRoster::IsFilesystemTranslationPreferred() const Lookup fails if a comment is present in the catalog entry. */ status_t -BLocaleRoster::GetLocalizedFileName(const entry_ref& ref, - BString& localizedFileName, bool traverse) +BLocaleRoster::GetLocalizedFileName(BString& localizedFileName, + const entry_ref& ref, bool traverse) +{ + BString signature; + BString context; + BString string; + + status_t status = _PrepareCatalogEntry(ref, signature, context, string, + traverse); + + if (status != B_OK) + return status; + + return GetLocalizedFileName(localizedFileName, signature, context, string); +} + + +/*! \brief Looks up a localized filename from a catalog. + \param localizedFileName A pre-allocated BString object for the result + of the lookup. + \param signature The "x-vnd..." part of an application signature. + \param context A catalog context. Likely B_TRANSLATE_SYSTEM_NAME_CONTEXT. + \param string A catalog string entry. Likely the unlocalized app name. + \return + - \c B_OK: success + - \c B_ENTRY_NOT_FOUND: failure. Catalog entry not found in catalog, etc + - other error codes: failure + + Lookup is done for the top preferred language, only. + Lookup fails if a comment is present in the catalog entry. +*/ +status_t +BLocaleRoster::GetLocalizedFileName(BString& localizedFileName, + const char* signature, const char* context, const char* string) +{ + BCatalog catalog(signature); + + const char* temp = catalog.GetString(string, context); + + if (temp == NULL) + return B_ENTRY_NOT_FOUND; + + localizedFileName = temp; + return B_OK; +} + + +status_t +BLocaleRoster::_PrepareCatalogEntry(const entry_ref& ref, BString& signature, + BString& context, BString& string, bool traverse) { BEntry entry(&ref, traverse); if (!entry.Exists()) @@ -412,44 +459,26 @@ BLocaleRoster::GetLocalizedFileName(const entry_ref& ref, if (status != B_OK) return status; - attr_info attr; - status = node.GetAttrInfo("SYS:NAME", &attr); + status = node.ReadAttrString("SYS:NAME", &signature); if (status != B_OK) return status; - char attribute[attr.size + 1]; - ssize_t bytes = node.ReadAttr("SYS:NAME", B_MIME_TYPE, 0, &attribute, - attr.size); - - if (bytes < 0) - return bytes; - - if (bytes == 0 || bytes != attr.size) + int32 first = signature.FindFirst(':'); + int32 last = signature.FindLast(':'); + if (first == last) return B_ENTRY_NOT_FOUND; - attribute[bytes] = '\0'; + context = signature; + string = signature; - char* signature = attribute; - char* context = strchr(signature, ':'); - if (context == NULL) + signature.Truncate(first); + context.Truncate(last); + context.Remove(0, first + 1); + string.Remove(0, last + 1); + + if (signature.Length() == 0 || context.Length() == 0 + || string.Length() == 0) return B_ENTRY_NOT_FOUND; - context[0] = '\0'; - context++; - - char* string = strchr(context, ':'); - if (string == NULL) - return B_ENTRY_NOT_FOUND; - - string[0] = '\0'; - string++; - - BCatalog catalog(signature); - - const char* temp = catalog.GetString(string, context); - if (temp == NULL) - return B_ENTRY_NOT_FOUND; - - localizedFileName = temp; return B_OK; } diff --git a/src/kits/tracker/FilePanelPriv.cpp b/src/kits/tracker/FilePanelPriv.cpp index 1068df0e2a..ccf4d0bf05 100644 --- a/src/kits/tracker/FilePanelPriv.cpp +++ b/src/kits/tracker/FilePanelPriv.cpp @@ -782,7 +782,7 @@ TFilePanel::Init(const BMessage *) if (be_app->GetAppInfo(&info) == B_OK) { if (!gLocalizedNamePreferred || BLocaleRoster::Default()->GetLocalizedFileName( - info.ref, title, false) != B_OK) + title, info.ref, false) != B_OK) title = info.ref.name; title << ": "; } diff --git a/src/kits/tracker/InfoWindow.cpp b/src/kits/tracker/InfoWindow.cpp index 93fa8c3833..c71aed6f32 100644 --- a/src/kits/tracker/InfoWindow.cpp +++ b/src/kits/tracker/InfoWindow.cpp @@ -299,7 +299,9 @@ BInfoWindow::BInfoWindow(Model *model, int32 group_index, LockingList * if (list) list->AddItem(this); - AddShortcut('E', 0, new BMessage(kEditItem)); + if (!model->IsTrash() && !model->HasLocalizedName()) + AddShortcut('E', 0, new BMessage(kEditItem)); + AddShortcut('O', 0, new BMessage(kOpenSelection)); AddShortcut('U', 0, new BMessage(kUnmountVolume)); AddShortcut('P', 0, new BMessage(kPermissionsSelected)); @@ -1222,6 +1224,7 @@ AttributeView::MouseDown(BPoint point) } else if (fTitleRect.Contains(point)) { // You can't change the name of the trash if (!fModel->IsTrash() + && !fModel->HasLocalizedName() && ConfirmChangeIfWellKnownDirectory(&entry, B_TRANSLATE_COMMENT("rename", "As in 'If you rename ...'"), B_TRANSLATE_COMMENT("rename", "As in 'To rename ...'"), true) @@ -2037,9 +2040,11 @@ AttributeView::BuildContextMenu(BMenu *parent) new BMessage(kOpenSelection), 'O')); if (!model.IsTrash()) { - parent->AddItem(new BMenuItem(B_TRANSLATE("Edit name"), - new BMessage(kEditItem), 'E')); - parent->AddSeparatorItem(); + if (!fModel->HasLocalizedName()) { + parent->AddItem(new BMenuItem(B_TRANSLATE("Edit name"), + new BMessage(kEditItem), 'E')); + parent->AddSeparatorItem(); + } if (fModel->IsVolume()) { BMenuItem* item = new BMenuItem(B_TRANSLATE("Unmount"), new BMessage(kUnmountVolume), 'U'); diff --git a/src/kits/tracker/Model.cpp b/src/kits/tracker/Model.cpp index 647139d254..115381b977 100644 --- a/src/kits/tracker/Model.cpp +++ b/src/kits/tracker/Model.cpp @@ -568,7 +568,7 @@ void Model::CacheLocalizedName() { if (BLocaleRoster::Default()->GetLocalizedFileName( - fEntryRef, fLocalizedName, true) == B_OK) + fLocalizedName, fEntryRef, true) == B_OK) fHasLocalizedName = true; else fHasLocalizedName = false; diff --git a/src/kits/tracker/Model.h b/src/kits/tracker/Model.h index a71450e7fa..31f8a3481e 100644 --- a/src/kits/tracker/Model.h +++ b/src/kits/tracker/Model.h @@ -204,6 +204,9 @@ class Model { bool Mimeset(bool force); // returns true if mime type changed + + bool HasLocalizedName() const; + private: status_t OpenNodeCommon(bool writable); void SetupBaseType(); @@ -451,6 +454,13 @@ Model::IsSymLink() const } +inline bool +Model::HasLocalizedName() const +{ + return fHasLocalizedName; +} + + inline ModelNodeLazyOpener::ModelNodeLazyOpener(Model *model, bool writable, bool openLater) : fModel(model),