Ticket #3825: URL-linkification-in-People.patch

File URL-linkification-in-People.patch, 5.2 KB (added by animux, 12 years ago)

patch on top of the current master

  • 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  
    44 *
    55 * Authors:
    66 *      Robert Polic
     7 *      Alexander Sulfrian
    78 *
    89 * Copyright 1999, Be Incorporated.   All Rights Reserved.
    910 * This file may be used under the terms of the Be Sample Code License.
     
    1213
    1314#include "AttributeTextControl.h"
    1415
     16#include <Roster.h>
     17#include <Cursor.h>
     18#include <StorageKit.h>
     19
    1520#include <string.h>
    1621#include <malloc.h>
    1722
     
    2328#define B_TRANSLATE_CONTEXT "People"
    2429
    2530
     31#ifndef B_URL_MIME
     32#define B_URL_MIME  "application/x-vnd.Be.URL."
     33#endif
     34
     35
    2636AttributeTextControl::AttributeTextControl(const char* label,
    2737        const char* attribute)
    2838    :
    AttributeTextControl::~AttributeTextControl()  
    4252}
    4353
    4454
     55void
     56AttributeTextControl::MouseDown(BPoint mousePosition)
     57{
     58    if(_VisibleLabelBounds().Contains(mousePosition) && _ContainsUrl())
     59        _HandleLabelClicked(Text());
     60    else
     61        BTextControl::MouseDown(mousePosition);
     62}
     63
     64
     65void
     66AttributeTextControl::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
    4578bool
    4679AttributeTextControl::HasChanged()
    4780{
    AttributeTextControl::Update()  
    6295{
    6396    fOriginalValue = Text();
    6497}
     98
     99
     100void
     101AttributeTextControl::_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
     129const BString&
     130AttributeTextControl::_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
     141const BString&
     142AttributeTextControl::_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
     159bool
     160AttributeTextControl::_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
     178BRect
     179AttributeTextControl::_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  
    44 *
    55 * Authors:
    66 *      Robert Polic
     7 *      Alexander Sulfrian
    78 *
    89 * Copyright 1999, Be Incorporated.   All Rights Reserved.
    910 * This file may be used under the terms of the Be Sample Code License.
    public:  
    2122                                    const char* attribute);
    2223    virtual                     ~AttributeTextControl();
    2324
     25    virtual void                MouseDown(BPoint);
     26    virtual void                MouseMoved(BPoint, uint32, const BMessage*);
    2427            bool                HasChanged();
    2528            void                Revert();
    2629            void                Update();
    public:  
    2932                                    { return fAttribute; }
    3033
    3134private:
     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
    3244            BString             fAttribute;
    3345            BString             fOriginalValue;
    3446};