diff --git a/headers/os/net/Url.h b/headers/os/net/Url.h index 0363989ef8..c4600ce9b5 100644 --- a/headers/os/net/Url.h +++ b/headers/os/net/Url.h @@ -71,6 +71,12 @@ public: static BString UrlDecode(const BString& url, bool strict = false); + // utility functionality + bool HasPreferredApplication() const; + BString PreferredApplication() const; + status_t OpenWithPreferredApplication( + bool onProblemAskUser = true) const; + // BArchivable members virtual status_t Archive(BMessage* into, bool deep = true) const; @@ -104,6 +110,8 @@ private: static bool _IsGenDelim(char c); static bool _IsSubDelim(char c); + BString _UrlMimeType() const; + private: mutable BString fUrlString; mutable BString fAuthority; diff --git a/src/kits/network/libnetapi/Url.cpp b/src/kits/network/libnetapi/Url.cpp index fb1d499aae..783e723d4d 100644 --- a/src/kits/network/libnetapi/Url.cpp +++ b/src/kits/network/libnetapi/Url.cpp @@ -14,6 +14,9 @@ #include #include +#include +#include + #include @@ -573,6 +576,76 @@ BUrl::UrlDecode(bool strict) } +// #pragma mark - utility functionality + + +bool +BUrl::HasPreferredApplication() const +{ + BString appSignature = PreferredApplication(); + BMimeType mime(appSignature.String()); + + if (appSignature.IFindFirst("application/") == 0 + && mime.IsValid()) + return true; + + return false; +} + + +BString +BUrl::PreferredApplication() const +{ + BString appSignature; + BMimeType mime(_UrlMimeType().String()); + mime.GetPreferredApp(appSignature.LockBuffer(B_MIME_TYPE_LENGTH)); + appSignature.UnlockBuffer(); + + return BString(appSignature); +} + + +status_t +BUrl::OpenWithPreferredApplication(bool onProblemAskUser) const +{ + if (!IsValid()) + return B_BAD_VALUE; + + BString urlString = UrlString(); + if (urlString.Length() > B_PATH_NAME_LENGTH) { + // TODO: BAlert + // if (onProblemAskUser) + // BAlert ... Too long URL! +#if DEBUG + fprintf(stderr, "URL too long"); +#endif + return B_NAME_TOO_LONG; + } + + char* argv[] = { + const_cast("BUrlInvokedApplication"), + const_cast(urlString.String()), + NULL + }; + +#if DEBUG + if (HasPreferredApplication()) + printf("HasPreferredApplication() == true\n"); + else + printf("HasPreferredApplication() == false\n"); +#endif + + status_t status = be_roster->Launch(_UrlMimeType().String(), 1, argv+1); + if (status != B_OK) { +#if DEBUG + fprintf(stderr, "Opening URL failed: %s\n", strerror(status)); +#endif + } + + return status; +} + + // #pragma mark Url encoding/decoding of string @@ -982,3 +1055,13 @@ BUrl::_IsSubDelim(char c) || c == ')' || c == '*' || c == '+' || c == ',' || c == ';' || c == '='; } + + +BString +BUrl::_UrlMimeType() const +{ + BString mime; + mime << "application/x-vnd.Be.URL." << fProtocol; + + return BString(mime); +}