~ubuntu-branches/ubuntu/quantal/psi/quantal

« back to all changes in this revision

Viewing changes to src/widgets/urlobject.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * urlobject.cpp - helper class for handling links
 
3
 * Copyright (C) 2003-2006  Michail Pishchagin
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include "urlobject.h"
 
22
 
 
23
#include <QApplication>
 
24
#include <QClipboard>
 
25
#include <QMenu>
 
26
 
 
27
#include "iconaction.h"
 
28
 
 
29
//! \if _hide_doc_
 
30
class URLObject::Private : QObject
 
31
{
 
32
        Q_OBJECT
 
33
public:
 
34
        QString link;
 
35
        IconAction *act_mailto, *act_browser, *act_add_to_roster, *act_copy;
 
36
        URLObject *urlObject;
 
37
 
 
38
        Private(URLObject *parent)
 
39
                : QObject(parent)
 
40
        {
 
41
                urlObject = parent;
 
42
                QString tr;
 
43
                
 
44
                tr = qApp->translate("URLLabel", "Open mail composer");
 
45
                act_mailto = new IconAction(tr, "psi/email", tr, 0, this);
 
46
                connect(act_mailto, SIGNAL(activated()), SLOT(popupAction()));
 
47
        
 
48
                tr = qApp->translate("URLLabel", "Open web browser");
 
49
                act_browser = new IconAction(tr, "psi/www", tr, 0, this);
 
50
                connect(act_browser, SIGNAL(activated()), SLOT(popupAction()));
 
51
 
 
52
                tr = qApp->translate("URLLabel", "Add to Roster");
 
53
                act_add_to_roster = new IconAction(tr, "psi/addContact", tr, 0, this);
 
54
                connect(act_add_to_roster, SIGNAL(activated()), SLOT(popupAction()));
 
55
 
 
56
                tr = qApp->translate("URLLabel", "Copy location");
 
57
                act_copy = new IconAction(tr, tr, 0, this);
 
58
                connect(act_copy, SIGNAL(activated()), SLOT(popupCopy()));
 
59
        }
 
60
                
 
61
        QString copyString(QString from)
 
62
        {
 
63
                QString l = from;
 
64
 
 
65
                int colon = l.find(':');
 
66
                if ( colon == -1 )
 
67
                        colon = 0;
 
68
                QString service = l.left( colon );
 
69
 
 
70
                if ( service == "mailto" || service == "jabber" || service == "jid" || service == "xmpp" ) {
 
71
                        if ( colon > -1 )
 
72
                                l = l.mid( colon + 1 );
 
73
 
 
74
                        while ( l[0] == '/' )
 
75
                                l = l.mid( 1 );
 
76
                }
 
77
 
 
78
                return l;
 
79
        }
 
80
 
 
81
public slots:
 
82
        void popupAction(QString lnk) {
 
83
                emit urlObject->openURL(lnk);
 
84
        }
 
85
 
 
86
        void popupAction() {
 
87
                popupAction(link);
 
88
        }
 
89
 
 
90
        void popupCopy(QString lnk) {
 
91
                QApplication::clipboard()->setText( copyString(lnk), QClipboard::Clipboard );
 
92
                if(QApplication::clipboard()->supportsSelection())
 
93
                        QApplication::clipboard()->setText( copyString(lnk), QClipboard::Selection );
 
94
        }
 
95
 
 
96
        void popupCopy() {
 
97
                popupCopy(link);
 
98
        }
 
99
};
 
100
//! \endif
 
101
 
 
102
/**
 
103
 * \class URLObject
 
104
 * \brief Helper class for handling clicking on URLs
 
105
 */
 
106
 
 
107
/**
 
108
 * Default constructor.
 
109
 */
 
110
URLObject::URLObject()
 
111
        : QObject(qApp)
 
112
{
 
113
        d = new Private(this);
 
114
}
 
115
 
 
116
/**
 
117
 * Returns instance of the class, and creates it if necessary.
 
118
 */
 
119
URLObject *URLObject::getInstance()
 
120
{
 
121
        static URLObject *urlObject = 0;
 
122
        if (!urlObject)
 
123
                urlObject = new URLObject();
 
124
        return urlObject;
 
125
}
 
126
 
 
127
/**
 
128
 * Creates QMenu with actions corresponding to link's type.
 
129
 * @param lnk link in service:url format
 
130
 */
 
131
QMenu *URLObject::createPopupMenu(const QString &lnk)
 
132
{
 
133
        d->link = lnk;
 
134
        if ( d->link.isEmpty() )
 
135
                return 0;
 
136
 
 
137
        int colon = d->link.find(':');
 
138
        if ( colon == -1 )
 
139
                colon = 0;
 
140
        QString service = d->link.left( colon );
 
141
 
 
142
        QMenu *m = new QMenu;
 
143
        
 
144
        if ( service == "mailto" ) {
 
145
                m->addAction(d->act_mailto);
 
146
        }
 
147
        else if ( service == "jabber" || service == "jid" || service == "xmpp" ) {
 
148
                // TODO: need more actions to jabber item. Ex: "add to roster", "send message"
 
149
                m->addAction(d->act_add_to_roster);
 
150
        }
 
151
        else { //if ( service == "http" || service == "https" || service.isEmpty() ) {
 
152
                m->addAction(d->act_browser);
 
153
        }
 
154
 
 
155
        m->addAction(d->act_copy);
 
156
        return m;
 
157
}
 
158
 
 
159
/**
 
160
 * Simulates default action using the passed URL.
 
161
 * \param lnk URL string
 
162
 */
 
163
void URLObject::popupAction(QString lnk)
 
164
{
 
165
        d->popupAction(lnk);
 
166
}
 
167
 
 
168
#include "urlobject.moc"