131dc79a1SAxel Dörfler /* 2d0c290bfSAxel Dörfler * Copyright 2002-2007, Haiku Inc. 331dc79a1SAxel Dörfler * Distributed under the terms of the MIT License. 431dc79a1SAxel Dörfler * 531dc79a1SAxel Dörfler * Authors: 631dc79a1SAxel Dörfler * Ingo Weinhold, bonefish@users.sf.net 721881ce5SIngo Weinhold */ 8d6b205f3SIngo Weinhold 931dc79a1SAxel Dörfler 1098da112cSIngo Weinhold #include <new> 1198da112cSIngo Weinhold #include <set> 12*18cd67c7SMichael Lotz #include <stdlib.h> 1398da112cSIngo Weinhold #include <string> 1498da112cSIngo Weinhold 15d6b205f3SIngo Weinhold #include <AppFileInfo.h> 1698da112cSIngo Weinhold #include <Bitmap.h> 1798da112cSIngo Weinhold #include <File.h> 1898da112cSIngo Weinhold #include <fs_attr.h> 199ecf9d1cSIngo Weinhold #include <IconUtils.h> 2098da112cSIngo Weinhold #include <MimeType.h> 2198da112cSIngo Weinhold #include <RegistrarDefs.h> 2298da112cSIngo Weinhold #include <Resources.h> 2398da112cSIngo Weinhold #include <Roster.h> 2498da112cSIngo Weinhold #include <String.h> 2598da112cSIngo Weinhold 2650f17542Shaydentech using namespace std; 2750f17542Shaydentech 2898da112cSIngo Weinhold // attributes 2998da112cSIngo Weinhold static const char *kTypeAttribute = "BEOS:TYPE"; 3098da112cSIngo Weinhold static const char *kSignatureAttribute = "BEOS:APP_SIG"; 3198da112cSIngo Weinhold static const char *kAppFlagsAttribute = "BEOS:APP_FLAGS"; 3298da112cSIngo Weinhold static const char *kSupportedTypesAttribute = "BEOS:FILE_TYPES"; 3398da112cSIngo Weinhold static const char *kVersionInfoAttribute = "BEOS:APP_VERSION"; 3498da112cSIngo Weinhold static const char *kMiniIconAttribute = "BEOS:M:"; 3598da112cSIngo Weinhold static const char *kLargeIconAttribute = "BEOS:L:"; 369ecf9d1cSIngo Weinhold static const char *kIconAttribute = "BEOS:"; 3798da112cSIngo Weinhold static const char *kStandardIconType = "STD_ICON"; 389ecf9d1cSIngo Weinhold static const char *kIconType = "ICON"; 3998da112cSIngo Weinhold 4098da112cSIngo Weinhold // resource IDs 4198da112cSIngo Weinhold static const int32 kTypeResourceID = 2; 4298da112cSIngo Weinhold static const int32 kSignatureResourceID = 1; 4398da112cSIngo Weinhold static const int32 kAppFlagsResourceID = 1; 4498da112cSIngo Weinhold static const int32 kSupportedTypesResourceID = 1; 4598da112cSIngo Weinhold static const int32 kMiniIconResourceID = 101; 4698da112cSIngo Weinhold static const int32 kLargeIconResourceID = 101; 477fb6186fSStephan Aßmus static const int32 kIconResourceID = 101; 4898da112cSIngo Weinhold static const int32 kVersionInfoResourceID = 1; 4998da112cSIngo Weinhold static const int32 kMiniIconForTypeResourceID = 0; 5098da112cSIngo Weinhold static const int32 kLargeIconForTypeResourceID = 0; 517fb6186fSStephan Aßmus static const int32 kIconForTypeResourceID = 0; 5298da112cSIngo Weinhold 5398da112cSIngo Weinhold // type codes 5498da112cSIngo Weinhold enum { 5598da112cSIngo Weinhold B_APP_FLAGS_TYPE = 'APPF', 5698da112cSIngo Weinhold B_VERSION_INFO_TYPE = 'APPV', 5798da112cSIngo Weinhold }; 5898da112cSIngo Weinhold 5988706bbeSAxel Dörfler // R5 also exports these (Tracker is using them): 6088706bbeSAxel Dörfler // (maybe we better want to drop them silently and declare 6188706bbeSAxel Dörfler // the above in a public Haiku header - and use that one in 6288706bbeSAxel Dörfler // Tracker when compiled for Haiku) 6388706bbeSAxel Dörfler extern const uint32 MINI_ICON_TYPE, LARGE_ICON_TYPE; 6488706bbeSAxel Dörfler const uint32 MINI_ICON_TYPE = 'MICN'; 6588706bbeSAxel Dörfler const uint32 LARGE_ICON_TYPE = 'ICON'; 6688706bbeSAxel Dörfler 6778b31a7cSIngo Weinhold // debugging 6878b31a7cSIngo Weinhold //#define DBG(x) x 6978b31a7cSIngo Weinhold #define DBG(x) 7078b31a7cSIngo Weinhold #define OUT printf 71d6b205f3SIngo Weinhold 72d6b205f3SIngo Weinhold // constructor 73d6b205f3SIngo Weinhold /*! \brief Creates an uninitialized BAppFileInfo object. 74d6b205f3SIngo Weinhold */ 75d6b205f3SIngo Weinhold BAppFileInfo::BAppFileInfo() 76d6b205f3SIngo Weinhold : fResources(NULL), 77d6b205f3SIngo Weinhold fWhere(B_USE_BOTH_LOCATIONS) 78d6b205f3SIngo Weinhold { 79d6b205f3SIngo Weinhold } 80d6b205f3SIngo Weinhold 81d6b205f3SIngo Weinhold // constructor 82d6b205f3SIngo Weinhold /*! \brief Creates an BAppFileInfo object and initializes it to the supplied 83d6b205f3SIngo Weinhold file. 84d6b205f3SIngo Weinhold 85d6b205f3SIngo Weinhold The caller retains ownership of the supplied BFile object. It must not 86d6b205f3SIngo Weinhold be deleted during the life time of the BAppFileInfo. It is not deleted 87d6b205f3SIngo Weinhold when the BAppFileInfo is destroyed. 88d6b205f3SIngo Weinhold 89d6b205f3SIngo Weinhold \param file The file the object shall be initialized to. 90d6b205f3SIngo Weinhold */ 91d6b205f3SIngo Weinhold BAppFileInfo::BAppFileInfo(BFile *file) 92d6b205f3SIngo Weinhold : fResources(NULL), 93d6b205f3SIngo Weinhold fWhere(B_USE_BOTH_LOCATIONS) 94d6b205f3SIngo Weinhold { 9598da112cSIngo Weinhold SetTo(file); 96d6b205f3SIngo Weinhold } 97d6b205f3SIngo Weinhold 98d6b205f3SIngo Weinhold // destructor 99d6b205f3SIngo Weinhold /*! \brief Frees all resources associated with this object. 100d6b205f3SIngo Weinhold 101d6b205f3SIngo Weinhold The BFile the object is set to is not deleted. 102d6b205f3SIngo Weinhold */ 103d6b205f3SIngo Weinhold BAppFileInfo::~BAppFileInfo() 104d6b205f3SIngo Weinhold { 10598da112cSIngo Weinhold delete fResources; 106d6b205f3SIngo Weinhold } 107d6b205f3SIngo Weinhold 108d6b205f3SIngo Weinhold // SetTo 109d6b205f3SIngo Weinhold /*! \brief Initializes the BAppFileInfo to the supplied file. 110d6b205f3SIngo Weinhold 111d6b205f3SIngo Weinhold The caller retains ownership of the supplied BFile object. It must not 112d6b205f3SIngo Weinhold be deleted during the life time of the BAppFileInfo. It is not deleted 113d6b205f3SIngo Weinhold when the BAppFileInfo is destroyed. 114d6b205f3SIngo Weinhold 115d6b205f3SIngo Weinhold \param file The file the object shall be initialized to. 116d6b205f3SIngo Weinhold 117d6b205f3SIngo Weinhold \return 118d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 119d6b205f3SIngo Weinhold - \c B_BAD_VALUE: \c NULL \a file or \a file is not properly initialized. 120d6b205f3SIngo Weinhold */ 121d6b205f3SIngo Weinhold status_t 122d6b205f3SIngo Weinhold BAppFileInfo::SetTo(BFile *file) 123d6b205f3SIngo Weinhold { 12498da112cSIngo Weinhold // unset the old file 12598da112cSIngo Weinhold BNodeInfo::SetTo(NULL); 12698da112cSIngo Weinhold if (fResources) { 12798da112cSIngo Weinhold delete fResources; 12898da112cSIngo Weinhold fResources = NULL; 12998da112cSIngo Weinhold } 130c2a2369dSAxel Dörfler 13198da112cSIngo Weinhold // check param 13298da112cSIngo Weinhold status_t error = (file && file->InitCheck() == B_OK ? B_OK : B_BAD_VALUE); 133c2a2369dSAxel Dörfler 134c2a2369dSAxel Dörfler info_location where = B_USE_BOTH_LOCATIONS; 135c2a2369dSAxel Dörfler 13698da112cSIngo Weinhold // create resources 13798da112cSIngo Weinhold if (error == B_OK) { 13898da112cSIngo Weinhold fResources = new(nothrow) BResources(); 139c2a2369dSAxel Dörfler if (fResources) { 14098da112cSIngo Weinhold error = fResources->SetTo(file); 141c2a2369dSAxel Dörfler if (error != B_OK) { 142c2a2369dSAxel Dörfler // no resources - this is no critical error, we'll just use 143c2a2369dSAxel Dörfler // attributes only, then 144c2a2369dSAxel Dörfler where = B_USE_ATTRIBUTES; 145c2a2369dSAxel Dörfler error = B_OK; 146c2a2369dSAxel Dörfler } 147c2a2369dSAxel Dörfler } else 14898da112cSIngo Weinhold error = B_NO_MEMORY; 14998da112cSIngo Weinhold } 150c2a2369dSAxel Dörfler 15198da112cSIngo Weinhold // set node info 15298da112cSIngo Weinhold if (error == B_OK) 15398da112cSIngo Weinhold error = BNodeInfo::SetTo(file); 154c2a2369dSAxel Dörfler 155c2a2369dSAxel Dörfler if (error != B_OK || (where & B_USE_RESOURCES) == 0) { 15698da112cSIngo Weinhold delete fResources; 15798da112cSIngo Weinhold fResources = NULL; 15898da112cSIngo Weinhold } 159c2a2369dSAxel Dörfler 160c2a2369dSAxel Dörfler // clean up on error 161c2a2369dSAxel Dörfler if (error != B_OK) { 16298da112cSIngo Weinhold if (InitCheck() == B_OK) 16398da112cSIngo Weinhold BNodeInfo::SetTo(NULL); 16498da112cSIngo Weinhold } 165c2a2369dSAxel Dörfler 16698da112cSIngo Weinhold // set data location 16798da112cSIngo Weinhold if (error == B_OK) 168c2a2369dSAxel Dörfler SetInfoLocation(where); 169c2a2369dSAxel Dörfler 17098da112cSIngo Weinhold // set error 17198da112cSIngo Weinhold fCStatus = error; 17298da112cSIngo Weinhold return error; 173d6b205f3SIngo Weinhold } 174d6b205f3SIngo Weinhold 175d6b205f3SIngo Weinhold // GetType 176d6b205f3SIngo Weinhold /*! \brief Gets the file's MIME type. 177d6b205f3SIngo Weinhold 178d6b205f3SIngo Weinhold \param type A pointer to a pre-allocated character buffer of size 179d6b205f3SIngo Weinhold \c B_MIME_TYPE_LENGTH or larger into which the MIME type of the 180d6b205f3SIngo Weinhold file shall be written. 181d6b205f3SIngo Weinhold \return 182d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 183d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 184d6b205f3SIngo Weinhold - \c B_BAD_VALUE: \c NULL \a type or the type string stored in the 185d6b205f3SIngo Weinhold attribute/resources is longer than \c B_MIME_TYPE_LENGTH. 186d6b205f3SIngo Weinhold - \c B_BAD_TYPE: The attribute/resources the type string is stored in have 187d6b205f3SIngo Weinhold the wrong type. 188d6b205f3SIngo Weinhold - \c B_ENTRY_NOT_FOUND: No type is set on the file. 189d6b205f3SIngo Weinhold - other error codes 190d6b205f3SIngo Weinhold */ 191d6b205f3SIngo Weinhold status_t 192d6b205f3SIngo Weinhold BAppFileInfo::GetType(char *type) const 193d6b205f3SIngo Weinhold { 19498da112cSIngo Weinhold // check param and initialization 19598da112cSIngo Weinhold status_t error = (type ? B_OK : B_BAD_VALUE); 19698da112cSIngo Weinhold if (error == B_OK && InitCheck() != B_OK) 19798da112cSIngo Weinhold error = B_NO_INIT; 19898da112cSIngo Weinhold // read the data 19998da112cSIngo Weinhold size_t read = 0; 20098da112cSIngo Weinhold if (error == B_OK) { 20198da112cSIngo Weinhold error = _ReadData(kTypeAttribute, kTypeResourceID, B_MIME_STRING_TYPE, 20298da112cSIngo Weinhold type, B_MIME_TYPE_LENGTH, read); 20398da112cSIngo Weinhold } 20498da112cSIngo Weinhold // check the read data -- null terminate the string 20598da112cSIngo Weinhold if (error == B_OK && type[read - 1] != '\0') { 20698da112cSIngo Weinhold if (read == B_MIME_TYPE_LENGTH) 20798da112cSIngo Weinhold error = B_ERROR; 20898da112cSIngo Weinhold else 20998da112cSIngo Weinhold type[read] = '\0'; 21098da112cSIngo Weinhold } 21198da112cSIngo Weinhold return error; 212d6b205f3SIngo Weinhold } 213d6b205f3SIngo Weinhold 214d6b205f3SIngo Weinhold // SetType 215d6b205f3SIngo Weinhold /*! \brief Sets the file's MIME type. 216d6b205f3SIngo Weinhold 217d6b205f3SIngo Weinhold If \a type is \c NULL the file's MIME type is unset. 218d6b205f3SIngo Weinhold 219d6b205f3SIngo Weinhold \param type The MIME type to be assigned to the file. Must not be longer 220d6b205f3SIngo Weinhold than \c B_MIME_TYPE_LENGTH (including the terminating null). 221d6b205f3SIngo Weinhold May be \c NULL. 222d6b205f3SIngo Weinhold \return 223d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 224d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 225d6b205f3SIngo Weinhold - \c B_BAD_VALUE: \a type is longer than \c B_MIME_TYPE_LENGTH. 226d6b205f3SIngo Weinhold - other error codes 227d6b205f3SIngo Weinhold */ 228d6b205f3SIngo Weinhold status_t 229d6b205f3SIngo Weinhold BAppFileInfo::SetType(const char *type) 230d6b205f3SIngo Weinhold { 23198da112cSIngo Weinhold // check initialization 23298da112cSIngo Weinhold status_t error = B_OK; 23398da112cSIngo Weinhold if (error == B_OK && InitCheck() != B_OK) 23498da112cSIngo Weinhold error = B_NO_INIT; 23598da112cSIngo Weinhold if (error == B_OK) { 23698da112cSIngo Weinhold if (type) { 23798da112cSIngo Weinhold // check param 23898da112cSIngo Weinhold size_t typeLen = strlen(type); 23998da112cSIngo Weinhold if (error == B_OK && typeLen >= B_MIME_TYPE_LENGTH) 24098da112cSIngo Weinhold error = B_BAD_VALUE; 24198da112cSIngo Weinhold // write the data 24298da112cSIngo Weinhold if (error == B_OK) { 24398da112cSIngo Weinhold error = _WriteData(kTypeAttribute, kTypeResourceID, 24498da112cSIngo Weinhold B_MIME_STRING_TYPE, type, typeLen + 1); 24598da112cSIngo Weinhold } 24698da112cSIngo Weinhold } else 24798da112cSIngo Weinhold error = _RemoveData(kTypeAttribute, B_MIME_STRING_TYPE); 24898da112cSIngo Weinhold } 24998da112cSIngo Weinhold return error; 250d6b205f3SIngo Weinhold } 251d6b205f3SIngo Weinhold 252d6b205f3SIngo Weinhold // GetSignature 253d6b205f3SIngo Weinhold /*! \brief Gets the file's application signature. 254d6b205f3SIngo Weinhold 255d6b205f3SIngo Weinhold \param signature A pointer to a pre-allocated character buffer of size 256d6b205f3SIngo Weinhold \c B_MIME_TYPE_LENGTH or larger into which the application 257d6b205f3SIngo Weinhold signature of the file shall be written. 258d6b205f3SIngo Weinhold \return 259d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 260d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 261d6b205f3SIngo Weinhold - \c B_BAD_VALUE: \c NULL \a signature or the signature stored in the 262d6b205f3SIngo Weinhold attribute/resources is longer than \c B_MIME_TYPE_LENGTH. 263d6b205f3SIngo Weinhold - \c B_BAD_TYPE: The attribute/resources the signature is stored in have 264d6b205f3SIngo Weinhold the wrong type. 265d6b205f3SIngo Weinhold - \c B_ENTRY_NOT_FOUND: No signature is set on the file. 266d6b205f3SIngo Weinhold - other error codes 267d6b205f3SIngo Weinhold */ 268d6b205f3SIngo Weinhold status_t 269d6b205f3SIngo Weinhold BAppFileInfo::GetSignature(char *signature) const 270d6b205f3SIngo Weinhold { 27198da112cSIngo Weinhold // check param and initialization 27298da112cSIngo Weinhold status_t error = (signature ? B_OK : B_BAD_VALUE); 27398da112cSIngo Weinhold if (error == B_OK && InitCheck() != B_OK) 27498da112cSIngo Weinhold error = B_NO_INIT; 27598da112cSIngo Weinhold // read the data 27698da112cSIngo Weinhold size_t read = 0; 27798da112cSIngo Weinhold if (error == B_OK) { 27898da112cSIngo Weinhold error = _ReadData(kSignatureAttribute, kSignatureResourceID, 27998da112cSIngo Weinhold B_MIME_STRING_TYPE, signature, B_MIME_TYPE_LENGTH, 28098da112cSIngo Weinhold read); 28198da112cSIngo Weinhold } 28298da112cSIngo Weinhold // check the read data -- null terminate the string 28398da112cSIngo Weinhold if (error == B_OK && signature[read - 1] != '\0') { 28498da112cSIngo Weinhold if (read == B_MIME_TYPE_LENGTH) 28598da112cSIngo Weinhold error = B_ERROR; 28698da112cSIngo Weinhold else 28798da112cSIngo Weinhold signature[read] = '\0'; 28898da112cSIngo Weinhold } 28998da112cSIngo Weinhold return error; 290d6b205f3SIngo Weinhold } 291d6b205f3SIngo Weinhold 292d6b205f3SIngo Weinhold // SetSignature 293d6b205f3SIngo Weinhold /*! \brief Sets the file's application signature. 294d6b205f3SIngo Weinhold 295d6b205f3SIngo Weinhold If \a signature is \c NULL the file's application signature is unset. 296d6b205f3SIngo Weinhold 297d6b205f3SIngo Weinhold \param signature The application signature to be assigned to the file. 298d6b205f3SIngo Weinhold Must not be longer than \c B_MIME_TYPE_LENGTH (including the 299d6b205f3SIngo Weinhold terminating null). May be \c NULL. 300d6b205f3SIngo Weinhold \return 301d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 302d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 303d6b205f3SIngo Weinhold - \c B_BAD_VALUE: \a signature is longer than \c B_MIME_TYPE_LENGTH. 304d6b205f3SIngo Weinhold - other error codes 305d6b205f3SIngo Weinhold */ 306d6b205f3SIngo Weinhold status_t 307d6b205f3SIngo Weinhold BAppFileInfo::SetSignature(const char *signature) 308d6b205f3SIngo Weinhold { 30998da112cSIngo Weinhold // check initialization 31098da112cSIngo Weinhold status_t error = B_OK; 31198da112cSIngo Weinhold if (error == B_OK && InitCheck() != B_OK) 31298da112cSIngo Weinhold error = B_NO_INIT; 31398da112cSIngo Weinhold if (error == B_OK) { 31498da112cSIngo Weinhold if (signature) { 31598da112cSIngo Weinhold // check param 31698da112cSIngo Weinhold size_t signatureLen = strlen(signature); 31798da112cSIngo Weinhold if (error == B_OK && signatureLen >= B_MIME_TYPE_LENGTH) 31898da112cSIngo Weinhold error = B_BAD_VALUE; 31998da112cSIngo Weinhold // write the data 32098da112cSIngo Weinhold if (error == B_OK) { 32198da112cSIngo Weinhold error = _WriteData(kSignatureAttribute, kSignatureResourceID, 32298da112cSIngo Weinhold B_MIME_STRING_TYPE, signature, 32398da112cSIngo Weinhold signatureLen + 1); 32498da112cSIngo Weinhold } 32598da112cSIngo Weinhold } else 32698da112cSIngo Weinhold error = _RemoveData(kSignatureAttribute, B_MIME_STRING_TYPE); 32798da112cSIngo Weinhold } 32898da112cSIngo Weinhold return error; 329d6b205f3SIngo Weinhold } 330d6b205f3SIngo Weinhold 331d6b205f3SIngo Weinhold // GetAppFlags 332d6b205f3SIngo Weinhold /*! \brief Gets the file's application flags. 333d6b205f3SIngo Weinhold 334d6b205f3SIngo Weinhold \param flags A pointer to a pre-allocated uint32 into which the application 335d6b205f3SIngo Weinhold flags of the file shall be written. 336d6b205f3SIngo Weinhold \return 337d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 338d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 339d6b205f3SIngo Weinhold - \c B_BAD_VALUE: \c NULL \a flags. 340d6b205f3SIngo Weinhold - \c B_BAD_TYPE: The attribute/resources the flags are stored in have 341d6b205f3SIngo Weinhold the wrong type. 342d6b205f3SIngo Weinhold - \c B_ENTRY_NOT_FOUND: No application flags are set on the file. 343d6b205f3SIngo Weinhold - other error codes 344d6b205f3SIngo Weinhold */ 345d6b205f3SIngo Weinhold status_t 346d6b205f3SIngo Weinhold BAppFileInfo::GetAppFlags(uint32 *flags) const 347d6b205f3SIngo Weinhold { 34898da112cSIngo Weinhold // check param and initialization 34998da112cSIngo Weinhold status_t error = (flags ? B_OK : B_BAD_VALUE); 35098da112cSIngo Weinhold if (error == B_OK && InitCheck() != B_OK) 35198da112cSIngo Weinhold error = B_NO_INIT; 35298da112cSIngo Weinhold // read the data 35398da112cSIngo Weinhold size_t read = 0; 35498da112cSIngo Weinhold if (error == B_OK) { 35598da112cSIngo Weinhold error = _ReadData(kAppFlagsAttribute, kAppFlagsResourceID, 35698da112cSIngo Weinhold B_APP_FLAGS_TYPE, flags, sizeof(uint32), 35798da112cSIngo Weinhold read); 35898da112cSIngo Weinhold } 35998da112cSIngo Weinhold // check the read data 36098da112cSIngo Weinhold if (error == B_OK && read != sizeof(uint32)) 36198da112cSIngo Weinhold error = B_ERROR; 36298da112cSIngo Weinhold return error; 363d6b205f3SIngo Weinhold } 364d6b205f3SIngo Weinhold 365d6b205f3SIngo Weinhold // SetAppFlags 366d6b205f3SIngo Weinhold /*! \brief Sets the file's application flags. 367d6b205f3SIngo Weinhold \param flags The application flags to be assigned to the file. 368d6b205f3SIngo Weinhold \return 369d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 370d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 371d6b205f3SIngo Weinhold - other error codes 372d6b205f3SIngo Weinhold */ 373d6b205f3SIngo Weinhold status_t 374d6b205f3SIngo Weinhold BAppFileInfo::SetAppFlags(uint32 flags) 375d6b205f3SIngo Weinhold { 37698da112cSIngo Weinhold // check initialization 37798da112cSIngo Weinhold status_t error = B_OK; 37898da112cSIngo Weinhold if (error == B_OK && InitCheck() != B_OK) 37998da112cSIngo Weinhold error = B_NO_INIT; 38098da112cSIngo Weinhold if (error == B_OK) { 38198da112cSIngo Weinhold // write the data 38298da112cSIngo Weinhold if (error == B_OK) { 38398da112cSIngo Weinhold error = _WriteData(kAppFlagsAttribute, kAppFlagsResourceID, 38498da112cSIngo Weinhold B_APP_FLAGS_TYPE, &flags, sizeof(uint32)); 38598da112cSIngo Weinhold } 38698da112cSIngo Weinhold } 38798da112cSIngo Weinhold return error; 388d6b205f3SIngo Weinhold } 389d6b205f3SIngo Weinhold 390d6b205f3SIngo Weinhold // GetSupportedTypes 391d6b205f3SIngo Weinhold /*! \brief Gets the MIME types supported by the application. 392d6b205f3SIngo Weinhold 393d6b205f3SIngo Weinhold The supported MIME types are added to a field "types" of type 394d6b205f3SIngo Weinhold \c B_STRING_TYPE in \a types. 395d6b205f3SIngo Weinhold 396d6b205f3SIngo Weinhold \param types A pointer to a pre-allocated BMessage into which the 397d6b205f3SIngo Weinhold MIME types supported by the appplication shall be written. 398d6b205f3SIngo Weinhold \return 399d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 400d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 401d6b205f3SIngo Weinhold - \c B_BAD_VALUE: \c NULL \a types. 402d6b205f3SIngo Weinhold - \c B_BAD_TYPE: The attribute/resources the supported types are stored in 403d6b205f3SIngo Weinhold have the wrong type. 404d6b205f3SIngo Weinhold - \c B_ENTRY_NOT_FOUND: No supported types are set on the file. 405d6b205f3SIngo Weinhold - other error codes 406d6b205f3SIngo Weinhold */ 407d6b205f3SIngo Weinhold status_t 408d6b205f3SIngo Weinhold BAppFileInfo::GetSupportedTypes(BMessage *types) const 409d6b205f3SIngo Weinhold { 41098da112cSIngo Weinhold // check param and initialization 41198da112cSIngo Weinhold status_t error = (types ? B_OK : B_BAD_VALUE); 41298da112cSIngo Weinhold if (error == B_OK && InitCheck() != B_OK) 41398da112cSIngo Weinhold error = B_NO_INIT; 41498da112cSIngo Weinhold // read the data 41598da112cSIngo Weinhold size_t read = 0; 41698da112cSIngo Weinhold void *buffer = NULL; 41798da112cSIngo Weinhold if (error == B_OK) { 41898da112cSIngo Weinhold error = _ReadData(kSupportedTypesAttribute, kSupportedTypesResourceID, 41998da112cSIngo Weinhold B_MESSAGE_TYPE, NULL, 0, read, &buffer); 42098da112cSIngo Weinhold } 42198da112cSIngo Weinhold // unflatten the buffer 42298da112cSIngo Weinhold if (error == B_OK) 42398da112cSIngo Weinhold error = types->Unflatten((const char*)buffer); 42498da112cSIngo Weinhold // clean up 42598da112cSIngo Weinhold if (buffer) 42698da112cSIngo Weinhold free(buffer); 42798da112cSIngo Weinhold return error; 428d6b205f3SIngo Weinhold } 429d6b205f3SIngo Weinhold 430d6b205f3SIngo Weinhold // SetSupportedTypes 431d6b205f3SIngo Weinhold /*! \brief Sets the MIME types supported by the application. 432d6b205f3SIngo Weinhold 433d6b205f3SIngo Weinhold If \a types is \c NULL the application's supported types are unset. 434d6b205f3SIngo Weinhold 435d6b205f3SIngo Weinhold The supported MIME types must be stored in a field "types" of type 436d6b205f3SIngo Weinhold \c B_STRING_TYPE in \a types. 437d6b205f3SIngo Weinhold 43883a812a1SIngo Weinhold The method informs the registrar about this news. 43983a812a1SIngo Weinhold For each supported type the result of BMimeType::GetSupportingApps() will 44083a812a1SIngo Weinhold afterwards include the signature of this application. That is, the 44183a812a1SIngo Weinhold application file needs to have a signature set. 44283a812a1SIngo Weinhold 44383a812a1SIngo Weinhold \a syncAll specifies whether the not longer supported types shall be 44483a812a1SIngo Weinhold updated as well, i.e. whether this application shall be remove from the 44583a812a1SIngo Weinhold lists of supporting applications. 44683a812a1SIngo Weinhold 447d6b205f3SIngo Weinhold \param types The supported types to be assigned to the file. 448d6b205f3SIngo Weinhold May be \c NULL. 44983a812a1SIngo Weinhold \param syncAll \c true to also synchronize the not longer supported 45083a812a1SIngo Weinhold types, \c false otherwise. 451d6b205f3SIngo Weinhold \return 452d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 453d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 454d6b205f3SIngo Weinhold - other error codes 455d6b205f3SIngo Weinhold */ 456d6b205f3SIngo Weinhold status_t 457d6b205f3SIngo Weinhold BAppFileInfo::SetSupportedTypes(const BMessage *types, bool syncAll) 458d6b205f3SIngo Weinhold { 45998da112cSIngo Weinhold // check initialization 46098da112cSIngo Weinhold status_t error = B_OK; 46198da112cSIngo Weinhold if (error == B_OK && InitCheck() != B_OK) 46298da112cSIngo Weinhold error = B_NO_INIT; 46398da112cSIngo Weinhold BMimeType mimeType; 46498da112cSIngo Weinhold if (error == B_OK) 46598da112cSIngo Weinhold error = GetMetaMime(&mimeType); 4667f20062dSJérôme Duval if (error == B_OK || error == B_ENTRY_NOT_FOUND) { 4677f20062dSJérôme Duval error = B_OK; 46898da112cSIngo Weinhold if (types) { 46917819be3SIngo Weinhold // check param -- supported types must be valid 47017819be3SIngo Weinhold const char *type; 47117819be3SIngo Weinhold for (int32 i = 0; 47217819be3SIngo Weinhold error == B_OK && types->FindString("types", i, &type) == B_OK; 47317819be3SIngo Weinhold i++) { 47417819be3SIngo Weinhold if (!BMimeType::IsValid(type)) 47517819be3SIngo Weinhold error = B_BAD_VALUE; 47617819be3SIngo Weinhold } 47717819be3SIngo Weinhold // get flattened size 47817819be3SIngo Weinhold ssize_t size = 0; 47917819be3SIngo Weinhold if (error == B_OK) { 48017819be3SIngo Weinhold size = types->FlattenedSize(); 48198da112cSIngo Weinhold if (size < 0) 48298da112cSIngo Weinhold error = size; 48317819be3SIngo Weinhold } 48498da112cSIngo Weinhold // allocate a buffer for the flattened data 48598da112cSIngo Weinhold char *buffer = NULL; 48698da112cSIngo Weinhold if (error == B_OK) { 48798da112cSIngo Weinhold buffer = new(nothrow) char[size]; 48898da112cSIngo Weinhold if (!buffer) 48998da112cSIngo Weinhold error = B_NO_MEMORY; 49098da112cSIngo Weinhold } 49198da112cSIngo Weinhold // flatten the message 49298da112cSIngo Weinhold if (error == B_OK) 49398da112cSIngo Weinhold error = types->Flatten(buffer, size); 49498da112cSIngo Weinhold // write the data 49598da112cSIngo Weinhold if (error == B_OK) { 49698da112cSIngo Weinhold error = _WriteData(kSupportedTypesAttribute, 49798da112cSIngo Weinhold kSupportedTypesResourceID, B_MESSAGE_TYPE, 49898da112cSIngo Weinhold buffer, size); 49998da112cSIngo Weinhold } 50098da112cSIngo Weinhold // clean up 50198da112cSIngo Weinhold if (buffer) 50298da112cSIngo Weinhold delete[] buffer; 50398da112cSIngo Weinhold } else 50498da112cSIngo Weinhold error = _RemoveData(kSupportedTypesAttribute, B_MESSAGE_TYPE); 50598da112cSIngo Weinhold // update the MIME database, if the app signature is installed 50617819be3SIngo Weinhold if (error == B_OK && mimeType.IsInstalled()) 50717819be3SIngo Weinhold error = mimeType.SetSupportedTypes(types, syncAll); 50898da112cSIngo Weinhold } 50998da112cSIngo Weinhold return error; 510d6b205f3SIngo Weinhold } 511d6b205f3SIngo Weinhold 512d6b205f3SIngo Weinhold // SetSupportedTypes 513d6b205f3SIngo Weinhold /*! \brief Sets the MIME types supported by the application. 514d6b205f3SIngo Weinhold 51598da112cSIngo Weinhold This method is a short-hand for SetSupportedTypes(types, false). 51683a812a1SIngo Weinhold \see SetSupportedType(const BMessage*, bool) for detailed information. 517d6b205f3SIngo Weinhold 518d6b205f3SIngo Weinhold \param types The supported types to be assigned to the file. 519d6b205f3SIngo Weinhold May be \c NULL. 520d6b205f3SIngo Weinhold \return 521d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 522d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 523d6b205f3SIngo Weinhold - other error codes 524d6b205f3SIngo Weinhold */ 525d6b205f3SIngo Weinhold status_t 526d6b205f3SIngo Weinhold BAppFileInfo::SetSupportedTypes(const BMessage *types) 527d6b205f3SIngo Weinhold { 52898da112cSIngo Weinhold return SetSupportedTypes(types, false); 529d6b205f3SIngo Weinhold } 530d6b205f3SIngo Weinhold 531d6b205f3SIngo Weinhold // IsSupportedType 532d6b205f3SIngo Weinhold /*! \brief Returns whether the application supports the supplied MIME type. 533d6b205f3SIngo Weinhold 534d6b205f3SIngo Weinhold If the application supports the wildcard type "application/octet-stream" 535d6b205f3SIngo Weinhold any this method returns \c true for any MIME type. 536d6b205f3SIngo Weinhold 537d6b205f3SIngo Weinhold \param type The MIME type in question. 538d6b205f3SIngo Weinhold \return \c true, if \a type is a valid MIME type and it is supported by 539d6b205f3SIngo Weinhold the application, \c false otherwise. 540d6b205f3SIngo Weinhold */ 541d6b205f3SIngo Weinhold bool 542d6b205f3SIngo Weinhold BAppFileInfo::IsSupportedType(const char *type) const 543d6b205f3SIngo Weinhold { 54498da112cSIngo Weinhold status_t error = (type ? B_OK : B_BAD_VALUE); 54598da112cSIngo Weinhold // get the supported types 54698da112cSIngo Weinhold BMessage types; 54798da112cSIngo Weinhold if (error == B_OK) 54898da112cSIngo Weinhold error = GetSupportedTypes(&types); 54998da112cSIngo Weinhold // turn type into a BMimeType 55098da112cSIngo Weinhold BMimeType mimeType; 55198da112cSIngo Weinhold if (error == B_OK) 55298da112cSIngo Weinhold error = mimeType.SetTo(type); 55398da112cSIngo Weinhold // iterate through the supported types 55498da112cSIngo Weinhold bool found = false; 55598da112cSIngo Weinhold if (error == B_OK) { 55698da112cSIngo Weinhold const char *supportedType; 55798da112cSIngo Weinhold for (int32 i = 0; 55898da112cSIngo Weinhold !found && types.FindString("types", i, &supportedType) == B_OK; 55998da112cSIngo Weinhold i++) { 56098da112cSIngo Weinhold found = !strcmp(supportedType, "application/octet-stream") 56198da112cSIngo Weinhold || BMimeType(supportedType).Contains(&mimeType); 56298da112cSIngo Weinhold } 56398da112cSIngo Weinhold } 56498da112cSIngo Weinhold return found; 565d6b205f3SIngo Weinhold } 566d6b205f3SIngo Weinhold 567d6b205f3SIngo Weinhold // Supports 568d6b205f3SIngo Weinhold /*! \brief Returns whether the application supports the supplied MIME type 569d6b205f3SIngo Weinhold explicitly. 570d6b205f3SIngo Weinhold 571d6b205f3SIngo Weinhold Unlike IsSupportedType(), this method returns \c true, only if the type 572d6b205f3SIngo Weinhold is explicitly supported, regardless of whether it supports 573d6b205f3SIngo Weinhold "application/octet-stream". 574d6b205f3SIngo Weinhold 575d6b205f3SIngo Weinhold \param type The MIME type in question. 576d6b205f3SIngo Weinhold \return \c true, if \a type is a valid MIME type and it is explicitly 577d6b205f3SIngo Weinhold supported by the application, \c false otherwise. 578d6b205f3SIngo Weinhold */ 579d6b205f3SIngo Weinhold bool 580d6b205f3SIngo Weinhold BAppFileInfo::Supports(BMimeType *type) const 581d6b205f3SIngo Weinhold { 58298da112cSIngo Weinhold status_t error = (type && type->InitCheck() == B_OK ? B_OK : B_BAD_VALUE); 58398da112cSIngo Weinhold // get the supported types 58498da112cSIngo Weinhold BMessage types; 58598da112cSIngo Weinhold if (error == B_OK) 58698da112cSIngo Weinhold error = GetSupportedTypes(&types); 58798da112cSIngo Weinhold // iterate through the supported types 58898da112cSIngo Weinhold bool found = false; 58998da112cSIngo Weinhold if (error == B_OK) { 59098da112cSIngo Weinhold const char *supportedType; 59198da112cSIngo Weinhold for (int32 i = 0; 59298da112cSIngo Weinhold !found && types.FindString("types", i, &supportedType) == B_OK; 59398da112cSIngo Weinhold i++) { 59498da112cSIngo Weinhold found = BMimeType(supportedType).Contains(type); 59598da112cSIngo Weinhold } 59698da112cSIngo Weinhold } 59798da112cSIngo Weinhold return found; 598d6b205f3SIngo Weinhold } 599d6b205f3SIngo Weinhold 600d6b205f3SIngo Weinhold // GetIcon 601d6b205f3SIngo Weinhold /*! \brief Gets the file's icon. 602d6b205f3SIngo Weinhold \param icon A pointer to a pre-allocated BBitmap of the correct dimension 603d6b205f3SIngo Weinhold to store the requested icon (16x16 for the mini and 32x32 for the 604d6b205f3SIngo Weinhold large icon). 605d6b205f3SIngo Weinhold \param which Specifies the size of the icon to be retrieved: 606d6b205f3SIngo Weinhold \c B_MINI_ICON for the mini and \c B_LARGE_ICON for the large icon. 607d6b205f3SIngo Weinhold \return 608d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 609d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 610d6b205f3SIngo Weinhold - \c B_BAD_VALUE: \c NULL \a icon, unsupported icon size \a which or bitmap 611d6b205f3SIngo Weinhold dimensions (\a icon) and icon size (\a which) do not match. 612d6b205f3SIngo Weinhold - other error codes 613d6b205f3SIngo Weinhold */ 614d6b205f3SIngo Weinhold status_t 615d6b205f3SIngo Weinhold BAppFileInfo::GetIcon(BBitmap *icon, icon_size which) const 616d6b205f3SIngo Weinhold { 61798da112cSIngo Weinhold return GetIconForType(NULL, icon, which); 618d6b205f3SIngo Weinhold } 619d6b205f3SIngo Weinhold 6207fb6186fSStephan Aßmus // GetIcon 6217fb6186fSStephan Aßmus /*! \brief Gets the file's icon. 6227fb6186fSStephan Aßmus \param data The pointer in which the flat icon data will be returned. 6237fb6186fSStephan Aßmus \param size The pointer in which the size of the data found will be returned. 6247fb6186fSStephan Aßmus \return 6257fb6186fSStephan Aßmus - \c B_OK: Everything went fine. 6267fb6186fSStephan Aßmus - \c B_NO_INIT: The object is not properly initialized. 6277fb6186fSStephan Aßmus - \c B_BAD_VALUE: \c NULL \a data or \c NULL size. 6287fb6186fSStephan Aßmus - other error codes 6297fb6186fSStephan Aßmus */ 6307fb6186fSStephan Aßmus status_t 6317fb6186fSStephan Aßmus BAppFileInfo::GetIcon(uint8** data, size_t* size) const 6327fb6186fSStephan Aßmus { 6337fb6186fSStephan Aßmus return GetIconForType(NULL, data, size); 6347fb6186fSStephan Aßmus } 6357fb6186fSStephan Aßmus 636d6b205f3SIngo Weinhold // SetIcon 637d6b205f3SIngo Weinhold /*! \brief Sets the file's icon. 638d6b205f3SIngo Weinhold 639d6b205f3SIngo Weinhold If \a icon is \c NULL the file's icon is unset. 640d6b205f3SIngo Weinhold 641d6b205f3SIngo Weinhold \param icon A pointer to the BBitmap containing the icon to be set. 642d6b205f3SIngo Weinhold May be \c NULL. 643d6b205f3SIngo Weinhold \param which Specifies the size of the icon to be set: \c B_MINI_ICON 644d6b205f3SIngo Weinhold for the mini and \c B_LARGE_ICON for the large icon. 645d6b205f3SIngo Weinhold \return 646d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 647d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 648d6b205f3SIngo Weinhold - \c B_BAD_VALUE: Unknown icon size \a which or bitmap dimensions (\a icon) 649d6b205f3SIngo Weinhold and icon size (\a which) do not match. 650d6b205f3SIngo Weinhold - other error codes 651d6b205f3SIngo Weinhold */ 652d6b205f3SIngo Weinhold status_t 653d6b205f3SIngo Weinhold BAppFileInfo::SetIcon(const BBitmap *icon, icon_size which) 654d6b205f3SIngo Weinhold { 65598da112cSIngo Weinhold return SetIconForType(NULL, icon, which); 656d6b205f3SIngo Weinhold } 657d6b205f3SIngo Weinhold 6587fb6186fSStephan Aßmus // SetIcon 6597fb6186fSStephan Aßmus /*! \brief Sets the file's icon. 6607fb6186fSStephan Aßmus 6617fb6186fSStephan Aßmus If \a icon is \c NULL the file's icon is unset. 6627fb6186fSStephan Aßmus 6637fb6186fSStephan Aßmus \param data A pointer to the data buffer containing the vector icon 6647fb6186fSStephan Aßmus to be set. May be \c NULL. 6657fb6186fSStephan Aßmus \param size Specifies the size of buffer pointed to by \a data. 6667fb6186fSStephan Aßmus \return 6677fb6186fSStephan Aßmus - \c B_OK: Everything went fine. 6687fb6186fSStephan Aßmus - \c B_NO_INIT: The object is not properly initialized. 6697fb6186fSStephan Aßmus - \c B_BAD_VALUE: \c NULL data. 6707fb6186fSStephan Aßmus - other error codes 6717fb6186fSStephan Aßmus */ 6727fb6186fSStephan Aßmus status_t 6737fb6186fSStephan Aßmus BAppFileInfo::SetIcon(const uint8* data, size_t size) 6747fb6186fSStephan Aßmus { 6757fb6186fSStephan Aßmus return SetIconForType(NULL, data, size); 6767fb6186fSStephan Aßmus } 6777fb6186fSStephan Aßmus 678d6b205f3SIngo Weinhold // GetVersionInfo 679d6b205f3SIngo Weinhold /*! \brief Gets the file's version info. 680d6b205f3SIngo Weinhold \param info A pointer to a pre-allocated version_info structure into which 681d6b205f3SIngo Weinhold the version info should be written. 682d6b205f3SIngo Weinhold \param kind Specifies the kind of the version info to be retrieved: 683d6b205f3SIngo Weinhold \c B_APP_VERSION_KIND for the application's version info and 684d6b205f3SIngo Weinhold \c B_SYSTEM_VERSION_KIND for the suite's info the application 685d6b205f3SIngo Weinhold belongs to. 686d6b205f3SIngo Weinhold \return 687d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 688d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 689d6b205f3SIngo Weinhold - \c B_BAD_VALUE: \c NULL \a info. 690d6b205f3SIngo Weinhold - other error codes 691d6b205f3SIngo Weinhold */ 692d6b205f3SIngo Weinhold status_t 693d6b205f3SIngo Weinhold BAppFileInfo::GetVersionInfo(version_info *info, version_kind kind) const 694d6b205f3SIngo Weinhold { 69598da112cSIngo Weinhold // check params and initialization 696b1970bb8SIngo Weinhold if (!info) 697b1970bb8SIngo Weinhold return B_BAD_VALUE; 698b1970bb8SIngo Weinhold 69998da112cSIngo Weinhold int32 index = 0; 70098da112cSIngo Weinhold switch (kind) { 70198da112cSIngo Weinhold case B_APP_VERSION_KIND: 70298da112cSIngo Weinhold index = 0; 70398da112cSIngo Weinhold break; 70498da112cSIngo Weinhold case B_SYSTEM_VERSION_KIND: 70598da112cSIngo Weinhold index = 1; 70698da112cSIngo Weinhold break; 70798da112cSIngo Weinhold default: 708b1970bb8SIngo Weinhold return B_BAD_VALUE; 70998da112cSIngo Weinhold } 710b1970bb8SIngo Weinhold 711b1970bb8SIngo Weinhold if (InitCheck() != B_OK) 712b1970bb8SIngo Weinhold return B_NO_INIT; 713b1970bb8SIngo Weinhold 71498da112cSIngo Weinhold // read the data 71598da112cSIngo Weinhold size_t read = 0; 71698da112cSIngo Weinhold version_info infos[2]; 717b1970bb8SIngo Weinhold status_t error = _ReadData(kVersionInfoAttribute, kVersionInfoResourceID, 718b1970bb8SIngo Weinhold B_VERSION_INFO_TYPE, infos, 2 * sizeof(version_info), read); 719b1970bb8SIngo Weinhold if (error != B_OK) 72098da112cSIngo Weinhold return error; 721b1970bb8SIngo Weinhold 722b1970bb8SIngo Weinhold // check the read data 723b1970bb8SIngo Weinhold if (read == sizeof(version_info)) { 724b1970bb8SIngo Weinhold // only the app version info is there -- return a cleared system info 725b1970bb8SIngo Weinhold if (index == 0) 726b1970bb8SIngo Weinhold *info = infos[index]; 727b1970bb8SIngo Weinhold else if (index == 1) 728b1970bb8SIngo Weinhold memset(info, 0, sizeof(version_info)); 729b1970bb8SIngo Weinhold } else if (read == 2 * sizeof(version_info)) { 730b1970bb8SIngo Weinhold *info = infos[index]; 731b1970bb8SIngo Weinhold } else 732b1970bb8SIngo Weinhold return B_ERROR; 733b1970bb8SIngo Weinhold 734b1970bb8SIngo Weinhold // return result 735b1970bb8SIngo Weinhold return B_OK; 736d6b205f3SIngo Weinhold } 737d6b205f3SIngo Weinhold 738d6b205f3SIngo Weinhold // SetVersionInfo 739d6b205f3SIngo Weinhold /*! \brief Sets the file's version info. 740d6b205f3SIngo Weinhold 741d6b205f3SIngo Weinhold If \a info is \c NULL the file's version info is unset. 742d6b205f3SIngo Weinhold 743d6b205f3SIngo Weinhold \param info The version info to be set. May be \c NULL. 744d6b205f3SIngo Weinhold \param kind Specifies kind of version info to be set: 745d6b205f3SIngo Weinhold \c B_APP_VERSION_KIND for the application's version info and 746d6b205f3SIngo Weinhold \c B_SYSTEM_VERSION_KIND for the suite's info the application 747d6b205f3SIngo Weinhold belongs to. 748d6b205f3SIngo Weinhold \return 749d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 750d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 751d6b205f3SIngo Weinhold - other error codes 752d6b205f3SIngo Weinhold */ 753d6b205f3SIngo Weinhold status_t 754d6b205f3SIngo Weinhold BAppFileInfo::SetVersionInfo(const version_info *info, version_kind kind) 755d6b205f3SIngo Weinhold { 75698da112cSIngo Weinhold // check initialization 75798da112cSIngo Weinhold status_t error = B_OK; 75898da112cSIngo Weinhold if (error == B_OK && InitCheck() != B_OK) 75998da112cSIngo Weinhold error = B_NO_INIT; 76098da112cSIngo Weinhold if (error == B_OK) { 76198da112cSIngo Weinhold if (info) { 76298da112cSIngo Weinhold // check param 76398da112cSIngo Weinhold int32 index = 0; 76498da112cSIngo Weinhold if (error == B_OK) { 76598da112cSIngo Weinhold switch (kind) { 76698da112cSIngo Weinhold case B_APP_VERSION_KIND: 76798da112cSIngo Weinhold index = 0; 76898da112cSIngo Weinhold break; 76998da112cSIngo Weinhold case B_SYSTEM_VERSION_KIND: 77098da112cSIngo Weinhold index = 1; 77198da112cSIngo Weinhold break; 77298da112cSIngo Weinhold default: 77398da112cSIngo Weinhold error = B_BAD_VALUE; 77498da112cSIngo Weinhold break; 77598da112cSIngo Weinhold } 77698da112cSIngo Weinhold } 77798da112cSIngo Weinhold // read both infos 77898da112cSIngo Weinhold version_info infos[2]; 77998da112cSIngo Weinhold if (error == B_OK) { 78098da112cSIngo Weinhold size_t read; 781b1970bb8SIngo Weinhold if (_ReadData(kVersionInfoAttribute, kVersionInfoResourceID, 782b1970bb8SIngo Weinhold B_VERSION_INFO_TYPE, infos, 2 * sizeof(version_info), 783b1970bb8SIngo Weinhold read) == B_OK) { 784b1970bb8SIngo Weinhold // clear the part that hasn't been read 785b1970bb8SIngo Weinhold if (read < sizeof(infos)) 786b1970bb8SIngo Weinhold memset((char*)infos + read, 0, sizeof(infos) - read); 787b1970bb8SIngo Weinhold } else { 788b1970bb8SIngo Weinhold // failed to read -- clear 789b1970bb8SIngo Weinhold memset(infos, 0, sizeof(infos)); 790b1970bb8SIngo Weinhold } 79198da112cSIngo Weinhold } 79298da112cSIngo Weinhold infos[index] = *info; 79398da112cSIngo Weinhold // write the data 79498da112cSIngo Weinhold if (error == B_OK) { 79598da112cSIngo Weinhold error = _WriteData(kVersionInfoAttribute, 79698da112cSIngo Weinhold kVersionInfoResourceID, 79798da112cSIngo Weinhold B_VERSION_INFO_TYPE, infos, 79898da112cSIngo Weinhold 2 * sizeof(version_info)); 79998da112cSIngo Weinhold } 80098da112cSIngo Weinhold } else 80198da112cSIngo Weinhold error = _RemoveData(kVersionInfoAttribute, B_VERSION_INFO_TYPE); 80298da112cSIngo Weinhold } 80398da112cSIngo Weinhold return error; 804d6b205f3SIngo Weinhold } 805d6b205f3SIngo Weinhold 806d6b205f3SIngo Weinhold // GetIconForType 807d6b205f3SIngo Weinhold /*! \brief Gets the icon the application provides for a given MIME type. 80898da112cSIngo Weinhold 80998da112cSIngo Weinhold If \a type is \c NULL, the application's icon is retrieved. 81098da112cSIngo Weinhold 81198da112cSIngo Weinhold \param type The MIME type in question. May be \c NULL. 812d6b205f3SIngo Weinhold \param icon A pointer to a pre-allocated BBitmap of the correct dimension 813d6b205f3SIngo Weinhold to store the requested icon (16x16 for the mini and 32x32 for the 814d6b205f3SIngo Weinhold large icon). 815d6b205f3SIngo Weinhold \param which Specifies the size of the icon to be retrieved: 816d6b205f3SIngo Weinhold \c B_MINI_ICON for the mini and \c B_LARGE_ICON for the large icon. 817d6b205f3SIngo Weinhold \return 818d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 819d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 82098da112cSIngo Weinhold - \c B_BAD_VALUE: \c NULL \a icon, unsupported icon size 821d6b205f3SIngo Weinhold \a which or bitmap dimensions (\a icon) and icon size (\a which) do 822d6b205f3SIngo Weinhold not match. 823d6b205f3SIngo Weinhold - other error codes 824d6b205f3SIngo Weinhold */ 825d6b205f3SIngo Weinhold status_t 826d6b205f3SIngo Weinhold BAppFileInfo::GetIconForType(const char* type, BBitmap* icon, 8279f507806SStephan Aßmus icon_size size) const 828d6b205f3SIngo Weinhold { 8299ecf9d1cSIngo Weinhold if (InitCheck() != B_OK) 8309ecf9d1cSIngo Weinhold return B_NO_INIT; 8319ecf9d1cSIngo Weinhold 8329ecf9d1cSIngo Weinhold if (!icon || icon->InitCheck() != B_OK) 8339ecf9d1cSIngo Weinhold return B_BAD_VALUE; 8349ecf9d1cSIngo Weinhold 8352a74c553SStephan Aßmus // TODO: for consistency with attribute based icon reading, we 8362a74c553SStephan Aßmus // could also prefer B_CMAP8 icons here if the provided bitmap 8372a74c553SStephan Aßmus // is in that format. Right now, an existing B_CMAP8 icon resource 8382a74c553SStephan Aßmus // would be ignored as soon as a vector icon is present. On the other 8392a74c553SStephan Aßmus // hand, maybe this still results in a more consistent user interface, 8402a74c553SStephan Aßmus // since Tracker/Deskbar would surely show the vector icon. 8412a74c553SStephan Aßmus 8429ecf9d1cSIngo Weinhold // try vector icon first 8439ecf9d1cSIngo Weinhold BString vectorAttributeName(kIconAttribute); 8449ecf9d1cSIngo Weinhold 8459ecf9d1cSIngo Weinhold // check type param 8469ecf9d1cSIngo Weinhold if (type) { 8479ecf9d1cSIngo Weinhold if (BMimeType::IsValid(type)) 8489ecf9d1cSIngo Weinhold vectorAttributeName += type; 8499ecf9d1cSIngo Weinhold else 8509ecf9d1cSIngo Weinhold return B_BAD_VALUE; 8519ecf9d1cSIngo Weinhold } else { 8529ecf9d1cSIngo Weinhold vectorAttributeName += kIconType; 8539ecf9d1cSIngo Weinhold } 8549ecf9d1cSIngo Weinhold const char* attribute = vectorAttributeName.String(); 8559ecf9d1cSIngo Weinhold 8569ecf9d1cSIngo Weinhold size_t bytesRead; 8579ecf9d1cSIngo Weinhold void* allocatedBuffer; 858bae87c91SAxel Dörfler status_t error = _ReadData(attribute, -1, B_VECTOR_ICON_TYPE, NULL, 0, 8599ecf9d1cSIngo Weinhold bytesRead, &allocatedBuffer); 8609ecf9d1cSIngo Weinhold if (error == B_OK) { 8617fb6186fSStephan Aßmus error = BIconUtils::GetVectorIcon((uint8*)allocatedBuffer, 8629ecf9d1cSIngo Weinhold bytesRead, icon); 8637fb6186fSStephan Aßmus free(allocatedBuffer); 8647fb6186fSStephan Aßmus return error; 8659ecf9d1cSIngo Weinhold } 8669ecf9d1cSIngo Weinhold 8679f507806SStephan Aßmus // no vector icon if we got this far, 8689f507806SStephan Aßmus // align size argument just in case 8699f507806SStephan Aßmus if (size < B_LARGE_ICON) 8709f507806SStephan Aßmus size = B_MINI_ICON; 8719f507806SStephan Aßmus else 8729f507806SStephan Aßmus size = B_LARGE_ICON; 8739ecf9d1cSIngo Weinhold 8749ecf9d1cSIngo Weinhold error = B_OK; 87598da112cSIngo Weinhold // set some icon size related variables 87698da112cSIngo Weinhold BString attributeString; 87798da112cSIngo Weinhold BRect bounds; 878a04efc92SIngo Weinhold uint32 attrType = 0; 879a04efc92SIngo Weinhold size_t attrSize = 0; 8809f507806SStephan Aßmus switch (size) { 88198da112cSIngo Weinhold case B_MINI_ICON: 88298da112cSIngo Weinhold attributeString = kMiniIconAttribute; 88398da112cSIngo Weinhold bounds.Set(0, 0, 15, 15); 88498da112cSIngo Weinhold attrType = B_MINI_ICON_TYPE; 88598da112cSIngo Weinhold attrSize = 16 * 16; 88698da112cSIngo Weinhold break; 88798da112cSIngo Weinhold case B_LARGE_ICON: 88898da112cSIngo Weinhold attributeString = kLargeIconAttribute; 88998da112cSIngo Weinhold bounds.Set(0, 0, 31, 31); 89098da112cSIngo Weinhold attrType = B_LARGE_ICON_TYPE; 89198da112cSIngo Weinhold attrSize = 32 * 32; 89298da112cSIngo Weinhold break; 89398da112cSIngo Weinhold default: 8949ecf9d1cSIngo Weinhold return B_BAD_VALUE; 89598da112cSIngo Weinhold } 89698da112cSIngo Weinhold // check type param 89798da112cSIngo Weinhold if (type) { 89817819be3SIngo Weinhold if (BMimeType::IsValid(type)) 89917819be3SIngo Weinhold attributeString += type; 90017819be3SIngo Weinhold else 9019ecf9d1cSIngo Weinhold return B_BAD_VALUE; 90298da112cSIngo Weinhold } else 90317819be3SIngo Weinhold attributeString += kStandardIconType; 9049ecf9d1cSIngo Weinhold 9059ecf9d1cSIngo Weinhold attribute = attributeString.String(); 90617819be3SIngo Weinhold 9079f507806SStephan Aßmus // check parameters 9089f507806SStephan Aßmus // currently, scaling B_CMAP8 icons is not supported 9099f507806SStephan Aßmus if (icon->ColorSpace() == B_CMAP8 && icon->Bounds() != bounds) 9109ecf9d1cSIngo Weinhold return B_BAD_VALUE; 9119ecf9d1cSIngo Weinhold 91298da112cSIngo Weinhold // read the data 91398da112cSIngo Weinhold if (error == B_OK) { 9149f507806SStephan Aßmus bool tempBuffer = (icon->ColorSpace() != B_CMAP8 9159f507806SStephan Aßmus || icon->Bounds() != bounds); 9169f507806SStephan Aßmus uint8* buffer = NULL; 91798da112cSIngo Weinhold size_t read; 9189f507806SStephan Aßmus if (tempBuffer) { 9199f507806SStephan Aßmus // other color space or bitmap size than stored in attribute 9209f507806SStephan Aßmus buffer = new(nothrow) uint8[attrSize]; 9219f507806SStephan Aßmus if (!buffer) { 92298da112cSIngo Weinhold error = B_NO_MEMORY; 9239f507806SStephan Aßmus } else { 92498da112cSIngo Weinhold error = _ReadData(attribute, -1, attrType, buffer, attrSize, 92598da112cSIngo Weinhold read); 92698da112cSIngo Weinhold } 92798da112cSIngo Weinhold } else { 92898da112cSIngo Weinhold error = _ReadData(attribute, -1, attrType, icon->Bits(), attrSize, 92998da112cSIngo Weinhold read); 93098da112cSIngo Weinhold } 93198da112cSIngo Weinhold if (error == B_OK && read != attrSize) 93298da112cSIngo Weinhold error = B_ERROR; 9339f507806SStephan Aßmus if (tempBuffer) { 93498da112cSIngo Weinhold // other color space than stored in attribute 93576ba3434SIngo Weinhold if (error == B_OK) { 9369f507806SStephan Aßmus error = BIconUtils::ConvertFromCMAP8(buffer, 9379f507806SStephan Aßmus (uint32)size, 9389f507806SStephan Aßmus (uint32)size, 9399f507806SStephan Aßmus (uint32)size, 9409f507806SStephan Aßmus icon); 94176ba3434SIngo Weinhold } 94298da112cSIngo Weinhold delete[] buffer; 94398da112cSIngo Weinhold } 94498da112cSIngo Weinhold } 94598da112cSIngo Weinhold return error; 946d6b205f3SIngo Weinhold } 947d6b205f3SIngo Weinhold 9487fb6186fSStephan Aßmus // GetIconForType 9497fb6186fSStephan Aßmus /*! \brief Gets the icon the application provides for a given MIME type. 9507fb6186fSStephan Aßmus 9517fb6186fSStephan Aßmus If \a type is \c NULL, the application's icon is retrieved. 9527fb6186fSStephan Aßmus 9537fb6186fSStephan Aßmus \param type The MIME type in question. May be \c NULL. 9547fb6186fSStephan Aßmus \param data A pointer in which the icon data will be returned. When you 9557fb6186fSStephan Aßmus are done with the data, you should use free() to deallocate it. 9567fb6186fSStephan Aßmus \param size A pointer in which the size of the retrieved data is returned. 9577fb6186fSStephan Aßmus \return 9587fb6186fSStephan Aßmus - \c B_OK: Everything went fine. 9597fb6186fSStephan Aßmus - \c B_NO_INIT: The object is not properly initialized. 9607fb6186fSStephan Aßmus - \c B_BAD_VALUE: \c NULL \a data and/or \a size. Or the supplied 9617fb6186fSStephan Aßmus \a type is not a valid MIME type. 9627fb6186fSStephan Aßmus - other error codes 9637fb6186fSStephan Aßmus */ 9647fb6186fSStephan Aßmus status_t 9657fb6186fSStephan Aßmus BAppFileInfo::GetIconForType(const char *type, uint8** data, 9667fb6186fSStephan Aßmus size_t* size) const 9677fb6186fSStephan Aßmus { 9687fb6186fSStephan Aßmus if (InitCheck() != B_OK) 9697fb6186fSStephan Aßmus return B_NO_INIT; 9707fb6186fSStephan Aßmus 9717fb6186fSStephan Aßmus if (!data || !size) 9727fb6186fSStephan Aßmus return B_BAD_VALUE; 9737fb6186fSStephan Aßmus 9747fb6186fSStephan Aßmus // get vector icon 9757fb6186fSStephan Aßmus BString attributeName(kIconAttribute); 9767fb6186fSStephan Aßmus 9777fb6186fSStephan Aßmus // check type param 9787fb6186fSStephan Aßmus if (type) { 9797fb6186fSStephan Aßmus if (BMimeType::IsValid(type)) 9807fb6186fSStephan Aßmus attributeName += type; 9817fb6186fSStephan Aßmus else 9827fb6186fSStephan Aßmus return B_BAD_VALUE; 9837fb6186fSStephan Aßmus } else { 9847fb6186fSStephan Aßmus attributeName += kIconType; 9857fb6186fSStephan Aßmus } 9867fb6186fSStephan Aßmus 9877fb6186fSStephan Aßmus void* allocatedBuffer = NULL; 9887fb6186fSStephan Aßmus status_t ret = _ReadData(attributeName.String(), -1, 989bae87c91SAxel Dörfler B_VECTOR_ICON_TYPE, NULL, 0, *size, &allocatedBuffer); 9907fb6186fSStephan Aßmus 9917fb6186fSStephan Aßmus if (ret < B_OK) 9927fb6186fSStephan Aßmus return ret; 9937fb6186fSStephan Aßmus 9947fb6186fSStephan Aßmus *data = (uint8*)allocatedBuffer; 9957fb6186fSStephan Aßmus return B_OK; 9967fb6186fSStephan Aßmus } 9977fb6186fSStephan Aßmus 998d6b205f3SIngo Weinhold // SetIconForType 999d6b205f3SIngo Weinhold /*! \brief Sets the icon the application provides for a given MIME type. 1000d6b205f3SIngo Weinhold 100198da112cSIngo Weinhold If \a type is \c NULL, the application's icon is set. 1002d6b205f3SIngo Weinhold If \a icon is \c NULL the icon is unset. 1003d6b205f3SIngo Weinhold 100498da112cSIngo Weinhold If the file has a signature, then the icon is also set on the MIME type. 100598da112cSIngo Weinhold If the type for the signature has not been installed yet, it is installed 100698da112cSIngo Weinhold before. 100798da112cSIngo Weinhold 100898da112cSIngo Weinhold \param type The MIME type in question. May be \c NULL. 1009d6b205f3SIngo Weinhold \param icon A pointer to the BBitmap containing the icon to be set. 1010d6b205f3SIngo Weinhold May be \c NULL. 1011d6b205f3SIngo Weinhold \param which Specifies the size of the icon to be set: \c B_MINI_ICON 1012d6b205f3SIngo Weinhold for the mini and \c B_LARGE_ICON for the large icon. 1013d6b205f3SIngo Weinhold \return 1014d6b205f3SIngo Weinhold - \c B_OK: Everything went fine. 1015d6b205f3SIngo Weinhold - \c B_NO_INIT: The object is not properly initialized. 10167fb6186fSStephan Aßmus - \c B_BAD_VALUE: Either the icon size \a which is unkown, bitmap dimensions (\a icon) 10177fb6186fSStephan Aßmus and icon size (\a which) do not match, or the provided \a type is 10187fb6186fSStephan Aßmus not a valid MIME type. 1019d6b205f3SIngo Weinhold - other error codes 1020d6b205f3SIngo Weinhold */ 1021d6b205f3SIngo Weinhold status_t 1022d6b205f3SIngo Weinhold BAppFileInfo::SetIconForType(const char *type, const BBitmap *icon, 1023d6b205f3SIngo Weinhold icon_size which) 1024d6b205f3SIngo Weinhold { 102598da112cSIngo Weinhold status_t error = B_OK; 102698da112cSIngo Weinhold // set some icon size related variables 102798da112cSIngo Weinhold BString attributeString; 102898da112cSIngo Weinhold BRect bounds; 1029a04efc92SIngo Weinhold uint32 attrType = 0; 1030a04efc92SIngo Weinhold size_t attrSize = 0; 1031a04efc92SIngo Weinhold int32 resourceID = 0; 103298da112cSIngo Weinhold switch (which) { 103398da112cSIngo Weinhold case B_MINI_ICON: 103498da112cSIngo Weinhold attributeString = kMiniIconAttribute; 103598da112cSIngo Weinhold bounds.Set(0, 0, 15, 15); 103698da112cSIngo Weinhold attrType = B_MINI_ICON_TYPE; 103798da112cSIngo Weinhold attrSize = 16 * 16; 103898da112cSIngo Weinhold resourceID = (type ? kMiniIconForTypeResourceID 103998da112cSIngo Weinhold : kMiniIconResourceID); 104098da112cSIngo Weinhold break; 104198da112cSIngo Weinhold case B_LARGE_ICON: 104298da112cSIngo Weinhold attributeString = kLargeIconAttribute; 104398da112cSIngo Weinhold bounds.Set(0, 0, 31, 31); 104498da112cSIngo Weinhold attrType = B_LARGE_ICON_TYPE; 104598da112cSIngo Weinhold attrSize = 32 * 32; 104698da112cSIngo Weinhold resourceID = (type ? kLargeIconForTypeResourceID 104798da112cSIngo Weinhold : kLargeIconResourceID); 104898da112cSIngo Weinhold break; 104998da112cSIngo Weinhold default: 105098da112cSIngo Weinhold error = B_BAD_VALUE; 105198da112cSIngo Weinhold break; 105298da112cSIngo Weinhold } 105398da112cSIngo Weinhold // check type param 105498da112cSIngo Weinhold if (error == B_OK) { 105598da112cSIngo Weinhold if (type) { 105617819be3SIngo Weinhold if (BMimeType::IsValid(type)) 105798da112cSIngo Weinhold attributeString += type; 105817819be3SIngo Weinhold else 105917819be3SIngo Weinhold error = B_BAD_VALUE; 106098da112cSIngo Weinhold } else 106198da112cSIngo Weinhold attributeString += kStandardIconType; 106298da112cSIngo Weinhold } 106398da112cSIngo Weinhold const char *attribute = attributeString.String(); 106498da112cSIngo Weinhold // check parameter and initialization 106598da112cSIngo Weinhold if (error == B_OK && icon 106698da112cSIngo Weinhold && (icon->InitCheck() != B_OK || icon->Bounds() != bounds)) { 106798da112cSIngo Weinhold error = B_BAD_VALUE; 106898da112cSIngo Weinhold } 106998da112cSIngo Weinhold if (error == B_OK && InitCheck() != B_OK) 107098da112cSIngo Weinhold error = B_NO_INIT; 107198da112cSIngo Weinhold // write/remove the attribute 107298da112cSIngo Weinhold if (error == B_OK) { 107398da112cSIngo Weinhold if (icon) { 107498da112cSIngo Weinhold bool otherColorSpace = (icon->ColorSpace() != B_CMAP8); 107598da112cSIngo Weinhold if (otherColorSpace) { 1076290bc091SIngo Weinhold BBitmap bitmap(bounds, B_BITMAP_NO_SERVER_LINK, B_CMAP8); 107798da112cSIngo Weinhold error = bitmap.InitCheck(); 107876ba3434SIngo Weinhold if (error == B_OK) 107976ba3434SIngo Weinhold error = bitmap.ImportBits(icon); 108098da112cSIngo Weinhold if (error == B_OK) { 108198da112cSIngo Weinhold error = _WriteData(attribute, resourceID, attrType, 108298da112cSIngo Weinhold bitmap.Bits(), attrSize, true); 108398da112cSIngo Weinhold } 108498da112cSIngo Weinhold } else { 108598da112cSIngo Weinhold error = _WriteData(attribute, resourceID, attrType, 108698da112cSIngo Weinhold icon->Bits(), attrSize, true); 108798da112cSIngo Weinhold } 108898da112cSIngo Weinhold } else // no icon given => remove 108998da112cSIngo Weinhold error = _RemoveData(attribute, attrType); 109098da112cSIngo Weinhold } 109198da112cSIngo Weinhold // set the attribute on the MIME type, if the file has a signature 109298da112cSIngo Weinhold BMimeType mimeType; 109398da112cSIngo Weinhold if (error == B_OK && GetMetaMime(&mimeType) == B_OK) { 109498da112cSIngo Weinhold if (!mimeType.IsInstalled()) 109598da112cSIngo Weinhold error = mimeType.Install(); 109698da112cSIngo Weinhold if (error == B_OK) 109798da112cSIngo Weinhold error = mimeType.SetIconForType(type, icon, which); 109898da112cSIngo Weinhold } 109998da112cSIngo Weinhold return error; 1100d6b205f3SIngo Weinhold } 1101d6b205f3SIngo Weinhold 11027fb6186fSStephan Aßmus // SetIconForType 11037fb6186fSStephan Aßmus /*! \brief Sets the icon the application provides for a given MIME type. 11047fb6186fSStephan Aßmus 11057fb6186fSStephan Aßmus If \a type is \c NULL, the application's icon is set. 11067fb6186fSStephan Aßmus If \a data is \c NULL the icon is unset. 11077fb6186fSStephan Aßmus 11087fb6186fSStephan Aßmus If the file has a signature, then the icon is also set on the MIME type. 11097fb6186fSStephan Aßmus If the type for the signature has not been installed yet, it is installed 11107fb6186fSStephan Aßmus before. 11117fb6186fSStephan Aßmus 11127fb6186fSStephan Aßmus \param type The MIME type in question. May be \c NULL. 11137fb6186fSStephan Aßmus \param data A pointer to the data containing the icon to be set. 11147fb6186fSStephan Aßmus May be \c NULL. 11157fb6186fSStephan Aßmus \param size Specifies the size of buffer provided in \a data. 11167fb6186fSStephan Aßmus \return 11177fb6186fSStephan Aßmus - \c B_OK: Everything went fine. 11187fb6186fSStephan Aßmus - \c B_NO_INIT: The object is not properly initialized. 11197fb6186fSStephan Aßmus - \c B_BAD_VALUE: The provided \a type is not a valid MIME type. 11207fb6186fSStephan Aßmus - other error codes 11217fb6186fSStephan Aßmus */ 11227fb6186fSStephan Aßmus status_t 11237fb6186fSStephan Aßmus BAppFileInfo::SetIconForType(const char* type, const uint8* data, 11247fb6186fSStephan Aßmus size_t size) 11257fb6186fSStephan Aßmus { 11267fb6186fSStephan Aßmus if (InitCheck() != B_OK) 11277fb6186fSStephan Aßmus return B_NO_INIT; 11287fb6186fSStephan Aßmus 11297fb6186fSStephan Aßmus // set some icon related variables 11307fb6186fSStephan Aßmus BString attributeString = kIconAttribute; 11317fb6186fSStephan Aßmus int32 resourceID = type ? kIconForTypeResourceID : kIconResourceID; 1132bae87c91SAxel Dörfler uint32 attrType = B_VECTOR_ICON_TYPE; 11337fb6186fSStephan Aßmus 11347fb6186fSStephan Aßmus // check type param 11357fb6186fSStephan Aßmus if (type) { 11367fb6186fSStephan Aßmus if (BMimeType::IsValid(type)) 11377fb6186fSStephan Aßmus attributeString += type; 11387fb6186fSStephan Aßmus else 11397fb6186fSStephan Aßmus return B_BAD_VALUE; 11407fb6186fSStephan Aßmus } else 11417fb6186fSStephan Aßmus attributeString += kIconType; 11427fb6186fSStephan Aßmus 11437fb6186fSStephan Aßmus const char *attribute = attributeString.String(); 11447fb6186fSStephan Aßmus 11457fb6186fSStephan Aßmus status_t error; 11467fb6186fSStephan Aßmus // write/remove the attribute 11477fb6186fSStephan Aßmus if (data) 11487fb6186fSStephan Aßmus error = _WriteData(attribute, resourceID, attrType, data, size, true); 11497fb6186fSStephan Aßmus else // no icon given => remove 11507fb6186fSStephan Aßmus error = _RemoveData(attribute, attrType); 11517fb6186fSStephan Aßmus 11527fb6186fSStephan Aßmus // set the attribute on the MIME type, if the file has a signature 11537fb6186fSStephan Aßmus BMimeType mimeType; 11547fb6186fSStephan Aßmus if (error == B_OK && GetMetaMime(&mimeType) == B_OK) { 11557fb6186fSStephan Aßmus if (!mimeType.IsInstalled()) 11567fb6186fSStephan Aßmus error = mimeType.Install(); 11577fb6186fSStephan Aßmus if (error == B_OK) 11587fb6186fSStephan Aßmus error = mimeType.SetIconForType(type, data, size); 11597fb6186fSStephan Aßmus } 11607fb6186fSStephan Aßmus return error; 11617fb6186fSStephan Aßmus } 11627fb6186fSStephan Aßmus 1163d6b205f3SIngo Weinhold // SetInfoLocation 1164d6b205f3SIngo Weinhold /*! \brief Specifies the location where the meta data shall be stored. 1165d6b205f3SIngo Weinhold 1166d6b205f3SIngo Weinhold The options for \a location are: 1167d6b205f3SIngo Weinhold - \c B_USE_ATTRIBUTES: Store the data in the attributes. 1168d6b205f3SIngo Weinhold - \c B_USE_RESOURCES: Store the data in the resources. 1169d6b205f3SIngo Weinhold - \c B_USE_BOTH_LOCATIONS: Store the data in attributes and resources. 1170d6b205f3SIngo Weinhold 1171d6b205f3SIngo Weinhold \param location The location where the meta data shall be stored. 1172d6b205f3SIngo Weinhold */ 1173d6b205f3SIngo Weinhold void 1174d6b205f3SIngo Weinhold BAppFileInfo::SetInfoLocation(info_location location) 1175d6b205f3SIngo Weinhold { 1176d0c290bfSAxel Dörfler // if the resources failed to initialize, we must not use them 1177d0c290bfSAxel Dörfler if (fResources == NULL) 1178d0c290bfSAxel Dörfler location = info_location(location & ~B_USE_RESOURCES); 1179d0c290bfSAxel Dörfler 118098da112cSIngo Weinhold fWhere = location; 1181d6b205f3SIngo Weinhold } 1182d6b205f3SIngo Weinhold 1183d6b205f3SIngo Weinhold // IsUsingAttributes 1184d6b205f3SIngo Weinhold /*! \brief Returns whether the object stores the meta data (also) in the 1185d6b205f3SIngo Weinhold file's attributes. 1186d6b205f3SIngo Weinhold \return \c true, if the meta data are (also) stored in the file's 1187d6b205f3SIngo Weinhold attributes, \c false otherwise. 1188d6b205f3SIngo Weinhold */ 1189d6b205f3SIngo Weinhold bool 1190d6b205f3SIngo Weinhold BAppFileInfo::IsUsingAttributes() const 1191d6b205f3SIngo Weinhold { 1192d0c290bfSAxel Dörfler return (fWhere & B_USE_ATTRIBUTES) != 0; 1193d6b205f3SIngo Weinhold } 1194d6b205f3SIngo Weinhold 1195d6b205f3SIngo Weinhold // IsUsingResources 1196d6b205f3SIngo Weinhold /*! \brief Returns whether the object stores the meta data (also) in the 1197d6b205f3SIngo Weinhold file's resources. 1198d6b205f3SIngo Weinhold \return \c true, if the meta data are (also) stored in the file's 1199d6b205f3SIngo Weinhold resources, \c false otherwise. 1200d6b205f3SIngo Weinhold */ 1201d6b205f3SIngo Weinhold bool 1202d6b205f3SIngo Weinhold BAppFileInfo::IsUsingResources() const 1203d6b205f3SIngo Weinhold { 1204d0c290bfSAxel Dörfler return (fWhere & B_USE_RESOURCES) != 0; 1205d6b205f3SIngo Weinhold } 1206d6b205f3SIngo Weinhold 1207d6b205f3SIngo Weinhold // FBC 1208d6b205f3SIngo Weinhold void BAppFileInfo::_ReservedAppFileInfo1() {} 1209d6b205f3SIngo Weinhold void BAppFileInfo::_ReservedAppFileInfo2() {} 1210d6b205f3SIngo Weinhold void BAppFileInfo::_ReservedAppFileInfo3() {} 1211d6b205f3SIngo Weinhold 1212d6b205f3SIngo Weinhold // = 1213d6b205f3SIngo Weinhold /*! \brief Privatized assignment operator to prevent usage. 1214d6b205f3SIngo Weinhold */ 1215d6b205f3SIngo Weinhold BAppFileInfo & 1216d6b205f3SIngo Weinhold BAppFileInfo::operator=(const BAppFileInfo &) 1217d6b205f3SIngo Weinhold { 1218d6b205f3SIngo Weinhold return *this; 1219d6b205f3SIngo Weinhold } 1220d6b205f3SIngo Weinhold 1221d6b205f3SIngo Weinhold // copy constructor 1222d6b205f3SIngo Weinhold /*! \brief Privatized copy constructor to prevent usage. 1223d6b205f3SIngo Weinhold */ 1224d6b205f3SIngo Weinhold BAppFileInfo::BAppFileInfo(const BAppFileInfo &) 1225d6b205f3SIngo Weinhold { 1226d6b205f3SIngo Weinhold } 1227d6b205f3SIngo Weinhold 122898da112cSIngo Weinhold // GetMetaMime 122998da112cSIngo Weinhold /*! \brief Initializes a BMimeType to the file's signature. 123098da112cSIngo Weinhold 123198da112cSIngo Weinhold The parameter \a meta is not checked. 123298da112cSIngo Weinhold 123398da112cSIngo Weinhold \param meta A pointer to a pre-allocated BMimeType that shall be 123498da112cSIngo Weinhold initialized to the file's signature. 123598da112cSIngo Weinhold \return 123698da112cSIngo Weinhold - \c B_OK: Everything went fine. 123798da112cSIngo Weinhold - \c B_BAD_VALUE: \c NULL \a meta 123898da112cSIngo Weinhold - \c B_ENTRY_NOT_FOUND: The file has not signature or the signature is 123998da112cSIngo Weinhold ( not installed in the MIME database.) 124098da112cSIngo Weinhold no valid MIME string. 124198da112cSIngo Weinhold - other error codes 124298da112cSIngo Weinhold */ 124398da112cSIngo Weinhold status_t 124498da112cSIngo Weinhold BAppFileInfo::GetMetaMime(BMimeType *meta) const 124598da112cSIngo Weinhold { 124698da112cSIngo Weinhold char signature[B_MIME_TYPE_LENGTH]; 124798da112cSIngo Weinhold status_t error = GetSignature(signature); 124898da112cSIngo Weinhold if (error == B_OK) 124998da112cSIngo Weinhold error = meta->SetTo(signature); 12507f20062dSJérôme Duval else if (error == B_BAD_VALUE) 12517f20062dSJérôme Duval error = B_ENTRY_NOT_FOUND; 125298da112cSIngo Weinhold if (error == B_OK && !meta->IsValid()) 125398da112cSIngo Weinhold error = B_BAD_VALUE; 125498da112cSIngo Weinhold return error; 125598da112cSIngo Weinhold } 125698da112cSIngo Weinhold 125798da112cSIngo Weinhold // _ReadData 125898da112cSIngo Weinhold /*! \brief Reads data from an attribute or resource. 125998da112cSIngo Weinhold 126098da112cSIngo Weinhold The data are read from the location specified by \a fWhere. 126198da112cSIngo Weinhold 126298da112cSIngo Weinhold The object must be properly initialized. The parameters are NOT checked. 126398da112cSIngo Weinhold 126498da112cSIngo Weinhold \param name The name of the attribute/resource to be read. 126598da112cSIngo Weinhold \param id The resource ID of the resource to be read. Is ignored, when 126698da112cSIngo Weinhold < 0. 126798da112cSIngo Weinhold \param type The type of the attribute/resource to be read. 126898da112cSIngo Weinhold \param buffer A pre-allocated buffer for the data to be read. 126998da112cSIngo Weinhold \param bufferSize The size of the supplied buffer. 127098da112cSIngo Weinhold \param bytesRead A reference parameter, set to the number of bytes 127198da112cSIngo Weinhold actually read. 127298da112cSIngo Weinhold \param allocatedBuffer If not \c NULL, the method allocates a buffer 127398da112cSIngo Weinhold large enough too store the whole data and writes a pointer to it 127498da112cSIngo Weinhold into this variable. If \c NULL, the supplied buffer is used. 127598da112cSIngo Weinhold \return 127698da112cSIngo Weinhold - \c B_OK: Everything went fine. 127798da112cSIngo Weinhold - error code 127898da112cSIngo Weinhold */ 127998da112cSIngo Weinhold status_t 128098da112cSIngo Weinhold BAppFileInfo::_ReadData(const char *name, int32 id, type_code type, 128198da112cSIngo Weinhold void *buffer, size_t bufferSize, 128298da112cSIngo Weinhold size_t &bytesRead, void **allocatedBuffer) const 128398da112cSIngo Weinhold { 128498da112cSIngo Weinhold status_t error = B_OK; 1285338b8dc3SIngo Weinhold 128698da112cSIngo Weinhold if (allocatedBuffer) 128798da112cSIngo Weinhold buffer = NULL; 1288338b8dc3SIngo Weinhold 1289338b8dc3SIngo Weinhold bool foundData = false; 1290338b8dc3SIngo Weinhold 129198da112cSIngo Weinhold if (IsUsingAttributes()) { 129298da112cSIngo Weinhold // get an attribute info 129398da112cSIngo Weinhold attr_info info; 129498da112cSIngo Weinhold if (error == B_OK) 129598da112cSIngo Weinhold error = fNode->GetAttrInfo(name, &info); 1296338b8dc3SIngo Weinhold 129798da112cSIngo Weinhold // check type and size, allocate a buffer, if required 129898da112cSIngo Weinhold if (error == B_OK && info.type != type) 129998da112cSIngo Weinhold error = B_BAD_VALUE; 1300b4598d95SIngo Weinhold if (error == B_OK && allocatedBuffer) { 130198da112cSIngo Weinhold buffer = malloc(info.size); 130298da112cSIngo Weinhold if (!buffer) 130398da112cSIngo Weinhold error = B_NO_MEMORY; 130498da112cSIngo Weinhold bufferSize = info.size; 130598da112cSIngo Weinhold } 130698da112cSIngo Weinhold if (error == B_OK && bufferSize < info.size) 130798da112cSIngo Weinhold error = B_BAD_VALUE; 1308338b8dc3SIngo Weinhold 130998da112cSIngo Weinhold // read the data 131098da112cSIngo Weinhold if (error == B_OK) { 131198da112cSIngo Weinhold ssize_t read = fNode->ReadAttr(name, type, 0, buffer, info.size); 131298da112cSIngo Weinhold if (read < 0) 131398da112cSIngo Weinhold error = read; 131498da112cSIngo Weinhold else if (read != info.size) 131598da112cSIngo Weinhold error = B_ERROR; 131698da112cSIngo Weinhold else 131798da112cSIngo Weinhold bytesRead = read; 131898da112cSIngo Weinhold } 1319338b8dc3SIngo Weinhold 1320338b8dc3SIngo Weinhold foundData = (error == B_OK); 1321b4598d95SIngo Weinhold 1322b4598d95SIngo Weinhold // free the allocated buffer on error 1323b4598d95SIngo Weinhold if (!foundData && allocatedBuffer && buffer) { 1324b4598d95SIngo Weinhold free(buffer); 1325b4598d95SIngo Weinhold buffer = NULL; 1326b4598d95SIngo Weinhold } 1327338b8dc3SIngo Weinhold } 1328338b8dc3SIngo Weinhold 1329338b8dc3SIngo Weinhold if (!foundData && IsUsingResources()) { 133098da112cSIngo Weinhold // get a resource info 1331338b8dc3SIngo Weinhold error = B_OK; 133298da112cSIngo Weinhold int32 idFound; 133398da112cSIngo Weinhold size_t sizeFound; 133498da112cSIngo Weinhold if (error == B_OK) { 133598da112cSIngo Weinhold if (!fResources->GetResourceInfo(type, name, &idFound, &sizeFound)) 133698da112cSIngo Weinhold error = B_ENTRY_NOT_FOUND; 133798da112cSIngo Weinhold } 1338338b8dc3SIngo Weinhold 133998da112cSIngo Weinhold // check id and size, allocate a buffer, if required 134098da112cSIngo Weinhold if (error == B_OK && id >= 0 && idFound != id) 134198da112cSIngo Weinhold error = B_ENTRY_NOT_FOUND; 1342b4598d95SIngo Weinhold if (error == B_OK && allocatedBuffer) { 134398da112cSIngo Weinhold buffer = malloc(sizeFound); 134498da112cSIngo Weinhold if (!buffer) 134598da112cSIngo Weinhold error = B_NO_MEMORY; 134698da112cSIngo Weinhold bufferSize = sizeFound; 134798da112cSIngo Weinhold } 134898da112cSIngo Weinhold if (error == B_OK && bufferSize < sizeFound) 134998da112cSIngo Weinhold error = B_BAD_VALUE; 1350338b8dc3SIngo Weinhold 135198da112cSIngo Weinhold // load resource 135298da112cSIngo Weinhold const void *resourceData = NULL; 135398da112cSIngo Weinhold if (error == B_OK) { 135498da112cSIngo Weinhold resourceData = fResources->LoadResource(type, name, &bytesRead); 135598da112cSIngo Weinhold if (resourceData && sizeFound == bytesRead) 135698da112cSIngo Weinhold memcpy(buffer, resourceData, bytesRead); 135798da112cSIngo Weinhold else 135898da112cSIngo Weinhold error = B_ERROR; 135998da112cSIngo Weinhold } 1360773be699SAxel Dörfler } else if (!foundData) 136198da112cSIngo Weinhold error = B_BAD_VALUE; 1362338b8dc3SIngo Weinhold 136398da112cSIngo Weinhold // return the allocated buffer, or free it on error 136498da112cSIngo Weinhold if (allocatedBuffer) { 136598da112cSIngo Weinhold if (error == B_OK) 136698da112cSIngo Weinhold *allocatedBuffer = buffer; 136798da112cSIngo Weinhold else 136898da112cSIngo Weinhold free(buffer); 136998da112cSIngo Weinhold } 1370338b8dc3SIngo Weinhold 137198da112cSIngo Weinhold return error; 137298da112cSIngo Weinhold } 137398da112cSIngo Weinhold 137498da112cSIngo Weinhold // _WriteData 137598da112cSIngo Weinhold /*! \brief Writes data to an attribute or resource. 137698da112cSIngo Weinhold 137798da112cSIngo Weinhold The data are written to the location(s) specified by \a fWhere. 137898da112cSIngo Weinhold 137998da112cSIngo Weinhold The object must be properly initialized. The parameters are NOT checked. 138098da112cSIngo Weinhold 138198da112cSIngo Weinhold \param name The name of the attribute/resource to be written. 138298da112cSIngo Weinhold \param id The resource ID of the resource to be written. 138398da112cSIngo Weinhold \param type The type of the attribute/resource to be written. 138498da112cSIngo Weinhold \param buffer A buffer containing the data to be written. 138598da112cSIngo Weinhold \param bufferSize The size of the supplied buffer. 138698da112cSIngo Weinhold \param findID If set to \c true use the ID that is already assigned to the 138798da112cSIngo Weinhold \a name / \a type pair or take the first unused ID >= \a id. 138898da112cSIngo Weinhold If \c false, \a id is used. 138998da112cSIngo Weinhold If \a id is already in use and . 139098da112cSIngo Weinhold \return 139198da112cSIngo Weinhold - \c B_OK: Everything went fine. 139298da112cSIngo Weinhold - error code 139398da112cSIngo Weinhold */ 139498da112cSIngo Weinhold status_t 139598da112cSIngo Weinhold BAppFileInfo::_WriteData(const char *name, int32 id, type_code type, 139698da112cSIngo Weinhold const void *buffer, size_t bufferSize, bool findID) 139798da112cSIngo Weinhold { 1398d0c290bfSAxel Dörfler if (!IsUsingAttributes() && !IsUsingResources()) 1399d0c290bfSAxel Dörfler return B_NO_INIT; 1400d0c290bfSAxel Dörfler 140198da112cSIngo Weinhold status_t error = B_OK; 1402d0c290bfSAxel Dörfler 140398da112cSIngo Weinhold // write to attribute 1404d0c290bfSAxel Dörfler if (IsUsingAttributes()) { 140598da112cSIngo Weinhold ssize_t written = fNode->WriteAttr(name, type, 0, buffer, bufferSize); 140698da112cSIngo Weinhold if (written < 0) 140798da112cSIngo Weinhold error = written; 140898da112cSIngo Weinhold else if (written != (ssize_t)bufferSize) 140998da112cSIngo Weinhold error = B_ERROR; 141098da112cSIngo Weinhold } 141198da112cSIngo Weinhold // write to resource 141298da112cSIngo Weinhold if (IsUsingResources() && error == B_OK) { 141398da112cSIngo Weinhold if (findID) { 141498da112cSIngo Weinhold // get the resource info 141598da112cSIngo Weinhold int32 idFound; 141698da112cSIngo Weinhold size_t sizeFound; 141798da112cSIngo Weinhold if (fResources->GetResourceInfo(type, name, &idFound, &sizeFound)) 141898da112cSIngo Weinhold id = idFound; 141998da112cSIngo Weinhold else { 142098da112cSIngo Weinhold // type-name pair doesn't exist yet -- find unused ID 142198da112cSIngo Weinhold while (fResources->HasResource(type, id)) 142298da112cSIngo Weinhold id++; 142398da112cSIngo Weinhold } 142498da112cSIngo Weinhold } 142598da112cSIngo Weinhold error = fResources->AddResource(type, id, buffer, bufferSize, name); 142698da112cSIngo Weinhold } 142798da112cSIngo Weinhold return error; 142898da112cSIngo Weinhold } 142998da112cSIngo Weinhold 143098da112cSIngo Weinhold // _RemoveData 143198da112cSIngo Weinhold /*! \brief Removes an attribute or resource. 143298da112cSIngo Weinhold 143398da112cSIngo Weinhold The removal location is specified by \a fWhere. 143498da112cSIngo Weinhold 143598da112cSIngo Weinhold The object must be properly initialized. The parameters are NOT checked. 143698da112cSIngo Weinhold 143798da112cSIngo Weinhold \param name The name of the attribute/resource to be remove. 143898da112cSIngo Weinhold \param type The type of the attribute/resource to be removed. 143998da112cSIngo Weinhold \return 144098da112cSIngo Weinhold - \c B_OK: Everything went fine. 144198da112cSIngo Weinhold - error code 144298da112cSIngo Weinhold */ 144398da112cSIngo Weinhold status_t 144498da112cSIngo Weinhold BAppFileInfo::_RemoveData(const char *name, type_code type) 144598da112cSIngo Weinhold { 1446d0c290bfSAxel Dörfler if (!IsUsingAttributes() && !IsUsingResources()) 1447d0c290bfSAxel Dörfler return B_NO_INIT; 1448d0c290bfSAxel Dörfler 144998da112cSIngo Weinhold status_t error = B_OK; 1450d0c290bfSAxel Dörfler 145198da112cSIngo Weinhold // remove the attribute 1452d0c290bfSAxel Dörfler if (IsUsingAttributes()) { 145398da112cSIngo Weinhold error = fNode->RemoveAttr(name); 145498da112cSIngo Weinhold // It's no error, if there has been no attribute. 145598da112cSIngo Weinhold if (error == B_ENTRY_NOT_FOUND) 145698da112cSIngo Weinhold error = B_OK; 145798da112cSIngo Weinhold } 145898da112cSIngo Weinhold // remove the resource 145998da112cSIngo Weinhold if (IsUsingResources() && error == B_OK) { 146098da112cSIngo Weinhold // get a resource info 146198da112cSIngo Weinhold int32 idFound; 146298da112cSIngo Weinhold size_t sizeFound; 146398da112cSIngo Weinhold if (fResources->GetResourceInfo(type, name, &idFound, &sizeFound)) 146498da112cSIngo Weinhold error = fResources->RemoveResource(type, idFound); 146598da112cSIngo Weinhold } 146698da112cSIngo Weinhold return error; 146798da112cSIngo Weinhold } 146898da112cSIngo Weinhold 1469