Ticket #3825: URL-linkification-in-People.patch
File URL-linkification-in-People.patch, 5.2 KB (added by , 12 years ago) |
---|
-
src/apps/people/AttributeTextControl.cpp
From 1be8d3927f6a04ce2438b2f6d815a9ec0d2557ab Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian <alexander@sulfrian.net> Date: Fri, 6 Apr 2012 17:08:15 +0200 Subject: [PATCH] URL linkification in People url and email label are now marked as a link and open the address in the browser/mail-app on click --- src/apps/people/AttributeTextControl.cpp | 123 ++++++++++++++++++++++++++++++ src/apps/people/AttributeTextControl.h | 12 +++ 2 files changed, 135 insertions(+), 0 deletions(-) diff --git a/src/apps/people/AttributeTextControl.cpp b/src/apps/people/AttributeTextControl.cpp index c01cc72..f18c65b 100644
a b 4 4 * 5 5 * Authors: 6 6 * Robert Polic 7 * Alexander Sulfrian 7 8 * 8 9 * Copyright 1999, Be Incorporated. All Rights Reserved. 9 10 * This file may be used under the terms of the Be Sample Code License. … … 12 13 13 14 #include "AttributeTextControl.h" 14 15 16 #include <Roster.h> 17 #include <Cursor.h> 18 #include <StorageKit.h> 19 15 20 #include <string.h> 16 21 #include <malloc.h> 17 22 … … 23 28 #define B_TRANSLATE_CONTEXT "People" 24 29 25 30 31 #ifndef B_URL_MIME 32 #define B_URL_MIME "application/x-vnd.Be.URL." 33 #endif 34 35 26 36 AttributeTextControl::AttributeTextControl(const char* label, 27 37 const char* attribute) 28 38 : … … AttributeTextControl::~AttributeTextControl() 42 52 } 43 53 44 54 55 void 56 AttributeTextControl::MouseDown(BPoint mousePosition) 57 { 58 if(_VisibleLabelBounds().Contains(mousePosition) && _ContainsUrl()) 59 _HandleLabelClicked(Text()); 60 else 61 BTextControl::MouseDown(mousePosition); 62 } 63 64 65 void 66 AttributeTextControl::MouseMoved(BPoint mousePosition, uint32 transit, const BMessage* dragMessage) 67 { 68 if (_VisibleLabelBounds().Contains(mousePosition) && _ContainsUrl()) { 69 BCursor linkCursor(B_CURSOR_ID_FOLLOW_LINK); 70 SetViewCursor(&linkCursor, true); 71 } else 72 SetViewCursor(B_CURSOR_SYSTEM_DEFAULT, true); 73 74 BTextControl::MouseMoved(mousePosition, transit, dragMessage); 75 } 76 77 45 78 bool 46 79 AttributeTextControl::HasChanged() 47 80 { … … AttributeTextControl::Update() 62 95 { 63 96 fOriginalValue = Text(); 64 97 } 98 99 100 void 101 AttributeTextControl::_HandleLabelClicked(const char *text) 102 { 103 BString argument(text); 104 105 if (Attribute() == "META:url") { 106 _MakeUniformUrl(argument); 107 108 BString mimeType = B_URL_MIME; 109 _BuildMimeString(mimeType, argument); 110 111 if (mimeType != "") { 112 const char *args[] = {argument.String(), 0}; 113 be_roster->Launch(mimeType.String(), 1, const_cast<char**>(args)); 114 } 115 } 116 else if (Attribute() == "META:email") { 117 if (argument.IFindFirst("mailto:") != 0 && argument != "") 118 argument.Prepend("mailto:"); 119 120 // TODO: Could check for possible e-mail patterns. 121 if (argument != "") { 122 const char *args[] = {argument.String(), 0}; 123 be_roster->Launch("text/x-email", 1, const_cast<char**>(args)); 124 } 125 } 126 } 127 128 129 const BString& 130 AttributeTextControl::_MakeUniformUrl(BString &url) const 131 { 132 if (url.IFindFirst("www") == 0) 133 url.Prepend("http://"); 134 else if (url.IFindFirst("ftp") == 0) 135 url.Prepend("ftp://"); 136 137 return url; 138 } 139 140 141 const BString& 142 AttributeTextControl::_BuildMimeString(BString &mimeType, 143 const BString &url) const 144 { 145 if (url.IFindFirst("http://") == 0 146 || url.IFindFirst("ftp://") == 0 147 || url.IFindFirst("https://") == 0) { 148 149 mimeType.Append(url, url.FindFirst(':')); 150 } 151 152 if (!BMimeType::IsValid(mimeType.String())) 153 mimeType = ""; 154 155 return mimeType; 156 } 157 158 159 bool 160 AttributeTextControl::_ContainsUrl() const 161 { 162 BString argument(Text()); 163 164 if (Attribute() == "META:url") { 165 BString mimeType = B_URL_MIME; 166 if (_BuildMimeString(mimeType, _MakeUniformUrl(argument)) != "") 167 return true; 168 } 169 else if (Attribute() == "META:email") { 170 if (argument != "") 171 return true; 172 } 173 174 return false; 175 } 176 177 178 BRect 179 AttributeTextControl::_VisibleLabelBounds() const 180 { 181 // TODO: Could actually check current alignment of the label. 182 // The code below works only for right aligned labels. 183 BRect bounds(Bounds()); 184 bounds.right = Divider(); 185 bounds.left = bounds.right - StringWidth(Label()); 186 return bounds; 187 } -
src/apps/people/AttributeTextControl.h
diff --git a/src/apps/people/AttributeTextControl.h b/src/apps/people/AttributeTextControl.h index 2374353..8270039 100644
a b 4 4 * 5 5 * Authors: 6 6 * Robert Polic 7 * Alexander Sulfrian 7 8 * 8 9 * Copyright 1999, Be Incorporated. All Rights Reserved. 9 10 * This file may be used under the terms of the Be Sample Code License. … … public: 21 22 const char* attribute); 22 23 virtual ~AttributeTextControl(); 23 24 25 virtual void MouseDown(BPoint); 26 virtual void MouseMoved(BPoint, uint32, const BMessage*); 24 27 bool HasChanged(); 25 28 void Revert(); 26 29 void Update(); … … public: 29 32 { return fAttribute; } 30 33 31 34 private: 35 const BString& _MakeUniformUrl(BString &url) const; 36 const BString& _BuildMimeString(BString &mimeType, 37 const BString &url) const; 38 39 bool _ContainsUrl() const; 40 41 BRect _VisibleLabelBounds() const; 42 void _HandleLabelClicked(const char*); 43 32 44 BString fAttribute; 33 45 BString fOriginalValue; 34 46 };